RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
params_base.hpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
2 // Copyright (c) Lawrence Livermore National Security, LLC and other
3 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
4 // files for dates and other details. No copyright assignment is required
5 // to contribute to RAJA.
6 //
7 // SPDX-License-Identifier: (BSD-3-Clause)
8 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
9 
10 #ifndef RAJA_PARAMS_BASE
11 #define RAJA_PARAMS_BASE
12 
13 namespace RAJA
14 {
15 namespace expt
16 {
17 
18 template<typename T, typename IndexType = RAJA::Index_type>
19 struct ValLoc
20 {
21  using index_type = IndexType;
22  using value_type = T;
23 
24  ValLoc() = default;
25 
26  RAJA_HOST_DEVICE constexpr explicit ValLoc(value_type v) : val(v) {}
27 
29  {}
30 
31  ValLoc(ValLoc const&) = default;
32  ValLoc(ValLoc&&) = default;
33  ValLoc& operator=(ValLoc const&) = default;
34  ValLoc& operator=(ValLoc&&) = default;
35 
36  RAJA_HOST_DEVICE constexpr bool operator<(const ValLoc& rhs) const
37  {
38  return val < rhs.val;
39  }
40 
41  RAJA_HOST_DEVICE constexpr bool operator>(const ValLoc& rhs) const
42  {
43  return val > rhs.val;
44  }
45 
46  RAJA_HOST_DEVICE constexpr const value_type& getVal() const { return val; }
47 
48  RAJA_HOST_DEVICE constexpr const index_type& getLoc() const { return loc; }
49 
50  RAJA_HOST_DEVICE void set(T inval, IndexType inindex)
51  {
52  val = inval;
53  loc = inindex;
54  }
55 
56  RAJA_HOST_DEVICE void setVal(T inval) { val = inval; }
57 
58  RAJA_HOST_DEVICE void setLoc(IndexType inindex) { loc = inindex; }
59 
62 };
63 
64 template<typename T, template<typename, typename, typename> class Op>
65 struct ValOp
66 {
67  using value_type = T;
68  using op_type = Op<T, T, T>;
69 
70  ValOp() = default;
71 
72  RAJA_HOST_DEVICE constexpr explicit ValOp(value_type v) : val(v) {}
73 
74  ValOp(ValOp const&) = default;
75  ValOp(ValOp&&) = default;
76  ValOp& operator=(ValOp const&) = default;
77  ValOp& operator=(ValOp&&) = default;
78 
79  template<
80  typename U = op_type,
81  std::enable_if_t<
82  std::is_same<U, RAJA::operators::minimum<T, T, T>>::value>* = nullptr>
84  {
85  if (v < val)
86  {
87  val = v;
88  }
89  return *this;
90  }
91 
92  template<
93  typename U = op_type,
94  std::enable_if_t<
95  std::is_same<U, RAJA::operators::maximum<T, T, T>>::value>* = nullptr>
97  {
98  if (v > val)
99  {
100  val = v;
101  }
102  return *this;
103  }
104 
105  template<
106  typename U = op_type,
107  std::enable_if_t<
108  std::is_same<U, RAJA::operators::plus<T, T, T>>::value>* = nullptr>
110  {
111  val += rhs;
112  return *this;
113  }
114 
115  template<
116  typename U = op_type,
117  std::enable_if_t<
118  std::is_same<U, RAJA::operators::bit_and<T, T, T>>::value>* = nullptr>
120  {
121  val &= rhs;
122  return *this;
123  }
124 
125  template<
126  typename U = op_type,
127  std::enable_if_t<
128  std::is_same<U, RAJA::operators::bit_or<T, T, T>>::value>* = nullptr>
130  {
131  val |= rhs;
132  return *this;
133  }
134 
135  template<
136  typename U = op_type,
137  std::enable_if_t<
138  std::is_same<U, RAJA::operators::bit_and<T, T, T>>::value>* = nullptr>
140  {
141  val &= rhs;
142  return *this;
143  }
144 
145  template<
146  typename U = op_type,
147  std::enable_if_t<
148  std::is_same<U, RAJA::operators::bit_or<T, T, T>>::value>* = nullptr>
150  {
151  val |= rhs;
152  return *this;
153  }
154 
155  RAJA_HOST_DEVICE constexpr bool operator<(const ValOp& rhs) const
156  {
157  val < rhs.val;
158  return *this;
159  }
160 
161  RAJA_HOST_DEVICE constexpr bool operator>(const ValOp& rhs) const
162  {
163  val > rhs.val;
164  return *this;
165  }
166 
167  value_type val = op_type::identity();
168 };
169 
170 template<typename T,
171  typename IndexType,
172  template<typename, typename, typename> class Op>
173 struct ValOp<ValLoc<T, IndexType>, Op>
174 {
175  using index_type = IndexType;
177  using op_type = Op<value_type, value_type, value_type>;
180 
181  ValOp() = default;
182 
183  RAJA_HOST_DEVICE constexpr explicit ValOp(value_type v) : val(v) {}
184 
186  : val(v, l)
187  {}
188 
189  ValOp(ValOp const&) = default;
190  ValOp(ValOp&&) = default;
191  ValOp& operator=(ValOp const&) = default;
192  ValOp& operator=(ValOp&&) = default;
193 
194  template<typename U = op_type,
195  std::enable_if_t<std::is_same<
196  U,
198  value>* = nullptr>
200  {
201  if (v < val)
202  {
203  val = v;
204  }
205  return *this;
206  }
207 
208  template<typename U = op_type,
209  std::enable_if_t<std::is_same<
210  U,
212  value>* = nullptr>
214  {
215  if (v > val)
216  {
217  val = v;
218  }
219  return *this;
220  }
221 
222  template<typename U = op_type,
223  std::enable_if_t<std::is_same<
224  U,
226  value>* = nullptr>
229  {
230  return min(value_type(v, l));
231  }
232 
233  template<typename U = op_type,
234  std::enable_if_t<std::is_same<
235  U,
237  value>* = nullptr>
240  {
241  return max(value_type(v, l));
242  }
243 
244  RAJA_HOST_DEVICE constexpr bool operator<(const ValOp& rhs) const
245  {
246  return val < rhs.val;
247  }
248 
249  RAJA_HOST_DEVICE constexpr bool operator>(const ValOp& rhs) const
250  {
251  return val > rhs.val;
252  }
253 
254  value_type val = op_type::identity();
255 };
256 
257 template<typename T,
258  typename IndexType,
259  template<typename, typename, typename> class Op>
261 
262 namespace detail
263 {
264 
265 struct ParamBase
266 {
267  // Some of this can be made virtual in c++20, for now must be defined in each
268  // child class if any arguments to the forall lambda are needed (e.g.
269  // Name is excluded.)
270  using ARG_TUP_T = camp::tuple<>;
271  using ARG_T = ParamBase;
272  using ARG_LIST_T = typename ARG_TUP_T::TList;
273 
274  RAJA_HOST_DEVICE ARG_TUP_T get_lambda_arg_tup() { return camp::make_tuple(); }
275 
277 
278  static constexpr size_t num_lambda_args = camp::tuple_size<ARG_TUP_T>::value;
279 };
280 
281 struct ForallParamBase : public ParamBase
282 {};
283 
284 // Convert a tuple of parameter types to their respective arg type,
285 // EG Reduction<Double> -> Valop<Double>
286 template<typename T>
288 {};
289 
290 template<typename ParamType, typename Enable = void>
292 {
293  using type = ParamType;
294 };
295 
296 template<typename ParamType>
297 struct GetArgType<
298  ParamType,
299  std::enable_if_t<std::is_base_of<ParamBase, ParamType>::value>>
300 {
301  using type = typename ParamType::ARG_T;
302 };
303 
304 template<typename... Params>
305 struct ParamToArgHelper<camp::tuple<Params...>>
306 {
307  using type = camp::tuple<typename GetArgType<Params>::type...>;
308 };
309 
310 template<typename T>
311 RAJA_HOST_DEVICE std::enable_if_t<
312  std::is_base_of<ParamBase, T>::value,
313  std::add_lvalue_reference_t<typename T::ARG_T>>
314 get_lambda_arg(T& Param)
315 {
316  return *Param.get_lambda_arg();
317 }
318 
319 template<typename T>
320 RAJA_HOST_DEVICE std::enable_if_t<!std::is_base_of<ParamBase, T>::value,
321  std::add_lvalue_reference_t<T>>
322 get_lambda_arg(T& Param)
323 {
324  return Param;
325 }
326 
327 } // namespace detail
328 
329 } // namespace expt
330 
331 } // namespace RAJA
332 
333 #endif // RAJA_PARAMS_BASE
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
RAJA_HOST_DEVICE std::enable_if_t< std::is_base_of< ParamBase, T >::value, std::add_lvalue_reference_t< typename T::ARG_T > > get_lambda_arg(T &Param)
Definition: params_base.hpp:314
Definition: AlignedRangeIndexSetBuilders.cpp:35
camp::list< internal::LambdaArg< internal::lambda_arg_param_t, args >... > Params
Definition: Lambda.hpp:95
Definition: ListSegment.hpp:416
Definition: params_base.hpp:20
constexpr RAJA_HOST_DEVICE ValLoc(value_type v)
Definition: params_base.hpp:26
ValLoc & operator=(ValLoc &&)=default
constexpr RAJA_HOST_DEVICE ValLoc(value_type v, index_type l)
Definition: params_base.hpp:28
IndexType index_type
Definition: params_base.hpp:21
index_type loc
Definition: params_base.hpp:61
ValLoc & operator=(ValLoc const &)=default
constexpr RAJA_HOST_DEVICE bool operator>(const ValLoc &rhs) const
Definition: params_base.hpp:41
value_type val
Definition: params_base.hpp:60
RAJA_HOST_DEVICE void setVal(T inval)
Definition: params_base.hpp:56
constexpr RAJA_HOST_DEVICE const value_type & getVal() const
Definition: params_base.hpp:46
ValLoc(ValLoc &&)=default
T value_type
Definition: params_base.hpp:22
RAJA_HOST_DEVICE void setLoc(IndexType inindex)
Definition: params_base.hpp:58
constexpr RAJA_HOST_DEVICE const index_type & getLoc() const
Definition: params_base.hpp:48
ValLoc(ValLoc const &)=default
constexpr RAJA_HOST_DEVICE bool operator<(const ValLoc &rhs) const
Definition: params_base.hpp:36
RAJA_HOST_DEVICE void set(T inval, IndexType inindex)
Definition: params_base.hpp:50
constexpr RAJA_HOST_DEVICE ValOp(value_type v)
Definition: params_base.hpp:183
IndexType index_type
Definition: params_base.hpp:175
typename value_type::value_type valloc_value_type
Definition: params_base.hpp:178
typename value_type::index_type valloc_index_type
Definition: params_base.hpp:179
constexpr RAJA_HOST_DEVICE ValOp & max(value_type v)
Definition: params_base.hpp:213
constexpr RAJA_HOST_DEVICE ValOp(valloc_value_type v, valloc_index_type l)
Definition: params_base.hpp:185
Op< value_type, value_type, value_type > op_type
Definition: params_base.hpp:177
constexpr RAJA_HOST_DEVICE bool operator>(const ValOp &rhs) const
Definition: params_base.hpp:249
constexpr RAJA_HOST_DEVICE bool operator<(const ValOp &rhs) const
Definition: params_base.hpp:244
constexpr RAJA_HOST_DEVICE ValOp & minloc(valloc_value_type v, valloc_index_type l)
Definition: params_base.hpp:227
ValOp & operator=(ValOp const &)=default
constexpr RAJA_HOST_DEVICE ValOp & min(value_type v)
Definition: params_base.hpp:199
constexpr RAJA_HOST_DEVICE ValOp & maxloc(valloc_value_type v, valloc_index_type l)
Definition: params_base.hpp:238
Definition: params_base.hpp:66
constexpr RAJA_HOST_DEVICE ValOp & operator&=(const value_type &rhs)
Definition: params_base.hpp:119
constexpr RAJA_HOST_DEVICE bool operator<(const ValOp &rhs) const
Definition: params_base.hpp:155
constexpr RAJA_HOST_DEVICE ValOp & max(value_type v)
Definition: params_base.hpp:96
constexpr RAJA_HOST_DEVICE ValOp(value_type v)
Definition: params_base.hpp:72
ValOp(ValOp const &)=default
constexpr RAJA_HOST_DEVICE ValOp & operator|=(const value_type &rhs)
Definition: params_base.hpp:129
RAJA_HOST_DEVICE ValOp & operator|=(value_type &rhs)
Definition: params_base.hpp:149
ValOp(ValOp &&)=default
ValOp & operator=(ValOp &&)=default
T value_type
Definition: params_base.hpp:67
Op< T, T, T > op_type
Definition: params_base.hpp:68
ValOp & operator=(ValOp const &)=default
constexpr RAJA_HOST_DEVICE ValOp & min(value_type v)
Definition: params_base.hpp:83
RAJA_HOST_DEVICE ValOp & operator&=(value_type &rhs)
Definition: params_base.hpp:139
value_type val
Definition: params_base.hpp:167
constexpr RAJA_HOST_DEVICE bool operator>(const ValOp &rhs) const
Definition: params_base.hpp:161
constexpr RAJA_HOST_DEVICE ValOp & operator+=(const value_type &rhs)
Definition: params_base.hpp:109
Definition: params_base.hpp:282
Definition: params_base.hpp:292
ParamType type
Definition: params_base.hpp:293
Definition: params_base.hpp:266
camp::tuple<> ARG_TUP_T
Definition: params_base.hpp:270
RAJA_HOST_DEVICE ARG_T * get_lambda_arg()
Definition: params_base.hpp:276
RAJA_HOST_DEVICE ARG_TUP_T get_lambda_arg_tup()
Definition: params_base.hpp:274
typename ARG_TUP_T::TList ARG_LIST_T
Definition: params_base.hpp:272
static constexpr size_t num_lambda_args
Definition: params_base.hpp:278
camp::tuple< typename GetArgType< Params >::type... > type
Definition: params_base.hpp:307
Definition: params_base.hpp:288
Definition: Operators.hpp:580
Definition: Operators.hpp:559