52 #ifndef SACADO_ELRCACHEFAD_SFAD_HPP
53 #define SACADO_ELRCACHEFAD_SFAD_HPP
63 namespace ELRCacheFad {
66 template <
typename T,
int Num>
70 template <
typename T,
int Num>
class SFad;
79 template <
typename T,
int Num>
94 static const int num_args = 1;
97 static const bool is_linear =
true;
112 template <
typename S>
125 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
127 throw "ELRCacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
141 Expr(
const int sz,
const int i,
const T & x) :
143 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
145 throw "ELRCacheFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
147 throw "ELRCacheFad::SFad() Error: Invalid derivative index.";
158 for (
int i=0; i<Num; i++)
163 template <
typename S>
166 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
168 throw "ELRCacheFad::SFad() Error: Attempt to assign with incompatible sizes";
173 this->
val() = x.val();
176 LocalAccumOp< Expr<S> > op(x);
179 for(
int i=0; i<Num; ++i) {
205 void diff(
const int ith,
const int n) {
206 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
208 throw "ELRCacheFad::diff() Error: Supplied derivative dimension does not match compile time length.";
222 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
224 throw "ELRCacheFad::resize() Error: Cannot resize fixed derivative array dimension";
253 template <
typename S>
257 if (x.size() != this->size())
return false;
258 bool eq = IE::eval(x.val(), this->
val());
259 for (
int i=0; i<this->size(); i++)
260 eq = eq && IE::eval(x.dx(i), this->
dx(i));
273 const T&
val()
const {
return val_;}
288 int size()
const {
return Num;}
311 const T*
dx()
const {
return &(dx_[0]);}
315 const T&
dx(
int i)
const {
return dx_[i]; }
334 dots[0] = this->dx_[i];
359 template <
typename S>
369 Expr& operator=(
const Expr& x) {
376 for (
int i=0; i<Num; i++)
383 template <
typename S>
386 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
388 throw "ELRCacheFad::operator=() Error: Attempt to assign with incompatible sizes";
394 LocalAccumOp< Expr<S> > op(x);
397 for(
int i=0; i<Num; ++i) {
409 this->
val() = x.val();
422 template <
typename S>
430 template <
typename S>
438 template <
typename S>
442 for (
int i=0; i<Num; ++i)
448 template <
typename S>
452 for (
int i=0; i<Num; ++i)
458 template <
typename S>
461 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
463 throw "ELRCacheFad::operator+=() Error: Attempt to assign with incompatible sizes";
469 LocalAccumOp< Expr<S> > op(x);
472 for(
int i=0; i<Num; ++i) {
484 this->
val() += x.val();
490 template <
typename S>
493 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
495 throw "ELRCacheFad::operator-=() Error: Attempt to assign with incompatible sizes";
501 LocalAccumOp< Expr<S> > op(x);
504 for(
int i=0; i<Num; ++i) {
516 this->
val() -= x.val();
522 template <
typename S>
529 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
531 throw "ELRCacheFad::operator*=() Error: Attempt to assign with incompatible sizes";
535 LocalAccumOp< Expr<S> > op(x);
538 for(
int i=0; i<Num; ++i) {
547 dx_[i] = val_ * op.t + dx_[i] * xval;
557 template <
typename S>
564 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
566 throw "ELRCacheFad::operator/=() Error: Attempt to assign with incompatible sizes";
570 LocalAccumOp< Expr<S> > op(x);
575 for(
int i=0; i<Num; ++i) {
584 dx_[i] = (dx_[i] * xval - val_ * op.t) / xval2;
610 template <
typename ExprT>
611 struct LocalAccumOp {
613 static const int N = ExprT::num_args;
622 template <
typename ArgT>
624 void operator () (ArgT arg)
const {
625 const int Arg = ArgT::value;
626 if (x.template isActive<Arg>())
627 t += partials[Arg] * x.template getTangent<Arg>(i);
637 #define FAD_NS ELRCacheFad
644 #endif // SACADO_ELRCACHEFAD_SFAD_HPP
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 Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
ScalarType< value_type >::type scalar_type
Typename of scalar's (which may be different from T)
KOKKOS_INLINE_FUNCTION Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
KOKKOS_INLINE_FUNCTION const T * dx() const
Returns derivative array.
KOKKOS_INLINE_FUNCTION const value_type * getDx(int j) const
Get dx array.
KOKKOS_INLINE_FUNCTION const T & val() const
Returns value.
KOKKOS_INLINE_FUNCTION LocalAccumOp(const ExprT &x_)
#define SACADO_ENABLE_VALUE_CTOR_DECL
KOKKOS_INLINE_FUNCTION void zero()
Zero out the derivative array.
KOKKOS_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
KOKKOS_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
#define SACADO_ENABLE_EXPR_CTOR_DECL
static KOKKOS_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
#define KOKKOS_INLINE_FUNCTION
KOKKOS_INLINE_FUNCTION void computePartials(const T &bar, value_type partials[]) const
Return partials w.r.t. arguments.
KOKKOS_INLINE_FUNCTION bool isActive() const
Return whether argument is active.
KOKKOS_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
KOKKOS_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
Base template specification for testing equivalence.
KOKKOS_INLINE_FUNCTION ~Expr()
Destructor.
KOKKOS_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
KOKKOS_INLINE_FUNCTION int size() const
Returns number of derivative components.
Wrapper for a generic expression template.
ExprT::value_type value_type
RemoveConst< T >::type value_type
Typename of values.
KOKKOS_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
KOKKOS_INLINE_FUNCTION Expr()
Default constructor.
KOKKOS_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
KOKKOS_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
KOKKOS_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
Initialize the derivative array.
KOKKOS_INLINE_FUNCTION T & val()
Returns value.
KOKKOS_INLINE_FUNCTION void getTangents(int i, value_type dots[]) const
Return tangent component i of arguments.
KOKKOS_INLINE_FUNCTION void cache() const
Cache values.
KOKKOS_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
KOKKOS_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
KOKKOS_INLINE_FUNCTION T getTangent(int i) const
Return tangent component i of argument Arg.
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 void setIsConstant(bool is_const)
Set whether variable is constant.
A tag for specializing Expr for SFad expressions.
KOKKOS_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.