CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
statementPushHelper.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 "../../traits/tapeTraits.hpp"
42
44namespace codi {
45
54 template<typename T_Type, typename T_Impl>
56 public:
57
59 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
60
63
64 using Real = typename Type::Real;
65
66 /*******************************************************************************/
69
71 void endPushStatement(Type& lhs, Real const& primal);
72
74 void pushArgument(Type const& arg, Real const& jacobian);
75
78
80 /*******************************************************************************/
83
85 template<typename ArgIter, typename JacobiIter>
86 CODI_INLINE void pushStatement(Type& lhs, Real const& primal, ArgIter const startArg, ArgIter const endArg,
87 JacobiIter const startJac) {
88 cast().startPushStatement();
89
90 JacobiIter jacPos = startJac;
91 ArgIter argPos = startArg;
92 while (argPos != endArg) {
93 cast().pushArgument(*argPos, *jacPos);
94
95 ++jacPos;
96 ++argPos;
97 }
98
99 cast().endPushStatement(lhs, primal);
100 }
101
103 template<typename ArgVector, typename JacobiVector>
104 CODI_INLINE void pushStatement(Type& lhs, Real const& primal, ArgVector const& arguments,
105 JacobiVector const& jacobians, size_t const size) {
106 cast().startPushStatement();
107
108 for (size_t i = 0; i < size; ++i) {
109 cast().pushArgument(arguments[i], jacobians[i]);
110 }
111
112 cast().endPushStatement(lhs, primal);
113 }
114
116
117 private:
118
119 CODI_INLINE Impl& cast() {
120 return static_cast<Impl&>(*this);
121 }
122 };
123
137 template<typename T_Type, typename = void>
138 struct StatementPushHelper : public StatementPushHelperBase<T_Type, StatementPushHelper<T_Type>> {
139 public:
140
142 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
143
144 using Real = typename Type::Real;
145 using Identifier = typename Type::Identifier;
147 using Tape = typename Type::Tape;
148
149 protected:
152 size_t dataPos;
153
154 public:
155
156 /*******************************************************************************/
159
162 dataPos = 0;
163 }
164
166 void pushArgument(Type const& arg, Real const& jacobian) {
167 Tape& tape = Type::getTape();
168
170 CODI_EXCEPTION("Adding more than %zu arguments to a statement.", Config::MaxArgumentSize);
171 }
172
173 if (CODI_ENABLE_CHECK(Config::CheckTapeActivity, tape.isActive())) {
174 if (CODI_ENABLE_CHECK(Config::CheckZeroIndex, 0 != arg.getIdentifier())) {
177 indexData[dataPos] = arg.getIdentifier();
178 jacobianData[dataPos] = jacobian;
179 dataPos += 1;
180 }
181 }
182 }
183 }
184 }
185
187 void endPushStatement(Type& lhs, Real const& primal) {
188 Tape& tape = Type::getTape();
189
190 if (CODI_ENABLE_CHECK(Config::CheckTapeActivity, tape.isActive())) {
191 if (0 != dataPos) {
192 tape.storeManual(primal, lhs.getIdentifier(), dataPos);
193
194 for (size_t i = 0; i < dataPos; ++i) {
195 tape.pushJacobianManual(jacobianData[i], 0.0, indexData[i]);
196 }
197 }
198 }
199
200 lhs.value() = primal;
201 }
202
204 };
205
206#ifndef DOXYGEN_DISABLE
207
209 template<typename T_Type>
210 struct StatementPushHelper<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>>
211 : public StatementPushHelperBase<T_Type, StatementPushHelper<T_Type>> {
212 public:
213
215 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
216
217 using Real = typename Type::Real;
218 using Gradient = typename Type::Gradient;
219
220 protected:
221
222 Gradient lhsTangent;
223
224 public:
225
226 /*******************************************************************************/
229
231 void startPushStatement() {
232 lhsTangent = Gradient();
233 }
234
236 void pushArgument(Type const& arg, Real const& jacobian) {
238 lhsTangent += jacobian * arg.getGradient();
239 }
240 }
241
243 void endPushStatement(Type& lhs, Real const& primal) {
244 lhs.gradient() = lhsTangent;
245 lhs.value() = primal;
246 }
247
249 };
250
252 template<>
253 struct StatementPushHelper<double, void> {
254 public:
255
256 using Type = double;
257 using Real = double;
258
259 /*******************************************************************************/
262
264 void startPushStatement() {}
265
267 void pushArgument(Type const& arg, Real const& jacobian) {
268 CODI_UNUSED(arg, jacobian);
269 }
270
272 void endPushStatement(Type& lhs, Real const& primal) {
273 lhs = primal;
274 }
275
278 template<typename ArgIter, typename JacobiIter>
279 void pushStatement(Type& lhs, Real const& primal, ArgIter const startArg, ArgIter const endArg,
280 JacobiIter const startJac) {
281 CODI_UNUSED(startArg, endArg, startJac);
282
283 endPushStatement(lhs, primal);
284 }
285
288 template<typename ArgVector, typename JacobiVector>
289 void pushStatement(Type& lhs, Real const& primal, ArgVector const& arguments, JacobiVector const& jacobians,
290 size_t const size) {
291 CODI_UNUSED(arguments, jacobians, size);
292
293 endPushStatement(lhs, primal);
294 }
295
297 };
298#endif
299}
#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
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:53
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
bool constexpr CheckZeroIndex
Ignore active types that are not dependent on any input value in Jacobian tapes.
Definition config.h:178
bool constexpr IgnoreInvalidJacobians
Ignore invalid Jacobians like NaN or Inf.
Definition config.h:240
bool constexpr CheckTapeActivity
Makes it possible to ignore certain code parts. If turned of everything will be recorded.
Definition config.h:170
bool constexpr CheckJacobianIsZero
Ignore Jacobians that are zero in Jacobian based tapes.
Definition config.h:162
size_t constexpr MaxArgumentSize
Maximum number of arguments in a statement.
Definition config.h:120
bool isTotalFinite(Type const &v)
Function for checking if all values of the type are finite.
Definition realTraits.hpp:133
bool isTotalZero(Type const &v)
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:139
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
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Gradient getGradient() const
Get the gradient of this lvalue from the tape.
Definition lhsExpressionInterface.hpp:115
Base class for manual statement pushes on the tape.
Definition statementPushHelper.hpp:55
T_Type Type
See StatementPushHelperBase.
Definition statementPushHelper.hpp:59
void pushArgument(Type const &arg, Real const &jacobian)
Add the Jacobian of an argument of the statement.
typename Type::Real Real
See LhsExpressionInterface.
Definition statementPushHelper.hpp:64
T_Impl Impl
See StatementPushHelperBase.
Definition statementPushHelper.hpp:62
void startPushStatement()
Initialize all data for the push of a statement.
void pushStatement(Type &lhs, Real const &primal, ArgIter const startArg, ArgIter const endArg, JacobiIter const startJac)
Push a complete statement where the Jacobians and arguments are provided as iterator objects.
Definition statementPushHelper.hpp:86
void endPushStatement(Type &lhs, Real const &primal)
Finish the push of a statement. Performs lhs = primal and cleans up all data.
void pushStatement(Type &lhs, Real const &primal, ArgVector const &arguments, JacobiVector const &jacobians, size_t const size)
Push a complete statement where the Jacobians and arguments are provided as arrays.
Definition statementPushHelper.hpp:104
Add statements to the tape where the Jacobians are computed manually.
Definition statementPushHelper.hpp:138
void endPushStatement(Type &lhs, Real const &primal)
Finish the push of a statement. Performs lhs = primal and cleans up all data.
Definition statementPushHelper.hpp:187
T_Type Type
< See StatementPushHelper.
Definition statementPushHelper.hpp:142
void startPushStatement()
Initialize all data for the push of a statement.
Definition statementPushHelper.hpp:161
size_t dataPos
Current number of arguments.
Definition statementPushHelper.hpp:152
void pushArgument(Type const &arg, Real const &jacobian)
Add the Jacobian of an argument of the statement.
Definition statementPushHelper.hpp:166
typename Type::Identifier Identifier
Definition statementPushHelper.hpp:145
Identifier indexData[Config::MaxArgumentSize]
Storage for the identifiers of the arguments.
Definition statementPushHelper.hpp:150
typename Type::Tape Tape
See LhsExpressionInterface.
Definition statementPushHelper.hpp:147
Real jacobianData[Config::MaxArgumentSize]
Storage for the Jacobians of the arguments.
Definition statementPushHelper.hpp:151
typename Type::Real Real
See LhsExpressionInterface.
Definition statementPushHelper.hpp:144