Parallel processing 计算内核上的意外索引排序问题

Parallel processing 计算内核上的意外索引排序问题,parallel-processing,glsl,gpgpu,vulkan,spir-v,Parallel Processing,Glsl,Gpgpu,Vulkan,Spir V,当我尝试按降序进行索引排序时,我遇到了一个奇怪的问题。我很确定我对GPU的工作原理缺乏一些基本的了解,因为我希望这个程序能够很好地执行: layout(binding = 0, std430) buffer _6_3 { uint _m0[]; } _3; layout(binding = 1, std430) buffer _8_4 { uint _m0[]; } _4; uint _51(uint _63, uint _64) { uint _66 = 0u;

当我尝试按降序进行索引排序时,我遇到了一个奇怪的问题。我很确定我对GPU的工作原理缺乏一些基本的了解,因为我希望这个程序能够很好地执行:

layout(binding = 0, std430) buffer _6_3
{
    uint _m0[];
} _3;

layout(binding = 1, std430) buffer _8_4
{
    uint _m0[];
} _4;

uint _51(uint _63, uint _64)
{
    uint _66 = 0u;
    uint _67 = 1u;
    for (;;)
    {
        uint _72 = _66;
        if (_72 < 150u)
        {
            memoryBarrierBuffer();
            barrier();
            uint _76 = atomicCounter(_3._m0[_72]);
            memoryBarrierBuffer();
            barrier();
            _67 += uint(((_76 == _63) && (_72 > _64)) || (_76 < _63));
            _66++;
            continue;
        }
        else
        {
            break;
        }
    }
    uint _87 = _67;
    uint _90 = _64 + 1u;
    memoryBarrierBuffer();
    barrier();
    _3._m0[150u - _87] = _90;
    memoryBarrierBuffer();
    barrier();
    return _90;
}

void main()
{
    uint _49 = _3._m0[gl_GlobalInvocationID.x];
    uint _50 = _51(_49, gl_GlobalInvocationID.x);
}
例如,如果我们将150u更改为250u并将结果可视化,我们将得到以下结果:

在这里,我们可以看到排序显示,在某些情况下,while循环似乎没有运行足够的时间——结果中存在重复的值,并且一些值消失了

欢迎您的任何意见

%rankDown = OpFunction %1 None %rankdown_uint_signature
%self = OpFunctionParameter %1
%sindex = OpFunctionParameter %1
%rd = OpLabel
%iter = OpVariable %u32ptr Function %uint_0
%rank = OpVariable %u32ptr Function %uint_1

    OpBranch %while_start
    %while_start = OpLabel
    OpLoopMerge %endloop %continueloop None
    OpBranch %rankloop
    %rankloop = OpLabel
    %rloopload = OpLoad %1 %iter
    %loopgate = OpULessThan %bool %rloopload %uint_500
    OpBranchConditional %loopgate %start %endloop
    %start = OpLabel

        %_thrdValue = OpAccessChain %_ptr_Uniform_uint %_ %uint_0 %rloopload
        OpControlBarrier %uint_1 %uint_1 %UniformMemory
        %thrdValue = OpAtomicLoad %1 %_thrdValue %uint_1 %UniformMemory
        OpControlBarrier %uint_1 %uint_1 %UniformMemory

        %smaller = OpULessThan %bool %thrdValue %self

        %same = OpIEqual %bool %thrdValue %self
        %_is_after = OpUGreaterThan %bool %rloopload %sindex
        %_if_gate = OpLogicalAnd %bool %same %_is_after

        %if_gate2 = OpLogicalOr %bool %_if_gate %smaller
        %sel = OpSelect %1 %if_gate2 %uint_1 %uint_0

        %rankLoad = OpLoad %1 %rank
        %rank_adder = OpIAdd %1 %rankLoad %sel
        OpStore %rank %rank_adder

    OpBranch %continueloop
    %continueloop = OpLabel

        %iterLoad = OpLoad %1 %iter
        %iterAdd = OpIAdd %1 %iterLoad %uint_1
        OpStore %iter %iterAdd

    OpBranch %while_start
    %endloop = OpLabel

    %rankll = OpLoad %1 %rank
    %ow_index = OpISub %1 %uint_500 %rankll
    %_overwrite = OpAccessChain %_ptr_Uniform_uint %_ %uint_0 %ow_index
    %ow_index2 = OpIAdd %1 %sindex %uint_1

    OpControlBarrier %uint_1 %uint_1 %UniformMemory
    OpStore %_overwrite %ow_index2
    OpControlBarrier %uint_1 %uint_1 %UniformMemory

    OpReturnValue %ow_index2
OpFunctionEnd