42#include "../expressions/lhsExpressionInterface.hpp"
43#include "../expressions/logic/compileTimeTraversalLogic.hpp"
44#include "../expressions/logic/constructStaticContext.hpp"
45#include "../expressions/logic/traversalLogic.hpp"
47#include "../misc/memberStore.hpp"
48#include "../traits/expressionTraits.hpp"
49#include "data/chunk.hpp"
50#include "indices/indexManagerInterface.hpp"
51#include "primalValueBaseTape.hpp"
63 template<
typename T_TapeTypes>
75 using Real =
typename TapeTypes::Real;
101 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
102 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
103 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
105 startIndex = std::min(startIndex, (IndexPosition)this->
adjoints.size() - 1);
106 endIndex = std::min(endIndex, (IndexPosition)this->
adjoints.size() - 1);
108 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
114 template<
typename Adjo
intVector>
116 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
117 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
118 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
120 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
132 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
137 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
139 size_t& curStatementPos,
size_t const& endStatementPos,
143 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
144 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos, endAdjointPos);
146 size_t curAdjointPos = startAdjointPos;
150#if !CODI_VariableAdjointInterfaceInPrimalTapes
154 while (curStatementPos < endStatementPos)
CODI_Likely {
159 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
169 StatementEvaluator::template call<StatementCall::Forward, PrimalValueLinearTape>(
170 stmtEvalHandle[curStatementPos], tape, lhsPrimals.data(), lhsTangents.data(), primalVector,
171 adjointVector, curAdjointPos, nPassiveValues, &stmtDataPtr[curStatementBytePos]);
173 curStatementBytePos += stmtByteSize[curStatementPos];
176 curStatementPos += 1;
185 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
190 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
192 size_t& curStatementPos,
size_t const& endStatementPos,
196 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
197 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos, endAdjointPos);
199 size_t curAdjointPos = startAdjointPos;
204 while (curStatementPos < endStatementPos)
CODI_Likely {
209 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
213 StatementEvaluator::template call<StatementCall::Primal, PrimalValueLinearTape>(
214 stmtEvalHandle[curStatementPos], tape, lhsPrimals.data(), primalVector, curAdjointPos, nPassiveValues,
215 &stmtDataPtr[curStatementBytePos]);
217 curStatementBytePos += stmtByteSize[curStatementPos];
220 curStatementPos += 1;
229 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
234 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
236 size_t& curStatementPos,
size_t const& endStatementPos,
240 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
241 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos, endAdjointPos);
243 size_t curAdjointPos = startAdjointPos;
246#if !CODI_VariableAdjointInterfaceInPrimalTapes
250 while (curStatementPos > endStatementPos)
CODI_Likely {
251 curStatementPos -= 1;
257 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
267 curStatementBytePos -= stmtByteSize[curStatementPos];
269 StatementEvaluator::template call<StatementCall::Reverse, PrimalValueLinearTape>(
270 stmtEvalHandle[curStatementPos], tape, lhsAdjoints.data(), primalVector, adjointVector, curAdjointPos,
271 nPassiveValues, &stmtDataPtr[curStatementBytePos]);
277 template<
typename TapeTypes>
284 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
289 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
291 size_t& curStatementPos,
size_t const& endStatementPos,
295 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
296 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos, endAdjointPos);
298 size_t curAdjointPos = startAdjointPos;
301 while (curStatementPos < endStatementPos) {
308 StatementEvaluator::template call<StatementCall::WriteInformation, PrimalValueLinearTape>(
309 stmtEvalHandle[curStatementPos], writeInfo, primalVector, nPassiveValues,
310 &stmtDataPtr[curStatementBytePos]);
316 Real const* lhsPrimalValues = &primalVector[curAdjointPos + 1];
319 lhsIdentifiers[i] = curAdjointPos;
322 writer->
writeStatement(writeInfo, lhsIdentifiers.data(), lhsPrimalValues, nPassiveValues,
323 pointers.rhsIdentifiers, pointers.passiveValues, pointers.constantValues,
324 stmtEvalHandle[curStatementPos]);
326 curStatementBytePos += stmtByteSize[curStatementPos];
329 curStatementPos += 1;
#define CODI_Unlikely
Declare unlikely evaluation of an execution path.
Definition config.h:408
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_VariableAdjointInterfaceInPrimalTapes
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:278
#define ADJOINT_VECTOR_TYPE
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:286
#define CODI_Likely
Declare likely evaluation of an execution path.
Definition config.h:406
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
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
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
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
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
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)
Called by the implementing tapes during a tape evaluation when a low level function statement has bee...
Definition commonTapeImplementation.hpp:517
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:156
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
PrimalValueBaseTape()
Definition primalValueBaseTape.hpp:228
std::vector< Gradient > adjoints
Definition primalValueBaseTape.hpp:183
inlinevoid clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Definition primalValueBaseTape.hpp:705
typename Base::Position Position
Definition primalValueBaseTape.hpp:158
PrimalAdjointVectorAccess< Real, Identifier, AdjointVector > VectorAccess
Definition primalValueBaseTape.hpp:162
typename TapeTypes::StatementEvaluator StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueLinearTape.hpp:80
T_TapeTypes TapeTypes
See PrimalValueLinearTape.
Definition primalValueLinearTape.hpp:67
void clearCustomAdjoints(Position const &start, Position const &end, AdjointVector data)
Clear all adjoint values, that is, set them to zero.
Definition primalValueLinearTape.hpp:115
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:82
typename TapeTypes::Identifier Identifier
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:78
PrimalValueLinearTape()
Constructor.
Definition primalValueLinearTape.hpp:90
inlinestatic void internalEvaluateReverse_EvalStatements(PrimalValueLinearTape &tape, Real *primalVector, Gradient *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 &curStatementBytePos, size_t const &endStatementBytePos, char *stmtDataPtr, size_t &curStatementPos, size_t const &endStatementPos, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, Config::LowLevelFunctionDataSize *const stmtByteSize, 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 primalValueLinearTape.hpp:225
typename TapeTypes::IndexManager IndexManager
See PrimalValueTapeTypes.
Definition primalValueLinearTape.hpp:77
typename TapeTypes::EvalHandle EvalHandle
See PrimalValueTapeTypes.
Definition primalValueLinearTape.hpp:81
void revertPrimals(Position const &pos)
Revert the primals to the state indicated by pos.
Definition primalValueLinearTape.hpp:344
typename Base::template StackArray< T > StackArray
See PrimalValueBaseTape.
Definition primalValueLinearTape.hpp:87
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:75
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:76
inlinevoid internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
Definition primalValueLinearTape.hpp:335
inlinestatic void internalEvaluatePrimal_EvalStatements(PrimalValueLinearTape &tape, Real *primalVector, 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 &curStatementBytePos, size_t const &endStatementBytePos, char *stmtDataPtr, size_t &curStatementPos, size_t const &endStatementPos, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, Config::LowLevelFunctionDataSize *const stmtByteSize, size_t const &startAdjointPos, size_t const &endAdjointPos)
Perform a primal evaluation of the tape. Arguments are from the recursive eval methods of the DataInt...
Definition primalValueLinearTape.hpp:181
PrimalValueBaseTape< T_TapeTypes, PrimalValueLinearTape< T_TapeTypes > > Base
Base class abbreviation.
Definition primalValueLinearTape.hpp:72
inlinestatic void internalWriteTape(Real *primalVector, 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 &curStatementBytePos, size_t const &endStatementBytePos, char *stmtDataPtr, size_t &curStatementPos, size_t const &endStatementPos, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, Config::LowLevelFunctionDataSize *const stmtByteSize, size_t const &startAdjointPos, size_t const &endAdjointPos)
Passes the statement information and the stmtEvalHandle to the writer.
Definition primalValueLinearTape.hpp:278
inlinestatic void internalEvaluateForward_EvalStatements(PrimalValueLinearTape &tape, Real *primalVector, Gradient *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 &curStatementBytePos, size_t const &endStatementBytePos, char *stmtDataPtr, size_t &curStatementPos, size_t const &endStatementPos, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, Config::LowLevelFunctionDataSize *const stmtByteSize, 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 primalValueLinearTape.hpp:128
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueLinearTape.hpp:79
typename Base::StatementDataPointers StatementDataPointers
Defined in PrimalValueBaseTape.
Definition primalValueLinearTape.hpp:84
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 primalValueLinearTape.hpp:97
Type definitions for the primal value tapes.
Definition primalValueBaseTape.hpp:81
Creation of handles for the evaluation of expressions in a context where the expression type is not a...
Definition statementEvaluatorInterface.hpp:103
The interface used by all the tape writers. The tape writers are used to generate text,...
Definition tapeReaderWriterInterface.hpp:129
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: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
This class is used during the writing process of a primal value tape. The WriteInfo is returned by St...
Definition tapeReaderWriterInterface.hpp:69
size_t numberOfActiveArguments
Number of active arguments.
Definition tapeReaderWriterInterface.hpp:71
size_t numberOfOutputArguments
Number of output arguments.
Definition tapeReaderWriterInterface.hpp:70
size_t numberOfConstantArguments
Number of constant arguments.
Definition tapeReaderWriterInterface.hpp:72