passing thrust::device_vector to a function by reference(通过引用将推力::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_referenceT 的成员,但可以转换成 >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_vector 或std::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 传递给函数
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
