通过引用将thrust :: device_vector传递给函数 [英] passing thrust::device_vector to a function by reference

查看:1802
本文介绍了通过引用将thrust :: device_vector传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递 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已正确初始化

myvector was initialized properly

print(myvector, 0);

我遇到以下错误:

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

它有什么问题?

推荐答案

不幸的是, device_reference< T> 不能公开 T ,但它可以转换为 T

Unfortunately, device_reference<T> cannot expose members of T, but it can convert to T.

c $ c>,将每个元素的临时副本转换为临时 temp

To implement print, make a temporary copy of each element by converting it to a temporary 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 en masse to a host_vector std :: vector (使用 thrust :: copy ),然后正常遍历集合。

Each time you invoke print, it causes a transfer from GPU to system memory to create the temporary. If you need to print the entire collection of points at once, a more efficient method would copy the entire vector points en masse to a host_vector or std::vector (using thrust::copy) and then iterate through the collection as normal.

这篇关于通过引用将thrust :: device_vector传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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