RAJA
RAJA provides a collection of platform portability abstractions for C++ HPC applications.
IndexValue.hpp
Go to the documentation of this file.
1 
11 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
12 // Copyright (c) Lawrence Livermore National Security, LLC and other
13 // RAJA Project Developers. See top-level LICENSE and COPYRIGHT
14 // files for dates and other details. No copyright assignment is required
15 // to contribute to RAJA.
16 //
17 // SPDX-License-Identifier: (BSD-3-Clause)
18 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
19 
20 #ifndef RAJA_INDEXVALUE_HPP
21 #define RAJA_INDEXVALUE_HPP
22 
23 #include "RAJA/config.hpp"
24 
25 #include <string>
26 
27 #include "RAJA/util/macros.hpp"
28 #include "RAJA/util/types.hpp"
29 
30 namespace RAJA
31 {
32 
34 {};
35 
48 template<typename TYPE, typename VALUE = RAJA::Index_type>
49 struct IndexValue : public IndexValueBase
50 {
51 
52  using value_type = VALUE;
53 
55  RAJA_INLINE constexpr IndexValue() = default;
56  constexpr RAJA_INLINE IndexValue(IndexValue const&) = default;
57  constexpr RAJA_INLINE IndexValue(IndexValue&&) = default;
58  RAJA_INLINE IndexValue& operator=(IndexValue const&) = default;
59  RAJA_INLINE IndexValue& operator=(IndexValue&&) = default;
60 
65  RAJA_HOST_DEVICE RAJA_INLINE constexpr explicit IndexValue(value_type v)
66  : value(v)
67  {}
68 
70  RAJA_HOST_DEVICE RAJA_INLINE value_type& operator*() { return value; }
71 
73  RAJA_HOST_DEVICE RAJA_INLINE const value_type& operator*() const
74  {
75  return value;
76  }
77 
79  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator++(int)
80  {
81  TYPE self(value);
82  value++;
83  return self;
84  }
85 
87  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator++()
88  {
89  value++;
90  return static_cast<TYPE&>(*this);
91  }
92 
94  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator--(int)
95  {
96  TYPE self(value);
97  value--;
98  return self;
99  }
100 
102  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator--()
103  {
104  value--;
105  return static_cast<TYPE&>(*this);
106  }
107 
109  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator+(value_type a) const
110  {
111  return TYPE(value + a);
112  }
113 
115  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator+(TYPE a) const
116  {
117  return TYPE(value + a.value);
118  }
119 
121  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator-(value_type a) const
122  {
123  return TYPE(value - a);
124  }
125 
127  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator-(TYPE a) const
128  {
129  return TYPE(value - a.value);
130  }
131 
133  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator*(value_type a) const
134  {
135  return TYPE(value * a);
136  }
137 
139  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator*(TYPE a) const
140  {
141  return TYPE(value * a.value);
142  }
143 
145  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator/(value_type a) const
146  {
147  return TYPE(value / a);
148  }
149 
151  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator/(TYPE a) const
152  {
153  return TYPE(value / a.value);
154  }
155 
157  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator%(value_type a) const
158  {
159  return TYPE(value % a);
160  }
161 
163  RAJA_HOST_DEVICE RAJA_INLINE TYPE operator%(TYPE a) const
164  {
165  return TYPE(value % a.value);
166  }
167 
169  {
170  value += x;
171  return static_cast<TYPE&>(*this);
172  }
173 
174  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator+=(TYPE x)
175  {
176  value += x.value;
177  return static_cast<TYPE&>(*this);
178  }
179 
181  {
182  value -= x;
183  return static_cast<TYPE&>(*this);
184  }
185 
186  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator-=(TYPE x)
187  {
188  value -= x.value;
189  return static_cast<TYPE&>(*this);
190  }
191 
193  {
194  value *= x;
195  return static_cast<TYPE&>(*this);
196  }
197 
198  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator*=(TYPE x)
199  {
200  value *= x.value;
201  return static_cast<TYPE&>(*this);
202  }
203 
205  {
206  value /= x;
207  return static_cast<TYPE&>(*this);
208  }
209 
210  RAJA_HOST_DEVICE RAJA_INLINE TYPE& operator/=(TYPE x)
211  {
212  value /= x.value;
213  return static_cast<TYPE&>(*this);
214  }
215 
216  RAJA_HOST_DEVICE RAJA_INLINE bool operator<(value_type x) const
217  {
218  return (value < x);
219  }
220 
221  RAJA_HOST_DEVICE RAJA_INLINE bool operator<(TYPE x) const
222  {
223  return (value < x.value);
224  }
225 
226  RAJA_HOST_DEVICE RAJA_INLINE bool operator<=(value_type x) const
227  {
228  return (value <= x);
229  }
230 
231  RAJA_HOST_DEVICE RAJA_INLINE bool operator<=(TYPE x) const
232  {
233  return (value <= x.value);
234  }
235 
236  RAJA_HOST_DEVICE RAJA_INLINE bool operator>(value_type x) const
237  {
238  return (value > x);
239  }
240 
241  RAJA_HOST_DEVICE RAJA_INLINE bool operator>(TYPE x) const
242  {
243  return (value > x.value);
244  }
245 
246  RAJA_HOST_DEVICE RAJA_INLINE bool operator>=(value_type x) const
247  {
248  return (value >= x);
249  }
250 
251  RAJA_HOST_DEVICE RAJA_INLINE bool operator>=(TYPE x) const
252  {
253  return (value >= x.value);
254  }
255 
256  RAJA_HOST_DEVICE RAJA_INLINE bool operator==(value_type x) const
257  {
258  return (value == x);
259  }
260 
261  RAJA_HOST_DEVICE RAJA_INLINE bool operator==(TYPE x) const
262  {
263  return (value == x.value);
264  }
265 
266  RAJA_HOST_DEVICE RAJA_INLINE bool operator!=(value_type x) const
267  {
268  return (value != x);
269  }
270 
271  RAJA_HOST_DEVICE RAJA_INLINE bool operator!=(TYPE x) const
272  {
273  return (value != x.value);
274  }
275 
276  // This is not implemented... but should be by the derived type
277  // this is done by the macro
278  static std::string getName();
279 
280 protected:
282 };
283 
284 namespace internal
285 {
286 
287 template<typename TO, typename FROM>
288 constexpr RAJA_HOST_DEVICE RAJA_INLINE TO convertIndex_helper(FROM const val)
289 {
290  return TO(val);
291 }
292 
293 template<typename TO, typename FROM>
294 constexpr RAJA_HOST_DEVICE RAJA_INLINE TO
295 convertIndex_helper(typename FROM::IndexValueType const val)
296 {
297  return static_cast<TO>(*val);
298 }
299 
300 
301 } // namespace internal
302 
308 template<typename TO, typename FROM>
309 constexpr RAJA_HOST_DEVICE RAJA_INLINE TO convertIndex(FROM const val)
310 {
311  return internal::convertIndex_helper<TO, FROM>(val);
312 }
313 
318 // This version is enabled if FROM is a strongly typed class
319 template<typename FROM>
320 constexpr RAJA_HOST_DEVICE RAJA_INLINE
321  typename std::enable_if<std::is_base_of<IndexValueBase, FROM>::value,
322  typename FROM::value_type>::type
323  stripIndexType(FROM const val)
324 {
325  return *val;
326 }
327 
328 /*
329  * enabled if FROM is not a strongly typed class
330  */
331 template<typename FROM>
332 constexpr RAJA_HOST_DEVICE RAJA_INLINE
333  typename std::enable_if<!std::is_base_of<IndexValueBase, FROM>::value,
334  FROM>::type
335  stripIndexType(FROM const val)
336 {
337  return val;
338 }
339 
340 namespace internal
341 {
342 template<typename FROM, typename Enable = void>
344 {
345  using type = FROM;
346 };
347 
348 template<typename FROM>
350  FROM,
351  typename std::enable_if<std::is_base_of<IndexValueBase, FROM>::value>::type>
352 {
353  using type = typename FROM::value_type;
354 };
355 } // namespace internal
356 
363 template<typename FROM>
365 
372 template<typename FROM>
374  typename std::conditional<std::is_floating_point<FROM>::value,
375  std::common_type<FROM>,
376  std::make_signed<FROM>>::type::type;
377 
378 } // namespace RAJA
379 
385 #define RAJA_INDEX_VALUE(TYPE, NAME) \
386  class TYPE : public ::RAJA::IndexValue<TYPE> \
387  { \
388  using parent = ::RAJA::IndexValue<TYPE>; \
389  \
390  public: \
391  using IndexValueType = TYPE; \
392  RAJA_HOST_DEVICE RAJA_INLINE TYPE() : parent::IndexValue() {} \
393  RAJA_HOST_DEVICE RAJA_INLINE explicit TYPE(::RAJA::Index_type v) \
394  : parent::IndexValue(v) \
395  {} \
396  static inline std::string getName() { return NAME; } \
397  };
398 
405 #define RAJA_INDEX_VALUE_T(TYPE, IDXT, NAME) \
406  class TYPE : public ::RAJA::IndexValue<TYPE, IDXT> \
407  { \
408  public: \
409  RAJA_HOST_DEVICE RAJA_INLINE TYPE() \
410  : RAJA::IndexValue<TYPE, IDXT>::IndexValue() \
411  {} \
412  RAJA_HOST_DEVICE RAJA_INLINE explicit TYPE(IDXT v) \
413  : RAJA::IndexValue<TYPE, IDXT>::IndexValue(v) \
414  {} \
415  static inline std::string getName() { return NAME; } \
416  };
417 
418 #endif
Header file for common RAJA internal macro definitions.
#define RAJA_HOST_DEVICE
Definition: macros.hpp:65
constexpr RAJA_HOST_DEVICE RAJA_INLINE TO convertIndex_helper(FROM const val)
Definition: IndexValue.hpp:288
Definition: AlignedRangeIndexSetBuilders.cpp:35
constexpr RAJA_HOST_DEVICE RAJA_INLINE TO convertIndex(FROM const val)
Function provides a way to take either an int or any Index<> type, and convert it to another type,...
Definition: IndexValue.hpp:309
typename internal::StripIndexTypeT< FROM >::type strip_index_type_t
Strips a strongly typed index to its underlying type In the case of a non-strongly typed index,...
Definition: IndexValue.hpp:364
constexpr RAJA_HOST_DEVICE RAJA_INLINE std::enable_if< std::is_base_of< IndexValueBase, FROM >::value, typename FROM::value_type >::type stripIndexType(FROM const val)
Function that strips the strongly typed Index<> and returns its underlying value_type value.
Definition: IndexValue.hpp:323
typename std::conditional< std::is_floating_point< FROM >::value, std::common_type< FROM >, std::make_signed< FROM > >::type::type make_signed_t
Converts a type into a signed type. Also handles floating point types as std::make_signed only suppor...
Definition: IndexValue.hpp:376
Definition: ListSegment.hpp:416
Definition: IndexValue.hpp:34
Strongly typed "integer" class.
Definition: IndexValue.hpp:50
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator++()
preincrement stored index
Definition: IndexValue.hpp:87
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator/(TYPE a) const
division to underlying index from another strong type
Definition: IndexValue.hpp:151
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator*=(TYPE x)
Definition: IndexValue.hpp:198
constexpr RAJA_INLINE IndexValue(IndexValue &&)=default
RAJA_HOST_DEVICE constexpr RAJA_INLINE IndexValue(value_type v)
Explicit constructor.
Definition: IndexValue.hpp:65
RAJA_HOST_DEVICE RAJA_INLINE const value_type & operator*() const
Dereference provides cast-to-integer.
Definition: IndexValue.hpp:73
RAJA_HOST_DEVICE RAJA_INLINE bool operator>=(TYPE x) const
Definition: IndexValue.hpp:251
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator-(TYPE a) const
subtraction to underlying index from another strong type
Definition: IndexValue.hpp:127
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator/(value_type a) const
division to underlying index from an Index_type
Definition: IndexValue.hpp:145
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator--(int)
postdecrement – returns a copy
Definition: IndexValue.hpp:94
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator+(TYPE a) const
addition to underlying index from another strong type
Definition: IndexValue.hpp:115
RAJA_HOST_DEVICE RAJA_INLINE bool operator<=(value_type x) const
Definition: IndexValue.hpp:226
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator/=(value_type x)
Definition: IndexValue.hpp:204
RAJA_HOST_DEVICE RAJA_INLINE bool operator!=(value_type x) const
Definition: IndexValue.hpp:266
RAJA_HOST_DEVICE RAJA_INLINE bool operator<(TYPE x) const
Definition: IndexValue.hpp:221
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator+(value_type a) const
addition to underlying index from an Index_type
Definition: IndexValue.hpp:109
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator%(value_type a) const
modulus to underlying index from an Index_type
Definition: IndexValue.hpp:157
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator+=(TYPE x)
Definition: IndexValue.hpp:174
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator*(value_type a) const
multiplication to underlying index from an Index_type
Definition: IndexValue.hpp:133
RAJA_HOST_DEVICE RAJA_INLINE bool operator==(value_type x) const
Definition: IndexValue.hpp:256
RAJA_INLINE IndexValue & operator=(IndexValue const &)=default
RAJA_HOST_DEVICE RAJA_INLINE bool operator>(value_type x) const
Definition: IndexValue.hpp:236
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator++(int)
postincrement – returns a copy
Definition: IndexValue.hpp:79
static std::string getName()
RAJA_INLINE IndexValue & operator=(IndexValue &&)=default
constexpr RAJA_INLINE IndexValue(IndexValue const &)=default
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator--()
preincrement stored index
Definition: IndexValue.hpp:102
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator*(TYPE a) const
multiplication to underlying index from another strong type
Definition: IndexValue.hpp:139
RAJA_HOST_DEVICE RAJA_INLINE bool operator!=(TYPE x) const
Definition: IndexValue.hpp:271
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator-(value_type a) const
subtraction to underlying index from an Index_type
Definition: IndexValue.hpp:121
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator*=(value_type x)
Definition: IndexValue.hpp:192
RAJA_HOST_DEVICE RAJA_INLINE bool operator<(value_type x) const
Definition: IndexValue.hpp:216
value_type value
Definition: IndexValue.hpp:281
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator-=(value_type x)
Definition: IndexValue.hpp:180
RAJA_HOST_DEVICE RAJA_INLINE bool operator<=(TYPE x) const
Definition: IndexValue.hpp:231
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator/=(TYPE x)
Definition: IndexValue.hpp:210
constexpr RAJA_INLINE IndexValue()=default
Default constructor initializes value to 0.
RAJA_HOST_DEVICE RAJA_INLINE bool operator==(TYPE x) const
Definition: IndexValue.hpp:261
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator-=(TYPE x)
Definition: IndexValue.hpp:186
RAJA_HOST_DEVICE RAJA_INLINE value_type & operator*()
Dereference provides cast-to-integer.
Definition: IndexValue.hpp:70
RAJA_HOST_DEVICE RAJA_INLINE bool operator>(TYPE x) const
Definition: IndexValue.hpp:241
RAJA_HOST_DEVICE RAJA_INLINE TYPE operator%(TYPE a) const
modulus to underlying index from another strong type
Definition: IndexValue.hpp:163
VALUE value_type
Definition: IndexValue.hpp:52
RAJA_HOST_DEVICE RAJA_INLINE bool operator>=(value_type x) const
Definition: IndexValue.hpp:246
RAJA_HOST_DEVICE RAJA_INLINE TYPE & operator+=(value_type x)
Definition: IndexValue.hpp:168
Definition: IndexValue.hpp:344
FROM type
Definition: IndexValue.hpp:345
Header file for RAJA type definitions.