41#include "../expressions/lhsExpressionInterface.hpp"
42#include "../expressions/logic/compileTimeTraversalLogic.hpp"
43#include "../expressions/logic/traversalLogic.hpp"
45#include "../traits/adjointVectorTraits.hpp"
46#include "../traits/expressionTraits.hpp"
47#include "data/chunk.hpp"
48#include "indices/linearIndexManager.hpp"
49#include "interfaces/reverseTapeInterface.hpp"
50#include "jacobianBaseTape.hpp"
62 template<
typename T_TapeTypes>
73 using Real =
typename TapeTypes::Real;
79 CODI_STATIC_ASSERT(IndexManager::IsLinear,
"This class requires an index manager with a linear scheme.");
93 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
94 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
95 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
97 startIndex = std::min(startIndex, (IndexPosition)this->
adjoints.size() - 1);
98 endIndex = std::min(endIndex, (IndexPosition)this->
adjoints.size() - 1);
100 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
110 template<
typename Adjo
intVector>
112 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
113 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
114 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
116 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
132 template<
typename Adjo
intVector>
137 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
142 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real const*
const rhsJacobians,
145 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
147 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
148 CODI_UNUSED(endJacobianPos, endStmtPos, endLLFByteDataPos, endLLFInfoDataPos);
154 size_t curAdjointPos = startAdjointPos;
156 while (curAdjointPos < endAdjointPos)
CODI_Likely {
162 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
163 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
167 Adjoint lhsAdjoint = Adjoint();
169 adjointVector[curAdjointPos] = lhsAdjoint;
180 template<
typename Adjo
intVector>
185 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
190 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real const*
const rhsJacobians,
193 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
195 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
196 CODI_UNUSED(endJacobianPos, endStmtPos, endLLFByteDataPos, endLLFInfoDataPos);
202 size_t curAdjointPos = startAdjointPos;
204 while (curAdjointPos > endAdjointPos)
CODI_Likely {
209 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
210 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
216 Adjoint
const lhsAdjoint = adjointVector[curAdjointPos];
220 tape, (
Identifier)curAdjointPos, GradientTraits::dim<Adjoint>(),
224 adjointVector[curAdjointPos] = Adjoint();
235 template<
typename Real>
240 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
245 size_t& curJacobianPos,
size_t const& endJacobianPos,
typename Real::Real const*
const rhsJacobians,
248 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
250 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
251 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endJacobianPos, endStmtPos);
255 size_t curAdjointPos = startAdjointPos;
256 while (curAdjointPos < endAdjointPos)
CODI_Likely {
258 curLhsIdentifier = curAdjointPos;
259 argsSize = numberOfJacobians[curStmtPos];
263 writer->
writeStatement(curLhsIdentifier, curJacobianPos, rhsJacobians, rhsIdentifiers, argsSize);
265 writer->
writeStatement(curLhsIdentifier, curJacobianPos, rhsJacobians, rhsIdentifiers, argsSize);
266 curJacobianPos += argsSize;
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:399
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:397
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_STATIC_ASSERT(cond, message)
Static assert in CoDiPack.
Definition macros.hpp:123
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
typename GradientImplementation< AdjointVector >::Gradient Gradient
Deduce the entry type from an adjoint vector type, usually identical to the gradient type of a tape.
Definition adjointVectorTraits.hpp:92
uint16_t LowLevelFunctionDataSize
Size store type for a low level function.
Definition config.h:98
uint16_t LowLevelFunctionToken
Token type for low level functions in the tapes.
Definition config.h:108
size_t constexpr StatementLowLevelFunctionTag
Statement tag for low level functions.
Definition config.h:126
bool constexpr ReversalZeroesAdjoints
With a linear index management, control if adjoints are set to zero during reversal.
Definition config.h:289
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
size_t constexpr StatementInputTag
Tag for statements that are inputs. Used in linear index management context.
Definition config.h:123
std::array< AtomicTraits::RemoveAtomic< typename TraitsImplementation< Gradient >::Real >, TraitsImplementation< Gradient >::dim > toArray(Gradient const &gradient)
Converts the (possibly multi-component) gradient to an array of Reals.
Definition gradientTraits.hpp:116
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:98
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
typename Tape::Identifier Identifier
See LhsExpressionInterface.
Definition activeTypeBase.hpp:78
typename Tape::Real Real
See LhsExpressionInterface.
Definition activeTypeBase.hpp:76
Implementation of VectorAccessInterface for adjoint vectors.
Definition adjointVectorAccess.hpp:61
Data is stored chunk-wise in this DataInterface implementation. If a chunk runs out of space,...
Definition chunkedData.hpp:64
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:156
static void notifyStatementEvaluateListeners(Tape &tape, Identifier const &lhsIdentifier, size_t sizeLhsAdjoint, Real const *lhsAdjoint)
Invoke callbacks for StatementEvaluate events.
Definition eventSystem.hpp:712
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
Base class for all standard Jacobian tape implementations.
Definition jacobianBaseTape.hpp:128
typename Base::Position Position
See TapeTypesInterface.
Definition jacobianBaseTape.hpp:152
StatementData statementData
Data stream for statement specific data.
Definition jacobianBaseTape.hpp:173
Adjoints adjoints
Evaluation vector for AD.
Definition jacobianBaseTape.hpp:176
static void incrementTangents(AdjointVector const &adjointVector, AdjointVectorTraits::Gradient< AdjointVector > &lhsAdjoint, Config::ArgumentSize const &numberOfArguments, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers)
Performs the AD forward equation for a statement.
Definition jacobianBaseTape.hpp:550
void clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Clear all adjoint values, that is, set them to zero.
Definition jacobianBaseTape.hpp:471
static void incrementAdjoints(AdjointVector &adjointVector, AdjointVectorTraits::Gradient< AdjointVector > const &lhsAdjoint, Config::ArgumentSize const &numberOfArguments, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers)
Performs the AD reverse equation for a statement.
Definition jacobianBaseTape.hpp:527
Final implementation for a Jacobian tape with a linear index management.
Definition jacobianLinearTape.hpp:63
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:73
void clearCustomAdjoints(Position const &start, Position const &end, AdjointVector &&data)
Clear all adjoint values, that is, set them to zero.
Definition jacobianLinearTape.hpp:111
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 >= ...
Definition jacobianLinearTape.hpp:87
typename TapeTypes::IndexManager IndexManager
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:75
JacobianLinearTape()
Constructor.
Definition jacobianLinearTape.hpp:82
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:74
static void internalEvaluateReverse_EvalStatements(JacobianLinearTape &tape, AdjointVector &&adjointVector, size_t &curLLFByteDataPos, size_t const &endLLFByteDataPos, char *dataPtr, size_t &curLLFInfoDataPos, size_t const &endLLFInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, size_t &curJacobianPos, size_t const &endJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, size_t &curStmtPos, size_t const &endStmtPos, Config::ArgumentSize const *const numberOfJacobians, size_t const &startAdjointPos, size_t const &endAdjointPos)
Perform a reverse evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition jacobianLinearTape.hpp:181
T_TapeTypes TapeTypes
See JacobianLinearTape.
Definition jacobianLinearTape.hpp:66
static void internalWriteTape(codi::TapeWriterInterface< Real > *writer, size_t &curLLFByteDataPos, size_t const &endLLFByteDataPos, char *dataPtr, size_t &curLLFInfoDataPos, size_t const &endLLFInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, size_t &curJacobianPos, size_t const &endJacobianPos, typename Real::Real const *const rhsJacobians, typename Real::Identifier const *const rhsIdentifiers, size_t &curStmtPos, size_t const &endStmtPos, Config::ArgumentSize const *const numberOfJacobians, size_t const &startAdjointPos, size_t const &endAdjointPos)
Passes the statement to the writer.
Definition jacobianLinearTape.hpp:236
typename TapeTypes::Identifier Identifier
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:76
friend Base
Allow the base class to call protected and private methods.
Definition jacobianLinearTape.hpp:71
void pushStmtData(Identifier const &index, Config::ArgumentSize const &numberOfArguments)
Add statement specific data to the data streams.
Definition jacobianLinearTape.hpp:125
typename Base::Position Position
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:77
static void internalEvaluateForward_EvalStatements(JacobianLinearTape &tape, AdjointVector &&adjointVector, size_t &curLLFByteDataPos, size_t const &endLLFByteDataPos, char *dataPtr, size_t &curLLFInfoDataPos, size_t const &endLLFInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, size_t &curJacobianPos, size_t const &endJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, size_t &curStmtPos, size_t const &endStmtPos, Config::ArgumentSize const *const numberOfJacobians, size_t const &startAdjointPos, size_t const &endAdjointPos)
Perform a forward evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition jacobianLinearTape.hpp:133
Type definitions for the Jacobian tapes.
Definition jacobianBaseTape.hpp:78
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:128
virtual void writeLowLevelFunction(size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr)
Used for statements that contain a low level function.
Definition tapeReaderWriterInterface.hpp:169
virtual void writeStatement(Identifier const &curLhsIdentifier, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers, Config::ArgumentSize const &nJacobians)
Called for each statement. The method writes the current statement to the file. This overload is used...
Definition tapeReaderWriterInterface.hpp:149