52 #ifndef SACADO_FAD_SFAD_HPP
53 #define SACADO_FAD_SFAD_HPP
57 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
63 template <
typename T,
int Num>
64 using SFad = Exp::GeneralFad< Exp::StaticFixedStorage<T,Num> >;
79 #ifndef SACADO_FAD_DERIV_LOOP
80 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
81 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
83 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
87 #ifndef SACADO_FAD_THREAD_SINGLE
88 #if (defined(SACADO_VIEW_CUDA_HIERARCHICAL) || defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD)) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
89 #define SACADO_FAD_THREAD_SINGLE if (threadIdx.x == 0)
91 #define SACADO_FAD_THREAD_SINGLE
96 template <
typename T,
int Num>
100 template <
typename T,
int Num>
class SFad;
109 template <
typename T,
int Num>
137 template <
typename S>
150 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
152 throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
165 Expr(
const int sz,
const int i,
const T &
x) :
167 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
169 throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
171 throw "SFad::SFad() Error: Invalid derivative index.";
182 for (
int i=0;
i<Num;
i++)
187 template <
typename S>
190 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
192 throw "SFad::SFad() Error: Attempt to assign with incompatible sizes";
195 for(
int i=0;
i<Num; ++
i)
196 dx_[
i] = x.fastAccessDx(
i);
198 this->
val() = x.val();
213 void diff(
const int ith,
const int n) {
214 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
216 throw "SFad::diff() Error: Supplied derivative dimension does not match compile time length.";
230 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
232 throw "SFad::resize() Error: Cannot resize fixed derivative array dimension";
261 template <
typename S>
265 if (x.size() != this->size())
return false;
266 bool eq = IE::eval(x.val(), this->
val());
267 for (
int i=0;
i<this->size();
i++)
268 eq = eq && IE::eval(x.dx(
i), this->
dx(
i));
296 int size()
const {
return Num;}
319 const T*
dx()
const {
return &(dx_[0]);}
323 const T&
dx(
int i)
const {
return dx_[
i]; }
341 template <
typename S>
351 Expr& operator=(
const Expr&
x) {
357 for (
int i=0;
i<Num;
i++)
364 template <
typename S>
367 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
369 throw "SFad::operator=() Error: Attempt to assign with incompatible sizes";
372 for(
int i=0;
i<Num; ++
i)
373 dx_[
i] = x.fastAccessDx(
i);
388 template <
typename S>
396 template <
typename S>
404 template <
typename S>
408 for (
int i=0;
i<Num; ++
i)
414 template <
typename S>
418 for (
int i=0;
i<Num; ++
i)
424 template <
typename S>
427 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
429 throw "SFad::operator+=() Error: Attempt to assign with incompatible sizes";
432 for (
int i=0;
i<Num; ++
i)
433 dx_[
i] += x.fastAccessDx(
i);
441 template <
typename S>
444 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
446 throw "SFad::operator-=() Error: Attempt to assign with incompatible sizes";
449 for(
int i=0;
i<Num; ++
i)
450 dx_[
i] -= x.fastAccessDx(
i);
458 template <
typename S>
463 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
465 throw "SFad::operator*=() Error: Attempt to assign with incompatible sizes";
468 for(
int i=0;
i<Num; ++
i)
469 dx_[
i] =
val_ * x.fastAccessDx(
i) + dx_[
i] * xval;
477 template <
typename S>
482 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
484 throw "SFad::operator/=() Error: Attempt to assign with incompatible sizes";
487 for(
int i=0;
i<Num; ++
i)
488 dx_[
i] = ( dx_[
i]*xval -
val_*x.fastAccessDx(
i) )/ (xval*xval);
517 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
521 #endif // SACADO_FAD_SFAD_HPP
SACADO_INLINE_FUNCTION Expr(const S &x, SACADO_ENABLE_VALUE_CTOR_DECL)
Constructor with supplied value x.
Wrapper for a generic expression template.
SACADO_INLINE_FUNCTION void setIsConstant(bool is_const)
Set whether variable is constant.
#define SACADO_ENABLE_VALUE_CTOR_DECL
SACADO_INLINE_FUNCTION bool isPassive() const
Returns true if derivative array is empty.
RemoveConst< T >::type value_type
Typename of values.
#define SACADO_ENABLE_EXPR_CTOR_DECL
SACADO_INLINE_FUNCTION void expand(int sz)
Expand derivative array to size sz.
SACADO_INLINE_FUNCTION void cache() const
Cache values.
SACADO_INLINE_FUNCTION const T & fastAccessDx(int i) const
Returns derivative component i without bounds checking.
SACADO_INLINE_FUNCTION Expr()
Default constructor.
#define SACADO_ENABLE_VALUE_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION Expr(const Expr &x)
Copy constructor.
Base template specification for testing equivalence.
SACADO_INLINE_FUNCTION bool hasFastAccess() const
Returns true if derivative array is not empty.
SACADO_INLINE_FUNCTION void zero()
Zero out the derivative array.
SACADO_INLINE_FUNCTION const T & dx(int i) const
Returns derivative component i with bounds checking.
SACADO_INLINE_FUNCTION void diff(const int ith, const int n)
Set Fad object as the ith independent variable.
SACADO_INLINE_FUNCTION bool updateValue() const
Return whether this Fad object has an updated value.
SACADO_INLINE_FUNCTION int availableSize() const
Returns number of derivative components that can be stored without reallocation.
#define SACADO_ENABLE_EXPR_FUNC(RETURN_TYPE)
SACADO_INLINE_FUNCTION ~Expr()
Destructor.
SACADO_INLINE_FUNCTION T & fastAccessDx(int i)
Returns derivative component i without bounds checking.
DerivInit
Enum use to signal whether the derivative array should be initialized in AD object constructors...
SACADO_INLINE_FUNCTION Expr(const int sz, const int i, const T &x)
Constructor with size sz, index i, and value x.
ScalarType< value_type >::type scalar_type
Typename of scalar's (which may be different from T)
SACADO_INLINE_FUNCTION const T & val() const
Returns value.
SACADO_INLINE_FUNCTION Expr(const int sz, const T &x, const DerivInit zero_out=InitDerivArray)
Constructor with size sz and value x.
static SACADO_INLINE_FUNCTION void zero(T *dest, int sz)
Zero out array dest of length sz.
Initialize the derivative array.
SACADO_INLINE_FUNCTION SACADO_ENABLE_EXPR_FUNC(bool) isEqualTo(const Expr< S > &x) const
Returns whether two Fad objects have the same values.
SACADO_INLINE_FUNCTION T & val()
Returns value.
SACADO_INLINE_FUNCTION void resize(int sz)
Resize derivative array to length sz.
#define SACADO_INLINE_FUNCTION
A tag for specializing Expr for SFad expressions.
SFad< value_type, Num > base_expr_type
Typename of base-expressions.
GeneralFad< StaticFixedStorage< T, Num > > SFad
SACADO_INLINE_FUNCTION int size() const
Returns number of derivative components.
SACADO_INLINE_FUNCTION const T * dx() const
Returns derivative array.
SACADO_INLINE_FUNCTION Expr(const Expr< S > &x, SACADO_ENABLE_EXPR_CTOR_DECL)
Copy constructor from any Expression object.
SACADO_INLINE_FUNCTION void setUpdateValue(bool update_val)
Set whether this Fad object should update values.