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 //
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 
39  // Forward declaration
40  template <typename T, int Num>
42 
56  template <typename ValueT, int Num>
57  class SLFad :
58  public Expr< GeneralFad<ValueT,Fad::StaticStorage<ValueT,Num> > > {
59 
60  public:
61 
64  typedef GeneralFad<ValueT,StorageType> GeneralFadType;
65  typedef Expr<GeneralFadType> ExprType;
66 
68  typedef typename ExprType::value_type value_type;
69 
71  typedef typename ExprType::scalar_type scalar_type;
72 
75 
77  template <typename T>
78  struct apply {
79  typedef SLFad<T,Num> type;
80  };
81 
83 
86  template <int N>
87  struct apply_N {
89  };
90 
95 
97 
101  SLFad() :
102  ExprType() {}
103 
105 
108  template <typename S>
111  ExprType(x) {}
112 
114 
118  SLFad(const int sz, const ValueT & x, const DerivInit zero_out = InitDerivArray) :
119  ExprType(sz,x,zero_out) {}
120 
122 
128  SLFad(const int sz, const int i, const ValueT & x) :
129  ExprType(sz,i,x) {}
130 
133  SLFad(const SLFad& x) :
134  ExprType(x) {}
135 
137  template <typename S>
139  SLFad(const Expr<S>& x, SACADO_ENABLE_EXPR_CTOR_DECL) :
140  ExprType(x) {}
141 
143 
146  ~SLFad() {}
147 
149  template <typename S>
151  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator=(const S& v) {
152  GeneralFadType::operator=(v);
153  return *this;
154  }
155 
158  SLFad& operator=(const SLFad& x) {
159  GeneralFadType::operator=(static_cast<const GeneralFadType&>(x));
160  return *this;
161  }
162 
164  template <typename S>
166  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator=(const Expr<S>& x)
167  {
168  GeneralFadType::operator=(x);
169  return *this;
170  }
171 
173  template <typename S>
175  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator += (const S& x) {
176  GeneralFadType::operator+=(x);
177  return *this;
178  }
179 
181  template <typename S>
183  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator -= (const S& x) {
184  GeneralFadType::operator-=(x);
185  return *this;
186  }
187 
189  template <typename S>
191  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator *= (const S& x) {
192  GeneralFadType::operator*=(x);
193  return *this;
194  }
195 
197  template <typename S>
199  SACADO_ENABLE_VALUE_FUNC(SLFad&) operator /= (const S& x) {
200  GeneralFadType::operator/=(x);
201  return *this;
202  }
203 
206  SLFad& operator += (const SLFad& x) {
207  GeneralFadType::operator+=(static_cast<const GeneralFadType&>(x));
208  return *this;
209  }
210 
213  SLFad& operator -= (const SLFad& x) {
214  GeneralFadType::operator-=(static_cast<const GeneralFadType&>(x));
215  return *this;
216  }
217 
220  SLFad& operator *= (const SLFad& x) {
221  GeneralFadType::operator*=(static_cast<const GeneralFadType&>(x));
222  return *this;
223  }
224 
227  SLFad& operator /= (const SLFad& x) {
228  GeneralFadType::operator/=(static_cast<const GeneralFadType&>(x));
229  return *this;
230  }
231 
233  template <typename S>
235  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator += (const Expr<S>& x) {
236  GeneralFadType::operator+=(x);
237  //*this = *this + x;
238  return *this;
239  }
240 
242  template <typename S>
244  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator -= (const Expr<S>& x) {
245  GeneralFadType::operator-=(x);
246  return *this;
247  }
248 
250  template <typename S>
252  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator *= (const Expr<S>& x) {
253  GeneralFadType::operator*=(x);
254  return *this;
255  }
256 
258  template <typename S>
260  SACADO_ENABLE_EXPR_FUNC(SLFad&) operator /= (const Expr<S>& x) {
261  GeneralFadType::operator/=(x);
262  return *this;
263  }
264 
265  }; // class SLFad<ValueT,Num>
266 
267  template <typename T, int N>
268  struct BaseExpr< GeneralFad<T,Fad::StaticStorage<T,N> > > {
270  };
271 
272  template <typename T, int N>
273  struct ExprLevel< SLFad<T,N> > {
274  static const unsigned value =
275  ExprLevel< typename SLFad<T,N>::value_type >::value + 1;
276  };
277 
278  template <typename T, int N>
279  struct IsFadExpr< SLFad<T,N> > {
280  static const bool value = true;
281  };
282 
283  } // namespace Fad
284 
285  template <typename T, int N>
286  struct IsFad< FAD_NS::SLFad<T,N> > {
287  static const bool value = true;
288  };
289 
290  template <typename T, int N>
291  struct IsExpr< FAD_NS::SLFad<T,N> > {
292  static const bool value = true;
293  };
294 
295  template <typename T, int N>
296  struct BaseExprType< FAD_NS::SLFad<T,N> > {
298  };
299 
300  template <typename T,unsigned,unsigned> struct ViewFadType;
301  namespace FAD_NS {
302  template <typename,unsigned,unsigned,typename> class ViewFad;
303  }
304 
306  template< class ValueType, int N, unsigned length, unsigned stride >
307  struct ViewFadType< Sacado::FAD_NS::SLFad< ValueType, N >, length, stride > {
309  };
310 
312 
315  template< class ValueType, int N, unsigned length, unsigned stride >
316  struct ViewFadType< const Sacado::FAD_NS::SLFad< ValueType, N >, length, stride > {
318  };
319 
320 } // namespace Sacado
321 
322 #if defined(HAVE_SACADO_KOKKOSCORE)
323 
324 //-------------------------- Atomic Operators -----------------------
325 
326 namespace Sacado {
327 
328  namespace FAD_NS {
329 
330  // Overload of Kokkos::atomic_add for Fad types.
331  template <typename T, int N>
333  void atomic_add(SLFad<T,N>* dst, const SLFad<T,N>& x) {
334  using Kokkos::atomic_add;
335 
336  const int xsz = x.size();
337  const int sz = dst->size();
338 
339  // We currently cannot handle resizing since that would need to be
340  // done atomically.
341  if (xsz > sz)
342  Kokkos::abort(
343  "Sacado error: Fad resize within atomic_add() not supported!");
344 
345  if (xsz != sz && sz > 0 && xsz > 0)
346  Kokkos::abort(
347  "Sacado error: Fad assignment of incompatiable sizes!");
348 
349 
350  if (sz > 0 && xsz > 0) {
352  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
353  }
355  atomic_add(&(dst->val()), x.val());
356  }
357 
358  } // namespace Fad
359 
360 } // namespace Sacado
361 
362 #endif // HAVE_SACADO_KOKKOSCORE
Turn SLFad into a meta-function class usable with mpl::apply.
KOKKOS_INLINE_FUNCTION SLFad(const int sz, const ValueT &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
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.
KOKKOS_INLINE_FUNCTION SLFad(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
KOKKOS_INLINE_FUNCTION SLFad(const int sz, const int i, const ValueT &x)
Constructor with size sz, index i, and value x.
Forward-mode AD class using dynamic memory allocation and expression templates.
GeneralFad< StaticStorage< T, Num > > SLFad
KOKKOS_INLINE_FUNCTION SLFad(const SLFad &x)
Copy constructor.
static const bool value
#define SACADO_ENABLE_EXPR_CTOR_DECL
KOKKOS_INLINE_FUNCTION SLFad()
Default constructor.
KOKKOS_INLINE_FUNCTION ~SLFad()
Destructor.
Is a type an expression.
Sacado::FAD_NS::ViewFad< const ValueType, length, stride, Sacado::FAD_NS::SLFad< ValueType, N > > type
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
ExprType::value_type value_type
Typename of values.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
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)
#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)
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
KOKKOS_INLINE_FUNCTION SLFad(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x convertible to ValueT.
static const bool value
#define FAD_NS
SLFad< typename GeneralFad< T, Fad::StaticStorage< T, N > >::value_type, N > type
Get view type for any Fad type.