简体   繁体   English

原子地址 - CUDA function

[英]atomicAdd - CUDA function

I apply atomicAdd function to add 10 in each array component The results are Not identical to my expection.我应用 atomicAdd function 在每个数组组件中添加 10 结果与我的预期不同。 Could you tell me why the value of list[1] is 12, while I expect 11=1+10.你能告诉我为什么 list[1] 的值为 12,而我期望 11=1+10。 Total threads are 5. The initial array values are线程总数为 5。初始数组值为

slist[0]=1
slist[1]=2
slist[2]=3
slist[3]=4
slist[4]=5

the results are结果是

list[0]= 1, list[0]= 1
list[0]= 1, list[1]= 12
list[0]= 1, list[2]= 13
list[0]= 1, list[3]= 14
list[0]= 1, list[4]= 15
__global__ void RunAtomicAdd(int* slist, int* val)
{
    int id = threadIdx.x;
    slist[0] = atomicAdd((slist +id), 10);
    printf("list[0]= %d, list[%d]= %d \n", slist[0], id, slist[id]);
}

Note that atomicAdd does not return the updated value, instead it returns the old value: cuda atomicAdd example fails to yield correct output请注意, atomicAdd不返回更新后的值,而是返回值: cuda atomicAdd 示例无法产生正确的 output

So all of your outputs are expected.所以你的所有输出都是预期的。 In slist[0] , even if you update the value with atomicAdd , you immediately overwrite it with the output of atomicAdd , the old value.slist[0]中,即使您使用atomicAdd更新值,您也会立即用旧值 atomicAdd 的atomicAdd覆盖它。 This does not happen with the rest of the id , except they do indeed store 1 in slist[0] , all of them. id的 rest 不会发生这种情况,除非它们确实将1存储在slist[0]中,所有这些。

You may want to have a new array to store the result of atomicAdd .您可能想要一个新数组来存储atomicAdd的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM