33 #ifndef SACADO_FAD_SFAD_HPP
34 #define SACADO_FAD_SFAD_HPP
38 #ifdef SACADO_NEW_FAD_DESIGN_IS_DEFAULT
44 template <
typename T,
int Num>
45 using SFad = Exp::GeneralFad< Exp::StaticFixedStorage<T,Num> >;
60 #ifndef SACADO_FAD_DERIV_LOOP
61 #if defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
62 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=threadIdx.x; I<SZ; I+=blockDim.x)
64 #define SACADO_FAD_DERIV_LOOP(I,SZ) for (int I=0; I<SZ; ++I)
68 #ifndef SACADO_FAD_THREAD_SINGLE
69 #if (defined(SACADO_VIEW_CUDA_HIERARCHICAL) || defined(SACADO_VIEW_CUDA_HIERARCHICAL_DFAD)) && !defined(SACADO_DISABLE_CUDA_IN_KOKKOS) && defined(__CUDA_ARCH__)
70 #define SACADO_FAD_THREAD_SINGLE if (threadIdx.x == 0)
72 #define SACADO_FAD_THREAD_SINGLE
77 template <
typename T,
int Num>
81 template <
typename T,
int Num>
class SFad;
90 template <
typename T,
int Num>
118 template <
typename S>
131 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
133 throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
146 Expr(
const int sz,
const int i,
const T &
x) :
148 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
150 throw "SFad::SFad() Error: Supplied derivative dimension does not match compile time length.";
152 throw "SFad::SFad() Error: Invalid derivative index.";
163 for (
int i=0;
i<Num;
i++)
168 template <
typename S>
171 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
173 throw "SFad::SFad() Error: Attempt to assign with incompatible sizes";
176 for(
int i=0;
i<Num; ++
i)
177 dx_[
i] = x.fastAccessDx(
i);
179 this->
val() = x.val();
194 void diff(
const int ith,
const int n) {
195 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
197 throw "SFad::diff() Error: Supplied derivative dimension does not match compile time length.";
211 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
213 throw "SFad::resize() Error: Cannot resize fixed derivative array dimension";
242 template <
typename S>
246 if (x.size() != this->size())
return false;
247 bool eq = IE::eval(x.val(), this->
val());
248 for (
int i=0;
i<this->size();
i++)
249 eq = eq && IE::eval(x.dx(
i), this->
dx(
i));
277 int size()
const {
return Num;}
300 const T*
dx()
const {
return &(dx_[0]);}
304 const T&
dx(
int i)
const {
return dx_[
i]; }
322 template <
typename S>
332 Expr& operator=(
const Expr&
x) {
338 for (
int i=0;
i<Num;
i++)
345 template <
typename S>
348 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
350 throw "SFad::operator=() Error: Attempt to assign with incompatible sizes";
353 for(
int i=0;
i<Num; ++
i)
354 dx_[
i] = x.fastAccessDx(
i);
369 template <
typename S>
377 template <
typename S>
385 template <
typename S>
389 for (
int i=0;
i<Num; ++
i)
395 template <
typename S>
399 for (
int i=0;
i<Num; ++
i)
405 template <
typename S>
408 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
410 throw "SFad::operator+=() Error: Attempt to assign with incompatible sizes";
413 for (
int i=0;
i<Num; ++
i)
414 dx_[
i] += x.fastAccessDx(
i);
422 template <
typename S>
425 #if defined(SACADO_DEBUG) && !defined(__CUDA_ARCH__ )
427 throw "SFad::operator-=() Error: Attempt to assign with incompatible sizes";
430 for(
int i=0;
i<Num; ++
i)
431 dx_[
i] -= x.fastAccessDx(
i);
439 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] =
val_ * x.fastAccessDx(
i) + dx_[
i] * xval;
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] = ( dx_[
i]*xval -
val_*x.fastAccessDx(
i) )/ (xval*xval);
498 #endif // SACADO_NEW_FAD_DESIGN_IS_DEFAULT
502 #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.