Это:
thrust::device_vector<int> J(H.begin()+3,H.begin()+9);
- это копия конструкции .Невозможно сделать то, что вы хотите, не прибегая к указателям на базовое хранилище, и даже в этом случае вам нужно быть осторожным, чтобы исходный вектор никогда не выпадал из области действия
Два вектора не могут использовать одно и то же базовое распределение.Это истинно для std::vector
, и это верно для векторов тяги.
Вы можете сделать что-то похожее на то, что вы предлагаете с thrust::device_ptr
:
$ cat t4.cu
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
#include <thrust/device_ptr.h>
#include <iostream>
int main(void)
{
// H has storage for 4 integers
thrust::device_vector<int> H(10);
thrust::sequence(thrust::device, H.begin(), H.end(),1);
std::cout << "H="<< std::endl;
thrust::copy(H.begin(), H.end(), std::ostream_iterator<int>(std::cout, ","));
std::cout<< std::endl;
thrust::device_ptr<int> J(H.data()+3);
thrust::device_ptr<int> J_end = J+6;
std::cout << "Before modifying J="<< std::endl;
thrust::copy(J, J_end, std::ostream_iterator<int>(std::cout, ","));
std::cout<< std::endl;
thrust::sequence(thrust::device, J, J_end,10);
std::cout << "after modifying J="<< std::endl;
thrust::copy(J, J_end, std::ostream_iterator<int>(std::cout, ","));
std::cout<< std::endl;
std::cout << "After modifying H="<< std::endl;
thrust::copy(H.begin(), H.end(), std::ostream_iterator<int>(std::cout, ","));
std::cout<< std::endl;
return 0;
}
$ nvcc -o t4 t4.cu
$ ./t4
H=
1,2,3,4,5,6,7,8,9,10,
Before modifying J=
4,5,6,7,8,9,
after modifying J=
10,11,12,13,14,15,
After modifying H=
1,2,3,10,11,12,13,14,15,10,
$