CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
primalValueLinearTape.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <algorithm>
38#include <functional>
39#include <type_traits>
40
41#include "../config.h"
42#include "../expressions/lhsExpressionInterface.hpp"
43#include "../expressions/logic/compileTimeTraversalLogic.hpp"
44#include "../expressions/logic/constructStaticContext.hpp"
45#include "../expressions/logic/traversalLogic.hpp"
46#include "../misc/macros.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"
52
54namespace codi {
55
63 template<typename T_TapeTypes>
64 struct PrimalValueLinearTape : public PrimalValueBaseTape<T_TapeTypes, PrimalValueLinearTape<T_TapeTypes>> {
65 public:
66
67 using TapeTypes =
68 CODI_DD(T_TapeTypes,
71
73 friend Base;
74
75 using Real = typename TapeTypes::Real;
76 using Gradient = typename TapeTypes::Gradient;
77 using IndexManager = typename TapeTypes::IndexManager;
78 using Identifier = typename TapeTypes::Identifier;
80 using StatementEvaluator = typename TapeTypes::StatementEvaluator;
81 using EvalHandle = typename TapeTypes::EvalHandle;
82 using Position = typename Base::Position;
83
86
88
92 void clearAdjoints(Position const& start, Position const& end,
94 CODI_UNUSED(adjointsManagement);
95
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);
99
100 startIndex = std::min(startIndex, (IndexPosition)this->adjoints.size() - 1);
101 endIndex = std::min(endIndex, (IndexPosition)this->adjoints.size() - 1);
102
103 for (IndexPosition curPos = endIndex + 1; curPos <= startIndex; curPos += 1) {
104 this->adjoints[curPos] = Gradient();
105 }
106 }
107
108 protected:
109
112 /* data from call */
113 PrimalValueLinearTape& tape, Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
114 /* data from low level function byte data vector */
115 size_t& curLLFByteDataPos, size_t const& endLLFByteDataPos, char* dataPtr,
116 /* data from low level function info data vector */
117 size_t& curLLFInfoDataPos, size_t const& endLLFInfoDataPos, Config::LowLevelFunctionToken* const tokenPtr,
118 Config::LowLevelFunctionDataSize* const dataSizePtr,
119 /* data from constantValueData */
120 size_t& curConstantPos, size_t const& endConstantPos, PassiveReal const* const constantValues,
121 /* data from passiveValueData */
122 size_t& curPassivePos, size_t const& endPassivePos, Real const* const passiveValues,
123 /* data from rhsIdentifiersData */
124 size_t& curRhsIdentifiersPos, size_t const& endRhsIdentifiersPos, Identifier const* const rhsIdentifiers,
125 /* data from statementData */
126 size_t& curStatementPos, size_t const& endStatementPos,
127 Config::ArgumentSize const* const numberOfPassiveArguments, EvalHandle const* const stmtEvalhandle,
128 /* data from index handler */
129 size_t const& startAdjointPos, size_t const& endAdjointPos) {
130 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
131 endStatementPos);
132
133 size_t curAdjointPos = startAdjointPos;
134
135#if !CODI_VariableAdjointInterfaceInPrimalTapes
136 typename Base::template VectorAccess<Gradient> vectorAccess(adjointVector, primalVector);
137#endif
138
139 while (curAdjointPos < endAdjointPos) CODI_Likely {
140 curAdjointPos += 1;
141
142 Config::ArgumentSize nPassiveValues = numberOfPassiveArguments[curStatementPos];
143
145 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Forward>(
146 tape, true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
148 adjointVector
149#else
150 &vectorAccess
151#endif
152 );
153 } else if (Config::StatementInputTag == nPassiveValues) CODI_Unlikely {
154 // Do nothing.
155 } else CODI_Likely {
156 Gradient lhsTangent = Gradient();
157
158 primalVector[curAdjointPos] = StatementEvaluator::template callForward<PrimalValueLinearTape>(
159 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsTangent, nPassiveValues,
160 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
161
162#if CODI_VariableAdjointInterfaceInPrimalTapes
163 adjointVector->setLhsTangent(curAdjointPos);
165 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
166#else
167 adjointVector[curAdjointPos] = lhsTangent;
168
170 tape, curAdjointPos, GradientTraits::dim<Gradient>(), GradientTraits::toArray(lhsTangent).data());
171#endif
173 primalVector[curAdjointPos]);
174 }
175
176 curStatementPos += 1;
177 }
178 }
179
182 /* data from call */
183 PrimalValueLinearTape& tape, Real* primalVector,
184 /* data from low level function byte data vector */
185 size_t& curLLFByteDataPos, size_t const& endLLFByteDataPos, char* dataPtr,
186 /* data from low level function info data vector */
187 size_t& curLLFInfoDataPos, size_t const& endLLFInfoDataPos, Config::LowLevelFunctionToken* const tokenPtr,
188 Config::LowLevelFunctionDataSize* const dataSizePtr,
189 /* data from constantValueData */
190 size_t& curConstantPos, size_t const& endConstantPos, PassiveReal const* const constantValues,
191 /* data from passiveValueData */
192 size_t& curPassivePos, size_t const& endPassivePos, Real const* const passiveValues,
193 /* data from rhsIdentifiersData */
194 size_t& curRhsIdentifiersPos, size_t const& endRhsIdentifiersPos, Identifier const* const rhsIdentifiers,
195 /* data from statementData */
196 size_t& curStatementPos, size_t const& endStatementPos,
197 Config::ArgumentSize const* const numberOfPassiveArguments, EvalHandle const* const stmtEvalhandle,
198 /* data from index handler */
199 size_t const& startAdjointPos, size_t const& endAdjointPos) {
200 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
201 endStatementPos);
202
203 size_t curAdjointPos = startAdjointPos;
204
205 typename Base::template VectorAccess<Gradient> vectorAccess(nullptr, primalVector);
206
207 while (curAdjointPos < endAdjointPos) CODI_Likely {
208 curAdjointPos += 1;
209
210 Config::ArgumentSize nPassiveValues = numberOfPassiveArguments[curStatementPos];
211
213 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Primal>(
214 tape, true, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr, &vectorAccess);
215 } else if (Config::StatementInputTag == nPassiveValues) CODI_Unlikely {
216 // Do nothing.
217 } else CODI_Likely {
218 primalVector[curAdjointPos] = StatementEvaluator::template callPrimal<PrimalValueLinearTape>(
219 stmtEvalhandle[curStatementPos], primalVector, nPassiveValues, curConstantPos, constantValues,
220 curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
221
223 primalVector[curAdjointPos]);
224 }
225
226 curStatementPos += 1;
227 }
228 }
229
232 /* data from call */
233 PrimalValueLinearTape& tape, Real* primalVector, ADJOINT_VECTOR_TYPE* adjointVector,
234 /* data from low level function byte data vector */
235 size_t& curLLFByteDataPos, size_t const& endLLFByteDataPos, char* dataPtr,
236 /* data from low level function info data vector */
237 size_t& curLLFInfoDataPos, size_t const& endLLFInfoDataPos, Config::LowLevelFunctionToken* const tokenPtr,
238 Config::LowLevelFunctionDataSize* const dataSizePtr,
239 /* data from constantValueData */
240 size_t& curConstantPos, size_t const& endConstantPos, PassiveReal const* const constantValues,
241 /* data from passiveValueData */
242 size_t& curPassivePos, size_t const& endPassivePos, Real const* const passiveValues,
243 /* data from rhsIdentifiersData */
244 size_t& curRhsIdentifiersPos, size_t const& endRhsIdentifiersPos, Identifier const* const rhsIdentifiers,
245 /* data from statementData */
246 size_t& curStatementPos, size_t const& endStatementPos,
247 Config::ArgumentSize const* const numberOfPassiveArguments, EvalHandle const* const stmtEvalhandle,
248 /* data from index handler */
249 size_t const& startAdjointPos, size_t const& endAdjointPos) {
250 CODI_UNUSED(endLLFByteDataPos, endLLFInfoDataPos, endConstantPos, endPassivePos, endRhsIdentifiersPos,
251 endStatementPos);
252
253 size_t curAdjointPos = startAdjointPos;
254
255#if !CODI_VariableAdjointInterfaceInPrimalTapes
256 typename Base::template VectorAccess<Gradient> vectorAccess(adjointVector, primalVector);
257#endif
258
259 while (curAdjointPos > endAdjointPos) CODI_Likely {
260 curStatementPos -= 1;
261
262 Config::ArgumentSize nPassiveValues = numberOfPassiveArguments[curStatementPos];
263
265 Base::template callLowLevelFunction<LowLevelFunctionEntryCallKind::Reverse>(
266 tape, false, curLLFByteDataPos, dataPtr, curLLFInfoDataPos, tokenPtr, dataSizePtr,
268 adjointVector
269#else
270 &vectorAccess
271#endif
272 );
273 } else if (Config::StatementInputTag == nPassiveValues) CODI_Unlikely {
274 // Do nothing.
275 } else CODI_Likely {
276#if CODI_VariableAdjointInterfaceInPrimalTapes
277
279 tape, curAdjointPos, adjointVector->getVectorSize(), adjointVector->getAdjointVec(curAdjointPos));
280
281 Gradient const lhsAdjoint{};
282 adjointVector->setLhsAdjoint(curAdjointPos);
283#else
284 Gradient const lhsAdjoint = adjointVector[curAdjointPos];
285
287 tape, curAdjointPos, GradientTraits::dim<Gradient>(), GradientTraits::toArray(lhsAdjoint).data());
288
290 adjointVector[curAdjointPos] = Gradient();
291 }
292#endif
294 primalVector[curAdjointPos]);
295
296 StatementEvaluator::template callReverse<PrimalValueLinearTape>(
297 stmtEvalhandle[curStatementPos], primalVector, adjointVector, lhsAdjoint, nPassiveValues,
298 curConstantPos, constantValues, curPassivePos, passiveValues, curRhsIdentifiersPos, rhsIdentifiers);
299 }
300
301 curAdjointPos -= 1;
302 }
303 }
304
308 CODI_UNUSED(pos);
309
310 // Nothing to do.
311 }
312
315 CODI_INLINE void pushStmtData(Identifier const& index, Config::ArgumentSize const& numberOfPassiveArguments,
316 Real const& oldPrimalValue, EvalHandle evalHandle) {
317 CODI_UNUSED(index, oldPrimalValue);
318
319 Base::statementData.pushData(numberOfPassiveArguments, evalHandle);
320 }
321
322 public:
325 void revertPrimals(Position const& pos) {
326 CODI_UNUSED(pos);
327
328 // Primal values do not need to be reset.
329 }
330 };
331}
#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