我希望使用Thrust库在CUDA中计算设备数组的前缀和。
我的数组是通过
cudaMalloc()
分配的。我的要求如下:main()
{
Launch kernel 1 on data allocated through cudaMalloc()
// This kernel will poplulate some data d.
Use thrust to calculate prefix sum of d.
Launch kernel 2 on prefix sum.
}
我希望在我的核函数之间使用Thrust,所以我需要一种将指针转换为设备迭代器并返回的方法。以下代码有什么问题?
int main()
{
int *a;
cudaMalloc((void**)&a,N*sizeof(int));
thrust::device_ptr<int> d=thrust::device_pointer_cast(a);
thrust::device_vector<int> v(N);
thrust::exclusive_scan(a,a+N,v);
return 0;
}