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

Thrust vector with custom data type

我正在为我的项目使用 thrust 库,运行 遇到以下问题:

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

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

我现在正在尝试将数据从 device_vector 个框复制到 host_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 中的元素数量(以及执行设备 -> 主机数据复制。)