7

Is it possible to create an array of device_vectors using Thrust? I know I can't create a device_vector of a device_vector, but how I would create an array of device_vectors?

Manolete
  • 3,431
  • 7
  • 54
  • 92
  • 1
    While you can do that in Thrust, it is not advisable. Thrust is only optimized for 1-D vectors. There are other better libraries for 2 or more dimensional data, e.g. OpenCV GPU, ArrayFire, etc. – Ben Stewart Sep 13 '12 at 14:03
  • I'll have a look to OpenCV GPU. ArrayFire isn't free for more than one GPU. – Manolete Sep 13 '12 at 14:23
  • Sounds good. OpenCV GPU (like Thrust) is significantly slower than ArrayFire, so that is the tradeoff you have to make. – Ben Stewart Sep 13 '12 at 16:24
  • 1
    Can you explain what you want to do? If you want a 2D array, then ArrayFire might be a better bet. If you just want a host-side array of device_vectors to use in your host code for some other reason, then that is trivial to do. – harrism Sep 14 '12 at 00:55
  • @harrism how would you do host-side array of device_vectors? I think that would work for me. It doesn't really to be a 2D array. Although 2D arrays could be addressed in CUDA. – Manolete Sep 14 '12 at 08:34

1 Answers1

10

The following code worked for me. If you place this code to a file with .cu extension it compiles well, but if you place it in a file with .cpp extension it gives compile time assertion failure.

thrust::device_vector<float> vectors[3];
//thrust::device_vector<float> *vectors = new thrust::device_vector<float>[3];

vectors[0] = thrust::device_vector<float>(10);
vectors[1] = thrust::device_vector<float>(10);
vectors[2] = thrust::device_vector<float>(10);

printf("Works\n");

The assertion failure is like the following

1>../for_each.inl(96) : error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE<x>'
phoad
  • 1,801
  • 2
  • 20
  • 31
  • 1
    Thanks, let me try it myself! – Manolete Sep 18 '12 at 16:10
  • Awesome! I think this question/answer will help lots of people! – Manolete Sep 19 '12 at 12:15
  • @Manolete, I sometimes forget to use the device oriented Thrust codes in .cu files and get some assertion failures. It might also remind it to me. – phoad Sep 19 '12 at 13:31
  • @ phoad: Unfortunately, Thrust cannot be populated within kernels, do you have a work-around for that? – Manolete Sep 19 '12 at 13:45
  • 1
    There are method like copy_if etc. Furthermore you may get the raw pointer from Thrust containers. http://stackoverflow.com/questions/7678995/from-thrustdevice-vector-to-raw-pointer-and-back – phoad Sep 19 '12 at 13:50
  • It might be better to ask your questions in a new stackoverflow question, so you may provide more information and get clearer answers ;) AFAIK, Thrust library is capable of performing most of the operations that STL containers, algorithms can do. – phoad Sep 19 '12 at 20:10
  • @phoad will it work if the size is unknown ?, that is instead of fixing 10 as the size of each vector we do not specify the size. – user3351750 Dec 24 '14 at 06:58
  • 1
    Yes. Size of these vectors are defined at run-time, not compile time. So unknown size should be ok. – phoad Dec 29 '14 at 22:05