Cuda 如何创建推力装置_矢量的浅拷贝

Cuda 如何创建推力装置_矢量的浅拷贝,cuda,thrust,shallow-copy,Cuda,Thrust,Shallow Copy,我有一个设备向量H。我想使用选定的索引创建H的浅拷贝。我把它叫做J,我想修改J的元素,从而修改H的相应元素 当我更改J的元素时,我下面的尝试未能修改H的元素。看来,推力将新内存分配给J,而不是使用分配给H的内存 #include <thrust/host_vector.h> #include <thrust/device_vector.h> #include <thrust/sequence.h> #include <thrust/execution_p

我有一个设备向量H。我想使用选定的索引创建H的浅拷贝。我把它叫做J,我想修改J的元素,从而修改H的相应元素

当我更改J的元素时,我下面的尝试未能修改H的元素。看来,推力将新内存分配给J,而不是使用分配给H的内存

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>
#include <thrust/execution_policy.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_vector<int> J(H.begin()+3,H.begin()+9);

  std::cout << "Before modifying J="<< std::endl;
  thrust::copy(J.begin(), J.end(), std::ostream_iterator<int>(std::cout, ","));
  std::cout<< std::endl;

  thrust::sequence(thrust::device, J.begin(), J.end(),10);

  std::cout << "after modifying J="<< std::endl;
  thrust::copy(J.begin(), 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;
}
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
//H有4个整数的存储空间
推力:装置_矢量H(10);
推力::顺序(推力::装置,H.开始(),H.结束(),1);
标准::cout这:

这:


我尝试使用迭代器。它似乎有效。结果在代码之后发布。内存位置似乎也被覆盖

#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_vector<int>::iterator J = H.begin()+3;
  thrust::device_vector<int>::iterator 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;
}

我尝试使用迭代器。它似乎有效。结果在代码之后发布。内存位置似乎也被覆盖

#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_vector<int>::iterator J = H.begin()+3;
  thrust::device_vector<int>::iterator 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;
}
我想使用选定的索引创建H的浅层副本

不,您不想创建浅拷贝

我称之为J[and],修改J的元素,从而修改H的相应元素

你实际上想做的事情——最后做的是修改容器的元素范围的子范围。在C++中,我们使用迭代器来完成这个过程;在很多情况下,这些迭代器基本上只是指针。

当元素在内存中是连续的时,另一种方法是使用一个-但这是一个C++20构造(由于缺乏明确的CUDA支持,您可能会遇到一些问题,即可能缺少
\uuu设备\uuuuuuuuuuu
属性;在某些实现中,
gsl::span
也是如此)

我想使用选定的索引创建H的浅层副本

不,您不想创建浅拷贝

我称之为J[and],修改J的元素,从而修改H的相应元素

你实际上想做的事情——最后做的是修改容器的元素范围的子范围。在C++中,我们使用迭代器来完成这个过程;在很多情况下,这些迭代器基本上只是指针。


当元素在内存中是连续的时,另一种方法是使用一个-但这是一个C++20构造(由于缺乏明确的CUDA支持,您可能会遇到一些问题,即可能缺少
\uuuu设备\uuuu
属性;在某些实现中,
gsl::span
也有同样的问题).

使用迭代器而不是device_ptr不是更好吗。@user27665:您的问题代码使用迭代器。这将使您处于副本构造区域,您已经知道该区域不起作用。下面使用迭代器的代码似乎起作用。如果有任何错误,请告诉我。
推力::device_vector J(H.begin()+3,H.begin()+9)
没有这样的代码,它可以编译但不能运行:@NicholasJela:我不知道你的评论和反对票是关于什么的,但你错了:。这段代码是完全有效的,它可以工作。使用迭代器而不是设备\u ptr不是更好吗。@user27665:你的问题代码使用迭代器。这让你进入复制构造territor您已经知道的y不起作用。我下面使用迭代器的代码似乎起作用。如果有任何错误,请告诉我
没有这样的代码,它可以编译但不能运行:@NicholasJela:我不知道你的评论和否决票是关于什么的,但你错了:。那代码是完全有效的,而且有效。
#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_vector<int>::iterator J = H.begin()+3;
  thrust::device_vector<int>::iterator 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;
}
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,