How to pass an array of vectors to cuda kernel?

走远了吗. 提交于 2019-12-20 06:15:33

问题


I now have

thrust::device_vector<int> A[N];

and my kernel function

__global__ void kernel(...) {
    auto a = A[threadIdx.x];
}

I know that via thrust::raw_pointer_cast I could pass a device_vector to kernel. But how could I pass an array of vector to it?


回答1:


The really short answer is that you basically can't, and the longer answer is that you really shouldn't even if you discover or are presented with a hacky way of doing this.

And in the spirit of that advice, what you can do is something like this:

 thrust::device_vector<int> A(N);
 thrust::device_vector<int> B(N);
 thrust::device_vector<int> C(N);
 thrust::device_vector<int> D(N);

 // .....

 thrust::device_vector<int*> E(4);
 E.push_back(thrust::raw_pointer_cast(A.data());
 E.push_back(thrust::raw_pointer_cast(B.data());
 E.push_back(thrust::raw_pointer_cast(C.data());
 E.push_back(thrust::raw_pointer_cast(D.data());

 int* E_p = thrust::raw_pointer_cast(E.data());

 // ....

 kernel<<<...>>>(E_p);

The code above should work, but there is so much wrong with it that I wouldn't recommend ever using it for anything. You have been warned.



来源:https://stackoverflow.com/questions/49579847/how-to-pass-an-array-of-vectors-to-cuda-kernel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!