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

Stores the Jacobian matrix for a code section. More...

#include <preaccumulationHelper.hpp>

Public Types

using Gradient = typename Type::Gradient
 See LhsExpressionInterface.
 
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 PreaccumulationHelper.
 

Public Member Functions

template<typename... Inputs>
void addInput (Inputs const &... inputs)
 Add multiple additional inputs. Inputs need to be of type Type. Called after start().
 
template<typename... Outputs>
void addOutput (Outputs &... outputs)
 Add multiple additional outputs. Outputs need to be of type Type. Called before finish().
 
template<typename... Outputs>
void finish (bool const storeAdjoints, Outputs &... outputs)
 Finish the preaccumulation region and perform the preaccumulation. See addOutput() for outputs.
 
 PreaccumulationHelper ()
 Constructor.
 
template<typename... Inputs>
void start (Inputs const &... inputs)
 Starts a preaccumulation region. Resets the internal state. See addInputs() for inputs.
 

Public Attributes

std::vector< IdentifierinputData
 
std::vector< IdentifieroutputData
 
std::vector< Type * > outputValues
 

Protected Attributes

JacobianCountNonZerosRow< Realjacobian
 Jacobian for the preaccumulation.
 
Position startPos
 Starting position for the region.
 
std::vector< GradientstoredAdjoints
 If adjoints of inputs should be stored, before the preaccumulation.
 

Detailed Description

template<typename T_Type, typename = void>
struct codi::PreaccumulationHelper< T_Type, typename >

Stores the Jacobian matrix for a code section.

The preaccumulation of a code section corresponds to the process of replacing the recorded tape entries with the Jacobian matrix of that section. If the code part is defined by the function $ f $, then the Jacobian $ \frac{df}{dx} $ is computed by the Preaccumulation helper and stored on the tape.

The preaccumulation of a code part is beneficial if it is complicated to compute but has only a few inputs and outputs. If the computation requires 200 statements with a total of 600 arguments, the storage for this would be 7400 byte on a Jacobian tape. If the function has only two input arguments and two output arguments, the storage for the Jacobian matrix of this function would require 50 byte.

The procedure for the preaccumulation of a code section is as follows.

Real A[4] = {u * 1.0, 0.5,
0.0, u * -1.0};
Real start[2] = {u * 10.0, u * 20.0};
Real end[2];
codi::PreaccumulationHelper<Real> ph; // Step 1: Create the helper structure
if(performPreAcc) {
ph.start(start[0], start[1]); // Step 2: Start the preaccumulation region and specify inputs
for(size_t i = 0; i < 4; ++i) {
ph.addInput(A[i]); // Step 3: Add additional inputs (optional)
}
}
ode(start, end, 1000, A, 1.0 / 1000.0, 2); // Step 4: Evaluate the function/region for the accumulation in a normal way
if(performPreAcc) {
ph.addOutput(end[1]); // Step 5: Add additional outputs (optional)
ph.finish(false, end[0]); // Step 6: Finish the preaccumulation region and specify outputs
}
Real w = sqrt(end[0] * end[0] + end[1] * end[1]);

The preaccumulation helper can be used multiple times, the start routine resets the state such that multiple evaluations are possible. This improves the performance of the helper since stack allocations are only performed once.

Template Parameters
T_TypeThe CoDiPack type on which the evaluations take place.

Member Data Documentation

◆ inputData

template<typename T_Type , typename = void>
std::vector<Identifier> codi::PreaccumulationHelper< T_Type, typename >::inputData

List of input identifiers. Can be added manually after start() was called.

◆ outputData

template<typename T_Type , typename = void>
std::vector<Identifier> codi::PreaccumulationHelper< T_Type, typename >::outputData

List of output identifiers. Can be added manually before finish() is called. Has to be in sync with outputValues.

◆ outputValues

template<typename T_Type , typename = void>
std::vector<Type*> codi::PreaccumulationHelper< T_Type, typename >::outputValues

List of output value pointers. Can be added manually before finish() is called. Has to be in sync with outputData.


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