Search code examples
vectorthrust

CUDA Thrust library: How can I create a host_vector of host_vectors of integers?


In C++ in order to create a vector that has 10 vectors of integers I would do the following:

  std::vector< std::vector<int> > test(10);

Since I thought Thrust was using the same logic with the STL I tried doing the same:

  thrust::host_vector< thrust::host_vector<int> > test(10);

However I got too many confusing errors. I tried doing:

  thrust::host_vector< thrust::host_vector<int> > test;

and it worked, however I can't add anything to this vector. Doing

  thrust::host_vector<int> temp(3);
  test.push_back(temp);

would give me the same errors(too many to paste them here).

Also generally speaking when using Thrust, does it make a difference between using host_vector and the STL's vector?

Thank you in advance


Solution

  • Thrust's containers are only designed for POD (plain old data) types. It isn't possible to create multidimensional vectors by instantiating "vectors of vectors" in thrust, mostly because of the limitations on the GPU side which make it impossible to pass and use in the device code path.

    There is some level of compatibility between C++ standard library types and algorithms and the thrust host implementation of those STL derived models, but you should really stick with host vectors when you want to work both with the host and device library back ends.