简体   繁体   English

Cuda AtomicAdd不递增

[英]Cuda AtomicAdd not increment

I'm stuck on such a stupid problem I suppose. 我猜想我陷入了如此愚蠢的问题。 This is a test kernel just to see the atomicAdd working: 这是一个测试内核,仅用于查看atomicAdd的工作原理:

__global__
void pixelcount_kernel(unsigned int * d_count,
                      const size_t numElems)
{ 
int myId = threadIdx.x + blockDim.x * blockIdx.x;
//avoid out of boundary access
if(myId > (numElems-1))
{return;
}

unsigned int inc=1;
atomicAdd(d_count, inc);
//debug code
printf("d_count: %d \n", *d_count);   
}

and this is the memory allocation,initialization and kernel call: 这是内存分配,初始化和内核调用:

unsigned int* d_count;
checkCudaErrors(cudaMalloc(&d_count, sizeof(unsigned int)));
checkCudaErrors(cudaMemset(d_count, 0, sizeof(unsigned int)));
pixelcount_kernel<<<gridSize, blockSize>>>( d_count, 10);

In the output I don't see any increment from 0 to numElems (10 in this call), but this: 在输出中,我看不到从0到numElems的任何增量(在此调用中为10),但这是:

d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10 
d_count: 10

What's wrong? 怎么了? Thanks Giuseppe 谢谢朱塞佩

There is nothing wrong with this output. 此输出没有错。 Different threads of a kernel launch execute in parallel, so it is entirely legit that all threads have incremented d_count by the time the first thread reaches printf(). 内核启动的不同线程是并行执行的,因此在第一个线程到达printf()之前,所有线程的d_count都已递增是完全合法的。

In fact, if you are running the code with a blocksize of at least 10, all 10 threads are part of the same warp and are certain to execute the same instruction at the same time. 实际上,如果您以至少10个块大小运行代码,则所有10个线程都是同一线程的一部分,并且一定要同时执行同一条指令。

If you want to see d_count incrementing (eg if you want to give each participating thread a unique id), use this code instead: 如果您想看到d_count递增(例如,如果您想给每个参与线程一个唯一的ID),请改用以下代码:

unsigned int my_d_count = atomicAdd(d_count, inc);
printf("d_count before atomic increment: %d \n", my_d_count); 

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

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