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

Allows user defined vectors for the forward and adjoint evaluation, and for clearing adjoints. More...

#include <customAdjointVectorEvaluationTapeInterface.hpp>

Inheritance diagram for codi::CustomAdjointVectorEvaluationTapeInterface< T_Position >:

Public Types

using Position = T_Position
 See CustomAdjointVectorEvaluationTapeInterface.
 
- Public Types inherited from codi::ForwardEvaluationTapeInterface< T_Position >
using Position = T_Position
 See ForwardEvaluationTapeInterface.
 
- Public Types inherited from codi::PositionalEvaluationTapeInterface< T_Position >
using Position = T_Position
 See PositionalEvaluationTapeInterface.
 

Public Member Functions

Interface definition
template<typename AdjointVector >
void evaluate (Position const &start, Position const &end, AdjointVector &&data)
 Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
 
template<typename AdjointVector >
void evaluateForward (Position const &start, Position const &end, AdjointVector &&data)
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
template<typename AdjointVector >
void clearCustomAdjoints (Position const &start, Position const &end, AdjointVector &&data)
 Clear all adjoint values, that is, set them to zero.
 
template<typename InternalAdjoints >
InternalAdjoints getInternalAdjoints ()
 Obtain a representation of the tape's internal adjoint vector that can be used as custom adjoints.
 
- Public Member Functions inherited from codi::ForwardEvaluationTapeInterface< T_Position >
void evaluateForward (Position const &start, Position const &end, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Perform a forward evaluation of a part of the tape. It has to hold start <= end.
 
void evaluateForward (AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Perform a forward evaluation of the full tape.
 
- Public Member Functions inherited from codi::PositionalEvaluationTapeInterface< T_Position >
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::CustomAdjointVectorEvaluationTapeInterface< T_Position >

Allows user defined vectors for the forward and adjoint evaluation, and for clearing adjoints.

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

The two additional evaluate methods allow for the evaluation of the tape with a custom adjoint vector, and the additional clearing method allows clearing the custom adjoint vector according to the recorded tape.

The adjoint vector type (template parameter AdjointVector in the member functions) must be a accessible with operator[]. Suitable choices are pointers, e.g., Adjoint*, or classes with overloaded operator[] like std::vector<Adjoint>.

codi::AdjointVectorTraits::GradientImplementation must be specialized for AdjointVector. The type of the vector entries deduced from these traits (gradient type) must support the following operators:

Here is an example for an evaluation with a custom adjoint vector (documentation/examples/customAdjointVectorEvaluationTapeInterface.cpp):

using Tape = typename Real::Tape;
using Gradient = codi::Direction<double, 2>;
Tape& tape = Real::getTape();
// Recording
Real x = 10.0;
tape.setActive();
tape.registerInput(x);
Real y1 = 42.0 * x * x;
Real y2 = 20.0 * x * x * x;
tape.registerOutput(y1);
tape.registerOutput(y2);
tape.setPassive();
// Reverse evaluation
size_t adjointSize = tape.getParameter(codi::TapeParameters::LargestIdentifier);
Gradient* adjoints = new Gradient[adjointSize + 1];
adjoints[y1.getIdentifier()] = {1.0, 0.0};
adjoints[y2.getIdentifier()] = {0.0, 1.0};
tape.evaluate(tape.getPosition(), tape.getZeroPosition(), adjoints); // Full tape evaluation
std::cout << "Gradient of dy1/dx: " << adjoints[x.getIdentifier()][0] << std::endl;
std::cout << "Gradient of dy2/dx: " << adjoints[x.getIdentifier()][1] << std::endl;
delete [] adjoints;
Template Parameters
T_PositionGlobal tape position, usually chosen as Tape::Position.

Member Function Documentation

◆ clearCustomAdjoints()

template<typename T_Position >
template<typename AdjointVector >
void codi::CustomAdjointVectorEvaluationTapeInterface< T_Position >::clearCustomAdjoints ( Position const & start,
Position const & end,
AdjointVector && data )

Clear all adjoint values, that is, set them to zero.

Clear custom adjoint vector according to a tape recording.

Template Parameters
AdjointVectorSee CustomAdjointVectorEvaluationTapeInterface documentation.

◆ evaluate()

template<typename T_Position >
template<typename AdjointVector >
void codi::CustomAdjointVectorEvaluationTapeInterface< T_Position >::evaluate ( Position const & start,
Position const & end,
AdjointVector && data )

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

Tape evaluation with a custom adjoint vector.

Template Parameters
AdjointVectorSee CustomAdjointVectorEvaluationTapeInterface documentation.

◆ evaluateForward()

template<typename T_Position >
template<typename AdjointVector >
void codi::CustomAdjointVectorEvaluationTapeInterface< T_Position >::evaluateForward ( Position const & start,
Position const & end,
AdjointVector && data )

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

Tape evaluation with a custom adjoint vector.

Template Parameters
AdjointVectorSee CustomAdjointVectorEvaluationTapeInterface documentation.

◆ getInternalAdjoints()

template<typename T_Position >
template<typename InternalAdjoints >
InternalAdjoints codi::CustomAdjointVectorEvaluationTapeInterface< T_Position >::getInternalAdjoints ( )

Obtain a representation of the tape's internal adjoint vector that can be used as custom adjoints.

To avoid that functionality has to be implemented both for custom, external and internal adjoints, this method provides access to the internal adjoints so that they can be used as if they were custom adjoints.

Warning: If you use this method, proceed with care. If internal adjoints are modified due to side effect of other methods, the object returned here might become invalid, or, conversely, modifications of the returned object other than reading/writing adjoints might interfere with the tape's management of internal adjoints.

Template Parameters
InternalAdjointsPlaceholder for the implementation-dependent return type.

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