cuda - 具有自定义数据类型的推力矢量

标签 cuda copy thrust

我正在为我的项目使用推力库,但遇到了以下问题:

我有一个名为 box 的结构,定义为

typedef struct {
    int coord[4];
    float h;
} box;

现在我正在尝试将数据从盒子的 device_vector 复制到盒子的主机向量:

thrust::device_vector<box> d_boxes(100);
thrust::host_vector<box> h_boxes;
thrust::copy(d_boxes.begin(), d_boxes.end(), h_boxes.begin());

但这会引发错误

terminate called after throwing an instance of 'thrust::system::system_error' what(): invalid argument

如果我用 int 而不是 box 做同样的事情,它工作得很好。 不幸的是,文档似乎没有任何自定义数据类型向量的示例。

我错过了什么?

最佳答案

thrust::copy 不会自动为您调整向量大小(实际上推力算法不会这样做。)

所以这是一个空向量,不足以容纳 100 个对象:

thrust::host_vector<box> h_boxes;

试试这个:

thrust::host_vector<box> h_boxes(100);

正如@JaredHoberock 所指出的,另一种实现可能是:

thrust::device_vector<box> d_boxes(100);
thrust::host_vector<box> h_boxes = d_boxes;

在这种情况下,h_boxes 的构造函数创建的大小适合容纳 d_boxes 中的元素数量(以及执行设备 -> 主机数据复制。)

关于cuda - 具有自定义数据类型的推力矢量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655844/

相关文章:

c++ - 使用分支优化 CUDA 代码

CUDA:编译我的第一个 cuda 程序时出错

cuda - 在 Maxwell GPU 上使用为计算能力 3.7 编译的 CUDA?

cuda - 正确使用cudaDeviceReset()

c# - 如何在同一个 ListView 中拖放项目?

php - 使用另一个 mysql/php 中一个表的数据

cuda - 使用 CUDA Thrust 并行执行多个一维移动平均线

excel - 选择要与选择本身联合的选择标题?

cuda - 在 CUDA C 项目中使用推力::max_element

compiler-errors - 推力集差异无法通过不允许从__host__ __device__函数调用__host__函数进行编译