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;
106 using StmtPosition =
typename StatementData::Position;
107 StmtPosition startStmt = this->
llfByteData.template extractPosition<StmtPosition>(start);
108 StmtPosition endStmt = this->
llfByteData.template extractPosition<StmtPosition>(end);
110 this->
statementData.forEachReverse(startStmt, endStmt, clearFunc);
114 template<
typename Adjo
intVector>
123 using StmtPosition =
typename StatementData::Position;
124 StmtPosition startStmt = this->
llfByteData.template extractPosition<StmtPosition>(start);
125 StmtPosition endStmt = this->
llfByteData.template extractPosition<StmtPosition>(end);
127 this->
statementData.forEachReverse(startStmt, endStmt, clearFunc);
137 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
142 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
144 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
146 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
148 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
151 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
153#if !CODI_VariableAdjointInterfaceInPrimalTapes
157 while (curStatementPos < endStatementPos)
CODI_Likely {
161 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
162 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
170 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
174 oldPrimalValues[curStatementPos] = primalVector[lhsIdentifier];
175 primalVector[lhsIdentifier] = StatementEvaluator::template callForward<PrimalValueReuseTape>(
176 stmtEvalHandle[curStatementPos], primalVector, adjointVector, lhsTangent, nPassiveValues,
177 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
179#if CODI_VariableAdjointInterfaceInPrimalTapes
180 adjointVector->setLhsTangent(lhsIdentifier);
182 tape, lhsIdentifier, adjointVector->getVectorSize(), adjointVector->getAdjointVec(lhsIdentifier));
184 adjointVector[lhsIdentifier] = lhsTangent;
189 primalVector[lhsIdentifier]);
192 curStatementPos += 1;
201 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
206 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
208 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
210 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
212 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
215 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
219 while (curStatementPos < endStatementPos)
CODI_Likely {
223 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Primal>(
224 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
226 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
228 oldPrimalValues[curStatementPos] = primalVector[lhsIdentifier];
229 primalVector[lhsIdentifier] = StatementEvaluator::template callPrimal<PrimalValueReuseTape>(
230 stmtEvalHandle[curStatementPos], primalVector, numberOfPassiveArguments[curStatementPos],
231 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
234 primalVector[lhsIdentifier]);
237 curStatementPos += 1;
246 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
251 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
253 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
255 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
257 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
260 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
262#if !CODI_VariableAdjointInterfaceInPrimalTapes
266 while (curStatementPos > endStatementPos)
CODI_Likely {
267 curStatementPos -= 1;
272 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
273 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
281 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
283#if CODI_VariableAdjointInterfaceInPrimalTapes
285 tape, lhsIdentifier, adjointVector->getVectorSize(), adjointVector->getAdjointVec(lhsIdentifier));
287 adjointVector->setLhsAdjoint(lhsIdentifier);
289 Gradient const lhsAdjoint = adjointVector[lhsIdentifier];
292 adjointVector[lhsIdentifier] =
Gradient();
295 primalVector[lhsIdentifier]);
297 primalVector[lhsIdentifier] = oldPrimalValues[curStatementPos];
299 StatementEvaluator::template callReverse<PrimalValueReuseTape>(
300 stmtEvalHandle[curStatementPos], primalVector, adjointVector, lhsAdjoint,
301 numberOfPassiveArguments[curStatementPos], curConstantPos, constantValues, curPassivePos, passiveValues,
302 curRhsIdentifiersPos, rhsIdentifiers);
308 template<
typename TapeTypes>
315 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
320 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
322 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
324 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
326 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
329 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
332 while (curStatementPos < endStatementPos) {
338 WriteInfo writeInfo = StatementEvaluator::template getWriteInformation<PrimalValueReuseTape>(
339 stmtEvalHandle[curStatementPos], primalVector, nPassiveValues, curConstantPos, constantValues,
340 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
341 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
343 writer->
writeStatement(writeInfo, lhsIdentifier, oldPrimalValues[curStatementPos], nPassiveValues,
344 curRhsIdentifiersPos, rhsIdentifiers, curPassivePos, passiveValues, curConstantPos,
345 constantValues, stmtEvalHandle[curStatementPos]);
349 curPassivePos += nPassiveValues;
352 curStatementPos += 1;
363 this->
primals[*lhsIndex] = *oldPrimal;
366 using StmtPosition =
typename StatementData::Position;
368 StmtPosition endStmt = this->
llfByteData.template extractPosition<StmtPosition>(pos);
370 this->
statementData.forEachReverse(startStmt, endStmt, clearFunc);
#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_VariableAdjointInterfaceInPrimalTapes
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:269
#define ADJOINT_VECTOR_TYPE
See codi::Config::VariableAdjointInterfaceInPrimalTapes.
Definition config.h:277
#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_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
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
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
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type, that was used in the application.
Definition realTraits.hpp:117
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.
Data is stored chunk-wise in this DataInterface implementation. If a chunk runs out of space,...
Definition chunkedData.hpp:64
Position getPosition() const
Current position of the tape.
Definition commonTapeImplementation.hpp:665
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:156
static void notifyStatementEvaluatePrimalListeners(Tape &tape, Identifier const &lhsIdentifier, Real const &lhsValue)
Invoke callbacks for StatementEvaluatePrimal events.
Definition eventSystem.hpp:745
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
Implementation of VectorAccessInterface for adjoint and primal vectors.
Definition primalAdjointVectorAccess.hpp:59
Base class for all standard Primal value tape implementations.
Definition primalValueBaseTape.hpp:137
std::vector< Real > primals
Current state of primal values in the program.
Definition primalValueBaseTape.hpp:190
std::vector< Gradient > adjoints
Evaluation vector for AD.
Definition primalValueBaseTape.hpp:189
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:166
void clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Clear all adjoint values, that is, set them to zero.
Definition primalValueBaseTape.hpp:524
StatementData statementData
Data stream for statement specific data.
Definition primalValueBaseTape.hpp:184
Final implementation for a primal value tape with a reuse index management.
Definition primalValueReuseTape.hpp:64
static 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 &curConstantPos, size_t const &endConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, size_t const &endPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, size_t const &endRhsIdentifiersPos, Identifier const *const rhsIdentifiers, size_t &curStatementPos, size_t const &endStatementPos, Identifier const *const lhsIdentifiers, Config::ArgumentSize const *const numberOfPassiveArguments, Real const *const oldPrimalValues, EvalHandle const *const stmtEvalHandle)
Perform a reverse evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition primalValueReuseTape.hpp:242
void internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
Definition primalValueReuseTape.hpp:357
friend Base
Allow the base class to call protected and private methods.
Definition primalValueReuseTape.hpp:73
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:75
void pushStmtData(Identifier const &index, Config::ArgumentSize const &numberOfPassiveArguments, Real const &oldPrimalValue, EvalHandle evalHandle)
Add statement specific data to the data streams.
Definition primalValueReuseTape.hpp:375
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueReuseTape.hpp:76
typename TapeTypes::StatementData StatementData
See PrimalValueTapeTypes.
Definition primalValueReuseTape.hpp:83
static 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 &curConstantPos, size_t const &endConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, size_t const &endPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, size_t const &endRhsIdentifiersPos, Identifier const *const rhsIdentifiers, size_t &curStatementPos, size_t const &endStatementPos, Identifier const *const lhsIdentifiers, Config::ArgumentSize const *const numberOfPassiveArguments, Real *const oldPrimalValues, EvalHandle const *const stmtEvalHandle)
Perform a primal evaluation of the tape. Arguments are from the recursive eval methods of the DataInt...
Definition primalValueReuseTape.hpp:197
void revertPrimals(Position const &pos)
Revert the primals to the state indicated by pos.
Definition primalValueReuseTape.hpp:382
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:93
static 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 &curConstantPos, size_t const &endConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, size_t const &endPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, size_t const &endRhsIdentifiersPos, Identifier const *const rhsIdentifiers, size_t &curStatementPos, size_t const &endStatementPos, Identifier const *const lhsIdentifiers, Config::ArgumentSize const *const numberOfPassiveArguments, Real *const oldPrimalValues, EvalHandle const *const stmtEvalHandle)
Passes the statement information and the stmtEvalHandle to the writer.
Definition primalValueReuseTape.hpp:309
T_TapeTypes TapeTypes
See PrimalValueReuseTape.
Definition primalValueReuseTape.hpp:67
static 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 &curConstantPos, size_t const &endConstantPos, PassiveReal const *const constantValues, size_t &curPassivePos, size_t const &endPassivePos, Real const *const passiveValues, size_t &curRhsIdentifiersPos, size_t const &endRhsIdentifiersPos, Identifier const *const rhsIdentifiers, size_t &curStatementPos, size_t const &endStatementPos, Identifier const *const lhsIdentifiers, Config::ArgumentSize const *const numberOfPassiveArguments, Real *const oldPrimalValues, EvalHandle const *const stmtEvalHandle)
Perform a forward evaluation of the tape. Arguments are from the recursive eval methods of the DataIn...
Definition primalValueReuseTape.hpp:133
void clearCustomAdjoints(Position const &start, Position const &end, AdjointVector data)
Clear all adjoint values, that is, set them to zero.
Definition primalValueReuseTape.hpp:115
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
PrimalValueReuseTape()
Constructor.
Definition primalValueReuseTape.hpp:86
Type definitions for the primal value tapes.
Definition primalValueBaseTape.hpp:78
Creation of handles for the evaluation of expressions in a context where the expression type is not a...
Definition statementEvaluatorInterface.hpp:104
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
This class is used during the writing process of a primal value tape. The WriteInfo is returned by co...
Definition tapeReaderWriterInterface.hpp:69
size_t numberOfActiveArguments
Number of active arguments.
Definition tapeReaderWriterInterface.hpp:70
size_t numberOfConstantArguments
Number of constant arguments.
Definition tapeReaderWriterInterface.hpp:71