功能对象无法正常工作 [英] Function object not working properly

查看:220
本文介绍了功能对象无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了以下函数对象:

  struct Predicate1 
{
__device__ bool operator )
(const DereferencedIteratorTuple& lhs,const DereferencedIteratorTuple& rhs)
{
using thrust :: get;
//如果你< =,返回最大元素的最后一次出现。 <首先返回
if(get< 0>(lhs)== get< 2>(lhs)&& get< 0>(lhs)!= 3)return get'(lhs)获得(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< 0>(lhs)== get< 2>(lhs) > if 表示此元组。因此,对于该元组的每次比较,运算符返回true。但是, thrust :: max_element 的定义如下:


对象使用函数对象comp。
具体来说,此版本的max_element返回[first,last)中的第一个迭代器i
,这样对于[first,last)中的每个迭代器j,
因此,没有办法选择这个元组作为这个元组,因为这个元组不能被选择,运算符不返回false。请让我知道我做错了什么

解决方案

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



我猜,你的结果在比较过程中不会出现 lhs 因为rhs的第二个值小于0.99。



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


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

这篇关于功能对象无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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