CUDA中的条件复制,其中数据向量比模具长

CUDA中的条件复制,其中数据向量比模具长,cuda,thrust,Cuda,Thrust,我想根据模板向量有条件地从向量复制数据,模板向量比模板向量短N倍。模具中的每个元素将负责数据向量中的N个元素。 假设向量如下所示(N=3) 我希望得到的结果是: result = {1,2,3,7,8,9} 有没有办法使用推力库中的函数来实现这一点 我知道,有: thrust::copy_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate

我想根据模板向量有条件地从向量复制数据,模板向量比模板向量短N倍。模具中的每个元素将负责数据向量中的N个元素。 假设向量如下所示(N=3)

我希望得到的结果是:

result = {1,2,3,7,8,9}
有没有办法使用推力库中的函数来实现这一点

我知道,有:

thrust::copy_if (InputIterator1 first, InputIterator1 last, InputIterator2 stencil, OutputIterator result, Predicate pred)

但这不允许我基于模具中的一个元素从数据向量复制N个值。

通常情况下,我认为有很多可能的方法可以做到这一点

我(使用)想到的方法是使用
模具
向量作为
推力::置换迭代器
的一部分,该迭代器采用
模具
向量,并使用
推力::转换迭代器
生成索引。在本例中,如果我们设想一个从0到8的复制索引,那么我们可以使用一个“映射”索引将其索引到“源”(即模具)向量中,该“映射”索引使用
推力::计数迭代器
计算,整数除以
N
(使用推力占位符)。复制谓词只是测试模具值是否=1

本文简要介绍了如何使用这些奇特的迭代器

以下是一个成功的例子:

$ cat t471.cu
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <iostream>

using namespace thrust::placeholders;

int main(){

  int data[] = {1,2,3,4,5,6,7,8,9};
  int stencil[] = {1,0,1};
  int ds = sizeof(data)/sizeof(data[0]);
  int ss = sizeof(stencil)/sizeof(stencil[0]);
  int N = ds/ss;  // assume this whole number divisible

  thrust::device_vector<int> d_data(data, data+ds);
  thrust::device_vector<int> d_stencil(stencil, stencil+ss);
  thrust::device_vector<int> d_result(ds);
  int rs = thrust::copy_if(d_data.begin(), d_data.end(), thrust::make_permutation_iterator(d_stencil.begin(), thrust::make_transform_iterator(thrust::counting_iterator<int>(0), _1 / N)), d_result.begin(), _1 == 1) - d_result.begin();
  thrust::copy_n(d_result.begin(), rs, std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;
  return 0;
}
$ nvcc -o t471 t471.cu
$ ./t471
1,2,3,7,8,9,
$

通常情况下,我认为有很多可能的方法可以做到这一点

我(使用)想到的方法是使用
模具
向量作为
推力::置换迭代器
的一部分,该迭代器采用
模具
向量,并使用
推力::转换迭代器
生成索引。在本例中,如果我们设想一个从0到8的复制索引,那么我们可以使用一个“映射”索引将其索引到“源”(即模具)向量中,该“映射”索引使用
推力::计数迭代器
计算,整数除以
N
(使用推力占位符)。复制谓词只是测试模具值是否=1

本文简要介绍了如何使用这些奇特的迭代器

以下是一个成功的例子:

$ cat t471.cu
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <iostream>

using namespace thrust::placeholders;

int main(){

  int data[] = {1,2,3,4,5,6,7,8,9};
  int stencil[] = {1,0,1};
  int ds = sizeof(data)/sizeof(data[0]);
  int ss = sizeof(stencil)/sizeof(stencil[0]);
  int N = ds/ss;  // assume this whole number divisible

  thrust::device_vector<int> d_data(data, data+ds);
  thrust::device_vector<int> d_stencil(stencil, stencil+ss);
  thrust::device_vector<int> d_result(ds);
  int rs = thrust::copy_if(d_data.begin(), d_data.end(), thrust::make_permutation_iterator(d_stencil.begin(), thrust::make_transform_iterator(thrust::counting_iterator<int>(0), _1 / N)), d_result.begin(), _1 == 1) - d_result.begin();
  thrust::copy_n(d_result.begin(), rs, std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;
  return 0;
}
$ nvcc -o t471 t471.cu
$ ./t471
1,2,3,7,8,9,
$

结果不应该是
{1,2,3,7,8,9}
?是的,的确如此。谢谢你,结果不应该是
{1,2,3,7,8,9}
?是的,的确如此。非常感谢。
$ cat t471.cu
#include <thrust/copy.h>
#include <thrust/reduce.h>
#include <thrust/device_vector.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <iostream>

using namespace thrust::placeholders;

int main(){

  int data[] = {1,2,3,4,5,6,7,8,9};
  int stencil[] = {1,0,1};
  int ds = sizeof(data)/sizeof(data[0]);
  int ss = sizeof(stencil)/sizeof(stencil[0]);
  int N = ds/ss;  // assume this whole number divisible

  thrust::device_vector<int> d_data(data, data+ds);
  thrust::device_vector<int> d_stencil(stencil, stencil+ss);
  int rs = thrust::reduce(d_stencil.begin(), d_stencil.end())*N;
  thrust::device_vector<int> d_result(rs);
  thrust::copy_if(d_data.begin(), d_data.end(), thrust::make_permutation_iterator(d_stencil.begin(), thrust::make_transform_iterator(thrust::counting_iterator<int>(0), _1 / N)), d_result.begin(), _1 == 1) - d_result.begin();
  thrust::copy_n(d_result.begin(), rs, std::ostream_iterator<int>(std::cout, ","));
  std::cout << std::endl;
  return 0;
}
$ nvcc -o t471 t471.cu
$ ./t471
1,2,3,7,8,9,
$