3

在 CUDA 推力变换中,是否可以在函数内部获取向量的索引,并将其传递给 operator() 函数?

说,我们有,

struct op{
    float operator()(const float& f){
        //do something like return the index
    }
};
vector<float> v(100);
thrust::transform(v.begin(),v.end(),v.begin(),op());

如何获取 operator() 中向量的索引?基本上我想要一种在 CUDA 中制作单位矩阵的简单方法。

4

1 回答 1

7

可能有很多方法可以做到这一点。一种方法是:

  1. 用于thrust::sequence创建与数据向量长度相同的索引向量(或者只使用 a counting_iterator
  2. 使用 azip_iterator返回 a thrust::tuple,结合数据向量和索引向量,返回一个数据项的元组加上它的索引
  3. 定义运算符op()以将特定元组作为其参数之一
  4. 在运算符中,用于thrust::get<>根据需要从元组中检索数据元素或索引

您可以在推力快速入门指南中了解更多关于这些概念的大部分内容。

编辑:针对下面的问题,这是一个有效的示例。尽管这实际上并没有使用任何device_vector,但如果我们在 GPU 上执行此操作(使用device_vector),唯一会产生任何重要 GPU 活动的活动将是调用thrust::transform,即。GPU上只有1个“通过”。

(是的,thrust::sequence 调用也会生成一个 GPU 内核,但我只是用它来为这个例子创建一些数据)。

#include <thrust/host_vector.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>

#define N 30
#define SELECT 3

typedef thrust::tuple<int, int>            tpl2int;
typedef thrust::host_vector<int>::iterator intiter;
typedef thrust::counting_iterator<int>     countiter;
typedef thrust::tuple<intiter, countiter>  tpl2intiter;
typedef thrust::zip_iterator<tpl2intiter>  idxzip;



struct select_unary_op : public thrust::unary_function<tpl2int, int>
{
  __host__ __device__
  int operator()(const tpl2int& x) const
  {
    if ((x.get<1>() %SELECT) == 0)
      return x.get<0>();
    else return -1;
   }
};

int main() {

  thrust::host_vector<int> A(N);
  thrust::host_vector<int> result(N);
  thrust::sequence(A.begin(), A.end());
  thrust::counting_iterator<int> idxfirst(0);
  thrust::counting_iterator<int> idxlast = idxfirst +N;

  idxzip first = thrust::make_zip_iterator(thrust::make_tuple(A.begin(), idxfirst));
  idxzip  last = thrust::make_zip_iterator(thrust::make_tuple(A.end(), idxlast));
  select_unary_op my_unary_op;

  thrust::transform(first, last, result.begin(), my_unary_op);
  std::cout << "Results :" << std::endl;
  thrust::copy(result.begin(), result.end(), std::ostream_iterator<int>( std::cout, " "));
  std::cout << std::endl;


  return 0;

}
于 2013-07-05T09:01:31.537 回答