Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过引用将推力::设备_向量传递给函数_C++_Cuda_Gpu_Thrust - Fatal编程技术网

C++ 通过引用将推力::设备_向量传递给函数

C++ 通过引用将推力::设备_向量传递给函数,c++,cuda,gpu,thrust,C++,Cuda,Gpu,Thrust,我正在尝试传递结构的设备向量 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::end

我正在尝试传递结构的
设备向量

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;
}
我发现以下错误:

error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"
错误:类“推力::设备_引用”没有成员“x”
错误:类“推力::设备_引用”没有成员“y”
有什么问题吗?

来自:

device_reference充当对存储在设备内存中的对象的引用。设备参考不打算直接使用;相反,这种类型是延迟设备ptr的结果。类似地,获取设备_引用的地址产生设备_ptr

也许你需要像这样的东西

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

points[index].x

这有点难看,但CUDA需要一种在RAM和GPU之间传输数据的机制。

不幸的是,
设备\u参考
无法公开
T
的成员,但它可以转换为
T

要实现
打印
,请通过将每个元素转换为临时的
temp
,来制作每个元素的临时副本:

void print(thrust::device_vector<point> &points, unsigned int index)
{
    point temp = points[index];
    std::cout << temp.y << temp.y << std::endl;
}
void打印(推力::设备向量和点,无符号整数索引)
{
点温度=点[索引];

std::cout我们不知道如何定义推力::设备_引用,所以我们无法回答这个问题。然而,看起来很明显,该类虽然在点上进行了模板化,但并不直接公开x和y。
索引
参数不带类型,应该是
int索引
?@RenéRichter:Oops,复制粘贴错误。它应该是无符号的int
void print(thrust::device_vector<point> &points, unsigned int index)
{
    point temp = points[index];
    std::cout << temp.y << temp.y << std::endl;
}