【问题标题】:Function object not working properly功能对象无法正常工作
【发布时间】:2012-12-09 23:29:23
【问题描述】:

我已经定义了以下函数对象:

struct Predicate1
{
   __device__ bool operator () 
       (const DereferencedIteratorTuple& lhs, const DereferencedIteratorTuple& rhs) 
  {
    using thrust::get;
    //if you do <=, returns last occurence of largest element. < returns first
    if (get<0>(lhs)== get<2>(lhs) && get<0>(lhs)!= 3) return get<1>(lhs) < get<1>(rhs); 
    else
    return true ;
  }
};

其中 DereferencedIteratorTuple 如下:

typedef thrust::tuple<int, float,int> DereferencedIteratorTuple;

另外,我这样称呼它:

result =  thrust::max_element(iter_begin, iter_end, Predicate1());

但结果是元组 (3,.99,4)。我很困惑为什么这是结果,因为条件 get&lt;0&gt;(lhs)== get&lt;2&gt;(lhs) 不适用于该元组的 if。因此,对于这个元组的每次比较,运算符都会返回 true。但是,thrust::max_element的定义如下:

"此版本使用函数对象 comp 比较对象。 具体来说,这个版本的 max_element 返回第一个迭代器 i 在 [first, last) 中,对于 [first, last) 中的每个迭代器 j, comp(*i, *j) 为假。”

因此,对于这个元组,没有办法选择它,运算符永远不会返回 false。请让我知道我做错了什么

【问题讨论】:

  • 如果有人要求关闭,请说明原因
  • 是否可能没有符合条件的值?

标签: c++ cuda gpu gpgpu thrust


【解决方案1】:

谓词帮助算法确定首选哪个元素。如果谓词返回true,算法会优先选择rhs 而不是lhs。如果它返回false,则算法更喜欢lhs 而不是rhs。在谓词总是返回true 的情况下,算法将选择数组中的最后一个元素。 stl 和推力算法都是如此。

我猜,您的结果在比较过程中从未出现过lhs,并且每次都没有过滤,因为 rhs 的第二个值小于 0.99。

如果你想过滤这些值,你最好重写你的谓词。

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 2013-11-06
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多