Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Fad_SLFad_tmpl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #if defined(HAVE_SACADO_KOKKOS)
11 #include "Kokkos_Atomic.hpp"
12 #include "impl/Kokkos_Error.hpp"
13 #endif
14 
15 namespace Sacado {
16 
17  namespace FAD_NS {
18 
19  // Forward declaration
20  template <typename T, int Num>
22 
36  template <typename ValueT, int Num>
37  class SLFad :
38  public Expr< GeneralFad<ValueT,Fad::StaticStorage<ValueT,Num> > > {
39 
40  public:
41 
44  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
45  typedef Expr<GeneralFadType> ExprType;
46 
48  typedef typename ExprType::value_type value_type;
49 
51  typedef typename ExprType::scalar_type scalar_type;
52 
55 
57  template <typename T>
58  struct apply {
59  typedef SLFad<T,Num> type;
60  };
61 
63 
66  template <int N>
67  struct apply_N {
69  };
70 
75 
77 
81  SLFad() :
82  ExprType() {}
83 
85 
88  template <typename S>
91  ExprType(x) {}
92 
94 
98  SLFad(const int sz, const ValueT & x, const DerivInit zero_out = InitDerivArray) :
99  ExprType(sz,x,zero_out) {}
100 
102 
108  SLFad(const int sz, const int i, const ValueT & x) :
109  ExprType(sz,i,x) {}
110 
113  SLFad(const SLFad& x) :
114  ExprType(x) {}
115 
117  template <typename S>
120  ExprType(x) {}
121 
123 
126  ~SLFad() {}
127 
129  template <typename S>
131  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator=(const S& v) {
132  GeneralFadType::operator=(v);
133  return *this;
134  }
135 
138  SLFad& operator=(const SLFad& x) {
139  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
140  return *this;
141  }
142 
144  template <typename S>
146  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator=(const Expr<S>& x)
147  {
148  GeneralFadType::operator=(x);
149  return *this;
150  }
151 
153  template <typename S>
155  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator += (const S& x) {
156  GeneralFadType::operator+=(x);
157  return *this;
158  }
159 
161  template <typename S>
163  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator -= (const S& x) {
164  GeneralFadType::operator-=(x);
165  return *this;
166  }
167 
169  template <typename S>
171  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator *= (const S& x) {
172  GeneralFadType::operator*=(x);
173  return *this;
174  }
175 
177  template <typename S>
179  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator /= (const S& x) {
180  GeneralFadType::operator/=(x);
181  return *this;
182  }
183 
186  SLFad& operator += (const SLFad& x) {
187  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
188  return *this;
189  }
190 
193  SLFad& operator -= (const SLFad& x) {
194  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
195  return *this;
196  }
197 
200  SLFad& operator *= (const SLFad& x) {
201  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
202  return *this;
203  }
204 
207  SLFad& operator /= (const SLFad& x) {
208  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
209  return *this;
210  }
211 
213  template <typename S>
215  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator += (const Expr<S>& x) {
216  GeneralFadType::operator+=(x);
217  //*this = *this + x;
218  return *this;
219  }
220 
222  template <typename S>
224  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator -= (const Expr<S>& x) {
225  GeneralFadType::operator-=(x);
226  return *this;
227  }
228 
230  template <typename S>
232  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator *= (const Expr<S>& x) {
233  GeneralFadType::operator*=(x);
234  return *this;
235  }
236 
238  template <typename S>
240  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator /= (const Expr<S>& x) {
241  GeneralFadType::operator/=(x);
242  return *this;
243  }
244 
245  }; // class SLFad<ValueT,Num>
246 
247  template <typename T, int N>
248  struct BaseExpr< GeneralFad<T,Fad::StaticStorage<T,N> > > {
250  };
251 
252  template <typename T, int N>
253  struct ExprLevel< SLFad<T,N> > {
254  static const unsigned value =
255  ExprLevel< typename SLFad<T,N>::value_type >::value + 1;
256  };
257 
258  template <typename T, int N>
259  struct IsFadExpr< SLFad<T,N> > {
260  static const bool value = true;
261  };
262 
263  } // namespace Fad
264 
265  template <typename T, int N>
266  struct IsFad< FAD_NS::SLFad<T,N> > {
267  static const bool value = true;
268  };
269 
270  template <typename T, int N>
271  struct IsExpr< FAD_NS::SLFad<T,N> > {
272  static const bool value = true;
273  };
274 
275  template <typename T, int N>
276  struct BaseExprType< FAD_NS::SLFad<T,N> > {
278  };
279 
280  template <typename T,unsigned,unsigned> struct ViewFadType;
281  namespace FAD_NS {
282  template <typename,unsigned,unsigned,typename> class ViewFad;
283  }
284 
286  template< class ValueType, int N, unsigned length, unsigned stride >
287  struct ViewFadType< Sacado::FAD_NS::SLFad< ValueType, N >, length, stride > {
289  };
290 
292 
295  template< class ValueType, int N, unsigned length, unsigned stride >
296  struct ViewFadType< const Sacado::FAD_NS::SLFad< ValueType, N >, length, stride > {
298  };
299 
300 } // namespace Sacado
301 
302 #if defined(HAVE_SACADO_KOKKOS)
303 
304 //-------------------------- Atomic Operators -----------------------
305 
306 namespace Sacado {
307 
308  namespace FAD_NS {
309 
310  // Overload of Kokkos::atomic_add for Fad types.
311  template <typename T, int N>
313  void atomic_add(SLFad<T,N>* dst, const SLFad<T,N>& x) {
314  using Kokkos::atomic_add;
315 
316  const int xsz = x.size();
317  const int sz = dst->size();
318 
319  // We currently cannot handle resizing since that would need to be
320  // done atomically.
321  if (xsz > sz)
322  Kokkos::abort(
323  "Sacado error: Fad resize within atomic_add() not supported!");
324 
325  if (xsz != sz && sz > 0 && xsz > 0)
326  Kokkos::abort(
327  "Sacado error: Fad assignment of incompatiable sizes!");
328 
329 
330  if (sz > 0 && xsz > 0) {
332  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
333  }
335  atomic_add(&(dst->val()), x.val());
336  }
337 
338  } // namespace Fad
339 
340 } // namespace Sacado
341 
342 #endif // HAVE_SACADO_KOKKOS
SACADO_INLINE_FUNCTION SLFad(const int sz, const ValueT &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
Turn SLFad into a meta-function class usable with mpl::apply.
Derivative array storage class using static memory allocation.
#define SACADO_FAD_THREAD_SINGLE
#define SACADO_ENABLE_VALUE_CTOR_DECL
Base template specification for whether a type is a Fad type.
Forward-mode AD class using dynamic memory allocation and expression templates.
GeneralFad< StaticStorage< T, Num > > SLFad
static const bool value
#define SACADO_ENABLE_EXPR_CTOR_DECL
Is a type an expression.
Sacado::FAD_NS::ViewFad< const ValueType, length, stride, Sacado::FAD_NS::SLFad< ValueType, N > > type
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION SLFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
ExprType::value_type value_type
Typename of values.
SACADO_INLINE_FUNCTION SLFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x convertible to ValueT.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION SLFad()
Default constructor.
FAD_NS::SLFad< T, N >::base_expr_type type
Fad::StaticStorage< ValueT, Num > StorageType
Base classes.
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
Replace static derivative length.
ExprType::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
const int N
SACADO_INLINE_FUNCTION SLFad(const int sz, const int i, const ValueT &x)
Constructor with size sz, index i, and value x.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
Expr< GeneralFadType > ExprType
Sacado::FAD_NS::ViewFad< ValueType, length, stride, Sacado::FAD_NS::SLFad< ValueType, N > > type
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
ScalarType< ValueT >::type ScalarT
Typename of scalar&#39;s (which may be different from ValueT)
int value
GeneralFad< ValueT, StorageType > GeneralFadType
Forward-mode AD class using static memory allocation with long arrays and expression templates...
Initialize the derivative array.
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION SLFad(const SLFad &x)
Copy constructor.
static const bool value
SACADO_INLINE_FUNCTION ~SLFad()
Destructor.
#define FAD_NS
SLFad< typename GeneralFad< T, Fad::StaticStorage< T, N > >::value_type, N > type
Get view type for any Fad type.