全局内存写在CUDA中是否被认为是原子的? [英] Is global memory write considered atomic in CUDA?

查看:283
本文介绍了全局内存写在CUDA中是否被认为是原子的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全局内存写入被认为是原子还是不在CUDA中?

Is global memory write considered atomic or not in CUDA?

考虑以下CUDA内核代码:

Considering the following CUDA kernel code:

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

全局内存写入 globalStorage ?,例如没有竞争条件,使并发内核线程写入存储在globalStorage中的相同变量的字节,这可能会弄乱结果(例如,parial写入)

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)?

注意,我不是在这里谈论像add / sub / bit-wise等原子操作,只是直接全局写。

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.

推荐答案

CUDA中的内存访问不是隐式原子的。但是,只要 idx 对正在运行的内核中的每个线程都有唯一值,您最初显示的代码本质上不是内存竞争。

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.

因此,您的原始代码:

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);

不会是因为多个线程可能写入 globalStorage中的相同条目。没有原子保护或序列化机制可以在这种情况下产生可预测的结果。

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.

这篇关于全局内存写在CUDA中是否被认为是原子的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆