CoDiPack  2.2.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://www.scicomp.uni-kl.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://www.scicomp.uni-kl.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 return 0;
132 }
133
135 bool hasParameter(TapeParameters parameter) const {
136 return false;
137 }
138
140 void setParameter(TapeParameters parameter, size_t value) {
141 CODI_UNUSED(parameter, value);
142 }
143
146 return nullptr;
147 }
148
150 template<typename Adjoint>
152 CODI_UNUSED(data);
153 return nullptr;
154 }
155
158 delete access;
159 }
160
162 void swap(TagTapeReverse& other) {
163 std::swap(active, other.active);
164 std::swap(parameters, other.parameters);
165 Base::swap(other);
166 }
167 void resetHard() {}
169
171 /*******************************************************************************/
174
176 template<typename Lhs>
178 registerInput(value);
179
180 return Real();
181 }
182
185 CODI_UNUSED(extFunc);
186 }
187
189 /*******************************************************************************/
192
194 void evaluateForward(Position const& start, Position const& end) {
195 CODI_UNUSED(start, end);
196 }
197
200
202 /*******************************************************************************/
205
207 void setGradient(Identifier const& identifier, Gradient const& gradient) {
209
210 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
211 }
212
214 Gradient const& getGradient(Identifier const& identifier) const {
215 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
216
217 return tempGradient;
218 }
219
221 Gradient& gradient(Identifier const& identifier) {
222 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
223
224 return tempGradient;
225 }
226
228 Gradient const& gradient(Identifier const& identifier) const {
229 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
230
231 return tempGradient;
232 }
233
235 /*******************************************************************************/
238
240 static bool constexpr LinearIndexHandling = true;
241
245 }
246
249 return Identifier(-1);
250 }
251
253 bool isIdentifierActive(Identifier const& index) const {
254 Base::verifyTag(index.tag);
255
256 return index.tag != Base::PassiveTag;
257 }
258
260 template<typename Lhs>
262 value.getIdentifier() = getPassiveIndex();
263 }
264
266 /*******************************************************************************/
269
271 static bool constexpr AllowJacobianOptimization = false;
272
274 template<typename Real>
275 void initIdentifier(Real& value, Identifier& identifier) {
276 CODI_UNUSED(value);
277 identifier = Identifier();
278 }
279
281 template<typename Real>
282 void destroyIdentifier(Real& value, Identifier& identifier) {
283 CODI_UNUSED(value, identifier);
284 }
285
287 template<typename Lhs, typename Rhs>
290 typename Base::ValidateTags validate;
292
293 validate.eval(rhs, vi, *this);
294
295 Base::checkLhsError(lhs, rhs.cast().getValue());
296
298
299 if (vi.isActive) {
300 Base::setTag(lhs.cast().getIdentifier().tag);
301 } else {
302 Base::resetTag(lhs.cast().getIdentifier().tag);
303 }
304 lhs.cast().value() = rhs.cast().getValue();
305 }
306
308 template<typename Lhs, typename Rhs>
311 store<Lhs, Rhs>(lhs, static_cast<ExpressionInterface<Real, Rhs> const&>(rhs));
312 }
313
315 template<typename Lhs>
317 Base::checkLhsError(lhs, rhs);
318
319 Base::resetTag(lhs.cast().getIdentifier().tag);
320
321 lhs.cast().value() = rhs;
322 }
323
325 /*******************************************************************************/
328
330 void pushJacobiManual(Real const& jacobian, Real const& value, Identifier const& index) {
331 CODI_UNUSED(jacobian, value, index);
332 }
333
335 void storeManual(Real const& lhsValue, Identifier& lhsIndex, Config::ArgumentSize const& size) {
336 CODI_UNUSED(lhsValue, size);
337
338 Base::checkLhsError(lhsValue, lhsIndex, lhsValue);
339 setTag(lhsIndex.tag);
340 }
341
343 /*******************************************************************************/
346
348 void evaluate(Position const& start, Position const& end) {
349 CODI_UNUSED(start, end);
350 }
351
353 void clearAdjoints(Position const& start, Position const& end) {
354 CODI_UNUSED(start, end);
355 }
356
359 return Position();
360 }
361
364 return Position();
365 }
366
368 void resetTo(Position const& pos, bool resetAdjoints = true) {
369 CODI_UNUSED(pos, resetAdjoints);
370 }
371
373 /*******************************************************************************/
376
378 void evaluateKeepState(Position const& start, Position const& end) {
379 CODI_UNUSED(start, end);
380 }
382 void evaluateForwardKeepState(Position const& start, Position const& end) {
383 CODI_UNUSED(start, end);
384 }
385
387 /*******************************************************************************/
390
391 static bool constexpr HasPrimalValues = false;
392 static bool constexpr RequiresPrimalRestore = false;
393
395 void evaluatePrimal(Position const& start, Position const& end) {
396 CODI_UNUSED(start, end);
397 }
398
401
403 void setPrimal(Identifier const& identifier, Real const& gradient) {
404 CODI_UNUSED(identifier, gradient);
405 }
406
408 Real const& getPrimal(Identifier const& identifier) const {
409 CODI_UNUSED(identifier);
410 return tempPrimal;
411 }
412
414 Real& primal(Identifier const& identifier) {
415 CODI_UNUSED(identifier);
416 return tempPrimal;
417 }
418
420 Real const& primal(Identifier const& identifier) const {
421 CODI_UNUSED(identifier);
422 return tempPrimal;
423 }
424
426 void revertPrimals(Position const& pos) {
427 CODI_UNUSED(pos);
428 }
429
431 /*******************************************************************************/
434
436 template<typename Lhs>
438 Base::setTag(value.cast().getIdentifier().tag);
439 Base::verifyRegisterValue(value, value.cast().getIdentifier()); // verification is mainly for the properties
440 }
441
443 template<typename Lhs>
445 Base::verifyRegisterValue(value, value.cast().getIdentifier());
446 }
447
449 void setActive() {
450 active = true;
451 }
452
454 void setPassive() {
455 active = false;
456 }
457
459 bool isActive() const {
460 return active;
461 }
462
464 bool isActive(Identifier const& identifier) const {
465 return identifier.tag != Base::PassiveTag;
466 }
467
468 void evaluate() {}
469
472
474 void reset(bool resetAdjoints = true) {
475 CODI_UNUSED(resetAdjoints);
476 }
477
479 template<typename Stream = std::ostream>
480 void printStatistics(Stream& out = std::cout) const {
481 CODI_UNUSED(out);
482 }
483
485 template<typename Stream = std::ostream>
486 void printTableHeader(Stream& out = std::cout) const {
487 CODI_UNUSED(out);
488 }
489
491 template<typename Stream = std::ostream>
492 void printTableRow(Stream& out = std::cout) const {
493 CODI_UNUSED(out);
494 }
495
498 return TapeValues("TagTapeReverse");
499 }
500
502 };
503}
#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:90
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
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:82
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:116
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:318
void resetTag(Tag &tag) const
Reset tag on value.
Definition: tagTapeBase.hpp:335
void handleError(ValidationIndicator< Real, Tag > &vi) const
Call tag error callback.
Definition: tagTapeBase.hpp:305
void setTag(Tag &tag) const
Set tag on value.
Definition: tagTapeBase.hpp:330
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:225
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:255
void checkLhsError(Real &lhsValue, Identifier &lhsIdentifier, const Real &rhs) const
Check if a property for the lhs value is triggered.
Definition: tagTapeBase.hpp:288
void swap(Impl &other)
Swap members.
Definition: tagTapeBase.hpp:129
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:167
void deleteAdjointVector()
Do nothing.
Definition: tagTapeReverse.hpp:168
void destroyIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition: tagTapeReverse.hpp:282
void clearAdjoints(Position const &start, Position const &end)
Do nothing.
Definition: tagTapeReverse.hpp:353
bool hasParameter(TapeParameters parameter) const
Do nothing.
Definition: tagTapeReverse.hpp:135
void evaluate()
Do nothing.
Definition: tagTapeReverse.hpp:468
Gradient & gradient(Identifier const &identifier)
Verify tag.
Definition: tagTapeReverse.hpp:221
Real const & primal(Identifier const &identifier) const
Do nothing.
Definition: tagTapeReverse.hpp:420
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
Do nothing.
Definition: tagTapeReverse.hpp:157
void printTableRow(Stream &out=std::cout) const
Do nothing.
Definition: tagTapeReverse.hpp:492
void deleteData()
Do nothing.
Definition: tagTapeReverse.hpp:122
Gradient const & getGradient(Identifier const &identifier) const
Verify tag.
Definition: tagTapeReverse.hpp:214
void setPassive()
Set tape to passive.
Definition: tagTapeReverse.hpp:454
Identifier getInvalidIndex() const
-1 tag.
Definition: tagTapeReverse.hpp:248
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:414
TapeValues getTapeValues() const
Do nothing.
Definition: tagTapeReverse.hpp:497
void setParameter(TapeParameters parameter, size_t value)
Do nothing.
Definition: tagTapeReverse.hpp:140
void initIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition: tagTapeReverse.hpp:275
VectorAccessInterface< Real, Identifier > * createVectorAccessCustomAdjoints(Adjoint *data)
Do nothing.
Definition: tagTapeReverse.hpp:151
bool isActive() const
Check if tape is active.
Definition: tagTapeReverse.hpp:459
TagData< Tag > Identifier
See TapeTypesInterface.
Definition: tagTapeReverse.hpp:70
void setActive()
Set tape to active.
Definition: tagTapeReverse.hpp:449
void setGradient(Identifier const &identifier, Gradient const &gradient)
Verify tag.
Definition: tagTapeReverse.hpp:207
Position getPosition() const
Do nothing.
Definition: tagTapeReverse.hpp:358
void clearAdjoints()
Do nothing.
Definition: tagTapeReverse.hpp:471
Gradient const & gradient(Identifier const &identifier) const
Verify tag.
Definition: tagTapeReverse.hpp:228
size_t getParameter(TapeParameters parameter) const
Do nothing.
Definition: tagTapeReverse.hpp:130
Identifier getPassiveIndex() const
Zero tag.
Definition: tagTapeReverse.hpp:243
static bool constexpr LinearIndexHandling
Behave as linear index handler.
Definition: tagTapeReverse.hpp:240
void revertPrimals(Position const &pos)
Do nothing.
Definition: tagTapeReverse.hpp:426
void pushExternalFunction(ExternalFunction< TagTapeReverse > const &extFunc)
Do nothing.
Definition: tagTapeReverse.hpp:184
void evaluatePrimal()
Do nothing.
Definition: tagTapeReverse.hpp:400
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:288
void registerOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify tag.
Definition: tagTapeReverse.hpp:444
void setPrimal(Identifier const &identifier, Real const &gradient)
Do nothing.
Definition: tagTapeReverse.hpp:403
static bool constexpr HasPrimalValues
No primal values.
Definition: tagTapeReverse.hpp:391
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verifies tag properties.
Definition: tagTapeReverse.hpp:177
void evaluateForward()
Do nothing.
Definition: tagTapeReverse.hpp:199
static bool constexpr AllowJacobianOptimization
Do not allow Jacobian optimization.
Definition: tagTapeReverse.hpp:271
void storeManual(Real const &lhsValue, Identifier &lhsIndex, Config::ArgumentSize const &size)
Set tag on lhs.
Definition: tagTapeReverse.hpp:335
void reset(bool resetAdjoints=true)
Do nothing.
Definition: tagTapeReverse.hpp:474
Real const & getPrimal(Identifier const &identifier) const
Do nothing.
Definition: tagTapeReverse.hpp:408
Position getZeroPosition() const
Do nothing.
Definition: tagTapeReverse.hpp:363
T_Real Real
See TagTapeReverse.
Definition: tagTapeReverse.hpp:57
void swap(TagTapeReverse &other)
Swap members.
Definition: tagTapeReverse.hpp:162
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:378
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:348
void store(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &lhs, PassiveReal const &rhs)
Verify the lhs properties.
Definition: tagTapeReverse.hpp:316
RealTraits::PassiveReal< Real > PassiveReal
Basic computation type.
Definition: tagTapeReverse.hpp:73
void deactivateValue(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Set tag to passive.
Definition: tagTapeReverse.hpp:261
void evaluateForward(Position const &start, Position const &end)
Do nothing.
Definition: tagTapeReverse.hpp:194
VectorAccessInterface< Real, Identifier > * createVectorAccess()
Do nothing.
Definition: tagTapeReverse.hpp:145
void resetTo(Position const &pos, bool resetAdjoints=true)
Do nothing.
Definition: tagTapeReverse.hpp:368
void evaluateForwardKeepState(Position const &start, Position const &end)
Do nothing.
Definition: tagTapeReverse.hpp:382
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:309
void evaluatePrimal(Position const &start, Position const &end)
Do nothing.
Definition: tagTapeReverse.hpp:395
void registerInput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify value properties.
Definition: tagTapeReverse.hpp:437
T_Tag Tag
See TagTapeReverse.
Definition: tagTapeReverse.hpp:58
void printStatistics(Stream &out=std::cout) const
Do nothing.
Definition: tagTapeReverse.hpp:480
bool isActive(Identifier const &identifier) const
Default check.
Definition: tagTapeReverse.hpp:464
bool isIdentifierActive(Identifier const &index) const
Verify tag.
Definition: tagTapeReverse.hpp:253
void pushJacobiManual(Real const &jacobian, Real const &value, Identifier const &index)
Do nothing.
Definition: tagTapeReverse.hpp:330
Real Gradient
See TapeTypesInterface.
Definition: tagTapeReverse.hpp:69
static bool constexpr RequiresPrimalRestore
No primal values.
Definition: tagTapeReverse.hpp:392
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:486
Tape information that can be printed in a pretty print format or a table format.
Definition: tapeValues.hpp:73
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