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

How to initilize thrust device vector from another device vector declared by cuda #1833

Closed
ztdepztdep opened this issue Nov 25, 2022 · 5 comments
Labels

Comments

@ztdepztdep
Copy link

I have a device vector d_b[100], I want to creat a thrust vector with d_b[80:100]. How to resolve this mission.

@miscco
Copy link
Collaborator

miscco commented Nov 25, 2022

I am not fully sure what you mean with d_b[80:100]

However, you could always use one of the algorithms such like https://github.com/NVIDIA/thrust/blob/main/thrust/fill.h

@ztdepztdep
Copy link
Author

I am not fully sure what you mean with d_b[80:100]

However, you could always use one of the algorithms such like https://github.com/NVIDIA/thrust/blob/main/thrust/fill.h

I mean fill thrust vector with d_b from the 80'th to the 100'th values.

@miscco
Copy link
Collaborator

miscco commented Nov 25, 2022

In that case you would need to use thrust::copy or thrust::copy_n You can find some documentation here

thrust/thrust/copy.h

Lines 118 to 130 in 125d281

* \code
* #include <thrust/copy.h>
* #include <thrust/device_vector.h>
* #include <thrust/execution_policy.h>
* ...
* size_t n = 100;
* thrust::device_vector<int> vec0(n);
* thrust::device_vector<int> vec1(n);
* ...
* thrust::copy_n(thrust::device, vec0.begin(), n, vec1.begin());
*
* // vec1 is now a copy of vec0
* \endcode

@pauleonix
Copy link

pauleonix commented Dec 5, 2022

If possibe, I would rather just use the right constructor to avoid unnecessary initialisation, i.e.

thrust::device_vector<float> d_c(d_b.cbegin() + 80, d_b.cend());
// or
thrust::device_vector<float> d_c(d_b.cbegin() + 80, d_b.cbegin() + 100);

@Pavelevich
Copy link

this can work for u:

  • device vector is already with some values

int main()
{
int size = 299;
int d_b[299];
thrust::device_vector d(d_b, d_b + size);
thrust::device_ptr d_ptr = thrust::make_device_ptr(d_b);
thrust::device_vector d_subset(d_ptr + 66, d_ptr + 299);
return 0;
}

@NVIDIA NVIDIA locked and limited conversation to collaborators Feb 28, 2023
@jrhemstad jrhemstad converted this issue into a discussion Feb 28, 2023
@github-project-automation github-project-automation bot moved this to Done in CCCL Feb 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
Archived in project
Development

No branches or pull requests

5 participants