I am using the thrust library for my project and ran into the following problem:
I have a struct called box defined as
typedef struct {
int coord[4];
float h;
} box;
and am now trying to copy data from a device_vector of boxes to a host_vector of boxes:
thrust::device_vector<box> d_boxes(100);
thrust::host_vector<box> h_boxes;
thrust::copy(d_boxes.begin(), d_boxes.end(), h_boxes.begin());
But this throws the error
terminate called after throwing an instance of 'thrust::system::system_error' what(): invalid argument
If I do the same with int instead of box, it works fine. Unfortunately, the documentation does not seem to have any example of vectors of custom data types.
What did I miss?
thrust::copy
doesn't automatically resize vectors for you (actually no thrust algorithms do.)
So this is an empty vector, not large enough to hold 100 objects:
thrust::host_vector<box> h_boxes;
Try this instead:
thrust::host_vector<box> h_boxes(100);
As pointed out by @JaredHoberock, an alternate realization could be:
thrust::device_vector<box> d_boxes(100);
thrust::host_vector<box> h_boxes = d_boxes;
In this case, the constructor for h_boxes
creates it with a size appropriate to hold the number of elements in d_boxes
(as well as performing the device -> host data copy.)