Is global memory write considered atomic in CUDA?

柔情痞子 提交于 2019-12-20 01:58:07

问题


Is global memory write considered atomic or not in CUDA?

Considering the following CUDA kernel code:

int idx = blockIdx.x*blockDim.x+threadIdx.x;
int gidx = idx%1000;
globalStorage[gidx] = somefunction(idx);

Is the global memory write to globalStorage atomic?, e.g. there is no race conditions such that concurrent kernel threads write to the bytes of the same variable stored in globalStorage, which could mess the results up (e.g. parial writes)?

Note that I am not talking about atomic operations like add/sub/bit-wise etc here, just straight global write.

Edited: Rewrote the example code to avoid confusion.


回答1:


Memory acesses in CUDA are not implicitly atomic. However, the code you originally showed isn't intrinsically a memory race as long as idx has a unique value for each thread in the running kernel.

So your original code:

int idx = blockIdx.x*blockDim.x+threadIdx.x;
globalStorage[idx] = somefunction(idx);

would be safe if the kernel launch uses a 1D grid and globalStorage is suitably sized, whereas your second version:

int idx = blockIdx.x*blockDim.x+threadIdx.x;
int gidx = idx%1000;
globalStorage[gidx] = somefunction(idx);

would not be because multiple thread could potentially write to the same entry in globalStorage. There is no atomic protections or serialisation mechanisms which would produce predictable results in such as case.



来源:https://stackoverflow.com/questions/20714860/is-global-memory-write-considered-atomic-in-cuda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!