简体   繁体   中英

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:

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). 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. Thus, the operator returns true for every comparison of this tuple. However, thrust::max_element is defined as follows :

"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."

Thus, there is no way this should be chosen as for this tuple, operator never returns 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 . If it return false the algorithm prefers lhs over rhs . In the case when predicate always returns true the algorithm will select last element in the array. This is true for both stl and thrust algorithms.

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.

If you want to filter such values you'd better rewrite your predicate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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