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://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() {}
173
175 /*******************************************************************************/
178
180 template<typename Lhs>
186
189 CODI_UNUSED(extFunc);
190 }
191
193 /*******************************************************************************/
196
198 void evaluateForward(Position const& start, Position const& end) {
199 CODI_UNUSED(start, end);
200 }
201
204
206 /*******************************************************************************/
209
211 void setGradient(Identifier const& identifier, Gradient const& gradient) {
213
214 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
215 }
216
218 Gradient const& getGradient(Identifier const& identifier) const {
219 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
220
221 return tempGradient;
222 }
223
225 Gradient& gradient(Identifier const& identifier) {
226 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
227
228 return tempGradient;
229 }
230
232 Gradient const& gradient(Identifier const& identifier) const {
233 Base::verifyTagAndProperties(identifier.tag, 0.0, identifier.properties);
234
235 return tempGradient;
236 }
237
239 /*******************************************************************************/
242
244 static bool constexpr LinearIndexHandling = true;
245
250
255
257 bool isIdentifierActive(Identifier const& index) const {
258 Base::verifyTag(index.tag);
259
260 return index.tag != Base::PassiveTag;
261 }
262
264 template<typename Lhs>
268
270 /*******************************************************************************/
273
275 static bool constexpr AllowJacobianOptimization = false;
276
278 template<typename Real>
279 void initIdentifier(Real& value, Identifier& identifier) {
280 CODI_UNUSED(value);
281 identifier = Identifier();
282 }
283
285 template<typename Real>
286 void destroyIdentifier(Real& value, Identifier& identifier) {
287 CODI_UNUSED(value, identifier);
288 }
289
291 template<typename Lhs, typename Rhs>
294 typename Base::ValidateTags validate;
296
297 validate.eval(rhs, vi, *this);
298
299 Base::checkLhsError(lhs, rhs.cast().getValue());
300
302
303 if (vi.isActive) {
304 Base::setTag(lhs.cast().getIdentifier().tag);
305 } else {
306 Base::resetTag(lhs.cast().getIdentifier().tag);
307 }
308 lhs.cast().value() = rhs.cast().getValue();
309 }
310
312 template<typename Lhs, typename Rhs>
317
319 template<typename Lhs>
321 Base::checkLhsError(lhs, rhs);
322
323 Base::resetTag(lhs.cast().getIdentifier().tag);
324
325 lhs.cast().value() = rhs;
326 }
327
329 /*******************************************************************************/
332
334 void pushJacobiManual(Real const& jacobian, Real const& value, Identifier const& index) {
335 CODI_UNUSED(jacobian, value, index);
336 }
337
339 void storeManual(Real const& lhsValue, Identifier& lhsIndex, Config::ArgumentSize const& size) {
340 CODI_UNUSED(lhsValue, size);
341
342 Base::checkLhsError(lhsValue, lhsIndex, lhsValue);
343 setTag(lhsIndex.tag);
344 }
345
347 /*******************************************************************************/
350
352 void evaluate(Position const& start, Position const& end) {
353 CODI_UNUSED(start, end);
354 }
355
357 void clearAdjoints(Position const& start, Position const& end) {
358 CODI_UNUSED(start, end);
359 }
360
363 return Position();
364 }
365
368 return Position();
369 }
370
372 void resetTo(Position const& pos, bool resetAdjoints = true) {
373 CODI_UNUSED(pos, resetAdjoints);
374 }
375
377 /*******************************************************************************/
380
382 void evaluateKeepState(Position const& start, Position const& end) {
383 CODI_UNUSED(start, end);
384 }
386 void evaluateForwardKeepState(Position const& start, Position const& end) {
387 CODI_UNUSED(start, end);
388 }
389
391 /*******************************************************************************/
394
395 static bool constexpr HasPrimalValues = false;
396 static bool constexpr RequiresPrimalRestore = false;
397
399 void evaluatePrimal(Position const& start, Position const& end) {
400 CODI_UNUSED(start, end);
401 }
402
405
407 void setPrimal(Identifier const& identifier, Real const& gradient) {
408 CODI_UNUSED(identifier, gradient);
409 }
410
412 Real const& getPrimal(Identifier const& identifier) const {
413 CODI_UNUSED(identifier);
414 return tempPrimal;
415 }
416
418 Real& primal(Identifier const& identifier) {
419 CODI_UNUSED(identifier);
420 return tempPrimal;
421 }
422
424 Real const& primal(Identifier const& identifier) const {
425 CODI_UNUSED(identifier);
426 return tempPrimal;
427 }
428
430 void revertPrimals(Position const& pos) {
431 CODI_UNUSED(pos);
432 }
433
435 /*******************************************************************************/
438
440 template<typename Lhs>
442 Base::setTag(value.cast().getIdentifier().tag);
443 Base::verifyRegisterValue(value, value.cast().getIdentifier()); // verification is mainly for the properties
444 }
445
447 template<typename Lhs>
451
453 void setActive() {
454 active = true;
455 }
456
458 void setPassive() {
459 active = false;
460 }
461
463 bool isActive() const {
464 return active;
465 }
466
468 bool isActive(Identifier const& identifier) const {
469 return identifier.tag != Base::PassiveTag;
470 }
471
472 void evaluate() {}
473
476
478 void reset(bool resetAdjoints = true) {
479 CODI_UNUSED(resetAdjoints);
480 }
481
483 template<typename Stream = std::ostream>
484 void printStatistics(Stream& out = std::cout) const {
485 CODI_UNUSED(out);
486 }
487
489 template<typename Stream = std::ostream>
490 void printTableHeader(Stream& out = std::cout) const {
491 CODI_UNUSED(out);
492 }
493
495 template<typename Stream = std::ostream>
496 void printTableRow(Stream& out = std::cout) const {
497 CODI_UNUSED(out);
498 }
499
502 return TapeValues("TagTapeReverse");
503 }
504
506 };
507}
#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: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:172
void destroyIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition tagTapeReverse.hpp:286
void clearAdjoints(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:357
bool hasParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:137
void evaluate()
Do nothing.
Definition tagTapeReverse.hpp:472
Gradient & gradient(Identifier const &identifier)
Verify tag.
Definition tagTapeReverse.hpp:225
Real const & primal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:424
void deleteVectorAccess(VectorAccessInterface< Real, Identifier > *access)
Do nothing.
Definition tagTapeReverse.hpp:161
void printTableRow(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:496
void deleteData()
Do nothing.
Definition tagTapeReverse.hpp:122
Gradient const & getGradient(Identifier const &identifier) const
Verify tag.
Definition tagTapeReverse.hpp:218
void setPassive()
Set tape to passive.
Definition tagTapeReverse.hpp:458
Identifier getInvalidIndex() const
-1 tag.
Definition tagTapeReverse.hpp:252
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:418
TapeValues getTapeValues() const
Do nothing.
Definition tagTapeReverse.hpp:501
void setParameter(TapeParameters parameter, size_t value)
Do nothing.
Definition tagTapeReverse.hpp:144
void initIdentifier(Real &value, Identifier &identifier)
Do nothing.
Definition tagTapeReverse.hpp:279
VectorAccessInterface< Real, Identifier > * createVectorAccessCustomAdjoints(Adjoint *data)
Do nothing.
Definition tagTapeReverse.hpp:155
bool isActive() const
Check if tape is active.
Definition tagTapeReverse.hpp:463
TagData< Tag > Identifier
See TapeTypesInterface.
Definition tagTapeReverse.hpp:70
void setActive()
Set tape to active.
Definition tagTapeReverse.hpp:453
void setGradient(Identifier const &identifier, Gradient const &gradient)
Verify tag.
Definition tagTapeReverse.hpp:211
Position getPosition() const
Do nothing.
Definition tagTapeReverse.hpp:362
void clearAdjoints()
Do nothing.
Definition tagTapeReverse.hpp:475
Gradient const & gradient(Identifier const &identifier) const
Verify tag.
Definition tagTapeReverse.hpp:232
size_t getParameter(TapeParameters parameter) const
Do nothing.
Definition tagTapeReverse.hpp:130
Identifier getPassiveIndex() const
Zero tag.
Definition tagTapeReverse.hpp:247
static bool constexpr LinearIndexHandling
Behave as linear index handler.
Definition tagTapeReverse.hpp:244
void revertPrimals(Position const &pos)
Do nothing.
Definition tagTapeReverse.hpp:430
void pushExternalFunction(ExternalFunction< TagTapeReverse > const &extFunc)
Do nothing.
Definition tagTapeReverse.hpp:188
void evaluatePrimal()
Do nothing.
Definition tagTapeReverse.hpp:404
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:292
T_Real Real
See TagTapeReverse.
Definition tagTapeReverse.hpp:57
void registerOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify tag.
Definition tagTapeReverse.hpp:448
void setPrimal(Identifier const &identifier, Real const &gradient)
Do nothing.
Definition tagTapeReverse.hpp:407
static bool constexpr HasPrimalValues
No primal values.
Definition tagTapeReverse.hpp:395
Real registerExternalFunctionOutput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verifies tag properties.
Definition tagTapeReverse.hpp:181
void evaluateForward()
Do nothing.
Definition tagTapeReverse.hpp:203
static bool constexpr AllowJacobianOptimization
Do not allow Jacobian optimization.
Definition tagTapeReverse.hpp:275
void storeManual(Real const &lhsValue, Identifier &lhsIndex, Config::ArgumentSize const &size)
Set tag on lhs.
Definition tagTapeReverse.hpp:339
void reset(bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:478
Real const & getPrimal(Identifier const &identifier) const
Do nothing.
Definition tagTapeReverse.hpp:412
Position getZeroPosition() const
Do nothing.
Definition tagTapeReverse.hpp:367
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:382
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:352
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:320
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:265
void evaluateForward(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:198
VectorAccessInterface< Real, Identifier > * createVectorAccess()
Do nothing.
Definition tagTapeReverse.hpp:149
void resetTo(Position const &pos, bool resetAdjoints=true)
Do nothing.
Definition tagTapeReverse.hpp:372
void evaluateForwardKeepState(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:386
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:313
void evaluatePrimal(Position const &start, Position const &end)
Do nothing.
Definition tagTapeReverse.hpp:399
void registerInput(LhsExpressionInterface< Real, Gradient, TagTapeReverse, Lhs > &value)
Verify value properties.
Definition tagTapeReverse.hpp:441
void printStatistics(Stream &out=std::cout) const
Do nothing.
Definition tagTapeReverse.hpp:484
bool isActive(Identifier const &identifier) const
Default check.
Definition tagTapeReverse.hpp:468
bool isIdentifierActive(Identifier const &index) const
Verify tag.
Definition tagTapeReverse.hpp:257
void pushJacobiManual(Real const &jacobian, Real const &value, Identifier const &index)
Do nothing.
Definition tagTapeReverse.hpp:334
Real Gradient
See TapeTypesInterface.
Definition tagTapeReverse.hpp:69
static bool constexpr RequiresPrimalRestore
No primal values.
Definition tagTapeReverse.hpp:396
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:490
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