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_DFad_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 
29  template <typename ValueT>
30  class DFad :
31  public Expr< GeneralFad<ValueT,Fad::DynamicStorage<ValueT> > > {
32 
33  public:
34 
37  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
38  typedef Expr<GeneralFadType> ExprType;
39 
41  typedef typename ExprType::value_type value_type;
42 
44  typedef typename ExprType::scalar_type scalar_type;
45 
48 
50  template <typename T>
51  struct apply {
52  typedef DFad<T> type;
53  };
54 
56  template <int N>
57  struct apply_N {
58  typedef DFad<ValueT> type;
59  };
60 
65 
67 
71  DFad() :
72  ExprType() {}
73 
75 
78  template <typename S>
81  ExprType(x) {}
82 
84 
88  DFad(const int sz, const ValueT& x, const DerivInit zero_out = InitDerivArray) :
89  ExprType(sz,x,zero_out) {}
90 
92 
98  DFad(const int sz, const int i, const ValueT & x) :
99  ExprType(sz,i,x) {}
100 
103  DFad(const DFad& x) :
104  ExprType(x) {}
105 
107  template <typename S>
110  ExprType(x) {}
111 
113 
116  ~DFad() {}
117 
119  template <typename S>
121  SACADO_ENABLE_VALUE_FUNC(DFad&) operator=(const S& v) {
122  GeneralFadType::operator=(v);
123  return *this;
124  }
125 
128  DFad& operator=(const DFad& x) {
129  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
130  return *this;
131  }
132 
134  template <typename S>
136  SACADO_ENABLE_EXPR_FUNC(DFad&) operator=(const Expr<S>& x)
137  {
138  GeneralFadType::operator=(x);
139  return *this;
140  }
141 
143  template <typename S>
145  SACADO_ENABLE_VALUE_FUNC(DFad&) operator += (const S& x) {
146  GeneralFadType::operator+=(x);
147  return *this;
148  }
149 
151  template <typename S>
153  SACADO_ENABLE_VALUE_FUNC(DFad&) operator -= (const S& x) {
154  GeneralFadType::operator-=(x);
155  return *this;
156  }
157 
159  template <typename S>
161  SACADO_ENABLE_VALUE_FUNC(DFad&) operator *= (const S& x) {
162  GeneralFadType::operator*=(x);
163  return *this;
164  }
165 
167  template <typename S>
169  SACADO_ENABLE_VALUE_FUNC(DFad&) operator /= (const S& x) {
170  GeneralFadType::operator/=(x);
171  return *this;
172  }
173 
176  DFad& operator += (const DFad& x) {
177  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
178  return *this;
179  }
180 
183  DFad& operator -= (const DFad& x) {
184  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
185  return *this;
186  }
187 
190  DFad& operator *= (const DFad& x) {
191  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
192  return *this;
193  }
194 
197  DFad& operator /= (const DFad& x) {
198  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
199  return *this;
200  }
201 
203  template <typename S>
205  SACADO_ENABLE_EXPR_FUNC(DFad&) operator += (const Expr<S>& x) {
206  GeneralFadType::operator+=(x);
207  return *this;
208  }
209 
211  template <typename S>
213  SACADO_ENABLE_EXPR_FUNC(DFad&) operator -= (const Expr<S>& x) {
214  GeneralFadType::operator-=(x);
215  return *this;
216  }
217 
219  template <typename S>
221  SACADO_ENABLE_EXPR_FUNC(DFad&) operator *= (const Expr<S>& x) {
222  GeneralFadType::operator*=(x);
223  return *this;
224  }
225 
227  template <typename S>
229  SACADO_ENABLE_EXPR_FUNC(DFad&) operator /= (const Expr<S>& x) {
230  GeneralFadType::operator/=(x);
231  return *this;
232  }
233 
234  }; // class DFad<ValueT>
235 
236  template <typename T>
237  struct BaseExpr< GeneralFad<T,Fad::DynamicStorage<T> > > {
239  };
240 
241  template <typename T>
242  struct ExprLevel< DFad<T> > {
243  static const unsigned value =
244  ExprLevel< typename DFad<T>::value_type >::value + 1;
245  };
246 
247  template <typename T>
248  struct IsFadExpr< DFad<T> > {
249  static const bool value = true;
250  };
251 
252  } // namespace Fad
253 
254  template <typename T>
255  struct IsFad< FAD_NS::DFad<T> > {
256  static const bool value = true;
257  };
258 
259  template <typename T>
260  struct IsExpr< FAD_NS::DFad<T> > {
261  static const bool value = true;
262  };
263 
264  template <typename T>
265  struct BaseExprType< FAD_NS::DFad<T> > {
267  };
268 
269  template <typename,unsigned,unsigned> struct ViewFadType;
270  namespace FAD_NS {
271  template <typename,unsigned,unsigned,typename> class ViewFad;
272  }
273 
275  template< class ValueType, unsigned length, unsigned stride >
276  struct ViewFadType< Sacado::FAD_NS::DFad< ValueType >, length, stride > {
278  };
279 
281 
284  template< class ValueType, unsigned length, unsigned stride >
285  struct ViewFadType< const Sacado::FAD_NS::DFad< ValueType >, length, stride > {
287  };
288 
289 } // namespace Sacado
290 
291 #if defined(HAVE_SACADO_KOKKOS)
292 
293 //-------------------------- Atomic Operators -----------------------
294 
295 namespace Sacado {
296 
297  namespace FAD_NS {
298 
299  // Overload of Kokkos::atomic_add for Fad types.
300  template <typename T>
302  void atomic_add(DFad<T>* dst, const DFad<T>& x) {
303  using Kokkos::atomic_add;
304 
305  const int xsz = x.size();
306  const int sz = dst->size();
307 
308  // We currently cannot handle resizing since that would need to be
309  // done atomically.
310  if (xsz > sz)
311  Kokkos::abort(
312  "Sacado error: Fad resize within atomic_add() not supported!");
313 
314  if (xsz != sz && sz > 0 && xsz > 0)
315  Kokkos::abort(
316  "Sacado error: Fad assignment of incompatiable sizes!");
317 
318 
319  if (sz > 0 && xsz > 0) {
321  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
322  }
324  atomic_add(&(dst->val()), x.val());
325  }
326 
327  } // namespace Fad
328 
329 } // namespace Sacado
330 
331 #endif // HAVE_SACADO_KOKKOS
Sacado::FAD_NS::ViewFad< const ValueType, length, stride, Sacado::FAD_NS::DFad< ValueType > > type
SACADO_INLINE_FUNCTION DFad(const int sz, const int i, const ValueT &x)
Constructor with size sz, index i, and value x.
Replace static derivative length.
SACADO_INLINE_FUNCTION ~DFad()
Destructor.
#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.
SACADO_INLINE_FUNCTION DFad(const DFad &x)
Copy constructor.
static const bool value
#define SACADO_ENABLE_EXPR_CTOR_DECL
ScalarType< ValueT >::type ScalarT
Typename of scalar&#39;s (which may be different from ValueT)
SACADO_INLINE_FUNCTION DFad(const int sz, const ValueT &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
Is a type an expression.
SACADO_INLINE_FUNCTION DFad()
Default constructor.
#define T
Definition: Sacado_rad.hpp:553
FAD_NS::DFad< T >::base_expr_type type
ExprType::scalar_type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
DFad< typename GeneralFad< T, Fad::DynamicStorage< T > >::value_type > type
GeneralFad< DynamicStorage< T > > DFad
#define SACADO_FAD_DERIV_LOOP(I, SZ)
Get the base Fad type from a view/expression.
Forward-mode AD class using dynamic memory allocation and expression templates.
SACADO_INLINE_FUNCTION DFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
Fad::DynamicStorage< ValueT > StorageType
Base classes.
ExprType::value_type value_type
Typename of values.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION DFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x convertible to ValueT.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
Expr< GeneralFadType > ExprType
int value
Initialize the derivative array.
#define SACADO_INLINE_FUNCTION
static const bool value
Sacado::FAD_NS::ViewFad< ValueType, length, stride, Sacado::FAD_NS::DFad< ValueType > > type
Turn DFad into a meta-function class usable with mpl::apply.
GeneralFad< ValueT, StorageType > GeneralFadType
#define FAD_NS
Get view type for any Fad type.