在CUDA中使用函子
我在CUDA中有以下类函子在CUDA中使用函子,cuda,thrust,Cuda,Thrust,我在CUDA中有以下类函子 class forSecondMax{ private: int toExclude; public: __device__ void setToExclude(int val){ toExclude = val; } __device__ bool operator () (const DereferencedIteratorTuple& lhs, const DereferencedItera
class forSecondMax{
private:
int toExclude;
public:
__device__ void setToExclude(int val){
toExclude = val;
}
__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>(rhs) == get<2>(rhs)*/ && get<0>(lhs) != toExclude/* && get<0>(rhs)!= toExclude */) return get<1>(lhs) < get<1>(rhs); else
return true ;
}
};
secondmax类{
私人:
int除外;
公众:
__设备\uuuuuvoid setToExclude(int val){
toExclude=val;
}
__设备布尔运算符()
(常数解引用编辑器整数和lhs、常数解引用编辑器整数和rhs)
{
使用推力::get;
//如果你做了你需要做的就是为functor定义一个构造函数,它从一个参数中设置数据成员
class forSecondMax{
private:
int toExclude;
public:
__device__ __host__ forSecondMax(int x) : toExclude(x) {};
__device__ __host__ bool operator ()
(const DereferencedIteratorTuple& lhs,
const DereferencedIteratorTuple& rhs)
{
using thrust::get;
if (get<0>(lhs)== get<2>(lhs) && get<0>(lhs) != toExclude)
return get<1>(lhs) < get<1>(rhs);
else
return true ;
}
};
这将在类的新实例中将数据成员设置为exclude
的值为10,并在流压缩调用中使用该实例。为什么不定义构造函数?这将是在对象实例化期间设置数据成员的标准方法,在主机上使用thust函子时会发生这种情况。…@talonmies你能看一下这个问题吗
forSecondMax op(10);
thrust::remove_if(A.begin(), A.end(), op);