我正在尝试传递结构的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"
它出什么问题了?
最佳答案
不幸的是,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
集合,则一种更有效的方法是将整个 vector points
批量复制到host_vector
或std::vector
(使用thrust::copy
),然后照常遍历整个集合。