Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_CacheFad_SFad.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 // ***********************************************************************
9 //
10 // The forward-mode AD classes in Sacado are a derivative work of the
11 // expression template classes in the Fad package by Nicolas Di Cesare.
12 // The following banner is included in the original Fad source code:
13 //
14 // ************ DO NOT REMOVE THIS BANNER ****************
15 //
16 // Nicolas Di Cesare <Nicolas.Dicesare@ann.jussieu.fr>
17 // http://www.ann.jussieu.fr/~dicesare
18 //
19 // CEMRACS 98 : C++ courses,
20 // templates : new C++ techniques
21 // for scientific computing
22 //
23 //********************************************************
24 //
25 // A short implementation ( not all operators and
26 // functions are overloaded ) of 1st order Automatic
27 // Differentiation in forward mode (FAD) using
28 // EXPRESSION TEMPLATES.
29 //
30 //********************************************************
31 // @HEADER
32 
33 #ifndef SACADO_CACHEFAD_SFAD_HPP
34 #define SACADO_CACHEFAD_SFAD_HPP
35 
39 
40 namespace Sacado {
41 
43  namespace CacheFad {
44 
46  template <typename T, int Num>
47  struct SFadExprTag {};
48 
49  // Forward declaration
50  template <typename T, int Num> class SFad;
51 
59  template <typename T, int Num>
60  class Expr< SFadExprTag<T,Num> > {
61 
62  public:
63 
65  typedef typename RemoveConst<T>::type value_type;
66 
69 
72 
77 
80  Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
81 
83 
86  template <typename S>
89  val_(x) {
90  ss_array<T>::zero(dx_, Num);
91  }
92 
94 
98  Expr(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) : val_(x) {
99 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
100  if (sz != Num)
101  throw "CacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
102 #endif
103 
104  if (zero_out == InitDerivArray)
105  ss_array<T>::zero(dx_, Num);
106  }
107 
109 
115  Expr(const int sz, const int i, const T & x) :
116  val_(x) {
117 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
118  if (sz != Num)
119  throw "CacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
120  if (i >= Num)
121  throw "CacheFad::SFad() Error: Invalid derivative index.";
122 #endif
123 
124  ss_array<T>::zero(dx_, Num);
125  dx_[i]=1.;
126  }
127 
130  Expr(const Expr& x) :
131  val_(x.val()) {
132  for (int i=0; i<Num; i++)
133  dx_[i] = x.dx_[i];
134  }
135 
137  template <typename S>
140 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
141  if (x.size() != Num)
142  throw "CacheFad::SFad() Error: Attempt to assign with incompatible sizes";
143 #endif
144 
145  x.cache();
146 
147  this->val() = x.val();
148 
149  for(int i=0; i<Num; ++i)
150  dx_[i] = x.fastAccessDx(i);
151  }
152 
155  ~Expr() {}
156 
158 
165  void diff(const int ith, const int n) {
166 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
167  if (n != Num)
168  throw "CacheFad::diff() Error: Supplied derivative dimension does not match compile time length.";
169 #endif
170 
171  ss_array<T>::zero(dx_, Num);
172  dx_[ith] = T(1.);
173  }
174 
176 
181  void resize(int sz) {
182 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
183  if (sz != Num)
184  throw "CacheFad::resize() Error: Cannot resize fixed derivative array dimension";
185 #endif
186  }
187 
189 
194  void expand(int sz) { resize(sz); }
195 
198  void zero() { ss_array<T>::zero(dx_, Num); }
199 
202  void setUpdateValue(bool update_val) { }
203 
206  bool updateValue() const { return true; }
207 
210  void cache() const {}
211 
213  template <typename S>
215  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
216  typedef IsEqual<value_type> IE;
217  if (x.size() != this->size()) return false;
218  bool eq = IE::eval(x.val(), this->val());
219  for (int i=0; i<this->size(); i++)
220  eq = eq && IE::eval(x.dx(i), this->dx(i));
221  return eq;
222  }
223 
225 
230 
233  const T& val() const { return val_;}
234 
237  T& val() { return val_;}
238 
240 
245 
248  int size() const { return Num;}
249 
255  int availableSize() const { return Num; }
256 
259  bool hasFastAccess() const { return true; }
260 
263  bool isPassive() const { return false; }
264 
267  void setIsConstant(bool is_const) {}
268 
271  const T* dx() const { return &(dx_[0]);}
272 
275  const T& dx(int i) const { return dx_[i]; }
276 
279  T& fastAccessDx(int i) { return dx_[i];}
280 
283  const T& fastAccessDx(int i) const { return dx_[i];}
284 
286 
291 
293  template <typename S>
295  SACADO_ENABLE_VALUE_FUNC(Expr&) operator=(const S& v) {
296  val_ = v;
297  ss_array<T>::zero(dx_, Num);
298  return *this;
299  }
300 
303  Expr& operator=(const Expr& x) {
304  if (this != &x) {
305  // Copy value
306  val_ = x.val_;
307 
308  // Copy dx_
309  for (int i=0; i<Num; i++)
310  dx_[i] = x.dx_[i];
311  }
312  return *this;
313  }
314 
316  template <typename S>
318  SACADO_ENABLE_EXPR_FUNC(Expr&) operator=(const Expr<S>& x) {
319 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
320  if (x.size() != Num)
321  throw "CacheFad::operator=() Error: Attempt to assign with incompatible sizes";
322 #endif
323 
324  x.cache();
325 
326  for(int i=0; i<Num; ++i)
327  dx_[i] = x.fastAccessDx(i);
328 
329  val_ = x.val();
330 
331  return *this;
332  }
333 
335 
340 
342  template <typename S>
344  SACADO_ENABLE_VALUE_FUNC(Expr&) operator += (const S& v) {
345  this->val() += v;
346  return *this;
347  }
348 
350  template <typename S>
352  SACADO_ENABLE_VALUE_FUNC(Expr&) operator -= (const S& v) {
353  this->val() -= v;
354  return *this;
355  }
356 
358  template <typename S>
360  SACADO_ENABLE_VALUE_FUNC(Expr&) operator *= (const S& v) {
361  this->val() *= v;
362  for (int i=0; i<Num; ++i)
363  dx_[i] *= v;
364  return *this;
365  }
366 
368  template <typename S>
370  SACADO_ENABLE_VALUE_FUNC(Expr&) operator /= (const S& v) {
371  this->val() /= v;
372  for (int i=0; i<Num; ++i)
373  dx_[i] /= v;
374  return *this;
375  }
376 
378  template <typename S>
380  SACADO_ENABLE_EXPR_FUNC(Expr&) operator += (const Expr<S>& x) {
381 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
382  if (x.size() != Num)
383  throw "CacheFad::operator+=() Error: Attempt to assign with incompatible sizes";
384 #endif
385 
386  x.cache();
387 
388  for (int i=0; i<Num; ++i)
389  dx_[i] += x.fastAccessDx(i);
390 
391  val_ += x.val();
392 
393  return *this;
394  }
395 
397  template <typename S>
399  SACADO_ENABLE_EXPR_FUNC(Expr&) operator -= (const Expr<S>& x) {
400 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
401  if (x.size() != Num)
402  throw "CacheFad::operator-=() Error: Attempt to assign with incompatible sizes";
403 #endif
404 
405  x.cache();
406 
407  for(int i=0; i<Num; ++i)
408  dx_[i] -= x.fastAccessDx(i);
409 
410  val_ -= x.val();
411 
412  return *this;
413  }
414 
416  template <typename S>
418  SACADO_ENABLE_EXPR_FUNC(Expr&) operator *= (const Expr<S>& x) {
419  x.cache();
420 
421  T xval = x.val();
422 
423 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
424  if (x.size() != Num)
425  throw "CacheFad::operator*=() Error: Attempt to assign with incompatible sizes";
426 #endif
427 
428  for(int i=0; i<Num; ++i)
429  dx_[i] = val_ * x.fastAccessDx(i) + dx_[i] * xval;
430 
431  val_ *= xval;
432 
433  return *this;
434  }
435 
437  template <typename S>
439  SACADO_ENABLE_EXPR_FUNC(Expr&) operator /= (const Expr<S>& x) {
440  x.cache();
441 
442  T xval = x.val();
443 
444 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
445  if (x.size() != Num)
446  throw "CacheFad::operator/=() Error: Attempt to assign with incompatible sizes";
447 #endif
448 
449  for(int i=0; i<Num; ++i)
450  dx_[i] = ( dx_[i]*xval - val_*x.fastAccessDx(i) )/ (xval*xval);
451 
452  val_ /= xval;
453 
454  return *this;
455  }
456 
458 
459  protected:
460 
462  T dx_[Num];
463 
466 
467  }; // class Expr<SFadExprTag>
468 
469  } // namespace CacheFad
470 
471 } // namespace Sacado
472 
473 #define FAD_NS CacheFad
474 #include "Sacado_Fad_SFad_tmpl.hpp"
475 #undef FAD_NS
476 
478 #include "Sacado_CacheFad_Ops.hpp"
479 
480 #endif // SACADO_CACHEFAD_SFAD_HPP
A tag for specializing Expr for SFad expressions.
SACADO_INLINE_FUNCTION Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
SACADO_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
expr expr dx(i)
SACADO_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
#define SACADO_ENABLE_VALUE_CTOR_DECL
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
#define SACADO_ENABLE_EXPR_CTOR_DECL
RemoveConst< T >::type value_type
Typename of values.
SACADO_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
SACADO_INLINE_FUNCTION Expr(const int sz, const int i, const T &x)
Constructor with size sz, index i, and value x.
expr val()
#define T
Definition: Sacado_rad.hpp:553
SACADO_INLINE_FUNCTION Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from T)
Base template specification for testing equivalence.
SACADO_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
SACADO_INLINE_FUNCTION Expr()
Default constructor.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
SACADO_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION void cache() const
Cache values.
SACADO_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
Initialize the derivative array.
SACADO_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
#define SACADO_INLINE_FUNCTION
SACADO_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
Wrapper for a generic expression template.
SACADO_INLINE_FUNCTION void zero()
Zero out the derivative array.
SACADO_INLINE_FUNCTION T & val()
Returns value.