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_SFad_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 
20 
26  template <typename ValueT, int Num>
27  class SFad :
28  public Expr< SFadExprTag<ValueT,Num > > {
29 
30  public:
31 
33  typedef Expr< SFadExprTag<ValueT,Num > > ExprType;
34 
36  typedef typename ExprType::value_type value_type;
37 
39  typedef typename ExprType::scalar_type scalar_type;
40 
43 
45  template <typename T>
46  struct apply {
47  typedef SFad<T,Num> type;
48  };
49 
51  template <int N>
52  struct apply_N {
54  };
55 
60 
62 
66  SFad() :
67  ExprType() {}
68 
70 
73  template <typename S>
76  ExprType(x) {}
77 
79 
83  SFad(const int sz, const ValueT & x, const DerivInit zero_out = InitDerivArray) :
84  ExprType(sz,x,zero_out) {}
85 
87 
93  SFad(const int sz, const int i, const ValueT & x) :
94  ExprType(sz,i,x) {}
95 
98  SFad(const SFad& x) :
99  ExprType(static_cast<const ExprType&>(x)) {}
100 
102  template <typename S>
105  ExprType(x) {}
106 
108 
111  ~SFad() {}
112 
114  template <typename S>
116  SACADO_ENABLE_VALUE_FUNC(SFad&) operator=(const S& v) {
117  ExprType::operator=(v);
118  return *this;
119  }
120 
123  SFad& operator=(const SFad& x) {
124  ExprType::operator=(static_cast<const ExprType&>(x));
125  return *this;
126  }
127 
129  template <typename S>
131  SACADO_ENABLE_EXPR_FUNC(SFad&) operator=(const Expr<S>& x)
132  {
133  ExprType::operator=(x);
134  return *this;
135  }
136 
138  template <typename S>
140  SACADO_ENABLE_VALUE_FUNC(SFad&) operator += (const S& x) {
141  ExprType::operator+=(x);
142  return *this;
143  }
144 
146  template <typename S>
148  SACADO_ENABLE_VALUE_FUNC(SFad&) operator -= (const S& x) {
149  ExprType::operator-=(x);
150  return *this;
151  }
152 
154  template <typename S>
156  SACADO_ENABLE_VALUE_FUNC(SFad&) operator *= (const S& x) {
157  ExprType::operator*=(x);
158  return *this;
159  }
160 
162  template <typename S>
164  SACADO_ENABLE_VALUE_FUNC(SFad&) operator /= (const S& x) {
165  ExprType::operator/=(x);
166  return *this;
167  }
168 
171  SFad& operator += (const SFad& x) {
172  ExprType::operator+=(static_cast<const ExprType&>(x));
173  return *this;
174  }
175 
178  SFad& operator -= (const SFad& x) {
179  ExprType::operator-=(static_cast<const ExprType&>(x));
180  return *this;
181  }
182 
185  SFad& operator *= (const SFad& x) {
186  ExprType::operator*=(static_cast<const ExprType&>(x));
187  return *this;
188  }
189 
192  SFad& operator /= (const SFad& x) {
193  ExprType::operator/=(static_cast<const ExprType&>(x));
194  return *this;
195  }
196 
198  template <typename S>
200  SACADO_ENABLE_EXPR_FUNC(SFad&) operator += (const Expr<S>& x) {
201  ExprType::operator+=(x);
202  return *this;
203  }
204 
206  template <typename S>
208  SACADO_ENABLE_EXPR_FUNC(SFad&) operator -= (const Expr<S>& x) {
209  ExprType::operator-=(x);
210  return *this;
211  }
212 
214  template <typename S>
216  SACADO_ENABLE_EXPR_FUNC(SFad&) operator *= (const Expr<S>& x) {
217  ExprType::operator*=(x);
218  return *this;
219  }
220 
222  template <typename S>
224  SACADO_ENABLE_EXPR_FUNC(SFad&) operator /= (const Expr<S>& x) {
225  ExprType::operator/=(x);
226  return *this;
227  }
228 
229  }; // class SFad<ValueT,Num>
230 
231  template <typename T, int Num>
232  std::ostream& operator << (std::ostream& os,
233  const Expr< SFadExprTag<T,Num> >& x) {
234  os << x.val() << " [";
235 
236  for (int i=0; i< x.size(); i++) {
237  os << " " << x.dx(i);
238  }
239 
240  os << " ]";
241  return os;
242  }
243 
244  template <typename T, int N>
245  struct ExprLevel< SFad<T,N> > {
246  static const unsigned value =
247  ExprLevel< typename SFad<T,N>::value_type >::value + 1;
248  };
249 
250  template <typename T, int N>
251  struct IsFadExpr< SFad<T,N> > {
252  static const bool value = true;
253  };
254 
255  } // namespace Fad
256 
257  template <typename T, int N>
258  struct IsFad< FAD_NS::SFad<T,N> > {
259  static const bool value = true;
260  };
261 
262  template <typename T, int N>
263  struct IsExpr< FAD_NS::SFad<T,N> > {
264  static const bool value = true;
265  };
266 
267  template <typename T, int N>
268  struct BaseExprType< FAD_NS::SFad<T,N> > {
270  };
271 
272  template <typename T,unsigned,unsigned> struct ViewFadType;
273  namespace FAD_NS {
274  template <typename,unsigned,unsigned,typename> class ViewFad;
275  }
276 
278  template< class ValueType, int N, unsigned length, unsigned stride >
279  struct ViewFadType< Sacado::FAD_NS::SFad< ValueType, N >, length, stride > {
281 };
282 
284 
287  template< class ValueType, int N, unsigned length, unsigned stride >
288  struct ViewFadType< const Sacado::FAD_NS::SFad< ValueType, N >, length, stride > {
290  };
291 
292 } // namespace Sacado
293 
294 #if defined(HAVE_SACADO_KOKKOS)
295 
296 //-------------------------- Atomic Operators -----------------------
297 
298 namespace Sacado {
299 
300  namespace FAD_NS {
301 
302  // Overload of Kokkos::atomic_add for Fad types.
303  template <typename T, int N>
305  void atomic_add(SFad<T,N>* dst, const SFad<T,N>& x) {
306  using Kokkos::atomic_add;
307 
308  const int xsz = x.size();
309  const int sz = dst->size();
310 
311  // We currently cannot handle resizing since that would need to be
312  // done atomically.
313  if (xsz > sz)
314  Kokkos::abort(
315  "Sacado error: Fad resize within atomic_add() not supported!");
316 
317  if (xsz != sz && sz > 0 && xsz > 0)
318  Kokkos::abort(
319  "Sacado error: Fad assignment of incompatiable sizes!");
320 
321 
322  if (sz > 0 && xsz > 0) {
324  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
325  }
327  atomic_add(&(dst->val()), x.val());
328  }
329 
330  } // namespace Fad
331 
332 } // namespace Sacado
333 
334 #endif // HAVE_SACADO_KOKKOS
ExprType::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
SACADO_INLINE_FUNCTION SFad()
Default constructor.
#define SACADO_FAD_THREAD_SINGLE
SACADO_INLINE_FUNCTION SFad(const int sz, const int i, const ValueT &x)
Constructor with size sz, index i, and value x.
SACADO_INLINE_FUNCTION SFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x convertible to ValueT.
#define SACADO_ENABLE_VALUE_CTOR_DECL
Base template specification for whether a type is a Fad type.
Sacado::FAD_NS::ViewFad< ValueType, length, stride, Sacado::FAD_NS::SFad< ValueType, N > > type
Forward-mode AD class using dynamic memory allocation and expression templates.
Turn SFad into a meta-function class usable with mpl::apply.
ScalarType< ValueT >::type ScalarT
Typename of scalar&#39;s (which may be different from ValueT)
static const bool value
#define SACADO_ENABLE_EXPR_CTOR_DECL
std::ostream & operator<<(std::ostream &os, const Expr< SFadExprTag< T, Num > > &x)
SACADO_INLINE_FUNCTION SFad(const int sz, const ValueT &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
SACADO_INLINE_FUNCTION SFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
FAD_NS::SFad< T, N >::base_expr_type type
Is a type an expression.
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION SFad(const SFad &x)
Copy constructor.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
Expr< SFadExprTag< ValueT, Num > > ExprType
Base classes.
const int N
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
Replace static derivative length.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
ExprType::value_type value_type
Typename of values.
int value
Forward-mode AD class using static memory allocation.
Initialize the derivative array.
Sacado::FAD_NS::ViewFad< const ValueType, length, stride, Sacado::FAD_NS::SFad< ValueType, N > > type
GeneralFad< ViewStorage< T, static_length, static_stride, U > > ViewFad
#define SACADO_INLINE_FUNCTION
static const bool value
GeneralFad< StaticFixedStorage< T, Num > > SFad
SACADO_INLINE_FUNCTION ~SFad()
Destructor.
#define FAD_NS
Get view type for any Fad type.