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

Allow for a direct access to the gradient information computed by the tape. More...

#include <gradientAccessTapeInterface.hpp>

Inheritance diagram for codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >:

Public Types

using Gradient = T_Gradient
 See GradientAccessTapeInterface.
 
using Identifier = T_Identifier
 See GradientAccessTapeInterface.
 

Public Member Functions

Interface definition
void setGradient (Identifier const &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Set the gradient.
 
Gradient const & getGradient (Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
 Set the gradient.
 
Gradientgradient (Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
 Reference access to gradient.
 
Gradient const & gradient (Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
 Constant reference access to gradient.
 

Detailed Description

template<typename T_Gradient, typename T_Identifier>
struct codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >

Allow for a direct access to the gradient information computed by the tape.

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

The gradient information is usually accessed via the helper functions of the ActiveType, for example

w.gradient() = 100.0;
std::cout << "Gradient of w: " << w.getGradient() << std::endl;
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
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

These helper function are shortcuts to the functions provided in this interface, but the functions here can also be used to obtain the sensitivity information of a variable that is no longer present, for example (documentation/examples/gradientAccessTapeInterface.cpp):

using Tape = typename Real::Tape;
using Identifier = typename Tape::Identifier;
Tape& tape = Real::getTape();
Identifier x_in;
Identifier x_out;
{
Real x = 10.0;
tape.setActive();
tape.registerInput(x);
x_in = x.getIdentifier(); // Identifier of x when it is defined as an input
// Do some heavy computation
x = 42.0 * x * x;
tape.registerOutput(x);
x_out = x.getIdentifier(); // Identifier of x when it is defined as an output
tape.setPassive();
}
tape.setGradient(x_out, 1.0); // Use this interface to set the gradient of x when it was defined as an output
tape.evaluate();
std::cout << "Gradient of df/dx: " << tape.getGradient(x_in) << std::endl;
RealReverseGen< double > RealReverse
Definition: codi.hpp:120
Identifier & getIdentifier()
Definition: activeTypeBase.hpp:156
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
T_Identifier Identifier
See GradientAccessTapeInterface.
Definition: gradientAccessTapeInterface.hpp:71
Template Parameters
T_GradientThe gradient type of a tape, usually chosen as ActiveType::Gradient.
T_IdentifierThe adjoint/tangent identification of a tape, usually chosen as ActiveType::Identifier.

Member Function Documentation

◆ getGradient()

template<typename T_Gradient , typename T_Identifier >
Gradient const & codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >::getGradient ( Identifier const &  identifier,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
) const

Set the gradient.

Automatic adjoints management involves bounds checking and locking. If no adjoint variable with the given identifier exists, a reference to adjoints[0] is returned. See AdjointsManagement for details.

◆ gradient() [1/2]

template<typename T_Gradient , typename T_Identifier >
Gradient & codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >::gradient ( Identifier const &  identifier,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
)

Reference access to gradient.

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

◆ gradient() [2/2]

template<typename T_Gradient , typename T_Identifier >
Gradient const & codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >::gradient ( Identifier const &  identifier,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
) const

Constant reference access to gradient.

Automatic adjoints management involves bounds checking and locking. If no adjoint variable with the given identifier exists, a reference to adjoints[0] is returned. See AdjointsManagement for details.

◆ setGradient()

template<typename T_Gradient , typename T_Identifier >
void codi::GradientAccessTapeInterface< T_Gradient, T_Identifier >::setGradient ( Identifier const &  identifier,
Gradient const &  gradient,
AdjointsManagement  adjointsManagement = AdjointsManagement::Automatic 
)

Set the gradient.

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


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