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;
96 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
97 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
98 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
100 startIndex = std::min(startIndex, (IndexPosition)this->
adjoints.size() - 1);
101 endIndex = std::min(endIndex, (IndexPosition)this->
adjoints.size() - 1);
103 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
109 template<
typename Adjo
intVector>
111 using IndexPosition =
CODI_DD(
typename IndexManager::Position,
int);
112 IndexPosition startIndex = this->
llfByteData.template extractPosition<IndexPosition>(start);
113 IndexPosition endIndex = this->
llfByteData.template extractPosition<IndexPosition>(end);
115 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
127 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
132 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
134 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
136 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
138 size_t& curStatementPos,
size_t const& endStatementPos,
141 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
142 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
145 size_t curAdjointPos = startAdjointPos;
147#if !CODI_VariableAdjointInterfaceInPrimalTapes
151 while (curAdjointPos < endAdjointPos)
CODI_Likely {
157 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
158 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
170 primalVector[curAdjointPos] = StatementEvaluator::template callForward<PrimalValueLinearTape>(
171 stmtEvalHandle[curStatementPos], primalVector, adjointVector, lhsTangent, nPassiveValues,
172 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
174#if CODI_VariableAdjointInterfaceInPrimalTapes
175 adjointVector->setLhsTangent(curAdjointPos);
177 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
179 adjointVector[curAdjointPos] = lhsTangent;
185 primalVector[curAdjointPos]);
188 curStatementPos += 1;
197 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
202 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
204 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
206 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
208 size_t& curStatementPos,
size_t const& endStatementPos,
211 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
212 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
215 size_t curAdjointPos = startAdjointPos;
219 while (curAdjointPos < endAdjointPos)
CODI_Likely {
225 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Primal>(
226 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
230 primalVector[curAdjointPos] = StatementEvaluator::template callPrimal<PrimalValueLinearTape>(
231 stmtEvalHandle[curStatementPos], primalVector, nPassiveValues, curConstantPos, constantValues,
232 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
235 primalVector[curAdjointPos]);
238 curStatementPos += 1;
247 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
252 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
254 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
256 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
258 size_t& curStatementPos,
size_t const& endStatementPos,
261 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
262 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
265 size_t curAdjointPos = startAdjointPos;
267#if !CODI_VariableAdjointInterfaceInPrimalTapes
271 while (curAdjointPos > endAdjointPos)
CODI_Likely {
272 curStatementPos -= 1;
277 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
278 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
288#if CODI_VariableAdjointInterfaceInPrimalTapes
291 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
294 adjointVector->setLhsAdjoint(curAdjointPos);
296 Gradient const lhsAdjoint = adjointVector[curAdjointPos];
302 adjointVector[curAdjointPos] =
Gradient();
306 primalVector[curAdjointPos]);
308 StatementEvaluator::template callReverse<PrimalValueLinearTape>(
309 stmtEvalHandle[curStatementPos], primalVector, adjointVector, lhsAdjoint, nPassiveValues,
310 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
318 template<
typename TapeTypes>
325 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
330 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
332 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
334 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
336 size_t& curStatementPos,
size_t const& endStatementPos,
339 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
340 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
343 size_t curAdjointPos = startAdjointPos;
345 while (curAdjointPos < endAdjointPos) {
352 WriteInfo writeInfo = StatementEvaluator::template getWriteInformation<PrimalValueLinearTape>(
353 stmtEvalHandle[curStatementPos], primalVector, nPassiveValues, curConstantPos, constantValues,
354 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
357 writer->
writeStatement(writeInfo, curAdjointPos, primalVector[curAdjointPos], nPassiveValues,
358 curRhsIdentifiersPos, rhsIdentifiers, curPassivePos, passiveValues, curConstantPos,
359 constantValues, stmtEvalHandle[curStatementPos]);
362 writer->
writeStatement(writeInfo, curAdjointPos, primalVector[curAdjointPos], nPassiveValues,
363 curRhsIdentifiersPos, rhsIdentifiers, curPassivePos, passiveValues, curConstantPos,
364 constantValues, stmtEvalHandle[curStatementPos]);
368 curPassivePos += nPassiveValues;
372 curStatementPos += 1;
#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
bool constexpr ReversalZeroesAdjoints
With a linear index management, control if adjoints are set to zero during reversal.
Definition config.h:289
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
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
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< 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 linear index management.
Definition primalValueLinearTape.hpp:64
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:110
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:85
static 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 &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, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, 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:243
friend Base
Allow the base class to call protected and private methods.
Definition primalValueLinearTape.hpp:73
void internalResetPrimalValues(Position const &pos)
Reset the primal values to the given position.
Definition primalValueLinearTape.hpp:378
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:396
void pushStmtData(Identifier const &index, Config::ArgumentSize const &numberOfPassiveArguments, Real const &oldPrimalValue, EvalHandle evalHandle)
Add statement specific data to the data streams.
Definition primalValueLinearTape.hpp:386
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:75
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:76
static 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 &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, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, 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:193
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, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, size_t const &startAdjointPos, size_t const &endAdjointPos)
Passes the statement information and the stmtEvalHandle to the writer.
Definition primalValueLinearTape.hpp:319
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueLinearTape.hpp:79
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:92
static 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 &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, Config::ArgumentSize const *const numberOfPassiveArguments, EvalHandle const *const stmtEvalHandle, 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:123
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