我想我被困在一个如此愚蠢的问题上。这是一个测试内核,只是为了查看 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);
}
这是内存分配、初始化和内核调用:
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);
在输出中,我没有看到从 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
怎么了?谢谢朱塞佩