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 //
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_KOKKOS)
31 #include "Kokkos_Atomic.hpp"
32 #include "impl/Kokkos_Error.hpp"
33 #endif
34 
35 namespace Sacado {
36 
37  namespace FAD_NS {
38 
40 
46  template <typename ValueT, int Num>
47  class SFad :
48  public Expr< SFadExprTag<ValueT,Num > > {
49 
50  public:
51 
53  typedef Expr< SFadExprTag<ValueT,Num > > ExprType;
54 
56  typedef typename ExprType::value_type value_type;
57 
59  typedef typename ExprType::scalar_type scalar_type;
60 
63 
65  template <typename T>
66  struct apply {
67  typedef SFad<T,Num> type;
68  };
69 
71  template <int N>
72  struct apply_N {
74  };
75 
80 
82 
86  SFad() :
87  ExprType() {}
88 
90 
93  template <typename S>
96  ExprType(x) {}
97 
99 
103  SFad(const int sz, const ValueT & x, const DerivInit zero_out = InitDerivArray) :
104  ExprType(sz,x,zero_out) {}
105 
107 
113  SFad(const int sz, const int i, const ValueT & x) :
114  ExprType(sz,i,x) {}
115 
118  SFad(const SFad& x) :
119  ExprType(static_cast<const ExprType&>(x)) {}
120 
122  template <typename S>
125  ExprType(x) {}
126 
128 
131  ~SFad() {}
132 
134  template <typename S>
136  SACADO_ENABLE_VALUE_FUNC(SFad&) operator=(const S& v) {
137  ExprType::operator=(v);
138  return *this;
139  }
140 
143  SFad& operator=(const SFad& x) {
144  ExprType::operator=(static_cast<const ExprType&>(x));
145  return *this;
146  }
147 
149  template <typename S>
151  SACADO_ENABLE_EXPR_FUNC(SFad&) operator=(const Expr<S>& x)
152  {
153  ExprType::operator=(x);
154  return *this;
155  }
156 
158  template <typename S>
160  SACADO_ENABLE_VALUE_FUNC(SFad&) operator += (const S& x) {
161  ExprType::operator+=(x);
162  return *this;
163  }
164 
166  template <typename S>
168  SACADO_ENABLE_VALUE_FUNC(SFad&) operator -= (const S& x) {
169  ExprType::operator-=(x);
170  return *this;
171  }
172 
174  template <typename S>
176  SACADO_ENABLE_VALUE_FUNC(SFad&) operator *= (const S& x) {
177  ExprType::operator*=(x);
178  return *this;
179  }
180 
182  template <typename S>
184  SACADO_ENABLE_VALUE_FUNC(SFad&) operator /= (const S& x) {
185  ExprType::operator/=(x);
186  return *this;
187  }
188 
191  SFad& operator += (const SFad& x) {
192  ExprType::operator+=(static_cast<const ExprType&>(x));
193  return *this;
194  }
195 
198  SFad& operator -= (const SFad& x) {
199  ExprType::operator-=(static_cast<const ExprType&>(x));
200  return *this;
201  }
202 
205  SFad& operator *= (const SFad& x) {
206  ExprType::operator*=(static_cast<const ExprType&>(x));
207  return *this;
208  }
209 
212  SFad& operator /= (const SFad& x) {
213  ExprType::operator/=(static_cast<const ExprType&>(x));
214  return *this;
215  }
216 
218  template <typename S>
220  SACADO_ENABLE_EXPR_FUNC(SFad&) operator += (const Expr<S>& x) {
221  ExprType::operator+=(x);
222  return *this;
223  }
224 
226  template <typename S>
228  SACADO_ENABLE_EXPR_FUNC(SFad&) operator -= (const Expr<S>& x) {
229  ExprType::operator-=(x);
230  return *this;
231  }
232 
234  template <typename S>
236  SACADO_ENABLE_EXPR_FUNC(SFad&) operator *= (const Expr<S>& x) {
237  ExprType::operator*=(x);
238  return *this;
239  }
240 
242  template <typename S>
244  SACADO_ENABLE_EXPR_FUNC(SFad&) operator /= (const Expr<S>& x) {
245  ExprType::operator/=(x);
246  return *this;
247  }
248 
249  }; // class SFad<ValueT,Num>
250 
251  template <typename T, int Num>
252  std::ostream& operator << (std::ostream& os,
253  const Expr< SFadExprTag<T,Num> >& x) {
254  os << x.val() << " [";
255 
256  for (int i=0; i< x.size(); i++) {
257  os << " " << x.dx(i);
258  }
259 
260  os << " ]";
261  return os;
262  }
263 
264  template <typename T, int N>
265  struct ExprLevel< SFad<T,N> > {
266  static const unsigned value =
267  ExprLevel< typename SFad<T,N>::value_type >::value + 1;
268  };
269 
270  template <typename T, int N>
271  struct IsFadExpr< SFad<T,N> > {
272  static const bool value = true;
273  };
274 
275  } // namespace Fad
276 
277  template <typename T, int N>
278  struct IsFad< FAD_NS::SFad<T,N> > {
279  static const bool value = true;
280  };
281 
282  template <typename T, int N>
283  struct IsExpr< FAD_NS::SFad<T,N> > {
284  static const bool value = true;
285  };
286 
287  template <typename T, int N>
288  struct BaseExprType< FAD_NS::SFad<T,N> > {
290  };
291 
292  template <typename T,unsigned,unsigned> struct ViewFadType;
293  namespace FAD_NS {
294  template <typename,unsigned,unsigned,typename> class ViewFad;
295  }
296 
298  template< class ValueType, int N, unsigned length, unsigned stride >
299  struct ViewFadType< Sacado::FAD_NS::SFad< ValueType, N >, length, stride > {
301 };
302 
304 
307  template< class ValueType, int N, unsigned length, unsigned stride >
308  struct ViewFadType< const Sacado::FAD_NS::SFad< ValueType, N >, length, stride > {
310  };
311 
312 } // namespace Sacado
313 
314 #if defined(HAVE_SACADO_KOKKOS)
315 
316 //-------------------------- Atomic Operators -----------------------
317 
318 namespace Sacado {
319 
320  namespace FAD_NS {
321 
322  // Overload of Kokkos::atomic_add for Fad types.
323  template <typename T, int N>
325  void atomic_add(SFad<T,N>* dst, const SFad<T,N>& x) {
326  using Kokkos::atomic_add;
327 
328  const int xsz = x.size();
329  const int sz = dst->size();
330 
331  // We currently cannot handle resizing since that would need to be
332  // done atomically.
333  if (xsz > sz)
334  Kokkos::abort(
335  "Sacado error: Fad resize within atomic_add() not supported!");
336 
337  if (xsz != sz && sz > 0 && xsz > 0)
338  Kokkos::abort(
339  "Sacado error: Fad assignment of incompatiable sizes!");
340 
341 
342  if (sz > 0 && xsz > 0) {
344  atomic_add(&(dst->fastAccessDx(i)), x.fastAccessDx(i));
345  }
347  atomic_add(&(dst->val()), x.val());
348  }
349 
350  } // namespace Fad
351 
352 } // namespace Sacado
353 
354 #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:573
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.