CoDiPack  2.2.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-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 "../../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 protected:
75
77
78 public:
79
82
85
86 /*******************************************************************************/
89
91 virtual void clearAdjoints() = 0;
92
94 virtual void deleteAdjointVector() = 0;
95
97 virtual void evaluate(Position const& start, Position const& end) = 0;
98
99 // clang-format off
101 // clang-format on
102 virtual void evaluateForward(Position const& start, Position const& end) = 0;
103
106
108 /*******************************************************************************/
111
113 void evaluate() {
114 evaluate(tape.getPosition(), tape.getZeroPosition());
115 }
116
119 evaluate(tape.getPosition(), tape.getZeroPosition());
120 }
121
124 this->tape = tape;
125 }
126
128 };
129
151 template<typename T_Type, typename T_Gradient>
153 public:
154
156 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
157 using Gradient = CODI_DD(T_Gradient, double);
158
160
161 using Real = typename Type::Real;
162 using Identifier = typename Type::Identifier;
163
165 using Tape = CODI_DD(typename Type::Tape, CODI_DEFAULT_TAPE);
166 using Position = typename Tape::Position;
167
168 protected:
169 std::vector<Gradient> adjointVector;
170
173
175
176 public:
177
181
184 if (nullptr != adjointInterface) {
185 delete adjointInterface;
186 }
187 }
188
189 /*******************************************************************************/
192
195 for (size_t i = 0; i < adjointVector.size(); i += 1) {
196 adjointVector[i] = Gradient();
197 }
198 }
199
202 adjointVector.resize(0);
203 adjointVector.shrink_to_fit();
204 }
205
207 void evaluate(Position const& start, Position const& end) {
208 checkAdjointVectorSize();
209
210 Base::tape.evaluate(start, end, adjointVector.data());
211 }
212 using Base::evaluate;
213
215 void evaluateForward(Position const& start, Position const& end) {
216 checkAdjointVectorSize();
217
218 Base::tape.evaluateForward(start, end, adjointVector.data());
219 }
221
224 if (nullptr != adjointInterface) {
225 delete adjointInterface;
226 }
227
228 checkAdjointVectorSize();
230 return adjointInterface;
231 }
232
234 /*******************************************************************************/
237
239 Gradient const& getGradient(Identifier const& identifier) const {
240 return gradient(identifier);
241 }
242
245 return adjointVector[identifier];
246 }
247
249 Gradient const& gradientUnchecked(Identifier const& identifier) const {
250 return adjointVector[identifier];
251 }
252
254 Gradient& gradient(Identifier const& identifier) {
255 checkAdjointVectorSize();
256
257 if (0 != identifier && identifier < (Identifier)adjointVector.size()) {
258 return adjointVector[identifier];
259 } else {
261 return zeroValue;
262 }
263 }
264
266 Gradient const& gradient(Identifier const& identifier) const {
267 if (0 != identifier && identifier < (Identifier)adjointVector.size()) {
268 return adjointVector[identifier];
269 } else {
270 return constZeroValue;
271 }
272 }
273
275 void setGradient(Identifier& identifier, Gradient const& gradientValue) {
276 gradient(identifier) = gradientValue;
277 }
278
280
281 private:
282
283 void checkAdjointVectorSize() {
284 if (adjointVector.size() <= Base::tape.getParameter(TapeParameters::LargestIdentifier)) {
286 }
287 }
288 };
289}
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
@ LargestIdentifier
[A: R] Largest identifier distributed by the index manger.
Implementation of VectorAccessInterface for adjoint vectors.
Definition adjointVectorAccess.hpp:59
Allows for an arbitrary adjoint evaluation of a recorded tape.
Definition customAdjointVectorHelper.hpp:152
void clearAdjoints()
Set all adjoints to zero.
Definition customAdjointVectorHelper.hpp:194
Gradient const & gradient(Identifier const &identifier) const
Get a constant reference to the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:266
Gradient & gradientUnchecked(Identifier const &identifier)
Get a reference to the gradient. Unchecked access.
Definition customAdjointVectorHelper.hpp:244
VectorAccessInterface< Real, Identifier > * getVectorInterface()
Get a new general interface to the adjoint vector.
Definition customAdjointVectorHelper.hpp:223
Gradient & gradient(Identifier const &identifier)
Get a reference to the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:254
~CustomAdjointVectorHelper()
Destructor.
Definition customAdjointVectorHelper.hpp:183
typename Tape::Position Position
See PositionalEvaluationTapeInterface.
Definition customAdjointVectorHelper.hpp:166
T_Type Type
< See CustomAdjointVectorHelper.
Definition customAdjointVectorHelper.hpp:156
Gradient const & gradientUnchecked(Identifier const &identifier) const
Get a constant reference to the gradient. Unchecked access.
Definition customAdjointVectorHelper.hpp:249
AdjointVectorAccess< Real, Identifier, Gradient > * adjointInterface
Last created adjoint interface.
Definition customAdjointVectorHelper.hpp:174
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:215
Gradient const & getGradient(Identifier const &identifier) const
Get a constant reference to the gradient.
Definition customAdjointVectorHelper.hpp:239
typename Type::Real Real
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:161
T_Gradient Gradient
See CustomAdjointVectorHelper.
Definition customAdjointVectorHelper.hpp:157
void deleteAdjointVector()
Delete the adjoint vector.
Definition customAdjointVectorHelper.hpp:201
Gradient zeroValue
Temporary zero value.
Definition customAdjointVectorHelper.hpp:171
std::vector< Gradient > adjointVector
Custom adjoint vector.
Definition customAdjointVectorHelper.hpp:169
typename Type::Tape Tape
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:165
Gradient const constZeroValue
Temporary constant zero value.
Definition customAdjointVectorHelper.hpp:172
CustomAdjointVectorHelper()
Constructor.
Definition customAdjointVectorHelper.hpp:179
typename Type::Identifier Identifier
See LhsExpressionInterface.
Definition customAdjointVectorHelper.hpp:162
void setGradient(Identifier &identifier, Gradient const &gradientValue)
Set the gradient. Checked access.
Definition customAdjointVectorHelper.hpp:275
void evaluate(Position const &start, Position const &end)
Perform a full reverse evaluation of the tape.
Definition customAdjointVectorHelper.hpp:207
General interface for an arbitrary adjoint evaluation.
Definition customAdjointVectorHelper.hpp:61
void evaluateForward()
Perform a forward evaluation of a part of the tape. It has to hold start <= end.
Definition customAdjointVectorHelper.hpp:118
CustomAdjointVectorInterface()
Constructor.
Definition customAdjointVectorHelper.hpp:81
void evaluate()
Perform a full reverse evaluation of the tape.
Definition customAdjointVectorHelper.hpp:113
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:123
virtual ~CustomAdjointVectorInterface()
Destructor.
Definition customAdjointVectorHelper.hpp:84
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 & tape
Current tape for evaluations. Default: the Type's current tape.
Definition customAdjointVectorHelper.hpp:76
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:91