https://mooseframework.inl.gov
KokkosScalar.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "KokkosReferenceWrapper.h"
13 
14 #include <type_traits>
15 #include <utility>
16 
17 #ifdef MOOSE_KOKKOS_SCOPE
18 #define KOKKOS_SCALAR_FUNCTION KOKKOS_FUNCTION
19 #else
20 #define KOKKOS_SCALAR_FUNCTION
21 #endif
22 
23 namespace Moose::Kokkos
24 {
25 
29 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
30 class Scalar : public ReferenceWrapper<T>
31 {
32 public:
33  using value_type = T;
34 
43  Scalar(const Scalar & object) : ReferenceWrapper<T>(object) {}
44 
49  auto & operator=(const Scalar & value)
50  {
51  this->_reference = static_cast<const T &>(value);
52 
53  return *this;
54  }
59  template <typename U>
60  auto operator=(const U & value) -> decltype(std::declval<T &>() = value, std::declval<Scalar &>())
61  {
62  this->_reference = value;
63 
64  return *this;
65  }
66 
71  KOKKOS_SCALAR_FUNCTION auto operator+() const -> decltype(+std::declval<const T &>())
72  {
73  return +value();
74  }
79  KOKKOS_SCALAR_FUNCTION auto operator-() const -> decltype(-std::declval<const T &>())
80  {
81  return -value();
82  }
83 
88  template <typename U>
89  auto operator+=(const U & value)
90  -> decltype(std::declval<T &>() += value, std::declval<Scalar &>())
91  {
92  this->_reference += value;
93 
94  return *this;
95  }
100  template <typename U>
101  auto operator-=(const U & value)
102  -> decltype(std::declval<T &>() -= value, std::declval<Scalar &>())
103  {
104  this->_reference -= value;
105 
106  return *this;
107  }
112  template <typename U>
113  auto operator*=(const U & value)
114  -> decltype(std::declval<T &>() *= value, std::declval<Scalar &>())
115  {
116  this->_reference *= value;
117 
118  return *this;
119  }
124  template <typename U>
125  auto operator/=(const U & value)
126  -> decltype(std::declval<T &>() /= value, std::declval<Scalar &>())
127  {
128  this->_reference /= value;
129 
130  return *this;
131  }
136  template <typename U>
137  auto operator%=(const U & value)
138  -> decltype(std::declval<T &>() %= value, std::declval<Scalar &>())
139  {
140  this->_reference %= value;
141 
142  return *this;
143  }
144 
149  template <typename U = T>
150  auto operator++() -> decltype(++std::declval<U &>(), std::declval<Scalar &>())
151  {
152  ++this->_reference;
153 
154  return *this;
155  }
160  template <typename U = T>
161  auto operator++(int) -> decltype(std::declval<U &>()++)
162  {
163  return this->_reference++;
164  }
169  template <typename U = T>
170  auto operator--() -> decltype(--std::declval<U &>(), std::declval<Scalar &>())
171  {
172  --this->_reference;
173 
174  return *this;
175  }
180  template <typename U = T>
181  auto operator--(int) -> decltype(std::declval<U &>()--)
182  {
183  return this->_reference--;
184  }
185 
191  template <typename U>
192  KOKKOS_SCALAR_FUNCTION auto operator+(const U & value) const
193  -> decltype(std::declval<const T &>() + value)
194  {
195  return this->value() + value;
196  }
202  template <typename U>
203  KOKKOS_SCALAR_FUNCTION auto operator-(const U & value) const
204  -> decltype(std::declval<const T &>() - value)
205  {
206  return this->value() - value;
207  }
213  template <typename U>
214  KOKKOS_SCALAR_FUNCTION auto operator*(const U & value) const
215  -> decltype(std::declval<const T &>() * value)
216  {
217  return this->value() * value;
218  }
224  template <typename U>
225  KOKKOS_SCALAR_FUNCTION auto operator/(const U & value) const
226  -> decltype(std::declval<const T &>() / value)
227  {
228  return this->value() / value;
229  }
235  template <typename U>
236  KOKKOS_SCALAR_FUNCTION auto operator%(const U & value) const
237  -> decltype(std::declval<const T &>() % value)
238  {
239  return this->value() % value;
240  }
241 
242 private:
243  KOKKOS_SCALAR_FUNCTION const T & value() const { return static_cast<const T &>(*this); }
244 };
245 
246 template <typename>
247 struct is_scalar : std::false_type
248 {
249 };
250 
251 template <typename T, typename Enable>
252 struct is_scalar<Scalar<T, Enable>> : std::true_type
253 {
254 };
255 
256 template <typename T,
257  typename U,
258  typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
259 KOKKOS_SCALAR_FUNCTION auto
260 operator+(const T & left, const Scalar<U> & right) -> decltype(left + static_cast<const U &>(right))
261 {
262  return left + static_cast<const U &>(right);
263 }
264 
265 template <typename T,
266  typename U,
267  typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
268 KOKKOS_SCALAR_FUNCTION auto
269 operator-(const T & left, const Scalar<U> & right) -> decltype(left - static_cast<const U &>(right))
270 {
271  return left - static_cast<const U &>(right);
272 }
273 
274 template <typename T,
275  typename U,
276  typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
277 KOKKOS_SCALAR_FUNCTION auto
278 operator*(const T & left, const Scalar<U> & right) -> decltype(left * static_cast<const U &>(right))
279 {
280  return left * static_cast<const U &>(right);
281 }
282 
283 template <typename T,
284  typename U,
285  typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
286 KOKKOS_SCALAR_FUNCTION auto
287 operator/(const T & left, const Scalar<U> & right) -> decltype(left / static_cast<const U &>(right))
288 {
289  return left / static_cast<const U &>(right);
290 }
291 
292 template <typename T,
293  typename U,
294  typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
295 KOKKOS_SCALAR_FUNCTION auto
296 operator%(const T & left, const Scalar<U> & right) -> decltype(left % static_cast<const U &>(right))
297 {
298  return left % static_cast<const U &>(right);
299 }
300 
301 template <typename T>
303 {
304  static constexpr bool value = true;
305 };
306 
307 // Mimic MOOSE convention
309 
310 } // namespace Moose::Kokkos
The Kokkos object that can hold the reference of a variable.
KOKKOS_SCALAR_FUNCTION auto operator+(const T &left, const Scalar< U > &right) -> decltype(left+static_cast< const U &>(right))
Definition: KokkosScalar.h:260
auto operator--() -> decltype(--std::declval< U &>(), std::declval< Scalar &>())
Prefix decrement the underlying host reference.
Definition: KokkosScalar.h:170
auto operator+=(const U &value) -> decltype(std::declval< T &>()+=value, std::declval< Scalar &>())
Add another value to the underlying host reference.
Definition: KokkosScalar.h:89
KOKKOS_SCALAR_FUNCTION auto operator+() const -> decltype(+std::declval< const T &>())
Get the positive value of the scalar.
Definition: KokkosScalar.h:71
KOKKOS_SCALAR_FUNCTION auto operator%(const T &left, const Scalar< U > &right) -> decltype(left % static_cast< const U &>(right))
Definition: KokkosScalar.h:296
auto operator%=(const U &value) -> decltype(std::declval< T &>() %=value, std::declval< Scalar &>())
Assign the remainder after division by another value to the underlying host reference.
Definition: KokkosScalar.h:137
auto operator++() -> decltype(++std::declval< U &>(), std::declval< Scalar &>())
Prefix increment the underlying host reference.
Definition: KokkosScalar.h:150
The Kokkos wrapper class that can hold the reference of an arithmetic scalar variable.
Definition: KokkosScalar.h:30
T & _reference
Writeable host reference of the variable.
KOKKOS_SCALAR_FUNCTION auto operator*(const T &left, const Scalar< U > &right) -> decltype(left *static_cast< const U &>(right))
Definition: KokkosScalar.h:278
auto operator*=(const U &value) -> decltype(std::declval< T &>() *=value, std::declval< Scalar &>())
Multiply the underlying host reference by another value.
Definition: KokkosScalar.h:113
Scalar(const Scalar &object)
Copy constructor.
Definition: KokkosScalar.h:43
KOKKOS_SCALAR_FUNCTION auto operator%(const U &value) const -> decltype(std::declval< const T &>() % value)
Get the remainder after division by another value.
Definition: KokkosScalar.h:236
KOKKOS_SCALAR_FUNCTION auto operator-(const U &value) const -> decltype(std::declval< const T &>() - value)
Subtract another value from this scalar.
Definition: KokkosScalar.h:203
auto operator--(int) -> decltype(std::declval< U &>() --)
Postfix decrement the underlying host reference.
Definition: KokkosScalar.h:181
KOKKOS_SCALAR_FUNCTION auto operator/(const T &left, const Scalar< U > &right) -> decltype(left/static_cast< const U &>(right))
Definition: KokkosScalar.h:287
auto operator++(int) -> decltype(std::declval< U &>()++)
Postfix increment the underlying host reference.
Definition: KokkosScalar.h:161
auto & operator=(const Scalar &value)
Assign a scalar value to the underlying host reference.
Definition: KokkosScalar.h:49
KOKKOS_SCALAR_FUNCTION auto operator-() const -> decltype(-std::declval< const T &>())
Get the negated value of the scalar.
Definition: KokkosScalar.h:79
The type trait that determines the default behavior of copy constructor and deepCopy() If this type t...
Definition: KokkosArray.h:90
KOKKOS_SCALAR_FUNCTION const T & value() const
Definition: KokkosScalar.h:243
KOKKOS_SCALAR_FUNCTION auto operator*(const U &value) const -> decltype(std::declval< const T &>() *value)
Multiply this scalar by another value.
Definition: KokkosScalar.h:214
Scalar(T &value)
Constructor.
Definition: KokkosScalar.h:39
auto operator/=(const U &value) -> decltype(std::declval< T &>()/=value, std::declval< Scalar &>())
Divide the underlying host reference by another value.
Definition: KokkosScalar.h:125
KOKKOS_SCALAR_FUNCTION auto operator/(const U &value) const -> decltype(std::declval< const T &>()/value)
Divide this scalar by another value.
Definition: KokkosScalar.h:225
auto operator-=(const U &value) -> decltype(std::declval< T &>() -=value, std::declval< Scalar &>())
Subtract another value from the underlying host reference.
Definition: KokkosScalar.h:101
KOKKOS_SCALAR_FUNCTION auto operator+(const U &value) const -> decltype(std::declval< const T &>()+value)
Add another value to this scalar.
Definition: KokkosScalar.h:192
KOKKOS_SCALAR_FUNCTION auto operator-(const T &left, const Scalar< U > &right) -> decltype(left - static_cast< const U &>(right))
Definition: KokkosScalar.h:269
static constexpr bool value
Definition: KokkosArray.h:92
auto operator=(const U &value) -> decltype(std::declval< T &>()=value, std::declval< Scalar &>())
Assign an arithmetic value to the underlying host reference.
Definition: KokkosScalar.h:60