CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
codi::PositionalEvaluationTapeInterface< T_Position > Struct Template Reference

Reverse AD evaluation for parts of a recorded tape. More...

#include <positionalEvaluationTapeInterface.hpp>

Inheritance diagram for codi::PositionalEvaluationTapeInterface< T_Position >:

Public Types

using Position = T_Position
 See PositionalEvaluationTapeInterface.
 

Public Member Functions

Interface definition
void evaluate (Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
 
void clearAdjoints (Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Clear all adjoints that would be set in a tape evaluation from start to end. It has to hold start >= end.
 
Position getPosition () const
 Current position of the tape.
 
Position getZeroPosition () const
 Initial position of the tape.
 
void resetTo (Position const &pos, bool resetAdjoints=true, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Reset the tape to the provided position.
 

Detailed Description

template<typename T_Position>
struct codi::PositionalEvaluationTapeInterface< T_Position >

Reverse AD evaluation for parts of a recorded tape.

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

The definitions in the ReverseTapeInterface provide only methods that operate on the full tape. With the methods in this interface, all these operations can be performed on parts of the tape.

Here is an example for a positional tape evaluation (Example 3 - Positional tape evaluations):

using Gradient = typename Real::Gradient;
using Tape = typename Real::Tape;
using Position = typename Tape::Position;
Real funcInner(Real x, Real y) {
return sqrt(x * x + y * y);
}
void positionalExample() {
Tape& tape = Real::getTape();
// Recording
Real a = 10.0;
Real b = 4.0;
tape.setActive();
tape.registerInput(a);
tape.registerInput(b);
Real u1 = a + b;
Real u2 = a - b;
// Now comes a really long and complicated function. Tape it, reverse it and then store the result on the tape.
// Step 1: Store the position
Position begin = tape.getPosition();
// Record the function
Real w = funcInner(u1, u2);
// Step 2: Reverse part of the tape
w.gradient() = 1.0;
tape.evaluate(tape.getPosition(), begin);
Gradient u1_d = u1.gradient();
Gradient u2_d = u2.gradient();
// Step 3: Cleanup the reversal
tape.resetTo(begin);
u1.gradient() = Gradient();
u2.gradient() = Gradient();
// Now store the computed gradient data
tape.storeManual(w.value(), w.getIdentifier(), 2);
tape.pushJacobianManual(u1_d, u1.value(), u1.getIdentifier());
tape.pushJacobianManual(u2_d, u2.value(), u2.getIdentifier());
Real y = cos(w);
tape.registerOutput(y);
tape.setPassive();
// Reverse evaluation
y.setGradient(1.0);
tape.evaluate();
std::cout << "Positional example:" << std::endl;
std::cout << "Gradient of dy/da: " << a.getGradient() << std::endl;
std::cout << "Gradient of dy/db: " << b.getGradient() << std::endl;
tape.reset();
}
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::Gradient Gradient
See LhsExpressionInterface.
Definition: activeTypeBase.hpp:79
void setGradient(Gradient const &g)
Set the gradient of this lvalue in the tape.
Definition: lhsExpressionInterface.hpp:120
Gradient & gradient()
Get the gradient of this lvalue from the tape.
Definition: lhsExpressionInterface.hpp:105
Gradient getGradient() const
Get the gradient of this lvalue from the tape.
Definition: lhsExpressionInterface.hpp:115
T_Position Position
See PositionalEvaluationTapeInterface.
Definition: positionalEvaluationTapeInterface.hpp:62
Template Parameters
T_PositionGlobal tape position, usually chosen as Tape::Position.

Member Function Documentation

◆ clearAdjoints()

template<typename T_Position >
void codi::PositionalEvaluationTapeInterface< T_Position >::clearAdjoints ( Position const &  start,
Position const &  end,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
)

Clear all adjoints that would be set in a tape evaluation from start to end. It has to hold start >= end.

Automatic adjoints management involves locking, see AdjointsManagement for details.

◆ evaluate()

template<typename T_Position >
void codi::PositionalEvaluationTapeInterface< T_Position >::evaluate ( Position const &  start,
Position const &  end,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
)

Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.

Automatic adjoints management involves bounds checking, resizing, and locking, see AdjointsManagement for details.

◆ resetTo()

template<typename T_Position >
void codi::PositionalEvaluationTapeInterface< T_Position >::resetTo ( Position const &  pos,
bool  resetAdjoints = true,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
)

Reset the tape to the provided position.

Automatic adjoints management involves locking, see AdjointsManagement for details.


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