Search code examples
cudathrust

Convert raw data to a vector of complex numbers in Thrust


I have a pointer to raw data of complex numbers in interleaved format, i.e. real and imaginary parts stored alternately - R I R I R I ...

How do I convert this to a host (or device) vector of thrust::complex without incurring extra copy? The following does not work -

double dos[8] = {9.3252,2.3742,7.2362,5.3562,2.3323,2.2322,7.2362,3.2352};
thrust::host_vector<thrust::complex<double > > comp(dos, dos+8);

Solution

  • Just cast. Something like this:

    double dos[8] = {9.3252,2.3742,7.2362,5.3562,2.3323,2.2322,7.2362,3.2352};
    typedef thrust::complex<double> cdub;
    cdub* cdos = reinterpret_cast<cdub*>(&dos[0]);
    thrust::host_vector<cdub> comp(cdos, cdos+4);
    

    should work.