Sacado Package Browser (Single Doxygen Collection)  Version of the Day
Sacado_CacheFad_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_CACHEFAD_SFAD_HPP
53 #define SACADO_CACHEFAD_SFAD_HPP
54 
58 
59 namespace Sacado {
60 
62  namespace CacheFad {
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> > {
80 
81  public:
82 
84  typedef typename RemoveConst<T>::type value_type;
85 
88 
91 
96 
99  Expr() : val_( T(0.)) { ss_array<T>::zero(dx_, Num); }
100 
102 
105  template <typename S>
108  val_(x) {
109  ss_array<T>::zero(dx_, Num);
110  }
111 
113 
117  Expr(const int sz, const T & x, const DerivInit zero_out = InitDerivArray) : val_(x) {
118 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
119  if (sz != Num)
120  throw "CacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
121 #endif
122 
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 "CacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
139  if (i >= Num)
140  throw "CacheFad::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 "CacheFad::SFad() Error: Attempt to assign with incompatible sizes";
162 #endif
163 
164  x.cache();
165 
166  this->val() = x.val();
167 
168  for(int i=0; i<Num; ++i)
169  dx_[i] = x.fastAccessDx(i);
170  }
171 
174  ~Expr() {}
175 
177 
184  void diff(const int ith, const int n) {
185 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
186  if (n != Num)
187  throw "CacheFad::diff() Error: Supplied derivative dimension does not match compile time length.";
188 #endif
189 
190  ss_array<T>::zero(dx_, Num);
191  dx_[ith] = T(1.);
192  }
193 
195 
200  void resize(int sz) {
201 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
202  if (sz != Num)
203  throw "CacheFad::resize() Error: Cannot resize fixed derivative array dimension";
204 #endif
205  }
206 
208 
213  void expand(int sz) { resize(sz); }
214 
217  void zero() { ss_array<T>::zero(dx_, Num); }
218 
221  void setUpdateValue(bool update_val) { }
222 
225  bool updateValue() const { return true; }
226 
229  void cache() const {}
230 
232  template <typename S>
234  SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr<S>& x) const {
235  typedef IsEqual<value_type> IE;
236  if (x.size() != this->size()) return false;
237  bool eq = IE::eval(x.val(), this->val());
238  for (int i=0; i<this->size(); i++)
239  eq = eq && IE::eval(x.dx(i), this->dx(i));
240  return eq;
241  }
242 
244 
249 
252  const T& val() const { return val_;}
253 
256  T& val() { return val_;}
257 
259 
264 
267  int size() const { return Num;}
268 
274  int availableSize() const { return Num; }
275 
278  bool hasFastAccess() const { return true; }
279 
282  bool isPassive() const { return false; }
283 
286  void setIsConstant(bool is_const) {}
287 
290  const T* dx() const { return &(dx_[0]);}
291 
294  const T& dx(int i) const { return dx_[i]; }
295 
298  T& fastAccessDx(int i) { return dx_[i];}
299 
302  const T& fastAccessDx(int i) const { return dx_[i];}
303 
305 
310 
312  template <typename S>
314  SACADO_ENABLE_VALUE_FUNC(Expr&) operator=(const S& v) {
315  val_ = v;
316  ss_array<T>::zero(dx_, Num);
317  return *this;
318  }
319 
322  Expr& operator=(const Expr& x) {
323  if (this != &x) {
324  // Copy value
325  val_ = x.val_;
326 
327  // Copy dx_
328  for (int i=0; i<Num; i++)
329  dx_[i] = x.dx_[i];
330  }
331  return *this;
332  }
333 
335  template <typename S>
337  SACADO_ENABLE_EXPR_FUNC(Expr&) operator=(const Expr<S>& x) {
338 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
339  if (x.size() != Num)
340  throw "CacheFad::operator=() Error: Attempt to assign with incompatible sizes";
341 #endif
342 
343  x.cache();
344 
345  for(int i=0; i<Num; ++i)
346  dx_[i] = x.fastAccessDx(i);
347 
348  val_ = x.val();
349 
350  return *this;
351  }
352 
354 
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  return *this;
374  }
375 
377  template <typename S>
379  SACADO_ENABLE_VALUE_FUNC(Expr&) operator *= (const S& v) {
380  this->val() *= v;
381  for (int i=0; i<Num; ++i)
382  dx_[i] *= v;
383  return *this;
384  }
385 
387  template <typename S>
389  SACADO_ENABLE_VALUE_FUNC(Expr&) operator /= (const S& v) {
390  this->val() /= v;
391  for (int i=0; i<Num; ++i)
392  dx_[i] /= v;
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 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
420  if (x.size() != Num)
421  throw "CacheFad::operator-=() Error: Attempt to assign with incompatible sizes";
422 #endif
423 
424  x.cache();
425 
426  for(int i=0; i<Num; ++i)
427  dx_[i] -= x.fastAccessDx(i);
428 
429  val_ -= x.val();
430 
431  return *this;
432  }
433 
435  template <typename S>
437  SACADO_ENABLE_EXPR_FUNC(Expr&) operator *= (const Expr<S>& x) {
438  x.cache();
439 
440  T xval = x.val();
441 
442 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
443  if (x.size() != Num)
444  throw "CacheFad::operator*=() Error: Attempt to assign with incompatible sizes";
445 #endif
446 
447  for(int i=0; i<Num; ++i)
448  dx_[i] = val_ * x.fastAccessDx(i) + dx_[i] * xval;
449 
450  val_ *= xval;
451 
452  return *this;
453  }
454 
456  template <typename S>
458  SACADO_ENABLE_EXPR_FUNC(Expr&) operator /= (const Expr<S>& x) {
459  x.cache();
460 
461  T xval = x.val();
462 
463 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
464  if (x.size() != Num)
465  throw "CacheFad::operator/=() Error: Attempt to assign with incompatible sizes";
466 #endif
467 
468  for(int i=0; i<Num; ++i)
469  dx_[i] = ( dx_[i]*xval - val_*x.fastAccessDx(i) )/ (xval*xval);
470 
471  val_ /= xval;
472 
473  return *this;
474  }
475 
477 
478  protected:
479 
481  T dx_[Num];
482 
485 
486  }; // class Expr<SFadExprTag>
487 
488  } // namespace CacheFad
489 
490 } // namespace Sacado
491 
492 #define FAD_NS CacheFad
493 #include "Sacado_Fad_SFad_tmpl.hpp"
494 #undef FAD_NS
495 
497 #include "Sacado_CacheFad_Ops.hpp"
498 
499 #endif // SACADO_CACHEFAD_SFAD_HPP
A tag for specializing Expr for SFad expressions.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
expr val()
#define SACADO_ENABLE_VALUE_CTOR_DECL
KOKKOS_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
#define SACADO_ENABLE_EXPR_CTOR_DECL
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
static KOKKOS_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
RemoveConst< T >::type value_type
Typename of values.
KOKKOS_INLINE_FUNCTION void zero()
Zero out the derivative array.
#define KOKKOS_INLINE_FUNCTION
#define T
Definition: Sacado_rad.hpp:573
KOKKOS_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from T)
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 cache() const
Cache values.
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 const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
KOKKOS_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
KOKKOS_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION Expr()
Default constructor.
KOKKOS_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
expr expr dx(i)
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 bool hasFastAccess() const
Returns true if derivative array is not empty.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
int n
Wrapper for a generic expression template.
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 void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
KOKKOS_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.