CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
tagTapeReverse.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 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 "../interfaces/fullTapeInterface.hpp"
38#include "../misc/adjointVectorAccess.hpp"
39#include "tagTapeBase.hpp"
40
42namespace codi {
43
54 template<typename T_Real, typename T_Tag>
55 struct TagTapeReverse : public FullTapeInterface<T_Real, T_Real, TagData<T_Tag>, EmptyPosition, TagData<T_Tag>>,
56 public TagTapeBase<T_Real, T_Tag, T_Real, TagTapeReverse<T_Real, T_Tag>> {
57 using Real = CODI_DD(T_Real, double);
58 using Tag = CODI_DD(T_Tag, int);
59
61 struct TapeTypes {
63 struct IndexManager {
65 using Index = int;
66 };
67 };
68
69 using Gradient = Real;
70 using Identifier = int;
73
75
77
78 private:
79 bool active;
80
81 Real tempPrimal;
82 Gradient tempGradient;
83
84 std::set<TapeParameters> parameters;
85
86 public:
87
89 TagTapeReverse() : Base(), active(), tempPrimal(), tempGradient(), parameters() {}
90
95 using Base::store;
96
97 /*******************************************************************************/
100
102 template<typename Adjoint>
103 void evaluate(Position const& start, Position const& end, Adjoint* data) {
104 CODI_UNUSED(start, end, data);
105 }
106
108 template<typename Adjoint>
109 void evaluateForward(Position const& start, Position const& end, Adjoint* data) {
110 CODI_UNUSED(start, end, data);
111 }
112
114 /*******************************************************************************/
117
119 void writeToFile(std::string const& filename) const {
120 CODI_UNUSED(filename);
121 }
122
124 void readFromFile(std::string const& filename) {
125 CODI_UNUSED(filename);
126 }
127
129 void deleteData() {}
130
132 std::set<TapeParameters> const& getAvailableParameters() const {
133 return parameters;
134 }
135
137 size_t getParameter(TapeParameters parameter) const {
138 CODI_UNUSED(parameter);
139
140 return 0;
141 }
142
144 bool hasParameter(TapeParameters parameter) const {
145 CODI_UNUSED(parameter);
146
147 return false;
148 }
149
151 void setParameter(TapeParameters parameter, size_t value) {
152 CODI_UNUSED(parameter, value);
153 }
154
159
161 template<typename Adjoint>
163 CODI_UNUSED(data);
164 return nullptr;
165 }
166
169 delete access;
170 }
171
173 void swap(TagTapeReverse& other) {
174 std::swap(active, other.active);
175 std::swap(parameters, other.parameters);
176 Base::swap(other);
177 }
178 void resetHard() {}
179
184
186 /*******************************************************************************/
189
191 template<typename Lhs>
197
200 CODI_UNUSED(extFunc);
201 }
202
204 /*******************************************************************************/
207
209 void evaluateForward(Position const& start, Position const& end) {
210 CODI_UNUSED(start, end);
211 }
212
215
217 /*******************************************************************************/
220
222 void setGradient(Identifier const& identifier, Gradient const& gradient,
224 CODI_UNUSED(identifier, gradient, adjointsManagement);
225 }
226
228 Gradient const& getGradient(Identifier const& identifier,
229 AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
230 CODI_UNUSED(identifier, adjointsManagement);
231
232 return tempGradient;
233 }
234
236 Gradient& gradient(Identifier const& identifier,
238 CODI_UNUSED(identifier, adjointsManagement);
239
240 return tempGradient;
241 }
242
244 Gradient const& gradient(Identifier const& identifier,
245 AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
246 CODI_UNUSED(identifier, adjointsManagement);
247
248 return tempGradient;
249 }
250
252 /*******************************************************************************/
255
257 static bool constexpr LinearIndexHandling = true;
258
263
268
270 bool isIdentifierActive(Identifier const& index) const {
271 return index != Base::PassiveTag;
272 }
273
275 template<typename Lhs>
279
281 /*******************************************************************************/
284
286 void pushJacobiManual(Real const& jacobian, Real const& value, ActiveTypeTapeData const& index) {
287 CODI_UNUSED(jacobian, value, index);
288 }
289
291 void storeManual(Real const& lhsValue, ActiveTypeTapeData& lhsIndex, Config::ArgumentSize const& size) {
292 CODI_UNUSED(lhsValue, size);
293
294 Base::checkLhsError(lhsValue, lhsIndex, lhsValue);
295 setTag(lhsIndex.tag);
296 }
297
299 /*******************************************************************************/
302
304 void evaluate(Position const& start, Position const& end) {
305 CODI_UNUSED(start, end);
306 }
307
309 void clearAdjoints(Position const& start, Position const& end) {
310 CODI_UNUSED(start, end);
311 }
312
315 return Position();
316 }
317
320 return Position();
321 }
322
324 void resetTo(Position const& pos, bool resetAdjoints = true) {
325 CODI_UNUSED(pos, resetAdjoints);
326 }
327
329 /*******************************************************************************/
332
334 void evaluateKeepState(Position const& start, Position const& end) {
335 CODI_UNUSED(start, end);
336 }
337
338 void evaluateForwardKeepState(Position const& start, Position const& end) {
339 CODI_UNUSED(start, end);
340 }
341
343 /*******************************************************************************/
346
347 static bool constexpr HasPrimalValues = false;
348 static bool constexpr RequiresPrimalRestore = false;
349
351 void evaluatePrimal(Position const& start, Position const& end) {
352 CODI_UNUSED(start, end);
353 }
354
357
359 void setPrimal(Identifier const& identifier, Real const& gradient) {
360 CODI_UNUSED(identifier, gradient);
361 }
362
364 Real const& getPrimal(Identifier const& identifier) const {
365 CODI_UNUSED(identifier);
366 return tempPrimal;
367 }
368
370 Real& primal(Identifier const& identifier) {
371 CODI_UNUSED(identifier);
372 return tempPrimal;
373 }
374
376 Real const& primal(Identifier const& identifier) const {
377 CODI_UNUSED(identifier);
378 return tempPrimal;
379 }
380
382 void revertPrimals(Position const& pos) {
383 CODI_UNUSED(pos);
384 }
385
387 /*******************************************************************************/
390
392 template<typename Lhs>
394 Base::setTag(value.cast().getTapeData().tag);
395 Base::verifyRegisterValue(value, value.cast().getTapeData()); // verification is mainly for the properties
396 }
397
399 template<typename Lhs>
403
405 void setActive() {
406 active = true;
407 }
408
410 void setPassive() {
411 active = false;
412 }
413
415 bool isActive() const {
416 return active;
417 }
418
420 bool isActive(Identifier const& identifier) const {
421 return identifier != getPassiveIndex();
422 }
423
424 void evaluate() {}
425
428
430 void reset(bool resetAdjoints = true) {
431 CODI_UNUSED(resetAdjoints);
432 }
433
435 template<typename Stream = std::ostream>
436 void printStatistics(Stream& out = std::cout) const {
437 CODI_UNUSED(out);
438 }
439
441 template<typename Stream = std::ostream>
442 void printTableHeader(Stream& out = std::cout) const {
443 CODI_UNUSED(out);
444 }
445
447 template<typename Stream = std::ostream>
448 void printTableRow(Stream& out = std::cout) const {
449 CODI_UNUSED(out);
450 }
451
454 return TapeValues("TagTapeReverse");
455 }
456
458 };
459}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
uint8_t ArgumentSize
Type for the number of arguments in statements.
Definition config.h:117
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
TapeParameters
Configuration options for a tape.
Definition tapeParameters.hpp:52
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
AdjointsManagement
Policies for management of the tape's interal adjoints.
Definition tapeParameters.hpp:100
@ Automatic
Manage internal adjoints automatically, including locking, bounds checking, and resizing.
Definition tapeParameters.hpp:102
Empty Position with no nested data.
Definition position.hpp:47
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:118
Full tape interface that supports all features of CoDiPack.
Definition fullTapeInterface.hpp:89
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:87
static Index constexpr InvalidIndex
Default invalid index for all index mangers (max value for unsigned types).
Definition indexManagerInterface.hpp:88
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
TapeData const & getTapeData() const
Get the data of the tape stored in this lhs expression.
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:103
Data for a tag.
Definition tagData.hpp:56
Tag tag
Current tag of the value.
Definition tagData.hpp:61
inlinevoid setTag(Tag &tag) const
Definition tagTapeBase.hpp:452
static bool constexpr AllowJacobianOptimization
Definition tagTapeBase.hpp:169
void destroyTapeData(Real &value, ActiveTypeTapeData &data)
Definition tagTapeBase.hpp:180
inlinevoid checkLhsError(Real &lhsValue, ActiveTypeTapeData &lhsData, const Real &rhs) const
Definition tagTapeBase.hpp:410
inlineIdentifier const & getIdentifier(ActiveTypeTapeData const &data)
Definition tagTapeBase.hpp:150
static Tag constexpr PassiveTag
Definition tagTapeBase.hpp:89
void initTapeData(Real &value, ActiveTypeTapeData &data)
Definition tagTapeBase.hpp:173
inlinevoid verifyRegisterValue(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, ActiveTypeTapeData const &tag)
Definition tagTapeBase.hpp:440
void swap(Impl &other)
Definition tagTapeBase.hpp:135
Required definition for event system.
Definition tagTapeReverse.hpp:63
int Index
Required definition for event system.
Definition tagTapeReverse.hpp:65
Required definition for event system.
Definition tagTapeReverse.hpp:61
TagTapeReverse()
Constructor.
Definition tagTapeReverse.hpp:89
void resetHard()
Do nothing.
Definition tagTapeReverse.hpp:178
void deleteAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:180
void clearAdjoints(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:309
bool hasParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:144
void evaluate()
Do nothing.
Definition tagTapeReverse.hpp:424
Real const & primal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:376
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
Do nothing.
Definition tagTapeReverse.hpp:168
void printTableRow(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:448
void resizeAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:181
void deleteData()
Do nothing.
Definition tagTapeReverse.hpp:129
void setPassive()
Set tape to passive.
Definition tagTapeReverse.hpp:410
Identifier getInvalidIndex() const
-1 id.
Definition tagTapeReverse.hpp:265
void evaluateForward(Position const &start, Position const &end, Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:109
Real & primal(Identifier const &identifier)
Do nothing.
Definition tagTapeReverse.hpp:370
TapeValues getTapeValues() const
Do nothing.
Definition tagTapeReverse.hpp:453
void setParameter(TapeParameters parameter, size_t value)
Do nothing.
Definition tagTapeReverse.hpp:151
TagData< Tag > ActiveTypeTapeData
See TapeTypesInterface.
Definition tagTapeReverse.hpp:71
int Identifier
See TapeTypesInterface.
Definition tagTapeReverse.hpp:70
Gradient const & getGradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Verify tag.
Definition tagTapeReverse.hpp:228
VectorAccessInterface< Real, Identifier > * createVectorAccessCustomAdjoints(Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:162
bool isActive() const
Check if tape is active.
Definition tagTapeReverse.hpp:415
Gradient & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Verify tag.
Definition tagTapeReverse.hpp:236
void setActive()
Set tape to active.
Definition tagTapeReverse.hpp:405
void endUseAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:183
Position getPosition() const
Do nothing.
Definition tagTapeReverse.hpp:314
void clearAdjoints()
Do nothing.
Definition tagTapeReverse.hpp:427
void pushJacobiManual(Real const &jacobian, Real const &value, ActiveTypeTapeData const &index)
Do nothing.
Definition tagTapeReverse.hpp:286
size_t getParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:137
Identifier getPassiveIndex() const
Zero id.
Definition tagTapeReverse.hpp:260
static bool constexpr LinearIndexHandling
Behave as linear index handler.
Definition tagTapeReverse.hpp:257
void revertPrimals(Position const &pos)
Do nothing.
Definition tagTapeReverse.hpp:382
void pushExternalFunction(ExternalFunction< TagTapeReverse > const &extFunc)
Do nothing.
Definition tagTapeReverse.hpp:199
void evaluatePrimal()
Do nothing.
Definition tagTapeReverse.hpp:356
T_Real Real
See TagTapeReverse.
Definition tagTapeReverse.hpp:57
void registerOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify tag.
Definition tagTapeReverse.hpp:400
void setPrimal(Identifier const &identifier, Real const &gradient)
Do nothing.
Definition tagTapeReverse.hpp:359
static bool constexpr HasPrimalValues
No primal values.
Definition tagTapeReverse.hpp:347
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verifies tag properties.
Definition tagTapeReverse.hpp:192
void evaluateForward()
Do nothing.
Definition tagTapeReverse.hpp:214
void reset(bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:430
Real const & getPrimal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:364
Position getZeroPosition() const
Do nothing.
Definition tagTapeReverse.hpp:319
void beginUseAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:182
void storeManual(Real const &lhsValue, ActiveTypeTapeData &lhsIndex, Config::ArgumentSize const &size)
Set tag on lhs.
Definition tagTapeReverse.hpp:291
void swap(TagTapeReverse &other)
Swap members.
Definition tagTapeReverse.hpp:173
std::set< TapeParameters > const & getAvailableParameters() const
Empty set.
Definition tagTapeReverse.hpp:132
void evaluateKeepState(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:334
Gradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Verify tag.
Definition tagTapeReverse.hpp:244
void evaluate(Position const &start, Position const &end, Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:103
void writeToFile(std::string const &filename) const
Do nothing.
Definition tagTapeReverse.hpp:119
void evaluate(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:304
T_Tag Tag
See TagTapeReverse.
Definition tagTapeReverse.hpp:58
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition tagTapeReverse.hpp:74
void setGradient(Identifier const &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Verify tag.
Definition tagTapeReverse.hpp:222
void deactivateValue(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Set tag to passive.
Definition tagTapeReverse.hpp:276
void evaluateForward(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:209
VectorAccessInterface< Real, Identifier > * createVectorAccess()
Do nothing.
Definition tagTapeReverse.hpp:156
void resetTo(Position const &pos, bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:324
void evaluateForwardKeepState(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:338
void evaluatePrimal(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:351
void registerInput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify value properties.
Definition tagTapeReverse.hpp:393
void printStatistics(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:436
bool isActive(Identifier const &identifier) const
Default check.
Definition tagTapeReverse.hpp:420
bool isIdentifierActive(Identifier const &index) const
Verify tag.
Definition tagTapeReverse.hpp:270
TagTapeBase< T_Real, T_Tag, T_Real, TagTapeReverse > Base
Base class abbreviation.
Definition tagTapeReverse.hpp:76
Real Gradient
See TapeTypesInterface.
Definition tagTapeReverse.hpp:69
static bool constexpr RequiresPrimalRestore
No primal values.
Definition tagTapeReverse.hpp:348
EmptyPosition Position
See TapeTypesInterface.
Definition tagTapeReverse.hpp:72
void readFromFile(std::string const &filename)
Do nothing.
Definition tagTapeReverse.hpp:124
void printTableHeader(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:442
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94