Search code examples
c++vectorcudathrust

Cuda thrust::device_vector get pointer from specific range


I have a vector of vectors:

thrust::device_vector weights_;

which is a continuous amount of memory, where every w items, represent a vector.

In one of my functions, I pass as parameters the begin and end of that range, like so:

 __host__ ann::d_vector ann::prop_layer (
                                           unsigned int weights_begin,
                                           unsigned int weights_end,
                                           ann::d_vector & input
                                        ) const

and then, I go and copy into a new vector that range, and then get a raw pointer which I can use in a kernel:

thrust::device_vector<float> weights ( weights_.begin() + weights_begin,
                                       weights_.begin() + weights_end );

float * weight_ptr = thrust::raw_pointer_cast( weights.data() );

some_kernel<<<numBlocks,numThreads>>>( weight_ptr, weight.size() );
  1. Can I get a pointer from that range, without first copying it to a new vector? That seems like a waste of copy-realloc to me.
  2. In case I can't get a pointer from that range, can I at least assign a vector to that range, without copying the actual values?

Solution

  • Can I get a pointer from that range, without first copying it to a new vector? That seems like a waste of copy-realloc to me.

    Yes, you can get a pointer to that range.

    float * weight_ptr = thrust::raw_pointer_cast( weights_.data() ) + weights_begin;
    

    In case I can't get a pointer from that range, can I at least assign a vector to that range, without copying the actual values?

    No, a thrust vector cannot be instantiated "on top" of existing data.