使用基于布尔条件的Thrust应用简化运算 [英] Applying reduction operation using Thrust subject to a boolean condition

查看:114
本文介绍了使用基于布尔条件的Thrust应用简化运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 thrust :: reduce 找到数组A中的最大值。但是, A [i] 只应选择为max,如果它也满足另一个数组B中的特定布尔条件。例如,B [i]应为true。是他们的一个版本的thrust :: reduce这样做。我看了文档,发现只有下面的API;

I want to use thrust::reduce to find the max value in an array A. However, A[i]should only be chosen as the max if it also satisfies a particular boolean condition in another array B. For example, B[i] should be true. Is their a version of thrust::reduce that does this. I looked at the documentation and found only following API;

thrust::reduce(begin,end, default value, operator)

但是,我很好奇他们的一个版本更适合我的问题吗?

However, i was curious is their a version more suitable to my problem?

编辑:编译在最后一行失败!

Compilation fails in last line!

      typedef thrust::device_ptr<int> IntIterator;
      typedef thrust::device_ptr<float> FloatIterator;
      typedef thrust::tuple<IntIterator,FloatIterator> IteratorTuple;
      typedef thrust::zip_iterator<IteratorTuple> myZipIterator;
      thrust::device_ptr<int> deviceNBMInt(gpuNBMInt);
    thrust::device_ptr<int> deviceIsActive(gpuIsActive);
    thrust::device_ptr<float> deviceNBMSim(gpuNBMSim);

    myZipIterator iter_begin = thrust::make_zip_iterator(thrust::make_tuple(deviceIsActive,deviceNBMSim));
    myZipIterator iter_end = thrust::make_zip_iterator(thrust::make_tuple(deviceIsActive + numRow,deviceNBMSim + numRow));
    myZipIterator result =  thrust::max_element(iter_begin, iter_end, Predicate());


推荐答案

我想你应该看看 Extrema Zip迭代器

这样的操作应该确保这个代码是否可用):

Something like this should do the trick (not sure if this code works out of the box):

typedef thrust::device_ptr<bool>  BoolIterator;
typedef thrust::device_ptr<float>  ValueIterator;

BoolIterator bools_begin, bools_end;
ValueIterator values_begin, values_end; 
// initialize these pointers
// ...

typedef thrust::tuple<BoolIterator, ValueIterator> IteratorTuple;
typedef thrust::tuple<bool, value> DereferencedIteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;

ZipIterator iter_begin(thrust::make_tuple(bools_begin, values_begin));
ZipIterator iter_end(thrust::make_tuple(bools_end, values_end));

struct Predicate
{
  __host__ __device__ bool operator () 
                      (const DereferencedIteratorTuple& lhs, 
                       const DereferencedIteratorTuple& lhs) 
  {
    using thrust::get;
    if (get<0>(lhs) && get<0>(rhs) ) return get<1>(lhs) < get<1>(rhs); else
    return ! get<0>(lhs) ;
  }
};

ZipIterator result =  thrust::max_element(iter_begin, iter_end, Predicate());

或者你可以考虑使用zip迭代器和 thrust :: reduce 。或者您可以尝试使用 inner_product 不知道什么会更快。

Or you may consider similar technique with zip iterator with thrust::reduce. Or you can try with inner_product Not sure what will work faster.

这篇关于使用基于布尔条件的Thrust应用简化运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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