将 thrust::device_vector 以引用的方式传递给函数

3
我正在尝试传递结构体的device_vector
struct point 
{
    unsigned int x;
    unsigned int y;
}

以以下方式将参数传递给函数:
void print(thrust::device_vector<point> &points, unsigned int index)
{
    std::cout << points[index].y << points[index].y << std::endl;
}

myvector已经正确地初始化了。

print(myvector, 0);

我遇到了以下错误:

error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"

这是什么问题?

我们不知道thrust :: device_reference如何定义,因此无法回答这个问题。 但是看起来很明显,该类虽然模板化为point,但并没有直接暴露x和y。 - John
index参数没有类型,应该改为int index - René Richter
@René Richter:啊,复制粘贴错误了。应该是无符号整数(unsigned int)。 - qutron
2个回答

6

很遗憾,device_reference<T>无法暴露T的成员,但它可以转换为T

要实现print,通过将其转换为临时temp的方式,制作每个元素的临时副本:

void print(thrust::device_vector<point> &points, unsigned int index)
{
    point temp = points[index];
    std::cout << temp.y << temp.y << std::endl;
}

每次调用print都会导致从GPU到系统内存的传输以创建临时对象。如果需要一次打印整个points集合,则更有效的方法是将整个向量points批量复制到host_vectorstd::vector(使用thrust::copy),然后像平常一样遍历集合。

1

来自http://thrust.googlecode.com/svn/tags/1.1.0/doc/html/structthrust_1_1device__reference.html

device_reference 作为对存储在设备内存中的对象的引用。device_reference 不打算直接使用;相反,这种类型是推迟一个 device_ptr 的结果。同样地,取一个 device_reference 的地址会产生一个 device_ptr。

也许你需要类似这样的东西

(&points[index]).get()->x

替代

points[index].x

有点丑陋,但CUDA需要一个在RAM和GPU之间传输数据的机制。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接