Updating a buffer from an atomic counter

  atomic, c++, metal

Let’s say var output:[Int32] = [0,0,0,0,0],grid size is 5,1,1 and var counter_atom:Int32 = 0

A:

[[kernel]] void compute_shader (device int* output [[buffer(0)]],
                                device atomic_int& counter_atom [[buffer(1)]]){
    
    int counter = atomic_fetch_add_explicit(&counter_atom, 1, memory_order_relaxed);
    output[ counter ] = 1;  
}

I get ouput with [1,1,1,1,1]

B:

[[kernel]] void compute_shader (device int* output [[buffer(0)]],
                                device atomic_int& counter_atom [[buffer(1)]]){
    
    atomic_fetch_add_explicit(&counter_atom, 1, memory_order_relaxed);
    int counter = atomic_load_explicit(&counter_atom, memory_order_relaxed)-1;
    output[ counter ] = 1;  
}

I get ouput with [0,0,0,0,1]


Why output of B is not equal to output of A ?!?

Source: Windows Questions C++

LEAVE A COMMENT