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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thethrust::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:
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.