25

I have a thrust device_vector. I want to cast it to a raw pointer so that I can pass it to a kernel. How can I do so?

thrust::device_vector<int> dv(10);
//CAST TO RAW
kernel<<<bl,tpb>>>(pass raw)
talonmies
  • 68,743
  • 34
  • 184
  • 258
Programmer
  • 6,373
  • 21
  • 77
  • 122

1 Answers1

30

You can do this using thrust::raw_pointer_cast. The device vector class has a member function data which will return a thrust::device_ptr to the memory held by the vector, which can be cast, something like this:

thrust::device_vector<int> dv(10);

int * dv_ptr = thrust::raw_pointer_cast(dv.data());

kernel<<<bl,tpb>>>(dv_ptr)

(disclaimer: written in browser, never compiled, never tested). There is a full working example of this included with thrust: unwrap_pointer.cu

talonmies
  • 68,743
  • 34
  • 184
  • 258
  • Do we need to copy that data back? Now that dv_ptr has the modified data from dv, what do we do to copy it back to dv? – RaenirSalazar Nov 08 '15 at 00:31
  • No, no need to copy it back, its just a static cast to avoid compile time checks. At run time nothing really changes. – Johan Oct 17 '19 at 11:21