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;
80 CODI_STATIC_ASSERT(IndexManager::IsLinear,
"This class requires an index manager with a linear scheme.");
94 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
95 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
96 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
98 startIndex = std::min(startIndex, (IndexPosition)this->
adjoints.size() - 1);
99 endIndex = std::min(endIndex, (IndexPosition)this->
adjoints.size() - 1);
101 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
111 template<
typename Adjo
intVector>
113 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
114 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
115 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
117 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
133 template<
typename Adjo
intVector>
138 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
143 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real const*
const rhsJacobians,
146 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
148 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
149 CODI_UNUSED(endJacobianPos, endStmtPos, endLLFByteDataPos, endLLFInfoDataPos);
155 size_t curAdjointPos = startAdjointPos;
157 while (curAdjointPos < endAdjointPos)
CODI_Likely {
164 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
168 Adjoint lhsAdjoint = Adjoint();
170 adjointVector[curAdjointPos] = lhsAdjoint;
181 template<
typename Adjo
intVector>
186 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
191 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real const*
const rhsJacobians,
194 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
196 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
197 CODI_UNUSED(endJacobianPos, endStmtPos, endLLFByteDataPos, endLLFInfoDataPos);
203 size_t curAdjointPos = startAdjointPos;
205 while (curAdjointPos > endAdjointPos)
CODI_Likely {
211 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
217 Adjoint
const lhsAdjoint = adjointVector[curAdjointPos];
225 adjointVector[curAdjointPos] = Adjoint();
236 template<
typename TapeTypes>
241 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
246 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real const*
const rhsJacobians,
249 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
251 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
252 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endJacobianPos, endStmtPos);
259 size_t curAdjointPos = startAdjointPos;
260 while (curAdjointPos < endAdjointPos)
CODI_Likely {
262 curLhsIdentifier = curAdjointPos;
263 argsSize = numberOfJacobians[curStmtPos];
269 writer->
writeStatement(curLhsIdentifier, curJacobianPos, rhsJacobians, rhsIdentifiers, argsSize);
271 writer->
writeStatement(curLhsIdentifier, curJacobianPos, rhsJacobians, rhsIdentifiers, argsSize);
272 curJacobianPos += argsSize;
285 template<
typename Callbacks>
292 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
294 size_t& curLLFInfoDataPos,
size_t const& endLLFInfoDataPos,
297 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real*
const rhsJacobians,
300 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
303 CODI_UNUSED(tape, endJacobianPos, endLLFByteDataPos, endLLFInfoDataPos, endStmtPos);
308 size_t curAdjointPos = startAdjointPos;
309 while (curAdjointPos < endAdjointPos)
CODI_Likely {
316 dataSizePtr, dataView, func);
317 callbacks.handleLowLevelFunction(*func, dataView);
324 callbacks.handleStatement(lhsIdentifier, argsSize, &rhsJacobians[curJacobianPos],
325 &rhsIdentifiers[curJacobianPos]);
330 curJacobianPos += argsSize;
342 template<
typename Callbacks>
349 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
351 size_t& curLLFInfoDataPos,
size_t const& endLLFInfoDataPos,
354 size_t& curJacobianPos,
size_t const& endJacobianPos,
Real*
const rhsJacobians,
357 size_t& curStmtPos,
size_t const& endStmtPos,
Config::ArgumentSize const*
const numberOfJacobians,
360 CODI_UNUSED(tape, endJacobianPos, endLLFByteDataPos, endLLFInfoDataPos, endStmtPos);
365 size_t curAdjointPos = startAdjointPos;
366 while (curAdjointPos > endAdjointPos)
CODI_Likely {
373 dataSizePtr, dataView, func);
374 callbacks.handleLowLevelFunction(*func, dataView);
380 curJacobianPos -= argsSize;
383 callbacks.handleStatement(lhsIdentifier, argsSize, &rhsJacobians[curJacobianPos],
384 &rhsIdentifiers[curJacobianPos]);
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:408
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:406
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
#define CODI_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:130
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
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:298
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
inlinestd::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
inlinesize_t constexpr dim()
Number of dimensions this gradient value has.
Definition gradientTraits.hpp:96
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:100
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Definition tapeParameters.hpp:102
ChunkedData< Chunk, NestedData > DefaultChunkedData
ChunkData DataInterface used in all regular tapes.
Definition chunkedData.hpp:520
Definition byteDataView.hpp:51
void iterateForward(Callbacks &&callbacks)
Iterate over the tape in a generalized fashion. callbacks needs to implement codi::CallbacksInterface...
Definition commonTapeImplementation.hpp:371
inlinestatic void callLowLevelFunction(Impl &impl, bool forward, size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFTInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, Args &&... args)
Definition commonTapeImplementation.hpp:569
LowLevelFunctionByteData llfByteData
Definition commonTapeImplementation.hpp:158
inlinestatic Config::LowLevelFunctionToken prepareLowLevelFunction(bool forward, size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFTInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr, ByteDataView &dataView, LowLevelFunctionEntry< Impl, Real, Identifier > const *&func)
Prepare the call of a low level function.
Definition commonTapeImplementation.hpp:540
void iterateReverse(Callbacks &&callbacks)
Iterate over the tape in a generalized fashion. callbacks needs to implement codi::CallbacksInterface...
Definition commonTapeImplementation.hpp:379
static inlinevoid 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
typename Base::Position Position
Definition jacobianBaseTape.hpp:157
inlinestatic void incrementAdjoints(AdjointVector &adjointVector, AdjointVectorTraits::Gradient< AdjointVector > const &lhsAdjoint, Config::ArgumentSize const &numberOfArguments, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers)
Definition jacobianBaseTape.hpp:655
StatementData statementData
Definition jacobianBaseTape.hpp:178
Adjoints adjoints
Definition jacobianBaseTape.hpp:181
inlinestatic void incrementTangents(AdjointVector const &adjointVector, AdjointVectorTraits::Gradient< AdjointVector > &lhsAdjoint, Config::ArgumentSize const &numberOfArguments, size_t &curJacobianPos, Real const *const rhsJacobians, Identifier const *const rhsIdentifiers)
Definition jacobianBaseTape.hpp:678
JacobianBaseTape()
Definition jacobianBaseTape.hpp:215
AdjointVectorAccess< Real, Identifier, AdjointVector > VectorAccess
Definition jacobianBaseTape.hpp:161
inlinevoid clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition jacobianBaseTape.hpp:599
inlinevoid pushStmtData(Identifier const &index, Config::ArgumentSize const &numberOfArguments)
Add statement specific data to the data streams.
Definition jacobianLinearTape.hpp:126
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:73
inlinestatic void internalWriteTape(codi::TapeWriterInterface< TapeTypes > *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, 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)
Passes the statement to the writer.
Definition jacobianLinearTape.hpp:237
inlinevoid iterateReverse(Callbacks &&callbacks, Position start, Position end)
Iterate over the tape in a generalized fashion. callbacks needs to implement codi::CallbacksInterface...
Definition jacobianLinearTape.hpp:343
typename TapeTypes::ActiveTypeTapeData ActiveTypeTapeData
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:77
void clearCustomAdjoints(Position const &start, Position const &end, AdjointVector &&data)
Clear all adjoint values, that is, set them to zero.
Definition jacobianLinearTape.hpp:112
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:88
typename TapeTypes::IndexManager IndexManager
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:75
JacobianLinearTape()
Constructor.
Definition jacobianLinearTape.hpp:83
inlinestatic 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:134
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:74
T_TapeTypes TapeTypes
See JacobianLinearTape.
Definition jacobianLinearTape.hpp:66
typename TapeTypes::Identifier Identifier
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:76
inlinevoid iterateForward(Callbacks &&callbacks, Position start, Position end)
Iterate over the tape in a generalized fashion. callbacks needs to implement codi::CallbacksInterface...
Definition jacobianLinearTape.hpp:286
JacobianBaseTape< TapeTypes, JacobianLinearTape > Base
Base class abbreviation.
Definition jacobianLinearTape.hpp:70
typename Base::Position Position
See TapeTypesInterface.
Definition jacobianLinearTape.hpp:78
inlinestatic 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:182
Type definitions for the Jacobian tapes.
Definition jacobianBaseTape.hpp:80
Low level function entry on the tape. See LowLevelFunctionTapeInterface for details.
Definition lowLevelFunctionEntry.hpp:69
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:129
virtual void writeLowLevelFunction(LowLevelFunctionEntry< Tape, Real, Identifier > const *func, ByteDataView &data)
Used for statements that contain a low level function.
Definition tapeReaderWriterInterface.hpp:171
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:152