GPU memory update atomicity

前端 未结 1 1640
轮回少年
轮回少年 2021-01-26 21:39

I have a main global memory (gpu_mem), along with a variable (gpu_mem_offset) to track the current offset of this global memory where a thread will upd

1条回答
  •  走了就别回头了
    2021-01-26 22:08

    The only way to ensure a consistent update of both counter and array in that example is like this:

    __global__ void kernel(int *gpu_mem, int *gpu_mem_offset)
    {
        int offset = atomicAdd(gpu_mem_offset, 1);
        gpu_mem[offset] = some_value;
    }
    

    i.e. if you need atomic updates, then use an atomic intrinsic. That is what they are for. Here the atomic access to gpu_mem_offset ensures every thread gets a unique value of the offset. Then the write is guaranteed to be safe, because each thread accesses a unique index.

    0 讨论(0)
提交回复
热议问题