CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::ManualStatementPushTapeInterface< T_Real, T_Gradient, T_Identifier > Struct Template Reference

Add derivative information for custom operations to the tape. More...

#include <manualStatementPushTapeInterface.hpp>

Inheritance diagram for codi::ManualStatementPushTapeInterface< T_Real, T_Gradient, T_Identifier >:

Public Types

using Gradient = T_Gradient
 See ManualStatementPushTapeInterface.
 
using Identifier = T_Identifier
 See ManualStatementPushTapeInterface.
 
using Real = T_Real
 See ManualStatementPushTapeInterface.
 

Public Member Functions

Interface definition
void pushJacobianManual (Real const &jacobian, Real const &value, Identifier const &index)
 
void storeManual (Real const &lhsValue, Identifier &lhsIndex, Config::ArgumentSize const &size)
 

Detailed Description

template<typename T_Real, typename T_Gradient, typename T_Identifier>
struct codi::ManualStatementPushTapeInterface< T_Real, T_Gradient, T_Identifier >

Add derivative information for custom operations to the tape.

See Tape Interface Design for a general overview of the tape interface design in CoDiPack.

The functions in this interface can be used to provide derivative information to CoDiPack for functions that are not known to CoDiPack but so small that an external function implementation is an overkill.

The forward and reverse AD equations are the base for this interface. The user has to provide the Jacobian $ \frac{\d \phi}{\d u} $ for all arguments $u$ and compute the value for $w$.

Before the call to storeManual the user has to update the value of the output, that is, $w$ in the above equations. This is usually done with output.value() = w. Afterwards, storeManual() has to be called. The size argument is the number of arguments $u$ from the equations above. This call ensures that output gets a proper identifier and the dependency chain is not broken or wrong for this variable.

Afterwards the user has to call pushJacobianManual() for each argument $u$.

The user has to ensure that the computations of the Jacobians are evaluated such that the CoDiPack tape does not accidentally record them.

Here is an example for manual statement push (documentation/examples/manualStatementPushTapeInterface.cpp):

using Tape = typename Real::Tape;
using Primal = typename Real::Real;
Primal func(Primal x, Primal y) {
return sqrt(x * x + y * y);
}
Primal func_dx(Primal x, Primal y) {
return x / func(x, y);
}
Primal func_dy(Primal x, Primal y) {
return y / func(x, y);
}
int main(int nargs, char** args) {
Tape& tape = Real::getTape();
// Recording
Real u1 = 10.0;
Real u2 = 4.0;
tape.setActive();
tape.registerInput(u1);
tape.registerInput(u2);
Real w = func(u1.value(), u2.value());
tape.storeManual(w.value(), w.getIdentifier(), 2);
tape.pushJacobianManual(func_dx(u1.value(), u2.value()), u1.value(), u1.getIdentifier());
tape.pushJacobianManual(func_dy(u1.value(), u2.value()), u2.value(), u2.getIdentifier());
tape.registerOutput(w);
tape.setPassive();
// Reverse evaluation
w.setGradient(1.0);
tape.evaluate();
std::cout << "Gradient of dy/du1: " << u1.getGradient() << std::endl;
std::cout << "Gradient of dy/du2: " << u2.getGradient() << std::endl;
}
RealReverseGen< double > RealReverse
Definition: codi.hpp:120
Real & value()
Get a reference to the lvalue represented by the expression.
Definition: activeTypeBase.hpp:166
Identifier & getIdentifier()
Definition: activeTypeBase.hpp:156
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
static Tape & getTape()
Get a reference to the tape which manages this expression.
Definition: activeType.hpp:99
T_Tape Tape
See ActiveType.
Definition: activeType.hpp:55
typename Tape::Real Real
See LhsExpressionInterface.
Definition: activeTypeBase.hpp:76
void setGradient(Gradient const &g)
Set the gradient of this lvalue in the tape.
Definition: lhsExpressionInterface.hpp:120
Gradient getGradient() const
Get the gradient of this lvalue from the tape.
Definition: lhsExpressionInterface.hpp:115
T_Real Real
See ManualStatementPushTapeInterface.
Definition: manualStatementPushTapeInterface.hpp:76
Template Parameters
T_RealThe computation type of a tape, usually chosen as ActiveType::Real.
T_GradientThe gradient type of a tape, usually chosen as ActiveType::Gradient.
T_IdentifierThe adjoint/tangent identification type of a tape, usually chosen as ActiveType::Identifier.

Member Function Documentation

◆ pushJacobianManual()

template<typename T_Real , typename T_Gradient , typename T_Identifier >
void codi::ManualStatementPushTapeInterface< T_Real, T_Gradient, T_Identifier >::pushJacobianManual ( Real const &  jacobian,
Real const &  value,
Identifier const &  index 
)

Push a Jacobian entry to the tape. storeManual() has to be called first and is passed the number of arguments. Afterwards, this method has to be called once for each argument.

Parameters
jacobianJacobian $ \frac{\d \phi}{\d u_i} $ of the argument $ u_i $.
valueValue of the argument $ u_i $. Usually u_i.value().
indexIdentifier of the argument $ u_i $. Usually u_i.identifier().

◆ storeManual()

template<typename T_Real , typename T_Gradient , typename T_Identifier >
void codi::ManualStatementPushTapeInterface< T_Real, T_Gradient, T_Identifier >::storeManual ( Real const &  lhsValue,
Identifier lhsIndex,
Config::ArgumentSize const &  size 
)

Initialize the storing of a hand computed statement. The primal value has to be updated already.

Parameters
lhsValueValue of the result $ w $. Usually w.value().
lhsIndexIdentifier of the result $ w $. Usually w.identifier().
sizeNumber of arguments of $ \phi $.

The documentation for this struct was generated from the following file: