如何将向量数组传递给cuda内核? [英] How to pass an array of vectors to cuda kernel?

查看:91
本文介绍了如何将向量数组传递给cuda内核?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有

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

和我的内核函数

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

我知道可以通过推力:: raw_pointer_cast将device_vector传递给内核。但是,如何向其传递向量数组呢?

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?

推荐答案

真正的简短答案是您基本上不能这样做,并且更长的答案是,即使您发现了这种做法,也不知道该怎么做。

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.

这篇关于如何将向量数组传递给cuda内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆