https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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
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
23namespace Moose::Kokkos
24{
25
29template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
30class Scalar : public ReferenceWrapper<T>
31{
32public:
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
242private:
243 KOKKOS_SCALAR_FUNCTION const T & value() const { return static_cast<const T &>(*this); }
244};
245
246template <typename>
247struct is_scalar : std::false_type
248{
249};
250
251template <typename T, typename Enable>
252struct is_scalar<Scalar<T, Enable>> : std::true_type
253{
254};
255
256template <typename T,
257 typename U,
258 typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
259KOKKOS_SCALAR_FUNCTION auto
260operator+(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
265template <typename T,
266 typename U,
267 typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
268KOKKOS_SCALAR_FUNCTION auto
269operator-(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
274template <typename T,
275 typename U,
276 typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
277KOKKOS_SCALAR_FUNCTION auto
278operator*(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
283template <typename T,
284 typename U,
285 typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
286KOKKOS_SCALAR_FUNCTION auto
287operator/(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
292template <typename T,
293 typename U,
294 typename = typename std::enable_if<!is_scalar<typename std::decay<T>::type>::value>::type>
295KOKKOS_SCALAR_FUNCTION auto
296operator%(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
301template <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.
T & _reference
Writeable host reference of the variable.
The Kokkos wrapper class that can hold the reference of an arithmetic scalar variable.
auto operator--(int) -> decltype(std::declval< U & >() --)
Postfix decrement the underlying host reference.
KOKKOS_SCALAR_FUNCTION auto operator-() const -> decltype(-std::declval< const T & >())
Get the negated value of the scalar.
auto operator++(int) -> decltype(std::declval< U & >()++)
Postfix increment the underlying host reference.
KOKKOS_SCALAR_FUNCTION auto operator+(const U &value) const -> decltype(std::declval< const T & >()+value)
Add another value to this scalar.
auto operator++() -> decltype(++std::declval< U & >(), std::declval< Scalar & >())
Prefix increment the underlying host reference.
KOKKOS_SCALAR_FUNCTION auto operator%(const U &value) const -> decltype(std::declval< const T & >() % value)
Get the remainder after division by another value.
KOKKOS_SCALAR_FUNCTION auto operator-(const U &value) const -> decltype(std::declval< const T & >() - value)
Subtract another value from this scalar.
auto & operator=(const Scalar &value)
Assign a scalar value to the underlying host reference.
auto operator=(const U &value) -> decltype(std::declval< T & >()=value, std::declval< Scalar & >())
Assign an arithmetic value to the underlying host reference.
KOKKOS_SCALAR_FUNCTION auto operator+() const -> decltype(+std::declval< const T & >())
Get the positive value of the scalar.
auto operator--() -> decltype(--std::declval< U & >(), std::declval< Scalar & >())
Prefix decrement the underlying host reference.
auto operator*=(const U &value) -> decltype(std::declval< T & >() *=value, std::declval< Scalar & >())
Multiply the underlying host reference by another value.
KOKKOS_SCALAR_FUNCTION auto operator*(const U &value) const -> decltype(std::declval< const T & >() *value)
Multiply this scalar by another value.
KOKKOS_SCALAR_FUNCTION const T & value() const
auto operator/=(const U &value) -> decltype(std::declval< T & >()/=value, std::declval< Scalar & >())
Divide the underlying host reference by another value.
auto operator+=(const U &value) -> decltype(std::declval< T & >()+=value, std::declval< Scalar & >())
Add another value to the underlying host reference.
Scalar(T &value)
Constructor.
KOKKOS_SCALAR_FUNCTION auto operator/(const U &value) const -> decltype(std::declval< const T & >()/value)
Divide this scalar by another value.
auto operator-=(const U &value) -> decltype(std::declval< T & >() -=value, std::declval< Scalar & >())
Subtract another value from the underlying host reference.
Scalar(const Scalar &object)
Copy constructor.
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.
KOKKOS_SCALAR_FUNCTION auto operator%(const T &left, const Scalar< U > &right) -> decltype(left % static_cast< const U & >(right))
KOKKOS_SCALAR_FUNCTION auto operator/(const T &left, const Scalar< U > &right) -> decltype(left/static_cast< const U & >(right))
KOKKOS_SCALAR_FUNCTION auto operator-(const T &left, const Scalar< U > &right) -> decltype(left - static_cast< const U & >(right))
KOKKOS_SCALAR_FUNCTION auto operator+(const T &left, const Scalar< U > &right) -> decltype(left+static_cast< const U & >(right))
KOKKOS_SCALAR_FUNCTION auto operator*(const T &left, const Scalar< U > &right) -> decltype(left *static_cast< const U & >(right))
The type trait that determines the default behavior of copy constructor and deepCopy() If this type t...
Definition KokkosArray.h:92
static constexpr bool value
Definition KokkosArray.h:93