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);
120 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
125 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
127 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
129 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
131 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
134 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
136#if !CODI_VariableAdjointInterfaceInPrimalTapes
140 while (curStatementPos < endStatementPos)
CODI_Likely {
144 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
145 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
153 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
157 oldPrimalValues[curStatementPos] = primalVector[lhsIdentifier];
158 primalVector[lhsIdentifier] = StatementEvaluator::template callForward<PrimalValueReuseTape>(
159 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsTangent, nPassiveValues,
160 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
162#if CODI_VariableAdjointInterfaceInPrimalTapes
163 adjointVector->setLhsTangent(lhsIdentifier);
165 tape, lhsIdentifier, adjointVector->getVectorSize(), adjointVector->getAdjointVec(lhsIdentifier));
167 adjointVector[lhsIdentifier] = lhsTangent;
172 primalVector[lhsIdentifier]);
175 curStatementPos += 1;
184 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
189 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
191 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
193 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
195 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
198 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
202 while (curStatementPos < endStatementPos)
CODI_Likely {
206 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Primal>(
207 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
209 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
211 oldPrimalValues[curStatementPos] = primalVector[lhsIdentifier];
212 primalVector[lhsIdentifier] = StatementEvaluator::template callPrimal<PrimalValueReuseTape>(
213 stmtEvalhandle[curStatementPos], primalVector, numberOfPassiveArguments[curStatementPos],
214 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
217 primalVector[lhsIdentifier]);
220 curStatementPos += 1;
229 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
234 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
236 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
238 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
240 size_t& curStatementPos,
size_t const& endStatementPos,
Identifier const*
const lhsIdentifiers,
243 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos);
245#if !CODI_VariableAdjointInterfaceInPrimalTapes
249 while (curStatementPos > endStatementPos)
CODI_Likely {
250 curStatementPos -= 1;
255 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
256 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
264 Identifier const lhsIdentifier = lhsIdentifiers[curStatementPos];
266#if CODI_VariableAdjointInterfaceInPrimalTapes
268 tape, lhsIdentifier, adjointVector->getVectorSize(), adjointVector->getAdjointVec(lhsIdentifier));
270 adjointVector->setLhsAdjoint(lhsIdentifier);
272 Gradient const lhsAdjoint = adjointVector[lhsIdentifier];
275 adjointVector[lhsIdentifier] =
Gradient();
278 primalVector[lhsIdentifier]);
280 primalVector[lhsIdentifier] = oldPrimalValues[curStatementPos];
282 StatementEvaluator::template callReverse<PrimalValueReuseTape>(
283 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsAdjoint,
284 numberOfPassiveArguments[curStatementPos], curConstantPos, constantValues, curPassivePos, passiveValues,
285 curRhsIdentifiersPos, rhsIdentifiers);
297 this->
primals[*lhsIndex] = *oldPrimal;
300 using StmtPosition =
typename StatementData::Position;
302 StmtPosition endStmt = this->
llfByteData.template extractPosition<StmtPosition>(pos);
304 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
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:90
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:629
LowLevelFunctionByteData llfByteData
Byte data for low level functions.
Definition commonTapeImplementation.hpp:155
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:58
Base class for all standard Primal value tape implementations.
Definition primalValueBaseTape.hpp:136
std::vector< Real > primals
Current state of primal values in the program.
Definition primalValueBaseTape.hpp:189
std::vector< Gradient > adjoints
Evaluation vector for AD.
Definition primalValueBaseTape.hpp:188
typename Base::Position Position
See TapeTypesInterface.
Definition primalValueBaseTape.hpp:165
void clearAdjoints(AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Clear all adjoint values, that is, set them to zero.
Definition primalValueBaseTape.hpp:522
StatementData statementData
Data stream for statement specific data.
Definition primalValueBaseTape.hpp:183
Final implementation for a primal value tape with a reuse index management.
Definition primalValueReuseTape.hpp:64
void internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
Definition primalValueReuseTape.hpp:291
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:225
friend Base
Allow the base class to call protected and private methods.
Definition primalValueReuseTape.hpp:73
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:116
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:309
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:180
void revertPrimals(Position const &pos)
Revert the primals to the state indicated by pos.
Definition primalValueReuseTape.hpp:316
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
T_TapeTypes TapeTypes
See PrimalValueReuseTape.
Definition primalValueReuseTape.hpp:67
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:77
Creation of handles for the evaluation of expressions in a context where the expression type is not a...
Definition statementEvaluatorInterface.hpp:103