EddyFunctors.h 5.65 KB
Newer Older
wangkx1's avatar
init  
wangkx1 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/////////////////////////////////////////////////////////////////////
///
/// \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 <hip/hip_runtime.h>
#include <thrust/functional.h>
#include <thrust/random.h>

namespace EDDY {

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor classes for binarising vectors.
///
/////////////////////////////////////////////////////////////////////
template<typename T>
class Binarise : public thrust::unary_function<T,T>
{
public:
  Binarise(const T& thr) : _ll(thr), _ul(std::numeric_limits<T>::max()) {}
  Binarise(const T& ll, const T& ul) : _ll(ll), _ul(ul) {}
  __host__ __device__ T operator()(const T& x) const { return(static_cast<T>(x > _ll && x < _ul)); }
private:
  const T _ll;
  const T _ul;
  Binarise() : _ll(static_cast<T>(0)), _ul(static_cast<T>(0)) {} // Hidden
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class for generating normal distributed random numbers
///
/////////////////////////////////////////////////////////////////////

template<typename T>
class MakeNormRand : public thrust::unary_function<unsigned int,T>
{
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<T> dist(_mu,_sigma);
    rng.discard(n);
    return(dist(rng));
  }
private:
  const T _mu;
  const T _sigma;
  MakeNormRand() : _mu(static_cast<T>(0)), _sigma(static_cast<T>(1)) {} // Hidden
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class for multiplying vector with a scalar.
///
/////////////////////////////////////////////////////////////////////
template<typename T>
class MulByScalar : public thrust::unary_function<T,T>
{
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<T>(1)) {} // Hidden
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class performing arg1*arg1*arg2.
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class MaskedSquare : public thrust::binary_function<T1,T1,T2>
{
public:
  MaskedSquare() {}
  __host__ __device__ T2 operator()(const T1& arg1, const T1& arg2) const { return(static_cast<T2>(arg1*arg1*arg2)); }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class performing arg1+arg2*arg2.
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class SumSquare : public thrust::binary_function<T2,T1,T2>
{
public:
  SumSquare() {}
  __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return(arg1 + static_cast<T2>(arg2*arg2)); }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class performing arg1*arg2.
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class Product : public thrust::binary_function<T1,T1,T2>
{
public:
  Product() {}
  __host__ __device__ T2 operator()(const T1& arg1, const T1& arg2) const { return(static_cast<T2>(arg1*arg2)); }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class performing arg1 + arg2.
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class Sum : public thrust::binary_function<T2,T1,T2>
{
public:
  Sum() {}
  __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const { return(arg1 + static_cast<T2>(arg2)); }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class returning the largest value of arg1 and arg2.
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class Max : public thrust::binary_function<T2,T1,T2>
{
public:
  Max() {}
  __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const {
    return((static_cast<double>(arg1) > arg2) ? static_cast<double>(arg1) : arg2);
  }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class returning the largest value of abs(arg1) and abs(arg2).
///
/////////////////////////////////////////////////////////////////////
template<typename T1, typename T2>
class MaxAbs : public thrust::binary_function<T2,T1,T2>
{
public:
  MaxAbs() {}
  __host__ __device__ T2 operator()(const T2& arg1, const T1& arg2) const {
    return((fabs(static_cast<double>(arg1)) > fabs(arg2)) ? fabs(static_cast<double>(arg1)) : fabs(arg2));
  }
};

/////////////////////////////////////////////////////////////////////
///
/// \brief Functor class performing arg1 + scalar*arg2. (SAXPY)
///
/////////////////////////////////////////////////////////////////////
template<typename T>
class MulAndAdd : public thrust::binary_function<T,T,T>
{
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<T>(1)) {} // Hidden
};

} // End namespace EDDY


#endif // End #ifndef EddyFunctors_h