Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Add vector_reference and conversion to std::vector #1407

Closed
wants to merge 2 commits into from

Conversation

hschwane
Copy link

@hschwane hschwane commented Mar 30, 2021

Hi,
I created this pull request to add two features I found helpful when using thrust together with existing cpu code as well as custom CUDA kernels.

vector_reference

The vector_reference allows access to data in a vector e.g. thrust::device_vector from host and device code. Semantically the name expresses clearly that the data is not owned, but only referenced. It implements all data access functions a vector typically has, including iterators. It does not allow access to any of the vectors functions that might change the capacity, as it only references the internal data.

conversion to std::vector

Adds an explicit operator std::vector<T>() to the thrust::vector_base class. So all of thust's vectors can be easily converted to std::vector, which allows thrust to be integrated seemlessly into existing cpu code.

examples

This will allow code like the following to be written:

__global__ void kernel(thrust::vector_reference<const int> in, thrust::vector_reference<int> out) {
  if(!in.size() == out.size())  {
    printf("different sizes");
    asm("trap;");
  }

  for(int i = blockIdx.x*blockDim.x+threadIdx.x; i < in.size(); i += blockDim.x*gridDim.x) {
    out[i] = in[i];
  }
}

std::vector<int> func(const std:.vector<int>& input) {
  thrust::device_vector<int> vin(input);
  thrust::device_vector<int> vout(vin.size());
  kernel<<< 128, 128>>>( thrust::make_device_vector_reference(vin),
                                          thrust::make_device_vector_reference(vout));
  return std::vector<int>(vout);
}

The vector_reference is also useful for functions that need to be called from host as well as device code.

Let me know if you have any feedback. I hope that others will find it useful as well.

Hendrik Schwanekamp added 2 commits March 30, 2021 17:57
Adds the vector_reference class (includeing tests).
It allows access to data in a vector e.g. thrust::device_vector,
by holding a non owning pointer as well as the size. It implements
all data access functions of a vector typically has, including
iterators. It does not allow access to any of the vectors functions
that might change the capacity, as it only references the internal
data. It can be used in host as well as device code.
Converting a host_vector or device_vector directly into an std::vector
simplifyes working with existing cpu code and make the transition seemless.
@GPUtester
Copy link
Collaborator

Can one of the admins verify this patch?

@jrhemstad
Copy link
Collaborator

vector_reference should be named span. See NVIDIA/cccl#752

@alliepiper alliepiper added the type: enhancement New feature or request. label Apr 2, 2021
@alliepiper alliepiper added this to the 1.14.0 milestone Apr 2, 2021
@alliepiper
Copy link
Collaborator

Thanks for the PR! Just a heads up, I'm working through a large backlog and it may be a while before I can give this a good review.

As Jake mentioned, this is very similar to an outstanding request for span-like functionality, this PR looks like it could be an initial implementation of that feature.

@alliepiper
Copy link
Collaborator

Hi @hschwane -- After looking through this and talking with the team, it looks like vector_reference provides functionality that overlaps with a planned feature, thrust::span, as @jrhemstad pointed out in NVIDIA/cccl#752. span will model the C++20 std::span implementation, which has strict semantic and functional requirements that wouldn't be compatible with vector_reference.

Rather than merge this now and then deprecate / remove it later when we add span / range abstractions, I'll reference this PR from the span issue as a work-around / patch that folks can use for similar functionality in the meantime. But unfortunately we won't be able to accept this part of your contribution in the main branch.

That said -- I'm open to adding a std::vector conversion, and I'm a little surprised we don't have one already. Feel free to submit a new PR with that feature and we can take a closer look at that part. Unsurprisingly, @jrhemstad has a bug for this one, too :) #953

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: enhancement New feature or request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants