写一个简单的推力函子对某些压缩数组进行操作 [英] Writing a simple thrust functor operating on some zipped arrays

查看:249
本文介绍了写一个简单的推力函子对某些压缩数组进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用zip和排列迭代器执行 thrust :: reduce_by_key
,即在几个虚拟置换数组的压缩数组上执行此操作。
我在编写函数 density_update 的语法时遇到问题。

I am trying to perform a thrust::reduce_by_key using zip and permutation iterators. i.e. doing this on a zipped array of several 'virtual' permuted arrays. I am having trouble in writing the syntax for the functor density_update.

但是首先要设置问题。

这是我的函数调用:

thrust::reduce_by_key(  dflagt,
                        dflagtend,
                        thrust::make_zip_iterator( 
                            thrust::make_tuple(
                                thrust::make_permutation_iterator(dmasst, dmapt), 
                                thrust::make_permutation_iterator(dvelt, dmapt),
                                thrust::make_permutation_iterator(dmasst, dflagt),
                                thrust::make_permutation_iterator(dvelt, dflagt)
                            )
                        ),
                        thrust::make_discard_iterator(),
                        danswert,
                        thrust::equal_to<int>(),
                        density_update()
                  ) 

dmapt dflagt 的类型为 thrust :: device_ptr< int> dvelt dmasst danst 的类型为
thrust :: device_ptr< double>

dmapt, dflagt are of type thrust::device_ptr<int> and dvelt , dmasst and danst are of type thrust::device_ptr<double>.

(它们是对原始cuda数组的推式封装)

(They are thrust wrappers to my raw cuda arrays)

数组 mapt flagt 都是索引向量,从中我需要从数组中执行收集操作 dmasst dvelt

The arrays mapt and flagt are both index vectors from which I need to perform a gather operation from the arrays dmasst and dvelt.

还原步骤后,我打算将数据写入 danswert 数组。由于在缩减中使用了多个数组,显然我使用zip迭代器。

After the reduction step I intend to write my data to the danswert array. Since multiple arrays are being used in the reduction, obviously I am using zip iterators.

我的问题在于编写函数 density_update ,这是二进制操作。

My problem lies in writing the functor density_update which is binary operation.

struct density_update
{
  typedef thrust::device_ptr<double> ElementIterator;
  typedef thrust::device_ptr<int>   IndexIterator;
  typedef thrust::permutation_iterator<ElementIterator,IndexIterator> PIt;

  typedef thrust::tuple< PIt , PIt , PIt, PIt> Tuple;
  __host__ __device__
  double operator()(const Tuple& x , const Tuple& y)
  {   
      return    thrust::get<0>(*x) * (thrust::get<1>(*x) - thrust::get<3>(*x)) + \
                thrust::get<0>(*y) * (thrust::get<1>(*y) - thrust::get<3>(*y));
  }
};

返回的值是 double 。为什么二进制操作看起来像上面的函子是
不重要。我只是想知道我将如何纠正以上的语法。
如上所示,代码引发了大量的编译错误。我不知道我在哪里错了。

The value being returned is a double . Why the binary operation looks like the above functor is not important. I just want to know how I would go about correcting the above syntactically. As shown above the code is throwing a number of compilation errors. I am not sure where I have gone wrong.

我在Ubuntu 10.10的GTX 570上使用CUDA 4.0

I am using CUDA 4.0 on GTX 570 on Ubuntu 10.10

推荐答案

density_update 不应该接收迭代器的元组作为参数 - 它需要迭代器的引用的元组。

density_update should not receive tuples of iterators as parameters -- it needs tuples of the iterators' references.

原则,你可以根据各种迭代器的特定引用类型写入 density_update :: operator()更简单的让编译器推断参数的类型:

In principle you could write density_update::operator() in terms of the particular reference type of the various iterators, but it's simpler to have the compiler infer the type of the parameters:

struct density_update
{
  template<typename Tuple>
  __host__ __device__
  double operator()(const Tuple& x, const Tuple& y)
  {   
    return thrust::get<0>(x) * (thrust::get<1>(x) - thrust::get<3>(x)) + \
           thrust::get<0>(y) * (thrust::get<1>(y) - thrust::get<3>(y));
  }
};

这篇关于写一个简单的推力函子对某些压缩数组进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆