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://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 "../../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>
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 static Tag constexpr InvalidTag = Tag(-1);
90
91 protected:
92
94
97
100
103
104 public:
105
115
117 struct ValidateTags : public ForEachLeafLogic<ValidateTags> {
118 public:
119
121 template<typename Node>
123 Identifier tagData = node.getIdentifier();
124 tape.verifyTag(vi, tagData.tag);
125 tape.verifyProperties(vi, node.getValue(), tagData.properties);
126 }
127 };
128
130 void swap(Impl& other) {
131 std::swap(curTag, other.curTag);
132 std::swap(tagPropertyErrorCallback, other.tagPropertyErrorCallback);
133 std::swap(tagPropertyErrorUserData, other.tagPropertyErrorUserData);
134 std::swap(tagErrorCallback, other.tagErrorCallback);
135 std::swap(tagErrorUserData, other.tagErrorUserData);
136 std::swap(preaccumulationHandling, other.preaccumulationHandling);
137 std::swap(preaccumulationTag, other.preaccumulationTag);
138 }
139
140 /*******************************************************************************/
143
145 void setCurTag(const Tag& tag) {
146 this->curTag = tag;
147 }
148
151 return this->curTag;
152 }
153
155 template<typename Lhs>
157 return value.cast().getIdentifier().tag;
158 }
159
161 template<typename Lhs>
163 value.cast().getIdentifier().tag = this->curTag;
164 }
165
167 template<typename Lhs>
169 value.cast().getIdentifier().tag = Tag();
170 }
171
173 template<typename Lhs>
175 value.cast().getIdentifier().properties.reset();
176 }
177
179 template<typename Lhs>
181 value.cast().getIdentifier().properties.set(flag);
182 }
183
185 template<typename Lhs>
187 return value.cast().getIdentifier().properties.test(flag);
188 }
189
191 void setTagPropertyErrorCallback(TagPropertyErrorCallback const& callback, void* userData) {
192 tagPropertyErrorCallback = callback;
193 tagPropertyErrorUserData = userData;
194 }
195
197 void setTagErrorCallback(TagErrorCallback const& callback, void* userData) {
198 tagErrorCallback = callback;
199 tagErrorUserData = userData;
200 }
201
207
212
217
222
223 protected:
224
227 if (PassiveTag != tag && InvalidTag != tag) {
228 vi.isActive = true;
229 if (tag != curTag) {
230 vi.hasError = true;
231 vi.hasTagError = true;
232 vi.errorTag = tag;
233 }
234 }
235 }
236
238 CODI_INLINE void verifyTag(Tag const& tag) const {
240
241 verifyTag(vi, tag);
242 handleError(vi);
243 }
244
247 const EnumBitset<TagFlags>& properties) const {
248 if (properties.test(TagFlags::DoNotUse)) {
249 vi.hasError = true;
250 vi.hasUseError = true;
251 vi.value = value;
252 }
253 }
254
256 CODI_INLINE void verifyTagAndProperties(Tag const& tag, Real const& value,
257 const EnumBitset<TagFlags>& properties) const {
259
260 verifyTag(vi, tag);
261 verifyProperties(vi, value, properties);
262 handleError(vi);
263 }
264
266 static void defaultPropertyErrorCallback(Real const& currentValue, Real const& newValue, TagFlags flag,
267 void* userData) {
268 CODI_UNUSED(userData);
269
270 std::cerr << "Property error '" << std::to_string(flag) << "' on value. current value: " << currentValue
271 << " new value: " << newValue << "" << std::endl;
272 }
273
275 static void defaultTagErrorCallback(Tag const& correctTag, Tag const& wrongTag, void* userData) {
276 TagTapeBase& impl = *static_cast<TagTapeBase*>(userData);
277
278 // output default warning if no handle is defined.
279 std::cerr << "Use of variable with bad tag '" << wrongTag << "', should be '" << correctTag << "'.";
280 if (wrongTag == impl.preaccumulationTag) {
281 std::cerr << " The value seems to be a preaccumulation output.";
282 } else if (correctTag == impl.preaccumulationTag) {
283 std::cerr << " The value seems to be used during a preaccumulation but is not declared as an input.";
284 }
285 std::cerr << std::endl;
286 }
287
289 CODI_INLINE void checkLhsError(Real& lhsValue, Identifier& lhsIdentifier, const Real& rhs) const {
290 if (lhsIdentifier.properties.test(TagFlags::DoNotChange)) {
291 if (lhsValue != rhs) {
293 }
294 } else if (lhsIdentifier.properties.test(TagFlags::DoNotWrite)) {
296 }
297 }
298
300 template<typename Lhs>
302 checkLhsError(lhs.cast().value(), lhs.cast().getIdentifier(), rhs);
303 }
304
316
318 template<typename Lhs>
320 const Identifier& tag) {
322
323 verifyTag(vi, tag.tag);
324 verifyProperties(vi, value.cast().getValue(), tag.properties);
325 handleError(vi);
326
327 checkLhsError(value, value.cast().getValue());
328 }
329
331 CODI_INLINE void setTag(Tag& tag) const {
332 tag = curTag;
333 }
334
336 CODI_INLINE void resetTag(Tag& tag) const {
337 tag = Tag();
338 }
339
341 };
342}
#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:117
void handleActive(Node const &node, ValidationIndicator< Real, Tag > &vi, Impl &tape)
Called for leaf nodes which implement LhsExpressionInterface.
Definition tagTapeBase.hpp:122
Base implementation for tagging tapes.
Definition tagTapeBase.hpp:73
bool isPreaccumulationHandlingEnabled()
If handling for preaccumulation is enabled.
Definition tagTapeBase.hpp:214
void setTagPropertyOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, TagFlags flag)
Set properties on a CoDiPack active type.
Definition tagTapeBase.hpp:180
void * tagPropertyErrorUserData
User data in call to callback for lhs value errors.
Definition tagTapeBase.hpp:96
void setPreaccumulationHandlingEnabled(bool enabled)
Enable or disable specialized handling for preaccumulation. Default: true Uses a special tag to sanit...
Definition tagTapeBase.hpp:204
void clearTagOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Clear tag on a CoDiPack active type.
Definition tagTapeBase.hpp:168
void verifyTag(Tag const &tag) const
Checks if the tag is correct and creates an error.
Definition tagTapeBase.hpp:238
TagErrorCallback tagErrorCallback
User defined callback for tag errors.
Definition tagTapeBase.hpp:98
T_Impl Impl
See TagTapeBase.
Definition tagTapeBase.hpp:77
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:93
void setTagErrorCallback(TagErrorCallback const &callback, void *userData)
Set the callback and user data for a tag error.
Definition tagTapeBase.hpp:197
T_Tag Tag
See TagTapeBase.
Definition tagTapeBase.hpp:75
void verifyProperties(ValidationIndicator< Real, Tag > &vi, Real const &value, const EnumBitset< TagFlags > &properties) const
Checks if the tag properties are correct.
Definition tagTapeBase.hpp:246
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 setTagOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > const &value)
Set tag on a CoDiPack active type.
Definition tagTapeBase.hpp:162
bool preaccumulationHandling
Parameter to enable/disable preaccumulation handling.
Definition tagTapeBase.hpp:101
TagTapeBase()
Constructor.
Definition tagTapeBase.hpp:107
Tag getTagFromVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Get tag of a CoDiPack active type.
Definition tagTapeBase.hpp:156
static void defaultPropertyErrorCallback(Real const &currentValue, Real const &newValue, TagFlags flag, void *userData)
Default callback for TagPropertyErrorCallback.
Definition tagTapeBase.hpp:266
T_Real Real
See TagTapeBase.
Definition tagTapeBase.hpp:74
void clearTagPropertiesOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value)
Clear properties on a CoDiPack active type.
Definition tagTapeBase.hpp:174
void handleError(ValidationIndicator< Real, Tag > &vi) const
Call tag error callback.
Definition tagTapeBase.hpp:306
TagPropertyErrorCallback tagPropertyErrorCallback
User defined callback for lhs value errors.
Definition tagTapeBase.hpp:95
Tag getPreaccumulationHandlingTag()
The special tag for preaccumulation.
Definition tagTapeBase.hpp:219
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 setTagPropertyErrorCallback(TagPropertyErrorCallback const &callback, void *userData)
Set the callback and user data for a property error error.
Definition tagTapeBase.hpp:191
T_Gradient Gradient
See TagTapeBase.
Definition tagTapeBase.hpp:76
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 void defaultTagErrorCallback(Tag const &correctTag, Tag const &wrongTag, void *userData)
Default callback for TagErrorCallback.
Definition tagTapeBase.hpp:275
static Tag constexpr InvalidTag
Tag indicating an invalid value.
Definition tagTapeBase.hpp:89
Tag getCurTag()
Get the current tag of the tape.
Definition tagTapeBase.hpp:150
bool hasTagPropertyOnVariable(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &value, TagFlags flag)
Check properties on a CoDiPack active type.
Definition tagTapeBase.hpp:186
void setCurTag(const Tag &tag)
Set the current tag of the tape.
Definition tagTapeBase.hpp:145
Tag preaccumulationTag
Tag used for preaccumulation specialized handling.
Definition tagTapeBase.hpp:102
void checkLhsError(LhsExpressionInterface< Real, Gradient, Impl, Lhs > &lhs, const Real &rhs) const
Check if the lhs value is changed.
Definition tagTapeBase.hpp:301
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 setPreaccumulationHandlingTag(Tag tag)
Set the special tag for preaccumulation regions. See setPreaccumulationHandlingEnabled().
Definition tagTapeBase.hpp:209
void swap(Impl &other)
Swap members.
Definition tagTapeBase.hpp:130
void(*)(Real const &currentValue, Real const &newValue, TagFlags flag, void *userData) TagPropertyErrorCallback
Callback for a change in a lhs value.
Definition tagTapeBase.hpp:82
void * tagErrorUserData
User data in call to callback for tag errors.
Definition tagTapeBase.hpp:99
void node(Node const &node, Args &&... args)
Called for each node in the expression.
Definition traversalLogic.hpp:86
void link(Child const &child, Root const &root, Args &&... args)
Called for all links in the expression.
Definition traversalLogic.hpp:110
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