【发布时间】:2013-08-03 06:09:40
【问题描述】:
我已经使用 atomicInc() 尝试了下面的程序。
__global__ void ker(int *count)
{
int n=1;
int x = atomicInc ((unsigned int *)&count[0],n);
CUPRINTF("In kernel count is %d\n",count[0]);
}
int main()
{
int hitCount[1];
int *hitCount_d;
hitCount[0]=1;
cudaMalloc((void **)&hitCount_d,1*sizeof(int));
cudaMemcpy(&hitCount_d[0],&hitCount[0],1*sizeof(int),cudaMemcpyHostToDevice);
ker<<<1,4>>>(hitCount_d);
cudaMemcpy(&hitCount[0],&hitCount_d[0],1*sizeof(int),cudaMemcpyDeviceToHost);
printf("count is %d\n",hitCount[0]);
return 0;
}
输出是:
In kernel count is 1
In kernel count is 1
In kernel count is 1
In kernel count is 1
count is 1
我不明白为什么它没有增加。谁能帮忙
【问题讨论】:
-
看起来你真的想使用 atomicAdd,而不是 atomicInc。
-
@talonmies 谢谢你。我已经使用 atomicAdd() 然后它正在工作。但我怀疑在哪种情况下我们可以使用 atomicInc().?
标签: cuda gpu-atomics