通过引用将thrust::device_vector传递给函数
passing thrust::device_vector to a function by reference
我试图通过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
集合,更有效的方法是将整个向量points
全部复制到host_vector
或std::vector
(使用thrust::copy
),然后像往常一样遍历集合。
From http://thrust.googlecode.com/svn/tags/1.1.0/doc/html/structthrust_1_1device__reference.html:
device_reference作为对存储在设备内存中的对象的引用。Device_reference不打算直接使用;相反,这种类型是推迟device_ptr的结果。类似地,获取device_reference的地址会产生device_ptr。
也许你需要一些像
(&points[index]).get()->x
不是points[index].x
这有点难看,但是CUDA需要一种机制来在RAM和GPU之间传输数据。
相关文章:
- 编译器如何区分std::vector的构造函数
- 为什么std::vector和std::valarray初始化构造函数不同
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- std::vector 没有重载函数的实例与参数列表匹配
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 创建一个没有复制构造函数的类的 std::vector 的 std::vector
- 为 std::vector 编写自定义插入函数
- 从返回 std::optional of std::vector 的函数中获取结果到调用方
- 将 vector<vector<int>> 传递到函数中会产生内存错误
- 是否可以将具有不同签名的 lambda 存储在 std::vector 中并在函数中执行它们(使用各自的参数)?
- 将 cv::Mat 转换为 std::vector 的通用函数
- 从 std::vector 迭代器中执行函数指针
- C++ std::vector 中的虚拟析构函数继承
- 错误:调用"es_queue::set_rpc_vector(std::vector >&, std::__cxx11::string)"没有匹配函数
- 调用2类继承的vector函数
- 在c++中,vector函数push_back会增加空数组的大小吗?
- 如何通过List
从Java到c++的std::vector函数参数使用JNA - 修改std::vector函数(继承?)
- 不能重载现有的std::vector函数