is there a better and a faster way to copy from CPU memory to GPU using thrust?

喜你入骨 提交于 2019-12-03 00:20:09

One of device_vector's constructors takes a range of elements specified by two iterators. It's smart enough to understand the raw pointer in your example, so you can construct a device_vector directly and avoid the temporary host_vector:

void my_function_taking_host_ptr(int *raw_ptr, size_t n)
{
  // device_vector assumes raw_ptrs point to system memory
  thrust::device_vector<int> vec(raw_ptr, raw_ptr + n);

  ...
}

If your raw pointer points to CUDA memory, introduce a device_ptr:

void my_function_taking_cuda_ptr(int *raw_ptr, size_t n)
{
  // wrap raw_ptr before passing to device_vector
  thrust::device_ptr<int> d_ptr(raw_ptr);

  thrust::device_vector<int> vec(d_ptr, d_ptr + n);

  ...
}

Using a device_ptr doesn't allocate any storage; it just encodes the location of the pointer in the type system.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!