最近,当我尝试使用CUDA编程时,我想向GPU内存发送一个向量。有人告诉我可以使用推力::device\u vector和推力::host\u vector。我也阅读了帮助文档,但仍然不知道如何将这样的向量发送到内核函数中。 我的代码如下:
thrust::device_vector<int> dev_firetime[1000]; __global__ void computeCurrent(thrust::device_vector<int> d_ftime) { int idx = blockDim.x*blockIdx.x + threadIdx.x; printf("ftime = %d\n", d_ftime[idx]); }
事实上,我不知道如何将向量发送给核函数。如果你知道,请告诉我一些关于这个问题,有没有更好的方法来完成同样的功能? 非常感谢!
推力设备向量不能直接传递给CUDA内核。您需要向内核传递一个指向底层设备内存的指针。可以这样做:
__global__ void computeCurrent(int* d_ftime) { int idx = blockDim.x*blockIdx.x + threadIdx.x; printf("ftime = %d\n", d_ftime[idx]); } thrust::device_vector<int> dev_firetime(1000); int* d_ftime = thrust::raw_pointer_cast<int*>(dev_firetime.data()); computeCurrent<<<....>>>(d_ftime);
如果你有一个向量数组,你需要像下面描述的那样 here 。