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

Allows for an arbitrary adjoint evaluation of a recorded tape. More...

#include <customAdjointVectorHelper.hpp>

Inheritance diagram for codi::CustomAdjointVectorHelper< T_Type, T_Gradient >:

Public Types

using Base = CustomAdjointVectorInterface<Type>
 Abbreviation for the base class.
 
using Gradient = T_Gradient
 See CustomAdjointVectorHelper.
 
using Identifier = typename Type::Identifier
 See LhsExpressionInterface.
 
using Position = typename Tape::Position
 See PositionalEvaluationTapeInterface.
 
using Real = typename Type::Real
 See LhsExpressionInterface.
 
using Tape = typename Type::Tape
 See LhsExpressionInterface.
 
using Type = T_Type
 < See CustomAdjointVectorHelper.
 
- Public Types inherited from codi::CustomAdjointVectorInterface< T_Type >
using Identifier = typename Type::Identifier
 See LhsExpressionInterface.
 
using Position = typename Tape::Position
 See PositionalEvaluationTapeInterface.
 
using Real = typename Type::Real
 See LhsExpressionInterface.
 
using Tape = typename Type::Tape
 See LhsExpressionInterface.
 
using Type = T_Type
 See CustomAdjointVectorInterface.
 

Public Member Functions

 CustomAdjointVectorHelper ()
 Constructor.
 
 ~CustomAdjointVectorHelper ()
 Destructor.
 
Implementation of CustomAdjointVectorInterface interface
void clearAdjoints ()
 Set all adjoints to zero.
 
void deleteAdjointVector ()
 Delete the adjoint vector.
 
void evaluate (Position const &start, Position const &end)
 Perform a full reverse evaluation of the tape.
 
void evaluateForward (Position const &start, Position const &end)
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
VectorAccessInterface< Real, Identifier > * getVectorInterface ()
 Get a new general interface to the adjoint vector.
 
virtual void evaluate (Position const &start, Position const &end)=0
 Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
 
void evaluate ()
 Perform a full reverse evaluation of the tape.
 
virtual void evaluateForward (Position const &start, Position const &end)=0
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
void evaluateForward ()
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
Gradient access methods
Gradient const & getGradient (Identifier const &identifier) const
 Get a constant reference to the gradient.
 
GradientgradientUnchecked (Identifier const &identifier)
 Get a reference to the gradient. Unchecked access.
 
Gradient const & gradientUnchecked (Identifier const &identifier) const
 Get a constant reference to the gradient. Unchecked access.
 
Gradientgradient (Identifier const &identifier)
 Get a reference to the gradient. Checked access.
 
Gradient const & gradient (Identifier const &identifier) const
 Get a constant reference to the gradient. Checked access.
 
void setGradient (Identifier &identifier, Gradient const &gradientValue)
 Set the gradient. Checked access.
 
- Public Member Functions inherited from codi::CustomAdjointVectorInterface< T_Type >
 CustomAdjointVectorInterface ()
 Constructor.
 
virtual ~CustomAdjointVectorInterface ()
 Destructor.
 
void evaluate ()
 Perform a full reverse evaluation of the tape.
 
void evaluateForward ()
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
void setTape (Tape &tape)
 Set the tape for the evaluations.
 

Protected Attributes

AdjointVectorAccess< Real, Identifier, Gradient > * adjointInterface
 Last created adjoint interface.
 
std::vector< GradientadjointVector
 Custom adjoint vector.
 
Gradient const constZeroValue
 Temporary constant zero value.
 
Gradient zeroValue
 Temporary zero value.
 
- Protected Attributes inherited from codi::CustomAdjointVectorInterface< T_Type >
Tapetape
 Current tape for evaluations. Default: the Type's current tape.
 

Detailed Description

template<typename T_Type, typename T_Gradient>
struct codi::CustomAdjointVectorHelper< T_Type, T_Gradient >

Allows for an arbitrary adjoint evaluation of a recorded tape.

The evaluation of a reverse AD tape is independent of the recording of the tape. For example, the reverse evaluation can be performed simultaneously on differtent adjoint vectors or with a vector mode that is not the same as the vector mode implied by the CoDiPack type.

An example for a different vector mode and the general usage is:

// Step 1: Perform a regular recording
for(size_t i = 0; i < 5; ++i) {
tape.registerInput(x[i]);
}
func(x, 5, y);
tape.registerOutput(y[0]);
tape.registerOutput(y[1]);
tape.setPassive();
codi::CustomAdjointVectorHelper<Real, codi::Direction<double, 2> > vh; // Step 2: Create the vector helper
vh.gradient(y[0].getIdentifier())[0] = 1.0; // Step 3: Set the seeding in the vector helper
vh.gradient(y[1].getIdentifier())[1] = 1.0;
vh.evaluate(); // Step 4: Call evaluate on the vector helper.
// Step 5: Get the gradients from the vector helper.
codi::Jacobian<double> jacobian(2,5);
for(size_t i = 0; i < 5; ++i) {
jacobian(0,i) = vh.getGradient(x[i].getIdentifier())[0];
jacobian(1,i) = vh.getGradient(x[i].getIdentifier())[1];
}

The major difference in using the vector helper is that you cannot interact with the adjoints directly, e.g. via the codi::LhsExpressionInterface::getGradient or codi::LhsExpressionInterface::setGradient functions. Instead, you extract the identifier from the AD variable via the codi::LhsExpressionInterface::getIdentifier() functions. The identifier is then used to access the adjoint values via the vector helper, e.g. for seeding or extraction.

For details on custom vector evaluations see CustomAdjointVectorEvaluationTapeInterface.

Template Parameters
T_TypeThe underlying CoDiPack type.
T_GradientType of the entries of the custom adjoint vector.

Member Function Documentation

◆ clearAdjoints()

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorHelper< T_Type, T_Gradient >::clearAdjoints ( )
inlinevirtual

Set all adjoints to zero.

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ deleteAdjointVector()

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorHelper< T_Type, T_Gradient >::deleteAdjointVector ( )
inlinevirtual

Delete the adjoint vector.

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ evaluate() [1/3]

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorInterface< T_Type >::evaluate ( )
inline

Perform a full reverse evaluation of the tape.

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

◆ evaluate() [2/3]

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorHelper< T_Type, T_Gradient >::evaluate ( Position const & start,
Position const & end )
inlinevirtual

Perform a full reverse evaluation of the tape.

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

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ evaluate() [3/3]

template<typename T_Type , typename T_Gradient >
virtual void codi::CustomAdjointVectorInterface< T_Type >::evaluate ( Position const & start,
Position const & end )
virtual

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.

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ evaluateForward() [1/3]

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorInterface< T_Type >::evaluateForward ( )
inline

Perform a forward evaluation of a part of the tape. It has to hold start <= end.

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

◆ evaluateForward() [2/3]

template<typename T_Type , typename T_Gradient >
void codi::CustomAdjointVectorHelper< T_Type, T_Gradient >::evaluateForward ( Position const & start,
Position const & end )
inlinevirtual

Perform a forward evaluation of a part of the tape. It has to hold start <= end.

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

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ evaluateForward() [3/3]

template<typename T_Type , typename T_Gradient >
virtual void codi::CustomAdjointVectorInterface< T_Type >::evaluateForward ( Position const & start,
Position const & end )
virtual

Perform a forward evaluation of a part of the tape. It has to hold start <= end.

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

Implements codi::CustomAdjointVectorInterface< T_Type >.

◆ getVectorInterface()

template<typename T_Type , typename T_Gradient >
VectorAccessInterface< Real, Identifier > * codi::CustomAdjointVectorHelper< T_Type, T_Gradient >::getVectorInterface ( )
inlinevirtual

Get a new general interface to the adjoint vector.

Implements codi::CustomAdjointVectorInterface< T_Type >.


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