【问题标题】:atomicAdd - CUDA functionatomicAdd - CUDA 函数
【发布时间】:2022-12-11 10:47:28
【问题描述】:

我应用 atomicAdd 函数在每个数组组件中添加 10 结果与我的预期不一样。 你能告诉我为什么 list[1] 的值为 12,而我期望 11=1+10。线程总数为 5。初始数组值为

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

结果是

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

【问题讨论】:

  • 这回答了你的问题了吗? cuda atomicAdd example fails to yield correct output
  • 您的评论也令人困惑。 list[1] 是 12,因为 2+10= 12。list[1] 在您的示例中包含值 2。
  • 我在下面从字面上回答了这个问题。
  • 我在下面从字面上解释了......你的代码确实slist[0]=为什么你期望0之外的其他人改变?
  • 因为你有竞争条件,list[0] 没有被自动更新

标签: cuda


【解决方案1】:

请注意,atomicAdd 不返回更新后的值,而是返回老的值:cuda atomicAdd example fails to yield correct output

所以你的所有输出都是预期的。在slist[0] 中,即使您用atomicAdd 更新值,您也会立即用atomicAdd 的输出(旧值)覆盖它。 id 的其余部分不会发生这种情况,除非它们确实将 1 存储在 slist[0] 中,所有这些。

您可能想要一个新数组来存储 atomicAdd 的结果。

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-22
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多