Search code examples
c++cudathrust

How to copy host vector to device vector by thrust


I want to copy host std::vector to thrust::device_vector

std::vector<double> p_a(100)
thrust::device_vector<double> d_vec

I want to copy p_a to d_vec


Solution

  • From the documention,

    You can use this constructor:

    __host__ thrust::device_vector< T, Alloc >::device_vector   
    (   const std::vector< OtherT, OtherAlloc > &   v   )   
    

    Copy constructor copies from an exemplar std::vector.

    This constructor receives as parameter the std::vector to copy. So, you can do:

    std::vector<double> p_a(100);
    thrust::device_vector<double> d_vec(p_a);
    

    And also you can use Copy-assignment:

    d_vec = p_a;