Search code examples
cudanvidiathrust

how to copy a Thrust::host_vector<char> to a char*


I'm trying my hands at CUDA thrust. but the environment I'm working in needs me to copy the data at the end into a char* and not thrust::host_vector<char> So my code right now looks something like below.

thrust::device_vector<unsigned int> sortindexDev(filesize);
thrust::host_vector<char>BWThost(filesize);
thrust::device_vector<char>BWTdev(filesize);
thrust::device_vector<char>inputDataDev(filesize);
  .
  .
  some code using thrust:: sort, thrust::transform, etc
  .
  .
  .
  .
BWThost = BWTdev;

After I have the copied data in BWThost. I want to copy it to a char* for the need of my framework. How do I do it? Below code doesn't work.

for(int i = o; i < upper; i++) {
          charData[i] = BWThost[i]
}

Solution

  • Just use thrust::copy, for example:

    thrust::device_vector<char>BWTdev(filesize);
    char *chardata = malloc(filesize);
    
    thrust::copy(BWTdev.begin(), BWTdev.end(), &chardata[0]);
    

    [disclaimer: written in browser, not compiled or tested, use at own risk]

    This is copy directly from the device_vector to a host array without the need for any intermediate host_vector or explicitly host side loop.