///////////////////////////////////////////////////////////////////// /// /// \file EddyFunctors.h /// \brief Declarations of functors that I use for the CUDA implementation of Eddy /// /// \author Jesper Andersson /// \version 1.0b, Nov., 2012. /// \Copyright (C) 2012 University of Oxford /// ///////////////////////////////////////////////////////////////////// #ifndef EddyFunctors_h #define EddyFunctors_h #include #include #include namespace EDDY { ///////////////////////////////////////////////////////////////////// /// /// \brief Functor classes for binarising vectors. /// ///////////////////////////////////////////////////////////////////// template class Binarise : public thrust::unary_function { public: Binarise(const T& thr) : _ll(thr), _ul(std::numeric_limits::max()) {} Binarise(const T& ll, const T& ul) : _ll(ll), _ul(ul) {} __host__ __device__ T operator()(const T& x) const { return(static_cast(x > _ll && x < _ul)); } private: const T _ll; const T _ul; Binarise() : _ll(static_cast(0)), _ul(static_cast(0)) {} // Hidden }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class for generating normal distributed random numbers /// ///////////////////////////////////////////////////////////////////// template class MakeNormRand : public thrust::unary_function { public: MakeNormRand(const T& mu, const T& sigma) : _mu(mu), _sigma(sigma) {} __host__ __device__ T operator()(const unsigned int n) const { thrust::default_random_engine rng; thrust::normal_distribution dist(_mu,_sigma); rng.discard(n); return(dist(rng)); } private: const T _mu; const T _sigma; MakeNormRand() : _mu(static_cast(0)), _sigma(static_cast(1)) {} // Hidden }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class for multiplying vector with a scalar. /// ///////////////////////////////////////////////////////////////////// template class MulByScalar : public thrust::unary_function { public: MulByScalar(const T& scalar) : _scalar(scalar) {} __host__ __device__ T operator()(const T& x) const { return(_scalar * x); } private: const T _scalar; MulByScalar() : _scalar(static_cast(1)) {} // Hidden }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class performing arg1*arg1*arg2. /// ///////////////////////////////////////////////////////////////////// template class MaskedSquare : public thrust::binary_function { public: MaskedSquare() {} __host__ __device__ T2 operator()(const T1& arg1, const T1& arg2) const { return(static_cast(arg1*arg1*arg2)); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class performing arg1+arg2*arg2. /// ///////////////////////////////////////////////////////////////////// template class SumSquare : public thrust::binary_function { public: SumSquare() {} __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return(arg1 + static_cast(arg2*arg2)); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class performing arg1*arg2. /// ///////////////////////////////////////////////////////////////////// template class Product : public thrust::binary_function { public: Product() {} __host__ __device__ T2 operator()(const T1& arg1, const T1& arg2) const { return(static_cast(arg1*arg2)); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class performing arg1 + arg2. /// ///////////////////////////////////////////////////////////////////// template class Sum : public thrust::binary_function { public: Sum() {} __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return(arg1 + static_cast(arg2)); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class returning the largest value of arg1 and arg2. /// ///////////////////////////////////////////////////////////////////// template class Max : public thrust::binary_function { public: Max() {} __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return((static_cast(arg1) > arg2) ? static_cast(arg1) : arg2); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class returning the largest value of abs(arg1) and abs(arg2). /// ///////////////////////////////////////////////////////////////////// template class MaxAbs : public thrust::binary_function { public: MaxAbs() {} __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return((fabs(static_cast(arg1)) > fabs(arg2)) ? fabs(static_cast(arg1)) : fabs(arg2)); } }; ///////////////////////////////////////////////////////////////////// /// /// \brief Functor class performing arg1 + scalar*arg2. (SAXPY) /// ///////////////////////////////////////////////////////////////////// template class MulAndAdd : public thrust::binary_function { public: MulAndAdd(const T& scalar) : _scalar(scalar) {} __host__ __device__ T operator()(const T& arg1, const T& arg2) const { return(arg1 + _scalar*arg2); } private: const T _scalar; MulAndAdd() : _scalar(static_cast(1)) {} // Hidden }; } // End namespace EDDY #endif // End #ifndef EddyFunctors_h