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;
97 CODI_INLINE static void internalClearAdjoints_EvalStatements(
101 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
106 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
108 size_t& curStatementPos,
size_t const& endStatementPos,
111 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
113 while (curStatementPos < endStatementPos)
CODI_Likely {
119 StatementEvaluator::template call<StatementCall::ClearAdjoints, PrimalValueReuseTape>(
120 stmtEvalHandle[curStatementPos], adjointVector, nPassiveValues, &stmtDataPtr[curStatementBytePos]);
122 curStatementBytePos += stmtByteSize[curStatementPos];
125 curStatementPos += 1;
142 template<
typename Adjo
intVector>
146 CODI_T(std::is_same<
typename std::remove_reference<AdjointVector>::type,
Gradient*>::value),
147 "Please enable 'CODI_VariableAdjointInterfaceInPrimalTapes' in order"
148 " to use custom adjoint vectors in the primal value tapes.");
154 this->
llfByteData.evaluateForward(end, start, internalClearAdjoints_EvalStatements, dataVector);
164 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
169 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
171 size_t& curStatementPos,
size_t const& endStatementPos,
174 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
176#if !CODI_VariableAdjointInterfaceInPrimalTapes
180 size_t linearAdjointPos = 0;
184 while (curStatementPos < endStatementPos)
CODI_Likely {
189 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
197 StatementEvaluator::template call<StatementCall::Forward, PrimalValueReuseTape>(
198 stmtEvalHandle[curStatementPos], tape, lhsPrimals.data(), lhsTangents.data(), primalVector,
199 adjointVector, linearAdjointPos, nPassiveValues, &stmtDataPtr[curStatementBytePos]);
201 curStatementBytePos += stmtByteSize[curStatementPos];
204 curStatementPos += 1;
213 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
218 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
220 size_t& curStatementPos,
size_t const& endStatementPos,
223 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
228 size_t linearAdjointPos = 0;
230 while (curStatementPos < endStatementPos)
CODI_Likely {
235 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
237 StatementEvaluator::template call<StatementCall::Primal, PrimalValueReuseTape>(
238 stmtEvalHandle[curStatementPos], tape, lhsPrimals.data(), primalVector, linearAdjointPos,
239 numberOfPassiveArguments[curStatementPos], &stmtDataPtr[curStatementBytePos]);
241 curStatementBytePos += stmtByteSize[curStatementPos];
244 curStatementPos += 1;
253 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
258 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
260 size_t& curStatementPos,
size_t const& endStatementPos,
263 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
265#if !CODI_VariableAdjointInterfaceInPrimalTapes
269 size_t linearAdjointPos = 0;
272 while (curStatementPos > endStatementPos)
CODI_Likely {
273 curStatementPos -= 1;
279 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
287 curStatementBytePos -= stmtByteSize[curStatementPos];
289 StatementEvaluator::template call<StatementCall::Reverse, PrimalValueReuseTape>(
290 stmtEvalHandle[curStatementPos], tape, lhsAdjoints.data(), primalVector, adjointVector,
291 linearAdjointPos, numberOfPassiveArguments[curStatementPos], &stmtDataPtr[curStatementBytePos]);
297 template<
typename TapeTypes>
304 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
309 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
311 size_t& curStatementPos,
size_t const& endStatementPos,
314 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
316 while (curStatementPos < endStatementPos) {
323 StatementEvaluator::template call<StatementCall::WriteInformation, PrimalValueReuseTape>(
324 stmtEvalHandle[curStatementPos], writeInfo, primalVector, nPassiveValues,
325 &stmtDataPtr[curStatementBytePos]);
331 writer->
writeStatement(writeInfo, pointers.lhsIdentifiers, pointers.oldLhsValues, nPassiveValues,
332 pointers.rhsIdentifiers, pointers.passiveValues, pointers.constantValues,
333 stmtEvalHandle[curStatementPos]);
335 curStatementBytePos += stmtByteSize[curStatementPos];
338 curStatementPos += 1;
344 CODI_INLINE static void internalResetPrimals_EvalStatements(
348 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
353 size_t& curStatementBytePos,
size_t const& endStatementBytePos,
char* stmtDataPtr,
355 size_t& curStatementPos,
size_t const& endStatementPos,
358 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endStatementBytePos);
360 while (curStatementPos > endStatementPos)
CODI_Likely {
361 curStatementPos -= 1;
368 curStatementBytePos -= stmtByteSize[curStatementPos];
370 StatementEvaluator::template call<StatementCall::ResetPrimals, PrimalValueReuseTape>(
371 stmtEvalHandle[curStatementPos], primalVector, nPassiveValues, &stmtDataPtr[curStatementBytePos]);
#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_STATIC_ASSERT(cond, message)
Static assert definition for CoDiPack. Not evaluated in IDE mode.
Definition macros.hpp:129
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
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 VariableAdjointInterfaceInPrimalTapes
Allow custom adjoint vector in primal values tapes.
Definition config.h:281
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
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
Position getPosition() const
Current position of the tape.
Definition commonTapeImplementation.hpp:684
inlinestatic void skipLowLevelFunction(bool forward, size_t &curLLFByteDataPos, char *dataPtr, size_t &curLLFTInfoDataPos, Config::LowLevelFunctionToken *const tokenPtr, Config::LowLevelFunctionDataSize *const dataSizePtr)
Skip the data for a low level function.
Definition commonTapeImplementation.hpp:552
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
Gradient * selectAdjointVector(VectorAccess< AdjointVector > *vectorAccess, AdjointVector data)
Definition primalValueBaseTape.hpp:773
std::vector< Real > primals
Definition primalValueBaseTape.hpp:184
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
inlinestatic void internalEvaluateForward_EvalStatements(PrimalValueReuseTape &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)
Perform a forward evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition primalValueReuseTape.hpp:160
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)
Passes the statement information and the stmtEvalHandle to the writer.
Definition primalValueReuseTape.hpp:298
inlinevoid internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
Definition primalValueReuseTape.hpp:379
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:75
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:76
typename TapeTypes::StatementData StatementData
See PrimalValueTapeTypes.
Definition primalValueReuseTape.hpp:83
typename Base::StatementDataPointers StatementDataPointers
Defined in PrimalValueBaseTape.
Definition primalValueReuseTape.hpp:85
inlinestatic void internalEvaluateReverse_EvalStatements(PrimalValueReuseTape &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)
Perform a reverse evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition primalValueReuseTape.hpp:249
typename Base::template StackArray< T > StackArray
See PrimalValueBaseTape.
Definition primalValueReuseTape.hpp:88
void revertPrimals(Position const &pos)
Revert the primals to the state indicated by pos.
Definition primalValueReuseTape.hpp:386
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 primalValueReuseTape.hpp:134
T_TapeTypes TapeTypes
See PrimalValueReuseTape.
Definition primalValueReuseTape.hpp:67
void clearCustomAdjoints(Position const &start, Position const &end, AdjointVector data)
Clear all adjoint values, that is, set them to zero.
Definition primalValueReuseTape.hpp:143
typename TapeTypes::Identifier Identifier
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:77
typename TapeTypes::StatementEvaluator StatementEvaluator
See PrimalValueTapeTypes.
Definition primalValueReuseTape.hpp:79
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:81
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueReuseTape.hpp:78
typename TapeTypes::EvalHandle EvalHandle
See PrimalValueTapeTypes.
Definition primalValueReuseTape.hpp:80
PrimalValueBaseTape< T_TapeTypes, PrimalValueReuseTape< T_TapeTypes > > Base
Base class abbreviation.
Definition primalValueReuseTape.hpp:72
PrimalValueReuseTape()
Constructor.
Definition primalValueReuseTape.hpp:91
inlinestatic void internalEvaluatePrimal_EvalStatements(PrimalValueReuseTape &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)
Perform a primal evaluation of the tape. Arguments are from the recursive eval methods of the DataInt...
Definition primalValueReuseTape.hpp:209
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