8

Recently I have been using thrust a lot. I have noticed that in order to use thrust, one must always copy the data from the cpu memory to the gpu memory.
Let's see the following example :

int foo(int *foo)
{
     host_vector<int> m(foo, foo+ 100000);
     device_vector<int> s = m;
}

I'm not quite sure how the host_vector constructor works, but it seems like I'm copying the initial data, coming from *foo, twice - once to the host_vector when it is initialized, and another time when device_vector is initialized. Is there a better way of copying from cpu to gpu without making an intermediate data copies? I know I can use device_ptras a wrapper, but that still doesn't fix my problem.
thanks!

talonmies
  • 70,661
  • 34
  • 192
  • 269
igal k
  • 1,883
  • 2
  • 28
  • 57

1 Answers1

16

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.

Jared Hoberock
  • 11,118
  • 3
  • 40
  • 76
  • the argument size_t n specify the number of elements in the vector , and should not be in size_t type name , size_t usually specify the data length in bytes. – TripleS May 01 '13 at 12:49
  • That's an odd comment. `size_t` is to specify the size of things. Chances are good that `thrust::device_vector::size()` ultimately returns `size_t`. See [`std::size_t`'](https://en.cppreference.com/w/cpp/types/size_t) – jwm Mar 30 '21 at 00:20