Adjoint Mode Type
AReal defines an active data type for adjoint mode for the underlying type
T, which tracks derivative information on a tape. It is designed to behave exactly like the built-in type
double, with all mathematical operations defined for this custom type.
Derivatives will only be tracked on tape if the variable has been registered or is dependent on other registered variables. Hence creating and using variables without an active tape is not problematic.
Tape, Global Functions, AD Mode Interface, Mathematical Operations
The type of the tape that is used to store operations on this class.
The type used for storing this instance's slot in the tape. This type is useful for checkpointing, where the slot of the inputs and outputs needs to be stored in the checkpoint in order to retrieve or increment their derivatives during adjoint computation.
The value-type of this class, i.e.,
Constructors and Destructors¶
AReal(const T& val = T()) // (1) construct from value or default-construct AReal(const AReal& val) // (2) copy-constructor AReal(AReal&& o) // (3) move-constructor AReal(const Expression<T,Expr>& expr) // (4) from expression ~AReal() // (5) destructor
The constructors create new instances of this class.
(1) creates a value that is not connected to any tape (it can be registered explicitly using
(3) copy or move from other values, taping the operation if the source type has been registered with a tape.
(4) creates a value from an expression template, evaluating the expression and recording the operations on tape if any of the variables on the right-hand side have been registered with a tape. It gets triggered for example by expressions like this:
x is an instance of
(5) unregisters the variable from the tape if applicable.
AReal& operator=(const T &val) // (1) assign from a scalar value AReal& operator=(const AReal& val) // (2) copy-assignment AReal& operator=(AReal&& val) // (3) move-assignment AReal& operator=(const Expression<T,Expr>& expr) // (4) from expression
These assignment operators for
AReal behave similar to the equivalent constructors above.
Values and Derivatives¶
T getValue() const returns the value as the underlying type (without tape information).
T getDerivative() const returns the derivative (adjoint) as stored on the tape (typically after rolling back the operation). It throws an instance of
NoTapeException if the variable has not been registered with an active tape.
void setDerivative(const T& a) sets the derivative (adjoint) on the tape. Typically this is called in the function outputs after recording the operation, before rolling back the tape.
T& value() and
const T& value() const return a reference to the underlying passive type value. This can be used to assign a value to the variable without tape recording, as
x.value() = 1.2.
T& derivative() and
const T& derivative() const return a reference to the underlying adjoint value. This can be used to assign a value to the adjoint, as
x.derivative() = 1.0, which is equivalent to
setDerivative. It can also be used as a replacement for
bool shouldRecord() const checks if the variable has been registered with a tape and should therefore be recorded.
AReal instances support all other mathematical arithmetic operations, such as
operator+= and friends. Also, since
AReal is an
Expression, all math functions defined for expressions also work on instances of this class.