CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
tagTapeBase.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 "../../expressions/logic/helpers/forEachLeafLogic.hpp"
38#include "../../misc/enumBitset.hpp"
39#include "../indices/indexManagerInterface.hpp"
40#include "../interfaces/fullTapeInterface.hpp"
41#include "../misc/adjointVectorAccess.hpp"
42#include "tagData.hpp"
43
45namespace codi {
46
48 template<typename Real, typename Tag>
50 bool isActive;
51 bool hasError;
56
59 : isActive(false), hasError(false), hasTagError(false), hasUseError(false), errorTag(), value() {}
60 };
61
72 template<typename T_Real, typename T_Tag, typename T_Gradient, typename T_Impl>
73 struct TagTapeBase {
74 using Real = CODI_DD(T_Real, double);
75 using Tag = CODI_DD(T_Tag, int);
76 using Gradient = CODI_DD(T_Gradient, int);
77 using Impl = CODI_DD(T_Impl, int);
78
80
82 using TagPropertyErrorCallback = void (*)(Real const& currentValue, Real const& newValue, TagFlags flag,
83 void* userData);
84
86 using TagErrorCallback = void (*)(Tag const& correctTag, Tag const& wrongTag, void* userData);
87
88 static Tag constexpr PassiveTag = Tag(0);
89
90 protected:
91
93
96
99
102
103 public:
104
107 : curTag(),
111 tagErrorUserData(this),
113 preaccumulationTag(1337) {}
114
116 struct ValidateTags : public ForEachLeafLogic<ValidateTags> {
117 public:
118
120 template<typename Node>
122 Identifier tagData = node.getIdentifier();
123 tape.verifyTag(vi, tagData.tag);
124 tape.verifyProperties(vi, node.getValue(), tagData.properties);
125 }
126 };
127
129 void swap(Impl& other) {
130 std::swap(curTag, other.curTag);
131 std::swap(tagPropertyErrorCallback, other.tagPropertyErrorCallback);
132 std::swap(tagPropertyErrorUserData, other.tagPropertyErrorUserData);
133 std::swap(tagErrorCallback, other.tagErrorCallback);
134 std::swap(tagErrorUserData, other.tagErrorUserData);
135 std::swap(preaccumulationHandling, other.preaccumulationHandling);
136 std::swap(preaccumulationTag, other.preaccumulationTag);
137 }
138
139 /*******************************************************************************/
142
144 void setCurTag(const Tag& tag) {
145 this->curTag = tag;
146 }
147
150 return this->curTag;
151 }
152
154 template<typename Lhs>
156 return value.cast().getIdentifier().tag;
157 }
158
160 template<typename Lhs>
162 value.cast().getIdentifier().tag = this->curTag;
163 }
164
166 template<typename Lhs>
168 value.cast().getIdentifier().tag = Tag();
169 }
170
172 template<typename Lhs>
174 value.cast().getIdentifier().properties.reset();
175 }
176
178 template<typename Lhs>
180 value.cast().getIdentifier().properties.set(flag);
181 }
182
184 template<typename Lhs>
186 return value.cast().getIdentifier().properties.test(flag);
187 }
188
190 void setTagPropertyErrorCallback(TagPropertyErrorCallback const& callback, void* userData) {
191 tagPropertyErrorCallback = callback;
192 tagPropertyErrorUserData = userData;
193 }
194
196 void setTagErrorCallback(TagErrorCallback const& callback, void* userData) {
197 tagErrorCallback = callback;
198 tagErrorUserData = userData;
199 }
200
204 preaccumulationHandling = enabled;
205 }
206
209 preaccumulationTag = tag;
210 }
211
215 }
216
219 return preaccumulationTag;
220 }
221
222 protected:
223
226 if (PassiveTag != tag) {
227 vi.isActive = true;
228 if (tag != curTag) {
229 vi.hasError = true;
230 vi.hasTagError = true;
231 vi.errorTag = tag;
232 }
233 }
234 }
235
237 CODI_INLINE void verifyTag(Tag const& tag) const {
239
240 verifyTag(vi, tag);
241 handleError(vi);
242 }
243
246 const EnumBitset<TagFlags>& properties) const {
247 if (properties.test(TagFlags::DoNotUse)) {
248 vi.hasError = true;
249 vi.hasUseError = true;
250 vi.value = value;
251 }
252 }
253
255 CODI_INLINE void verifyTagAndProperties(Tag const& tag, Real const& value,
256 const EnumBitset<TagFlags>& properties) const {
258
259 verifyTag(vi, tag);
260 verifyProperties(vi, value, properties);
261 handleError(vi);
262 }
263
265 static void defaultPropertyErrorCallback(Real const& currentValue, Real const& newValue, TagFlags flag,
266 void* userData) {
267 CODI_UNUSED(userData);
268
269 std::cerr << "Property error '" << std::to_string(flag) << "' on value. current value: " << currentValue
270 << " new value: " << newValue << "" << std::endl;
271 }
272
274 static void defaultTagErrorCallback(Tag const& correctTag, Tag const& wrongTag, void* userData) {
275 TagTapeBase& impl = *static_cast<TagTapeBase*>(userData);
276
277 // output default warning if no handle is defined.
278 std::cerr << "Use of variable with bad tag '" << wrongTag << "', should be '" << correctTag << "'.";
279 if (wrongTag == impl.preaccumulationTag) {
280 std::cerr << " The value seems to be a preaccumulation output.";
281 } else if (correctTag == impl.preaccumulationTag) {
282 std::cerr << " The value seems to be used during a preaccumulation but is not declared as an input.";
283 }
284 std::cerr << std::endl;
285 }
286
288 CODI_INLINE void checkLhsError(Real& lhsValue, Identifier& lhsIdentifier, const Real& rhs) const {
289 if (lhsIdentifier.properties.test(TagFlags::DoNotChange)) {
290 if (lhsValue != rhs) {
292 }
293 } else if (lhsIdentifier.properties.test(TagFlags::DoNotWrite)) {
295 }
296 }
297
299 template<typename Lhs>
301 checkLhsError(lhs.cast().value(), lhs.cast().getIdentifier(), rhs);
302 }
303
306 if (vi.hasError) {
307 if (vi.hasTagError) {
309 }
310 if (vi.hasUseError) {
312 }
313 }
314 }
315
317 template<typename Lhs>
319 const Identifier& tag) {
321
322 verifyTag(vi, tag.tag);
323 verifyProperties(vi, value.cast().getValue(), tag.properties);
324 handleError(vi);
325
326 checkLhsError(value, value.cast().getValue());
327 }
328
330 CODI_INLINE void setTag(Tag& tag) const {
331 tag = curTag;
332 }
333
335 CODI_INLINE void resetTag(Tag& tag) const {
336 tag = Tag();
337 }
338
340 };
341}
#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
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
TagFlags
Properties for values.
Definition: tagData.hpp:46
@ DoNotChange
DoNotChange: Value can be assigned, but it should not change.
@ DoNotWrite
DoNotWrite: Value can not be assigned.
Represents a concrete lvalue in the CoDiPack expression tree.
Definition: activeType.hpp:52
A bitset with enum items as flags.
Definition: enumBitset.hpp:64
bool test(Enum pos) const
Test if the bit for the enum is set.
Definition: enumBitset.hpp:93
Implement logic for leaf nodes only.
Definition: forEachLeafLogic.hpp:60
ValidateTags Impl
See ForEachLeafLogic.
Definition: forEachLeafLogic.hpp:63
Base class for all CoDiPack lvalue expression.
Definition: lhsExpressionInterface.hpp:63
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
void handleActive(Node const &node, ValidationIndicator< Real, Tag > &vi, Impl &tape)
Called for leaf nodes which implement LhsExpressionInterface.
Definition: tagTapeBase.hpp:121
Base implementation for tagging tapes.
Definition: tagTapeBase.hpp:73
bool isPreaccumulationHandlingEnabled()
If handling for preaccumulation is enabled.
Definition: tagTapeBase.hpp:213
void setTagPropertyOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, TagFlags flag)
Set properties on a CoDiPack active type.
Definition: tagTapeBase.hpp:179
void * tagPropertyErrorUserData
User data in call to callback for lhs value errors.
Definition: tagTapeBase.hpp:95
void setPreaccumulationHandlingEnabled(bool enabled)
Enable or disable specialized handling for preaccumulation. Default: true Uses a special tag to sanit...
Definition: tagTapeBase.hpp:203
void clearTagOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Clear tag on a CoDiPack active type.
Definition: tagTapeBase.hpp:167
T_Gradient Gradient
See TagTapeBase.
Definition: tagTapeBase.hpp:76
T_Tag Tag
See TagTapeBase.
Definition: tagTapeBase.hpp:75
void verifyTag(Tag const &tag) const
Checks if the tag is correct and creates an error.
Definition: tagTapeBase.hpp:237
TagErrorCallback tagErrorCallback
User defined callback for tag errors.
Definition: tagTapeBase.hpp:97
void(*)(Tag const &correctTag, Tag const &wrongTag, void *userData) TagErrorCallback
Callback for a tag error.
Definition: tagTapeBase.hpp:86
Tag curTag
Current tag for new values.
Definition: tagTapeBase.hpp:92
void setTagErrorCallback(TagErrorCallback const &callback, void *userData)
Set the callback and user data for a tag error.
Definition: tagTapeBase.hpp:196
void verifyProperties(ValidationIndicator< Real, Tag > &vi, Real const &value, const EnumBitset< TagFlags > &properties) const
Checks if the tag properties are correct.
Definition: tagTapeBase.hpp:245
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 setTagOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > const &value)
Set tag on a CoDiPack active type.
Definition: tagTapeBase.hpp:161
bool preaccumulationHandling
Parameter to enable/disable preaccumulation handling.
Definition: tagTapeBase.hpp:100
TagTapeBase()
Constructor.
Definition: tagTapeBase.hpp:106
Tag getTagFromVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Get tag of a CoDiPack active type.
Definition: tagTapeBase.hpp:155
static void defaultPropertyErrorCallback(Real const &currentValue, Real const &newValue, TagFlags flag, void *userData)
Default callback for TagPropertyErrorCallback.
Definition: tagTapeBase.hpp:265
void clearTagPropertiesOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Clear properties on a CoDiPack active type.
Definition: tagTapeBase.hpp:173
void handleError(ValidationIndicator< Real, Tag > &vi) const
Call tag error callback.
Definition: tagTapeBase.hpp:305
T_Impl Impl
See TagTapeBase.
Definition: tagTapeBase.hpp:77
TagPropertyErrorCallback tagPropertyErrorCallback
User defined callback for lhs value errors.
Definition: tagTapeBase.hpp:94
Tag getPreaccumulationHandlingTag()
The special tag for preaccumulation.
Definition: tagTapeBase.hpp:218
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 setTagPropertyErrorCallback(TagPropertyErrorCallback const &callback, void *userData)
Set the callback and user data for a property error error.
Definition: tagTapeBase.hpp:190
T_Real Real
See TagTapeBase.
Definition: tagTapeBase.hpp:74
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
static void defaultTagErrorCallback(Tag const &correctTag, Tag const &wrongTag, void *userData)
Default callback for TagErrorCallback.
Definition: tagTapeBase.hpp:274
Tag getCurTag()
Get the current tag of the tape.
Definition: tagTapeBase.hpp:149
bool hasTagPropertyOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, TagFlags flag)
Check properties on a CoDiPack active type.
Definition: tagTapeBase.hpp:185
void(*)(Real const &currentValue, Real const &newValue, TagFlags flag, void *userData) TagPropertyErrorCallback
Callback for a change in a lhs value.
Definition: tagTapeBase.hpp:83
void setCurTag(const Tag &tag)
Set the current tag of the tape.
Definition: tagTapeBase.hpp:144
Tag preaccumulationTag
Tag used for preaccumulation specialized handling.
Definition: tagTapeBase.hpp:101
void checkLhsError(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &lhs, const Real &rhs) const
Check if the lhs value is changed.
Definition: tagTapeBase.hpp:300
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 setPreaccumulationHandlingTag(Tag tag)
Set the special tag for preaccumulation regions. See setPreaccumulationHandlingEnabled().
Definition: tagTapeBase.hpp:208
void swap(Impl &other)
Swap members.
Definition: tagTapeBase.hpp:129
void * tagErrorUserData
User data in call to callback for tag errors.
Definition: tagTapeBase.hpp:98
void node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition: traversalLogic.hpp:86
Helper class for statement validation.
Definition: tagTapeBase.hpp:49
bool hasError
true if an error is detected.
Definition: tagTapeBase.hpp:51
Tag errorTag
Value of the wrong tag.
Definition: tagTapeBase.hpp:54
ValidationIndicator()
Constructor.
Definition: tagTapeBase.hpp:58
Real value
Primal value of the value with the tag error.
Definition: tagTapeBase.hpp:55
bool hasUseError
true if a value is used in the wrong way.
Definition: tagTapeBase.hpp:53
bool isActive
true if an active rhs is detected. tag != 0
Definition: tagTapeBase.hpp:50
bool hasTagError
true if a tag not the current required tag.
Definition: tagTapeBase.hpp:52