因为我需要使用 CUDA 对大量数字进行排序,所以我使用了 thrust。到目前为止,还不错……但是当我想调用一个包含数据的 thrust::host_vector 的“手写”内核时怎么办?
我的方法是(缺少备份):
int CUDA_CountAndAdd_Kernel(thrust::host_vector<float> *samples, thrust::host_vector<int> *counts, int n) {
thrust::device_ptr<float> dSamples = thrust::device_malloc<float>(n);
thrust::copy(samples->begin(), samples->end(), dSamples);
thrust::device_ptr<int> dCounts = thrust::device_malloc<int>(n);
thrust::copy(counts->begin(), counts->end(), dCounts);
float *dSamples_raw = thrust::raw_pointer_cast(dSamples);
int *dCounts_raw = thrust::raw_pointer_cast(dCounts);
CUDA_CountAndAdd_Kernel<<<1, n>>>(dSamples_raw, dCounts_raw);
thrust::device_free(dCounts);
thrust::device_free(dSamples);
}
内核看起来像:
__global__ void CUDA_CountAndAdd_Kernel_Device(float *samples, int *counts)
但是编译失败:
error: argument of type "float **" is incompatible with parameter of type "thrust::host_vector> *"
嗯?!我以为我在给 float 和 int 原始指针?还是我遗漏了什么?
最佳答案
您正在使用调用所在函数的名称调用内核,而不是内核的名称 - 因此参数不匹配。
改变:
CUDA_CountAndAdd_Kernel<<<1, n>>>(dSamples_raw, dCounts_raw);
到
CUDA_CountAndAdd_Kernel_Device<<<1, n>>>(dSamples_raw, dCounts_raw);
看看会发生什么。
关于c++ - 用推力调用手写的CUDA内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2398031/