通过引用将推力::device_vector 传递给函数

passing thrust::device_vector to a function by reference(通过引用将推力::device_vector 传递给函数)

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

问题描述

我正在尝试传递结构的 device_vector

I'm trying to pass device_vector of structures

struct point 
{
    unsigned int x;
    unsigned int y;
}

以下列方式传递给函数:

to a function in a following manner:

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.

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

要实现 print,通过将每个元素转换为临时 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 集合,更有效的方法是将整个向量 points 整体复制到 host_vectorstd::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.

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

本文标题为:通过引用将推力::device_vector 传递给函数

基础教程推荐