我正在为我的项目使用推力库,但遇到了以下问题:
我有一个名为 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/