Search code examples
cudathrust

Thrust: How to create device_vector from host array?


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?

Solution

  • 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);