CUDA - 来自设备的 int 在将其复制到主机时未更新
Posted
技术标签:
【中文标题】CUDA - 来自设备的 int 在将其复制到主机时未更新【英文标题】:CUDA - int from device get not updated while copying it to host 【发布时间】:2022-01-11 00:56:27 【问题描述】:我在 CUDA 中很新(在 C 中也是..),我试图使用 int shared
作为标志来在设置 finish
时停止所有线程设备,但是当我将它复制回来时托管它永远不会更新,我可以用char *
来做,但它在使用简单的 int 时不起作用
最小代码示例:
__global__ void bingo(int * finish)
__shared__ int shared;
if(threadIdx.x == 5)
printf("\nassign to finish %d",threadIdx.x);
shared = threadIdx.x;
finish = (int*) threadIdx.x;
printf("GPU says: %d\n",*finish);
return;
__syncthreads();
if(shared != NULL)
printf("\nreturn from thread: %d", threadIdx.x);
return;
int main()
int* threadBingo;
cudaMalloc((void**)&threadBingo, sizeof( int));
bingo<<<1,10>>>(threadBingo );
cudaDeviceSynchronize();
int* threadWhoMadeBingo = (int *) malloc(sizeof(int));
cudaMemcpy(threadWhoMadeBingo, threadBingo, sizeof(int), cudaMemcpyDeviceToHost);
printf("\n thread who made bingo %d\n", *threadWhoMadeBingo);
cudaDeviceReset();
cudaDeviceSynchronize();
return 0;
还有输出:
assign to finish 5
GPU says: 5
return from thread: 0
return from thread: 1
return from thread: 2
return from thread: 3
return from thread: 4
return from thread: 6
return from thread: 7
return from thread: 8
return from thread: 9
thread who made bingo 0
如您所见,最后一行应该是 5 而不是 0
【问题讨论】:
【参考方案1】:好的,我找到了:
finish = (int*) threadIdx.x;
行应该是 -> *finish = threadIdx.x;
。
我会在两天内接受这个答案。
【讨论】:
以上是关于CUDA - 来自设备的 int 在将其复制到主机时未更新的主要内容,如果未能解决你的问题,请参考以下文章