Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_Fad_SFad.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 //
29 // The forward-mode AD classes in Sacado are a derivative work of the
30 // expression template classes in the Fad package by Nicolas Di Cesare.
31 // The following banner is included in the original Fad source code:
32 //
33 // ************ DO NOT REMOVE THIS BANNER ****************
34 //
35 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
36 // http://www.ann.jussieu.fr/~dicesare
37 //
38 // CEMRACS 98 : C++ courses,
39 // templates : new C++ techniques
40 // for scientific computing
41 //
42 //********************************************************
43 //
44 // A short implementation ( not all operators and
45 // functions are overloaded ) of 1st order Automatic
46 // Differentiation in forward mode (FAD) using
47 // EXPRESSION TEMPLATES.
48 //
49 //********************************************************
50 // @HEADER
51 
52 #ifndef SACADO_FAD_SFAD_HPP
53 #define SACADO_FAD_SFAD_HPP
54 
58 
59 namespace Sacado {
60 
62  namespace Fad {
63 
65  template <typename T, int Num>
66  struct SFadExprTag {};
67 
68  // Forward declaration
69  template <typename T, int Num> class SFad;
70 
78  template <typename T, int Num>
79  class Expr< SFadExprTag<T,Num>, ExprSpecDefault > {
80 
81  public:
82 
84  typedef typename RemoveConst<T>::type value_type;
85 
88 
91 
96 
99  Expr() : val_( T(0.)) {
100  ss_array<T>::zero(dx_, Num); }
101 
103 
106  template <typename S>
109  val_(x) {
110  ss_array<T>::zero(dx_, Num);
111  }
112 
114 
118  Expr(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) : val_(x) {
119 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
120  if (sz != Num)
121  throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
122 #endif
123  if (zero_out == InitDerivArray)
124  ss_array<T>::zero(dx_, Num);
125  }
126 
128 
134  Expr(const int sz, const int i, const T & x) :
135  val_(x) {
136 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
137  if (sz != Num)
138  throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
139  if (i >= Num)
140  throw "SFad::SFad() Error: Invalid derivative index.";
141 #endif
142 
143  ss_array<T>::zero(dx_, Num);
144  dx_[i]=1.;
145  }
146 
149  Expr(const Expr& x) :
150  val_(x.val()) {
151  for (int i=0; i<Num; i++)
152  dx_[i] = x.dx_[i];
153  }
154 
156  template <typename S>
159 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
160  if (x.size() != Num)
161  throw "SFad::SFad() Error: Attempt to assign with incompatible sizes";
162 #endif
163 
164  for(int i=0; i<Num; ++i)
165  dx_[i] = x.fastAccessDx(i);
166 
167  this->val() = x.val();
168  }
169 
172  ~Expr() {}
173 
175 
182  void diff(const int ith, const int n) {
183 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
184  if (n != Num)
185  throw "SFad::diff() Error: Supplied derivative dimension does not match compile time length.";
186 #endif
187 
188  ss_array<T>::zero(dx_, Num);
189  dx_[ith] = T(1.);
190  }
191 
193 
198  void resize(int sz) {
199 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
200  if (sz != Num)
201  throw "SFad::resize() Error: Cannot resize fixed derivative array dimension";
202 #endif
203  }
204 
206 
211  void expand(int sz) { resize(sz); }
212 
215  void zero() { ss_array<T>::zero(dx_, Num); }
216 
219  void setUpdateValue(bool update_val) { }
220 
223  bool updateValue() const { return true; }
224 
226  template <typename S>
228  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
229  typedef IsEqual<value_type> IE;
230  if (x.size() != this->size()) return false;
231  bool eq = IE::eval(x.val(), this->val());
232  for (int i=0; i<this->size(); i++)
233  eq = eq && IE::eval(x.dx(i), this->dx(i));
234  return eq;
235  }
236 
238 
243 
246  const T& val() const { return val_;}
247 
250  T& val() { return val_;}
251 
253 
258 
261  int size() const { return Num;}
262 
268  int availableSize() const { return Num; }
269 
272  bool hasFastAccess() const { return true; }
273 
276  bool isPassive() const { return false; }
277 
280  void setIsConstant(bool is_const) {}
281 
284  const T* dx() const { return &(dx_[0]);}
285 
288  const T& dx(int i) const { return dx_[i]; }
289 
292  T& fastAccessDx(int i) { return dx_[i];}
293 
296  const T& fastAccessDx(int i) const { return dx_[i];}
297 
299 
304 
306  template <typename S>
308  SACADO_ENABLE_VALUE_FUNC(Expr&) operator=(const S& v) {
309  val_ = v;
310  ss_array<T>::zero(dx_, Num);
311  return *this;
312  }
313 
316  Expr& operator=(const Expr& x) {
317  if (this != &x) {
318  // Copy value
319  val_ = x.val_;
320 
321  // Copy dx_
322  for (int i=0; i<Num; i++)
323  dx_[i] = x.dx_[i];
324  }
325  return *this;
326  }
327 
329  template <typename S>
331  SACADO_ENABLE_EXPR_FUNC(Expr&) operator=(const Expr<S>& x) {
332 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
333  if (x.size() != Num)
334  throw "SFad::operator=() Error: Attempt to assign with incompatible sizes";
335 #endif
336 
337  for(int i=0; i<Num; ++i)
338  dx_[i] = x.fastAccessDx(i);
339 
340  val_ = x.val();
341 
342  return *this;
343  }
344 
346 
351 
353  template <typename S>
355  SACADO_ENABLE_VALUE_FUNC(Expr&) operator += (const S& v) {
356  this->val() += v;
357  return *this;
358  }
359 
361  template <typename S>
363  SACADO_ENABLE_VALUE_FUNC(Expr&) operator -= (const S& v) {
364  this->val() -= v;
365  return *this;
366  }
367 
369  template <typename S>
371  SACADO_ENABLE_VALUE_FUNC(Expr&) operator *= (const S& v) {
372  this->val() *= v;
373  for (int i=0; i<Num; ++i)
374  dx_[i] *= v;
375  return *this;
376  }
377 
379  template <typename S>
381  SACADO_ENABLE_VALUE_FUNC(Expr&) operator /= (const S& v) {
382  this->val() /= v;
383  for (int i=0; i<Num; ++i)
384  dx_[i] /= v;
385  return *this;
386  }
387 
389  template <typename S>
391  SACADO_ENABLE_EXPR_FUNC(Expr&) operator += (const Expr<S>& x) {
392 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
393  if (x.size() != Num)
394  throw "SFad::operator+=() Error: Attempt to assign with incompatible sizes";
395 #endif
396 
397  for (int i=0; i<Num; ++i)
398  dx_[i] += x.fastAccessDx(i);
399 
400  val_ += x.val();
401 
402  return *this;
403  }
404 
406  template <typename S>
408  SACADO_ENABLE_EXPR_FUNC(Expr&) operator -= (const Expr<S>& x) {
409 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
410  if (x.size() != Num)
411  throw "SFad::operator-=() Error: Attempt to assign with incompatible sizes";
412 #endif
413 
414  for(int i=0; i<Num; ++i)
415  dx_[i] -= x.fastAccessDx(i);
416 
417  val_ -= x.val();
418 
419  return *this;
420  }
421 
423  template <typename S>
425  SACADO_ENABLE_EXPR_FUNC(Expr&) operator *= (const Expr<S>& x) {
426  T xval = x.val();
427 
428 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
429  if (x.size() != Num)
430  throw "SFad::operator*=() Error: Attempt to assign with incompatible sizes";
431 #endif
432 
433  for(int i=0; i<Num; ++i)
434  dx_[i] = val_ * x.fastAccessDx(i) + dx_[i] * xval;
435 
436  val_ *= xval;
437 
438  return *this;
439  }
440 
442  template <typename S>
444  SACADO_ENABLE_EXPR_FUNC(Expr&) operator /= (const Expr<S>& x) {
445  T xval = x.val();
446 
447 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
448  if (x.size() != Num)
449  throw "SFad::operator/=() Error: Attempt to assign with incompatible sizes";
450 #endif
451 
452  for(int i=0; i<Num; ++i)
453  dx_[i] = ( dx_[i]*xval - val_*x.fastAccessDx(i) )/ (xval*xval);
454 
455  val_ /= xval;
456 
457  return *this;
458  }
459 
461 
462  protected:
463 
465  T dx_[Num];
466 
469 
470  }; // class Expr<SFadExprTag>
471 
472  } // namespace Fad
473 
474 } // namespace Sacado
475 
476 #define FAD_NS Fad
477 #include "Sacado_Fad_SFad_tmpl.hpp"
478 #undef FAD_NS
479 
480 #include "Sacado_Fad_ViewFad.hpp"
481 #include "Sacado_Fad_Ops.hpp"
482 
483 #endif // SACADO_FAD_SFAD_HPP
Wrapper for a generic expression template.
expr val()
KOKKOS_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
#define SACADO_ENABLE_VALUE_CTOR_DECL
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION void zero()
Zero out the derivative array.
KOKKOS_INLINE_FUNCTION Expr()
Default constructor.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
KOKKOS_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
RemoveConst< T >::type value_type
Typename of values.
#define SACADO_ENABLE_EXPR_CTOR_DECL
KOKKOS_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
static KOKKOS_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
KOKKOS_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
KOKKOS_INLINE_FUNCTION Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
Base template specification for testing equivalence.
KOKKOS_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
KOKKOS_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
KOKKOS_INLINE_FUNCTION SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
KOKKOS_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from T)
KOKKOS_INLINE_FUNCTION Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
KOKKOS_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION Expr(const int sz, const int i, const T &x)
Constructor with size sz, index i, and value x.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
expr expr dx(i)
A tag for specializing Expr for SFad expressions.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.