This simple program showcases the usage of the thrust::device_vector and the thrust::host_vector templates.
- A
thrust::host_vector<int>is instantiated, its elements are set one-by-one, and the vector is printed to the standard output. - The
host_vectoris resized and it is printed again to the standard output. - A
thrust::device_vector<int>is instantiated with the aforementionedhost_vector. The contents are copied to the device. - The
device_vector's elements are modified from host code, and it is printed to the standard output.
- Thrust's device and host vectors implement RAII-style ownership over device and host memory pointers (similarly to
std::vector). The instances are aware of the requested element count, allocate the required amount of memory, and free it upon destruction. When resized, the memory is reallocated if needed. - Additionally, using
device_vectorandhost_vectorsimplifies the transfers between device and host memory to a copy assignment. - It is suggested that developers use
device_vectorandhost_vectorinstead of explicit invocations tomallocandfreefunctions.
thrust::host_vector::host_vectorthrust::host_vector::~host_vectorthrust::host_vector::operator[]thrust::host_vector::resize()thrust::device_vector::device_vectorthrust::device_vector::~device_vectorthrust::device_vector::operator[]