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 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #if defined(HAVE_SACADO_KOKKOSCORE)
31 #include "Kokkos_Atomic.hpp"
32 #include "impl/Kokkos_Error.hpp"
33 #endif
34 
35 namespace Sacado {
36 
37  namespace FAD_NS {
38 
49  template <typename ValueT>
50  class DFad :
51  public Expr< GeneralFad<ValueT,Fad::DynamicStorage<ValueT> > > {
52 
53  public:
54 
57  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
58  typedef Expr<GeneralFadType> ExprType;
59 
61  typedef typename ExprType::value_type value_type;
62 
64  typedef typename ExprType::scalar_type scalar_type;
65 
68 
70  template <typename T>
71  struct apply {
72  typedef DFad<T> type;
73  };
74 
76  template <int N>
77  struct apply_N {
78  typedef DFad<ValueT> type;
79  };
80 
85 
87 
91  DFad() :
92  ExprType() {}
93 
95 
98  template <typename S>
101  ExprType(x) {}
102 
104 
108  DFad(const int sz, const ValueT& x, const DerivInit zero_out = InitDerivArray) :
109  ExprType(sz,x,zero_out) {}
110 
112 
118  DFad(const int sz, const int i, const ValueT & x) :
119  ExprType(sz,i,x) {}
120 
123  DFad(const DFad& x) :
124  ExprType(x) {}
125 
127  template <typename S>
129  DFad(const Expr<S>& x, SACADO_ENABLE_EXPR_CTOR_DECL) :
130  ExprType(x) {}
131 
133 
136  ~DFad() {}
137 
139  template <typename S>
141  SACADO_ENABLE_VALUE_FUNC(DFad&) operator=(const S& v) {
142  GeneralFadType::operator=(v);
143  return *this;
144  }
145 
148  DFad& operator=(const DFad& x) {
149  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
150  return *this;
151  }
152 
154  template <typename S>
156  SACADO_ENABLE_EXPR_FUNC(DFad&) operator=(const Expr<S>& x)
157  {
158  GeneralFadType::operator=(x);
159  return *this;
160  }
161 
163  template <typename S>
165  SACADO_ENABLE_VALUE_FUNC(DFad&) operator += (const S& x) {
166  GeneralFadType::operator+=(x);
167  return *this;
168  }
169 
171  template <typename S>
173  SACADO_ENABLE_VALUE_FUNC(DFad&) operator -= (const S& x) {
174  GeneralFadType::operator-=(x);
175  return *this;
176  }
177 
179  template <typename S>
181  SACADO_ENABLE_VALUE_FUNC(DFad&) operator *= (const S& x) {
182  GeneralFadType::operator*=(x);
183  return *this;
184  }
185 
187  template <typename S>
189  SACADO_ENABLE_VALUE_FUNC(DFad&) operator /= (const S& x) {
190  GeneralFadType::operator/=(x);
191  return *this;
192  }
193 
196  DFad& operator += (const DFad& x) {
197  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
198  return *this;
199  }
200 
203  DFad& operator -= (const DFad& x) {
204  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
205  return *this;
206  }
207 
210  DFad& operator *= (const DFad& x) {
211  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
212  return *this;
213  }
214 
217  DFad& operator /= (const DFad& x) {
218  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
219  return *this;
220  }
221 
223  template <typename S>
225  SACADO_ENABLE_EXPR_FUNC(DFad&) operator += (const Expr<S>& x) {
226  GeneralFadType::operator+=(x);
227  return *this;
228  }
229 
231  template <typename S>
233  SACADO_ENABLE_EXPR_FUNC(DFad&) operator -= (const Expr<S>& x) {
234  GeneralFadType::operator-=(x);
235  return *this;
236  }
237 
239  template <typename S>
241  SACADO_ENABLE_EXPR_FUNC(DFad&) operator *= (const Expr<S>& x) {
242  GeneralFadType::operator*=(x);
243  return *this;
244  }
245 
247  template <typename S>
249  SACADO_ENABLE_EXPR_FUNC(DFad&) operator /= (const Expr<S>& x) {
250  GeneralFadType::operator/=(x);
251  return *this;
252  }
253 
254  }; // class DFad<ValueT>
255 
256  template <typename T>
257  struct BaseExpr< GeneralFad<T,Fad::DynamicStorage<T> > > {
259  };
260 
261  template <typename T>
262  struct ExprLevel< DFad<T> > {
263  static const unsigned value =
264  ExprLevel< typename DFad<T>::value_type >::value + 1;
265  };
266 
267  template <typename T>
268  struct IsFadExpr< DFad<T> > {
269  static const bool value = true;
270  };
271 
272  } // namespace Fad
273 
274  template <typename T>
275  struct IsFad< FAD_NS::DFad<T> > {
276  static const bool value = true;
277  };
278 
279  template <typename T>
280  struct IsExpr< FAD_NS::DFad<T> > {
281  static const bool value = true;
282  };
283 
284  template <typename T>
285  struct BaseExprType< FAD_NS::DFad<T> > {
287  };
288 
289  template <typename,unsigned,unsigned> struct ViewFadType;
290  namespace FAD_NS {
291  template <typename,unsigned,unsigned,typename> class ViewFad;
292  }
293 
295  template< class ValueType, unsigned length, unsigned stride >
296  struct ViewFadType< Sacado::FAD_NS::DFad< ValueType >, length, stride > {
298  };
299 
301 
304  template< class ValueType, unsigned length, unsigned stride >
305  struct ViewFadType< const Sacado::FAD_NS::DFad< ValueType >, length, stride > {
307  };
308 
309 } // namespace Sacado
310 
311 #if defined(HAVE_SACADO_KOKKOSCORE)
312 
313 //-------------------------- Atomic Operators -----------------------
314 
315 namespace Sacado {
316 
317  namespace FAD_NS {
318 
319  // Overload of Kokkos::atomic_add for Fad types.
320  template <typename T>
322  void atomic_add(DFad<T>* dst, const DFad<T>& x) {
323  using Kokkos::atomic_add;
324 
325  const int xsz = x.size();
326  const int sz = dst->size();
327 
328  // We currently cannot handle resizing since that would need to be
329  // done atomically.
330  if (xsz > sz)
331  Kokkos::abort(
332  "Sacado error: Fad resize within atomic_add() not supported!");
333 
334  if (xsz != sz && sz > 0 && xsz > 0)
335  Kokkos::abort(
336  "Sacado error: Fad assignment of incompatiable sizes!");
337 
338 
339  if (sz > 0 && xsz > 0) {
341  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
342  }
344  atomic_add(&(dst->val()), x.val());
345  }
346 
347  } // namespace Fad
348 
349 } // namespace Sacado
350 
351 #endif // HAVE_SACADO_KOKKOSCORE
Sacado::FAD_NS::ViewFad< const ValueType, length, stride, Sacado::FAD_NS::DFad< ValueType > > type
KOKKOS_INLINE_FUNCTION DFad(const int sz, const ValueT &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
Replace static derivative length.
#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.
KOKKOS_INLINE_FUNCTION DFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
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)
KOKKOS_INLINE_FUNCTION DFad()
Default constructor.
Is a type an expression.
KOKKOS_INLINE_FUNCTION DFad(const DFad &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION DFad(const int sz, const int i, const ValueT &x)
Constructor with size sz, index i, and value x.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
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.
Fad::DynamicStorage< ValueT > StorageType
Base classes.
ExprType::value_type value_type
Typename of values.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
Expr< GeneralFadType > ExprType
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION ~DFad()
Destructor.
KOKKOS_INLINE_FUNCTION DFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x convertible to ValueT.
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.