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) {
115 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
120 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
122 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
124 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
126 size_t& curStatementPos,
size_t const& endStatementPos,
129 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
130 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
133 size_t curAdjointPos = startAdjointPos;
135#if !CODI_VariableAdjointInterfaceInPrimalTapes
139 while (curAdjointPos < endAdjointPos)
CODI_Likely {
145 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
146 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
158 primalVector[curAdjointPos] = StatementEvaluator::template callForward<PrimalValueLinearTape>(
159 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsTangent, nPassiveValues,
160 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
162#if CODI_VariableAdjointInterfaceInPrimalTapes
163 adjointVector->setLhsTangent(curAdjointPos);
165 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
167 adjointVector[curAdjointPos] = lhsTangent;
173 primalVector[curAdjointPos]);
176 curStatementPos += 1;
185 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
190 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
192 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
194 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
196 size_t& curStatementPos,
size_t const& endStatementPos,
199 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
200 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
203 size_t curAdjointPos = startAdjointPos;
207 while (curAdjointPos < endAdjointPos)
CODI_Likely {
213 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Primal>(
214 tape,
true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
218 primalVector[curAdjointPos] = StatementEvaluator::template callPrimal<PrimalValueLinearTape>(
219 stmtEvalhandle[curStatementPos], primalVector, nPassiveValues, curConstantPos, constantValues,
220 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
223 primalVector[curAdjointPos]);
226 curStatementPos += 1;
235 size_t& curLLFByteDataPos,
size_t const& endLLFByteDataPos,
char* dataPtr,
240 size_t& curConstantPos,
size_t const& endConstantPos,
PassiveReal const*
const constantValues,
242 size_t& curPassivePos,
size_t const& endPassivePos,
Real const*
const passiveValues,
244 size_t& curRhsIdentifiersPos,
size_t const& endRhsIdentifiersPos,
Identifier const*
const rhsIdentifiers,
246 size_t& curStatementPos,
size_t const& endStatementPos,
249 size_t const& startAdjointPos,
size_t const& endAdjointPos) {
250 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
253 size_t curAdjointPos = startAdjointPos;
255#if !CODI_VariableAdjointInterfaceInPrimalTapes
259 while (curAdjointPos > endAdjointPos)
CODI_Likely {
260 curStatementPos -= 1;
265 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
266 tape,
false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
276#if CODI_VariableAdjointInterfaceInPrimalTapes
279 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
282 adjointVector->setLhsAdjoint(curAdjointPos);
284 Gradient const lhsAdjoint = adjointVector[curAdjointPos];
290 adjointVector[curAdjointPos] =
Gradient();
294 primalVector[curAdjointPos]);
296 StatementEvaluator::template callReverse<PrimalValueLinearTape>(
297 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsAdjoint, nPassiveValues,
298 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
#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
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: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
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< 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 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
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
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:307
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:325
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:315
typename TapeTypes::Real Real
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:75
typename TapeTypes::Gradient Gradient
See TapeTypesInterface.
Definition primalValueLinearTape.hpp:76
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:111
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:231
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition primalValueLinearTape.hpp:79
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:181
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
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