简体   繁体   English

功能对象无法正常工作

[英]Function object not working properly

I have defined the following function object: 我定义了以下函数对象:

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 ;
  }
};

where the DereferencedIteratorTuple is as follows: 其中DereferencedIteratorTuple如下:

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

Moreover, i call it as follows: 而且,我称其为:

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

But the result is the tuple (3,.99,4). 但是结果是元组(3,.99,4)。 I am confused why this is the result because the condition get<0>(lhs)== get<2>(lhs) does not hold in the if for this tuple. 我很困惑为什么会这样,因为对于该元组,条件get<0>(lhs)== get<2>(lhs)不包含在if中。 Thus, the operator returns true for every comparison of this tuple. 因此,对于该元组的每次比较,运算符都返回true。 However, thrust::max_element is defined as follows : 但是, thrust::max_element定义如下:

"This version compares objects using a function object comp. Specifically, this version of max_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*i, *j) is false." “此版本使用函数对象comp来比较对象。具体来说,此版本的max_element返回[first,last)中的第一个迭代器i,这样,对于[first,last)中的每个迭代器j,comp(* i,* j)是假的。”

Thus, there is no way this should be chosen as for this tuple, operator never returns false. 因此,对于这种元组,没有办法选择它,运算符永远不会返回false。 Please let me know what i am doing wrong 请让我知道我做错了

The predicate helps algorithm to determine which element to prefer. 谓词帮助算法确定首选元素。 If predicate returns true the algorithm prefers rhs over lhs . 如果谓词返回true则算法优先使用rhs不是lhs If it return false the algorithm prefers lhs over rhs . 如果返回false则算法优先选择lhs不是rhs In the case when predicate always returns true the algorithm will select last element in the array. 在谓词始终返回true的情况下,算法将选择数组中的最后一个元素。 This is true for both stl and thrust algorithms. 对于stl和推力算法都是如此。

I guess, that your result never occured as lhs during comparison process and every time it was not filtered since rhs's second value was smaller than 0.99. 我猜想,由于rhs的第二个值小于0.99,因此在比较过程中以及每次未过滤时,您的结果都不会像lhs那样发生。

If you want to filter such values you'd better rewrite your predicate. 如果要过滤此类值,则最好重写谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM