13

I get some data from a library on the host as a pointer to an array. How do I create a device_vector that holds this data on the device?

int* data;
int num;
get_data_from_library( &data, &num );

thrust::device_vector< int > iVec; // How to construct this from data?
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292

1 Answers1

14

As per this answer, all you need is:

int* data;
int num;
get_data_from_library( &data, &num );

thrust::device_vector< int > iVec(data, data+num);
Community
  • 1
  • 1
talonmies
  • 70,661
  • 34
  • 192
  • 269
  • Talonmies: Thanks! I did not know Thrust could detect whether a given pointer lies in host or device space and act as necessary. – Ashwin Nanjappa Mar 01 '12 at 02:55
  • 4
    @Ashwin: It can't. If you want to pass a pointer in device memory to the thrust::vector constructor, you need to use a thrust::device_ptr. That is how device and host pointers are differentiated in Thrust. – talonmies Mar 01 '12 at 04:51
  • Talonmies: But, the answer you have given above does not use device_ptr and I tried it and it works fine. – Ashwin Nanjappa Mar 01 '12 at 07:44
  • 6
    @Ashwin: yes, because your pointers are *host* pointers. If, in your example, `data` were a device pointer, it would fail. In that case you would need to wrap the device pointer in a `thrust::device_ptr` and constructor the `device_vector` with that. The constructors assume that a bare pointer is in host memory, and a `device_pointer` is in device memory. There is no magic here. – talonmies Mar 01 '12 at 08:25
  • Talonmies: Ah, I see. I was wondering what *magic* it was using to differentiate the two. Thanks! :-) – Ashwin Nanjappa Mar 01 '12 at 08:32