qut*_*ron 2 c++ cuda gpu thrust
我正试图通过device_vector结构
struct point
{
unsigned int x;
unsigned int y;
}
Run Code Online (Sandbox Code Playgroud)
以下列方式执行某项功能:
void print(thrust::device_vector<point> &points, unsigned int index)
{
std::cout << points[index].y << points[index].y << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
myvector已正确初始化
print(myvector, 0);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"
Run Code Online (Sandbox Code Playgroud)
它出什么问题了?
不幸的是,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;
}
Run Code Online (Sandbox Code Playgroud)
每次调用时print,都会导致从GPU传输到系统内存以创建临时存储.如果你需要points一次打印整个集合,一个更有效的方法是将整个矢量points集体复制到一个host_vector或std::vector(使用thrust::copy),然后像往常一样遍历集合.