CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
customAdjointVectorHelper.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2026 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 "../../config.h"
38#include "../../expressions/lhsExpressionInterface.hpp"
39#include "../../misc/macros.hpp"
40#include "../../tapes/interfaces/fullTapeInterface.hpp"
41#include "../../tapes/misc/adjointVectorAccess.hpp"
42#include "../../tapes/misc/vectorAccessInterface.hpp"
43#include "../../traits/tapeTraits.hpp"
44
46namespace codi {
47
60 template<typename T_Type>
62 public:
63
65 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
66
67 using Real = typename Type::Real;
68 using Identifier = typename Type::Identifier;
69
71 using Tape = typename Type::Tape;
72 using Position = typename Tape::Position;
73
74 private:
75
76 Tape* tape;
77
78 public:
79
82
85
86 /*******************************************************************************/
89
91 virtual void clearAdjoints() = 0;
92
94 virtual void deleteAdjointVector() = 0;
95
98 virtual bool isAdjointSizeFixed() = 0;
99
102 virtual void resizeAdjointToFixedSize(size_t size) = 0;
103
105 virtual void resizeAdjointToTapeSize() = 0;
106
108 virtual void evaluate(Position const& start, Position const& end) = 0;
109
110 // clang-format off
112 // clang-format on
113 virtual void evaluateForward(Position const& start, Position const& end) = 0;
114
117
119 /*******************************************************************************/
122
124 void evaluate() {
125 evaluate(tape->getPosition(), tape->getZeroPosition());
126 }
127
130 evaluate(tape->getPosition(), tape->getZeroPosition());
131 }
132
134 void setTape(Tape& tape) {
135 this->tape = &tape;
136 }
137
140 return *this->tape;
141 }
142
144 };
145
167 template<typename T_Type, typename T_Gradient>
169 public:
170
172 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
173 using Gradient = CODI_DD(T_Gradient, double);
174
176
177 using Real = typename Type::Real;
178 using Identifier = typename Type::Identifier;
179
181 using Tape = CODI_DD(typename Type::Tape, CODI_DEFAULT_TAPE);
182 using Position = typename Tape::Position;
183
184 protected:
185 std::vector<Gradient> adjointVector;
186
189
192
196 size_t fixedAdjointSize = {};
197
198 public:
199
203
206 if (nullptr != adjointInterface) {
207 delete adjointInterface;
208 }
209 }
210
211 /*******************************************************************************/
214
217 for (size_t i = 0; i < adjointVector.size(); i += 1) {
218 adjointVector[i] = Gradient();
219 }
220 }
221
224 adjointVector.resize(0);
225 adjointVector.shrink_to_fit();
226 }
227
230 return fixedAdjointSize != 0;
231 }
232
234 void resizeAdjointToFixedSize(size_t size) {
235 fixedAdjointSize = size;
236 adjointVector.resize(size);
237 adjointVector.shrink_to_fit();
238 }
239
243 checkAdjointVectorSize();
244 }
245
247 void evaluate(Position const& start, Position const& end) {
248 checkAdjointVectorSize();
249
250 Base::getTape().evaluate(start, end, adjointVector.data());
251 }
252 using Base::evaluate;
253
255 void evaluateForward(Position const& start, Position const& end) {
256 checkAdjointVectorSize();
257
258 Base::getTape().evaluateForward(start, end, adjointVector.data());
259 }
261
264 if (nullptr != adjointInterface) {
265 delete adjointInterface;
266 }
267
268 checkAdjointVectorSize();
270 return adjointInterface;
271 }
272
274 /*******************************************************************************/
277
279 Gradient const& getGradient(Identifier const& identifier) const {
280 return gradient(identifier);
281 }
282
285 return adjointVector[identifier];
286 }
287
289 Gradient const& gradientUnchecked(Identifier const& identifier) const {
290 return adjointVector[identifier];
291 }
292
294 Gradient& gradient(Identifier const& identifier) {
295 checkAdjointVectorSize();
296
297 if (0 != identifier && identifier < (Identifier)adjointVector.size()) {
298 return adjointVector[identifier];
299 } else {
301 return zeroValue;
302 }
303 }
304
306 Gradient const& gradient(Identifier const& identifier) const {
307 if (0 != identifier && identifier < (Identifier)adjointVector.size()) {
308 return adjointVector[identifier];
309 } else {
310 return constZeroValue;
311 }
312 }
313
315 void setGradient(Identifier& identifier, Gradient const& gradientValue) {
316 gradient(identifier) = gradientValue;
317 }
318
320
321 private:
322
323 void checkAdjointVectorSize() {
324 if (0 == fixedAdjointSize) { // Skip if fixed size is set
325 if (adjointVector.size() <= Base::getTape().getParameter(TapeParameters::LargestIdentifier)) {
327 }
328 }
329 }
330 };
331}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
@ LargestIdentifier
[A: R] Largest identifier distributed by the index manger.
Definition tapeParameters.hpp:60
Implementation of VectorAccessInterface for adjoint vectors.
Definition adjointVectorAccess.hpp:61
void clearAdjoints()
Set all adjoints to zero.
Definition customAdjointVectorHelper.hpp:216
Gradient const & gradient(Identifier const &identifier) const
Get a constant reference to the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:306
Gradient & gradientUnchecked(Identifier const &identifier)
Get a reference to the gradient. Unchecked access.
Definition customAdjointVectorHelper.hpp:284
VectorAccessInterface< Real, Identifier > * getVectorInterface()
Get a new general interface to the adjoint vector.
Definition customAdjointVectorHelper.hpp:263
CustomAdjointVectorInterface< Type > Base
Abbreviation for the base class.
Definition customAdjointVectorHelper.hpp:175
Gradient & gradient(Identifier const &identifier)
Get a reference to the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:294
~CustomAdjointVectorHelper()
Destructor.
Definition customAdjointVectorHelper.hpp:205
typename Tape::Position Position
See PositionalEvaluationTapeInterface.
Definition customAdjointVectorHelper.hpp:182
T_Type Type
< See CustomAdjointVectorHelper.
Definition customAdjointVectorHelper.hpp:172
bool isAdjointSizeFixed()
Definition customAdjointVectorHelper.hpp:229
Gradient const & gradientUnchecked(Identifier const &identifier) const
Get a constant reference to the gradient. Unchecked access.
Definition customAdjointVectorHelper.hpp:289
void evaluateForward(Position const &start, Position const &end)
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition customAdjointVectorHelper.hpp:255
Gradient const & getGradient(Identifier const &identifier) const
Get a constant reference to the gradient.
Definition customAdjointVectorHelper.hpp:279
typename Type::Real Real
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:177
size_t fixedAdjointSize
Definition customAdjointVectorHelper.hpp:196
void resizeAdjointToTapeSize()
Resize the adjoint vector to the largest identifier of the tape. This will remove any fixed size that...
Definition customAdjointVectorHelper.hpp:241
void resizeAdjointToFixedSize(size_t size)
Definition customAdjointVectorHelper.hpp:234
T_Gradient Gradient
See CustomAdjointVectorHelper.
Definition customAdjointVectorHelper.hpp:173
AdjointVectorAccess< Real, Identifier, Gradient * > * adjointInterface
Last created adjoint interface.
Definition customAdjointVectorHelper.hpp:191
void deleteAdjointVector()
Delete the adjoint vector.
Definition customAdjointVectorHelper.hpp:223
Gradient zeroValue
Temporary zero value.
Definition customAdjointVectorHelper.hpp:187
std::vector< Gradient > adjointVector
Custom adjoint vector.
Definition customAdjointVectorHelper.hpp:185
typename Type::Tape Tape
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:181
Gradient const constZeroValue
Temporary constant zero value.
Definition customAdjointVectorHelper.hpp:188
CustomAdjointVectorHelper()
Constructor.
Definition customAdjointVectorHelper.hpp:201
typename Type::Identifier Identifier
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:178
void setGradient(Identifier &identifier, Gradient const &gradientValue)
Set the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:315
void evaluate(Position const &start, Position const &end)
Perform a full reverse evaluation of the tape.
Definition customAdjointVectorHelper.hpp:247
void evaluateForward()
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition customAdjointVectorHelper.hpp:129
CustomAdjointVectorInterface()
Constructor.
Definition customAdjointVectorHelper.hpp:81
void evaluate()
Perform a full reverse evaluation of the tape.
Definition customAdjointVectorHelper.hpp:124
typename Type::Identifier Identifier
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:68
virtual VectorAccessInterface< Real, Identifier > * getVectorInterface()=0
Get a new general interface to the adjoint vector.
T_Type Type
See CustomAdjointVectorInterface.
Definition customAdjointVectorHelper.hpp:65
typename Tape::Position Position
See PositionalEvaluationTapeInterface.
Definition customAdjointVectorHelper.hpp:72
void setTape(Tape &tape)
Set the tape for the evaluations.
Definition customAdjointVectorHelper.hpp:134
virtual void resizeAdjointToFixedSize(size_t size)=0
virtual ~CustomAdjointVectorInterface()
Destructor.
Definition customAdjointVectorHelper.hpp:84
virtual void resizeAdjointToTapeSize()=0
Resize the adjoint vector to the largest identifier of the tape. This will remove any fixed size that...
virtual void deleteAdjointVector()=0
Delete the adjoint vector.
virtual void clearAdjoints()=0
Set all adjoints to zero.
typename Type::Real Real
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:67
virtual void evaluateForward(Position const &start, Position const &end)=0
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
virtual void evaluate(Position const &start, Position const &end)=0
Perform a reverse evaluation for a part of the tape. It hast to hold start >= end.
Tape & getTape()
Definition customAdjointVectorHelper.hpp:139
typename Type::Tape Tape
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:71
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94