CoDiPack  2.3.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-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 "../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>,
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;
72
74
76
77 private:
78 bool active;
79
80 Real tempPrimal;
81 Gradient tempGradient;
82
83 std::set<TapeParameters> parameters;
84
85 public:
86
88 TagTapeReverse() : Base(), active(), tempPrimal(), tempGradient(), parameters() {}
89
90 /*******************************************************************************/
93
95 template<typename Adjoint>
96 void evaluate(Position const& start, Position const& end, Adjoint* data) {
97 CODI_UNUSED(start, end, data);
98 }
99
101 template<typename Adjoint>
102 void evaluateForward(Position const& start, Position const& end, Adjoint* data) {
103 CODI_UNUSED(start, end, data);
104 }
105
107 /*******************************************************************************/
110
112 void writeToFile(std::string const& filename) const {
113 CODI_UNUSED(filename);
114 }
115
117 void readFromFile(std::string const& filename) {
118 CODI_UNUSED(filename);
119 }
120
122 void deleteData() {}
123
125 std::set<TapeParameters> const& getAvailableParameters() const {
126 return parameters;
127 }
128
130 size_t getParameter(TapeParameters parameter) const {
131 CODI_UNUSED(parameter);
132
133 return 0;
134 }
135
137 bool hasParameter(TapeParameters parameter) const {
138 CODI_UNUSED(parameter);
139
140 return false;
141 }
142
144 void setParameter(TapeParameters parameter, size_t value) {
145 CODI_UNUSED(parameter, value);
146 }
147
152
154 template<typename Adjoint>
156 CODI_UNUSED(data);
157 return nullptr;
158 }
159
162 delete access;
163 }
164
166 void swap(TagTapeReverse& other) {
167 std::swap(active, other.active);
168 std::swap(parameters, other.parameters);
169 Base::swap(other);
170 }
171 void resetHard() {}
172
177
179 /*******************************************************************************/
182
184 template<typename Lhs>
190
193 CODI_UNUSED(extFunc);
194 }
195
197 /*******************************************************************************/
200
202 void evaluateForward(Position const& start, Position const& end) {
203 CODI_UNUSED(start, end);
204 }
205
208
210 /*******************************************************************************/
213
215 void setGradient(Identifier const& identifier, Gradient const& gradient,
217 CODI_UNUSED(gradient, adjointsManagement);
218
219 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
220 }
221
223 Gradient const& getGradient(Identifier const& identifier,
224 AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
225 CODI_UNUSED(adjointsManagement);
226
227 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
228
229 return tempGradient;
230 }
231
233 Gradient& gradient(Identifier const& identifier,
235 CODI_UNUSED(adjointsManagement);
236
237 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
238
239 return tempGradient;
240 }
241
243 Gradient const& gradient(Identifier const& identifier,
244 AdjointsManagement adjointsManagement = AdjointsManagement::Automatic) const {
245 CODI_UNUSED(adjointsManagement);
246
247 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
248
249 return tempGradient;
250 }
251
253 /*******************************************************************************/
256
258 static bool constexpr LinearIndexHandling = true;
259
264
269
271 bool isIdentifierActive(Identifier const& index) const {
272 Base::verifyTag(index.tag);
273
274 return index.tag != Base::PassiveTag;
275 }
276
278 template<typename Lhs>
282
284 /*******************************************************************************/
287
289 static bool constexpr AllowJacobianOptimization = false;
290
292 template<typename Real>
293 void initIdentifier(Real& value, Identifier& identifier) {
294 CODI_UNUSED(value);
295 identifier = Identifier();
296 }
297
299 template<typename Real>
300 void destroyIdentifier(Real& value, Identifier& identifier) {
301 CODI_UNUSED(value, identifier);
302 }
303
305 template<typename Lhs, typename Rhs>
308 typename Base::ValidateTags validate;
310
311 validate.eval(rhs, vi, *this);
312
313 Base::checkLhsError(lhs, rhs.cast().getValue());
314
316
317 if (vi.isActive) {
318 Base::setTag(lhs.cast().getIdentifier().tag);
319 } else {
320 Base::resetTag(lhs.cast().getIdentifier().tag);
321 }
322 lhs.cast().value() = rhs.cast().getValue();
323 }
324
326 template<typename Lhs, typename Rhs>
331
333 template<typename Lhs>
335 Base::checkLhsError(lhs, rhs);
336
337 Base::resetTag(lhs.cast().getIdentifier().tag);
338
339 lhs.cast().value() = rhs;
340 }
341
343 /*******************************************************************************/
346
348 void pushJacobiManual(Real const& jacobian, Real const& value, Identifier const& index) {
349 CODI_UNUSED(jacobian, value, index);
350 }
351
353 void storeManual(Real const& lhsValue, Identifier& lhsIndex, Config::ArgumentSize const& size) {
354 CODI_UNUSED(lhsValue, size);
355
356 Base::checkLhsError(lhsValue, lhsIndex, lhsValue);
357 setTag(lhsIndex.tag);
358 }
359
361 /*******************************************************************************/
364
366 void evaluate(Position const& start, Position const& end) {
367 CODI_UNUSED(start, end);
368 }
369
371 void clearAdjoints(Position const& start, Position const& end) {
372 CODI_UNUSED(start, end);
373 }
374
377 return Position();
378 }
379
382 return Position();
383 }
384
386 void resetTo(Position const& pos, bool resetAdjoints = true) {
387 CODI_UNUSED(pos, resetAdjoints);
388 }
389
391 /*******************************************************************************/
394
396 void evaluateKeepState(Position const& start, Position const& end) {
397 CODI_UNUSED(start, end);
398 }
400 void evaluateForwardKeepState(Position const& start, Position const& end) {
401 CODI_UNUSED(start, end);
402 }
403
405 /*******************************************************************************/
408
409 static bool constexpr HasPrimalValues = false;
410 static bool constexpr RequiresPrimalRestore = false;
411
413 void evaluatePrimal(Position const& start, Position const& end) {
414 CODI_UNUSED(start, end);
415 }
416
419
421 void setPrimal(Identifier const& identifier, Real const& gradient) {
422 CODI_UNUSED(identifier, gradient);
423 }
424
426 Real const& getPrimal(Identifier const& identifier) const {
427 CODI_UNUSED(identifier);
428 return tempPrimal;
429 }
430
432 Real& primal(Identifier const& identifier) {
433 CODI_UNUSED(identifier);
434 return tempPrimal;
435 }
436
438 Real const& primal(Identifier const& identifier) const {
439 CODI_UNUSED(identifier);
440 return tempPrimal;
441 }
442
444 void revertPrimals(Position const& pos) {
445 CODI_UNUSED(pos);
446 }
447
449 /*******************************************************************************/
452
454 template<typename Lhs>
456 Base::setTag(value.cast().getIdentifier().tag);
457 Base::verifyRegisterValue(value, value.cast().getIdentifier()); // verification is mainly for the properties
458 }
459
461 template<typename Lhs>
465
467 void setActive() {
468 active = true;
469 }
470
472 void setPassive() {
473 active = false;
474 }
475
477 bool isActive() const {
478 return active;
479 }
480
482 bool isActive(Identifier const& identifier) const {
483 return identifier.tag != Base::PassiveTag;
484 }
485
486 void evaluate() {}
487
490
492 void reset(bool resetAdjoints = true) {
493 CODI_UNUSED(resetAdjoints);
494 }
495
497 template<typename Stream = std::ostream>
498 void printStatistics(Stream& out = std::cout) const {
499 CODI_UNUSED(out);
500 }
501
503 template<typename Stream = std::ostream>
504 void printTableHeader(Stream& out = std::cout) const {
505 CODI_UNUSED(out);
506 }
507
509 template<typename Stream = std::ostream>
510 void printTableRow(Stream& out = std::cout) const {
511 CODI_UNUSED(out);
512 }
513
516 return TapeValues("TagTapeReverse");
517 }
518
520 };
521}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
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: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
TapeParameters
Configuration options for a tape.
Definition tapeParameters.hpp:52
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.
Empty Position with no nested data.
Definition position.hpp:47
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:59
Impl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:75
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:102
Full tape interface that supports all features of CoDiPack.
Definition fullTapeInterface.hpp:84
Base class for all CoDiPack lvalue expression.
Definition lhsExpressionInterface.hpp:63
Identifier const & getIdentifier() const
Impl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
EnumBitset< TagFlags > properties
Current properties of the value.
Definition tagData.hpp:62
Tag tag
Current tag of the value.
Definition tagData.hpp:61
Looks at the tags for the expression.
Definition tagTapeBase.hpp:117
Base implementation for tagging tapes.
Definition tagTapeBase.hpp:73
void verifyRegisterValue(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, const Identifier &tag)
Verify tag, properties and lhs error.
Definition tagTapeBase.hpp:319
void resetTag(Tag &tag) const
Reset tag on value.
Definition tagTapeBase.hpp:336
void handleError(ValidationIndicator< Real, Tag > &vi) const
Call tag error callback.
Definition tagTapeBase.hpp:306
void setTag(Tag &tag) const
Set tag on value.
Definition tagTapeBase.hpp:331
static Tag constexpr PassiveTag
Tag indicating an inactive value.
Definition tagTapeBase.hpp:88
void verifyTag(ValidationIndicator< Real, Tag > &vi, Tag const &tag) const
Checks if the tag is correct. Errors are set on the ValidationIndicator object.
Definition tagTapeBase.hpp:226
void verifyTagAndProperties(Tag const &tag, Real const &value, const EnumBitset< TagFlags > &properties) const
Checks if the tag and the properties are correct.
Definition tagTapeBase.hpp:256
static Tag constexpr InvalidTag
Tag indicating an invalid value.
Definition tagTapeBase.hpp:89
void checkLhsError(Real &lhsValue, Identifier &lhsIdentifier, const Real &rhs) const
Check if a property for the lhs value is triggered.
Definition tagTapeBase.hpp:289
void swap(Impl &other)
Swap members.
Definition tagTapeBase.hpp:130
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
Tape for tagging variables and find errors in the AD workflow.
Definition tagTapeReverse.hpp:56
TagTapeReverse()
Constructor.
Definition tagTapeReverse.hpp:88
void resetHard()
Do nothing.
Definition tagTapeReverse.hpp:171
void deleteAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:173
void destroyIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition tagTapeReverse.hpp:300
void clearAdjoints(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:371
bool hasParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:137
void evaluate()
Do nothing.
Definition tagTapeReverse.hpp:486
Real const & primal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:438
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
Do nothing.
Definition tagTapeReverse.hpp:161
void printTableRow(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:510
void resizeAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:174
void deleteData()
Do nothing.
Definition tagTapeReverse.hpp:122
void setPassive()
Set tape to passive.
Definition tagTapeReverse.hpp:472
Identifier getInvalidIndex() const
-1 tag.
Definition tagTapeReverse.hpp:266
void evaluateForward(Position const &start, Position const &end, Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:102
Real & primal(Identifier const &identifier)
Do nothing.
Definition tagTapeReverse.hpp:432
TapeValues getTapeValues() const
Do nothing.
Definition tagTapeReverse.hpp:515
void setParameter(TapeParameters parameter, size_t value)
Do nothing.
Definition tagTapeReverse.hpp:144
Gradient const & getGradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Verify tag.
Definition tagTapeReverse.hpp:223
void initIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition tagTapeReverse.hpp:293
VectorAccessInterface< Real, Identifier > * createVectorAccessCustomAdjoints(Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:155
bool isActive() const
Check if tape is active.
Definition tagTapeReverse.hpp:477
Gradient & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Verify tag.
Definition tagTapeReverse.hpp:233
TagData< Tag > Identifier
See TapeTypesInterface.
Definition tagTapeReverse.hpp:70
void setActive()
Set tape to active.
Definition tagTapeReverse.hpp:467
void endUseAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:176
Position getPosition() const
Do nothing.
Definition tagTapeReverse.hpp:376
void clearAdjoints()
Do nothing.
Definition tagTapeReverse.hpp:489
size_t getParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:130
Identifier getPassiveIndex() const
Zero tag.
Definition tagTapeReverse.hpp:261
static bool constexpr LinearIndexHandling
Behave as linear index handler.
Definition tagTapeReverse.hpp:258
void revertPrimals(Position const &pos)
Do nothing.
Definition tagTapeReverse.hpp:444
void pushExternalFunction(ExternalFunction< TagTapeReverse > const &extFunc)
Do nothing.
Definition tagTapeReverse.hpp:192
void evaluatePrimal()
Do nothing.
Definition tagTapeReverse.hpp:418
void store(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &lhs, ExpressionInterface< Real, Rhs > const &rhs)
Verify all tags of the rhs and the lhs properties.
Definition tagTapeReverse.hpp:306
T_Real Real
See TagTapeReverse.
Definition tagTapeReverse.hpp:57
void registerOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify tag.
Definition tagTapeReverse.hpp:462
void setPrimal(Identifier const &identifier, Real const &gradient)
Do nothing.
Definition tagTapeReverse.hpp:421
static bool constexpr HasPrimalValues
No primal values.
Definition tagTapeReverse.hpp:409
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verifies tag properties.
Definition tagTapeReverse.hpp:185
void evaluateForward()
Do nothing.
Definition tagTapeReverse.hpp:207
static bool constexpr AllowJacobianOptimization
Do not allow Jacobian optimization.
Definition tagTapeReverse.hpp:289
void storeManual(Real const &lhsValue, Identifier &lhsIndex, Config::ArgumentSize const &size)
Set tag on lhs.
Definition tagTapeReverse.hpp:353
void reset(bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:492
Real const & getPrimal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:426
Position getZeroPosition() const
Do nothing.
Definition tagTapeReverse.hpp:381
void beginUseAdjointVector()
Do nothing.
Definition tagTapeReverse.hpp:175
void swap(TagTapeReverse &other)
Swap members.
Definition tagTapeReverse.hpp:166
std::set< TapeParameters > const & getAvailableParameters() const
Empty set.
Definition tagTapeReverse.hpp:125
void evaluateKeepState(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:396
Gradient const & gradient(Identifier const &identifier, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic) const
Verify tag.
Definition tagTapeReverse.hpp:243
void evaluate(Position const &start, Position const &end, Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:96
void writeToFile(std::string const &filename) const
Do nothing.
Definition tagTapeReverse.hpp:112
void evaluate(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:366
T_Tag Tag
See TagTapeReverse.
Definition tagTapeReverse.hpp:58
void store(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &lhs, PassiveReal const &rhs)
Verify the lhs properties.
Definition tagTapeReverse.hpp:334
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition tagTapeReverse.hpp:73
void setGradient(Identifier const &identifier, Gradient const &gradient, AdjointsManagement adjointsManagement=AdjointsManagement::Automatic)
Verify tag.
Definition tagTapeReverse.hpp:215
void deactivateValue(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Set tag to passive.
Definition tagTapeReverse.hpp:279
void evaluateForward(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:202
VectorAccessInterface< Real, Identifier > * createVectorAccess()
Do nothing.
Definition tagTapeReverse.hpp:149
void resetTo(Position const &pos, bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:386
void evaluateForwardKeepState(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:400
void store(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &lhs, LhsExpressionInterface< Real, Gradient, TagTapeReverse, Rhs > const &rhs)
Verify all tags of the rhs and the lhs properties.
Definition tagTapeReverse.hpp:327
void evaluatePrimal(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:413
void registerInput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify value properties.
Definition tagTapeReverse.hpp:455
void printStatistics(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:498
bool isActive(Identifier const &identifier) const
Default check.
Definition tagTapeReverse.hpp:482
bool isIdentifierActive(Identifier const &index) const
Verify tag.
Definition tagTapeReverse.hpp:271
void pushJacobiManual(Real const &jacobian, Real const &value, Identifier const &index)
Do nothing.
Definition tagTapeReverse.hpp:348
Real Gradient
See TapeTypesInterface.
Definition tagTapeReverse.hpp:69
static bool constexpr RequiresPrimalRestore
No primal values.
Definition tagTapeReverse.hpp:410
EmptyPosition Position
See TapeTypesInterface.
Definition tagTapeReverse.hpp:71
void readFromFile(std::string const &filename)
Do nothing.
Definition tagTapeReverse.hpp:117
void printTableHeader(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:504
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void eval(NodeInterface< Node > const &node, Args &&... args)
Start the evaluation of the logic on the given expression.
Definition traversalLogic.hpp:70
Helper class for statement validation.
Definition tagTapeBase.hpp:49
bool isActive
true if an active rhs is detected. tag != 0
Definition tagTapeBase.hpp:50
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91