CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
lhsExpressionInterface.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2025 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 "../misc/eventSystem.hpp"
39#include "../misc/macros.hpp"
40#include "../misc/toConst.hpp"
41#include "../tapes/interfaces/fullTapeInterface.hpp"
42#include "../traits/expressionTraits.hpp"
43#include "../traits/realTraits.hpp"
44#include "expressionInterface.hpp"
45
47namespace codi {
48
62 template<typename T_Real, typename T_Gradient, typename T_Tape, typename T_Impl>
63 struct LhsExpressionInterface : public ExpressionInterface<T_Real, T_Impl> {
64 public:
65
66 using Real = CODI_DD(T_Real, double);
67 using Gradient = CODI_DD(T_Gradient, Real);
68 using Tape = CODI_DD(T_Tape, CODI_DEFAULT_TAPE);
70
72
73 using Identifier = typename Tape::Identifier;
75
78
79 /*******************************************************************************/
82
83 Real const& value() const;
85
86 Identifier const& getIdentifier() const;
90
91 static Tape& getTape();
92
94 /*******************************************************************************/
97
100 return static_cast<Impl&>(*this);
101 }
102 using Base::cast;
103
106 return Impl::getTape().gradient(cast().getIdentifier());
107 }
108
111 return toConst(Impl::getTape()).gradient(cast().getIdentifier());
112 }
116 return cast().gradient();
117 }
118
121 cast().gradient() = g;
122 }
123
125 CODI_INLINE Real const& getValue() const {
126 return cast().value();
127 }
128
130 CODI_INLINE void setValue(Real const& v) {
131 cast().value() = v;
132 }
133
137 rhs, EventHints::Statement::Passive);
138 Impl::getTape().store(cast(), rhs);
139 return cast();
140 }
143 template<typename U = Real, typename = RealTraits::EnableIfNotPassiveReal<U>>
146 rhs, EventHints::Statement::Passive);
147 Impl::getTape().store(cast(), rhs);
148 return cast();
149 }
150
152 template<typename Rhs>
155 rhs.cast().getValue(), EventHints::Statement::Expression);
156 Impl::getTape().store(cast(), rhs.cast());
157 return cast();
158 }
159
161 template<typename Rhs, typename U = Real, typename = RealTraits::EnableIfNotPassiveReal<U>>
164 rhs.cast().getValue(), EventHints::Statement::Passive);
165 Impl::getTape().store(cast(), Real(rhs));
166 return cast();
167 }
168
172 rhs.cast().getValue(), EventHints::Statement::Copy);
173 Impl::getTape().store(cast(), rhs);
174 return cast();
175 }
176
178 template<typename Rhs>
181 rhs.cast().getValue(), EventHints::Statement::Copy);
182 Impl::getTape().store(cast(), rhs);
183 return cast();
184 }
185
187 /*******************************************************************************/
190
191 static size_t constexpr LinkCount = 0;
192
193 protected:
194
198 CODI_INLINE void init(Real const& newValue, EventHints::Statement statementType) {
199 Impl::getTape().initIdentifier(cast().value(), cast().getIdentifier());
201 statementType);
202 }
203
208 Impl::getTape().destroyIdentifier(cast().value(), cast().getIdentifier());
209 }
210
212 };
213
215 template<typename Expr>
217 typename Expr::Real temp;
218
219 stream >> temp;
220 v.setValue(temp);
221
222 return stream;
223 }
224
225#ifndef DOXYGEN_DISABLE
226
228 template<typename T_Type>
229 struct RealTraits::DataExtraction<T_Type, ExpressionTraits::EnableIfLhsExpression<T_Type>> {
230 public:
231 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
232
233 using Real = typename Type::Real;
234 using Identifier = typename Type::Identifier;
235
237 CODI_INLINE static Real getValue(Type const& v) {
238 return v.getValue();
239 }
240
242 CODI_INLINE static Identifier getIdentifier(Type const& v) {
243 return v.getIdentifier();
244 }
245
247 CODI_INLINE static void setValue(Type& v, Real const& value) {
248 v.setValue(value);
249 }
250
252 CODI_INLINE static void setIdentifier(Type& v, Identifier const& identifier) {
253 v.getIdentifier() = identifier;
254 }
255 };
256
258 template<typename T_Type>
260 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
261
262 using Real = typename DataExtraction<Type>::Real;
263
265 CODI_INLINE static void registerInput(Type& v) {
266 Type::getTape().registerInput(v);
267 }
268
270 CODI_INLINE static void registerOutput(Type& v) {
271 Type::getTape().registerOutput(v);
272 }
273
276 return Type::getTape().registerExternalFunctionOutput(v);
277 }
278 };
279
281 template<typename T_Type>
283 : RealTraits::ArrayAggregatedTypeTraitsBase<T_Type, T_Type, typename T_Type::Real, 1> {
284 public:
285
287 static CODI_INLINE std::string getMathRep() {
288 return "()";
289 }
290 };
291#endif
292}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:96
Statement
Classify statements.
Definition eventSystem.hpp:65
Traits for everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:65
typename std::enable_if< IsLhsExpression< Expr >::value, T >::type EnableIfLhsExpression
Enable if wrapper for IsLhsExpression.
Definition expressionTraits.hpp:183
typename TraitsImplementation< Type >::PassiveReal PassiveReal
The original computation type that was used in the application.
Definition realTraits.hpp:123
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
ExpressionTraits::EnableIfLhsExpression< Expr, std::istream > & operator>>(std::istream &stream, Expr &v)
Read the primal value from a stream.
Definition lhsExpressionInterface.hpp:216
inlineT const toConst(T &&v)
Constant cast function that works with CUDA.
Definition toConst.hpp:47
static inlinevoid notifyStatementPrimalListeners(Tape &tape, Real const &lhsValue, Identifier const &lhsIdentifier, Real const &newValue, EventHints::Statement statement)
Invoke callbacks for StatementPrimal events.
Definition eventSystem.hpp:264
ExpressionInterface()=default
Constructor.
inlineImpl const & cast() const
Cast to the implementation.
Definition expressionInterface.hpp:76
Real & value()
Get a reference to the lvalue represented by the expression.
inlineImpl & operator=(Real const &rhs)
Assignment operator for passive values. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:135
LhsExpressionInterface(LhsExpressionInterface const &other)=default
Constructor.
inlineGradient & gradient()
Get the gradient of this lvalue from the tape.
Definition lhsExpressionInterface.hpp:105
inlinevoid setGradient(Gradient const &g)
Set the gradient of this lvalue in the tape.
Definition lhsExpressionInterface.hpp:120
Real const & value() const
Get a constant reference to the lvalue represented by the expression.
inlineImpl & operator=(LhsExpressionInterface< Real, Gradient, Tape, Rhs > const &rhs)
Assignment operator for lhs expressions. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:179
static size_t constexpr LinkCount
Definition lhsExpressionInterface.hpp:191
RealTraits::PassiveReal< Real > PassiveReal
Definition lhsExpressionInterface.hpp:74
typename Tape::Identifier Identifier
Definition lhsExpressionInterface.hpp:73
inlineImpl & operator=(LhsExpressionInterface const &rhs)
Assignment operator for lhs expressions. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:170
Real Real
Definition lhsExpressionInterface.hpp:66
LhsExpressionInterface()=default
Constructor.
T_Tape Tape
Definition lhsExpressionInterface.hpp:68
inlineImpl & operator=(ExpressionInterface< Real, Rhs > const &rhs)
Assignment operator for expressions. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:153
ExpressionInterface< Real, T_Impl > Base
Definition lhsExpressionInterface.hpp:71
inlineGradient const & gradient() const
Get the gradient of this lvalue from the tape.
Definition lhsExpressionInterface.hpp:110
inlinevoid init(Real const &newValue, EventHints::Statement statementType)
Definition lhsExpressionInterface.hpp:198
Identifier const & getIdentifier() const
inlineImpl & operator=(PassiveReal const &rhs)
Assignment operator for passive values. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:144
inlineGradient getGradient() const
Get the gradient of this lvalue from the tape.
Definition lhsExpressionInterface.hpp:115
Gradient Gradient
Definition lhsExpressionInterface.hpp:67
static Tape & getTape()
Get a reference to the tape which manages this expression.
inlinevoid setValue(Real const &v)
Set the primal value of this lvalue.
Definition lhsExpressionInterface.hpp:130
inlineReal const & getValue() const
Get the primal value of this lvalue.
Definition lhsExpressionInterface.hpp:125
T_Impl Impl
Definition lhsExpressionInterface.hpp:69
inlineImpl & cast()
Cast to the implementation.
Definition lhsExpressionInterface.hpp:99
inlineImpl & operator=(ExpressionInterface< typename U::Real, Rhs > const &rhs)
Assignment operator for expressions. Calls store on the InternalStatementRecordingTapeInterface.
Definition lhsExpressionInterface.hpp:162
inlinevoid destroy()
Definition lhsExpressionInterface.hpp:207
Methods that access inner values of aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:226
static inlinestd::string getMathRep()
Get the math symbol of the operation. E.g. + for operators and pow() for functions.
Definition realTraits.hpp:291
Base implementation of AggregatedTypeTraits for aggregated types that can be defined as an array.
Definition realTraits.hpp:301
inlinestatic void setValue(Type &v, Real const &value)
Set the primal values of an aggregate of active types.
inlinestatic Identifier getIdentifier(Type const &v)
Extract an aggregate of identifiers from an aggregate of active types.
typename Type::Real Real
Type of a corresponding aggregate of primal values.
Definition realTraits.hpp:171
inlinestatic Real getValue(Type const &v)
Extract an aggregate of primal values from an aggregate of active types.
typename Type::Identifier Identifier
Type of a corresponding aggregate of identifiers.
Definition realTraits.hpp:172
inlinestatic void setIdentifier(Type &v, Identifier const &identifier)
Set the identifiers of a type of aggregated active types.
T_Type Type
See DataExtraction.
Definition realTraits.hpp:169
Tape registration methods for aggregated types that contain CoDiPack active types.
Definition realTraits.hpp:197
T_Type Type
See TapeRegistration.
Definition realTraits.hpp:202
inlinestatic void registerInput(Type &v)
Register all active types of an aggregated type as tape inputs.
inlinestatic void registerOutput(Type &v)
Register all active types of an aggregated type as tape outputs.
typename DataExtraction< Type >::Real Real
See DataExtraction::Real.
Definition realTraits.hpp:204
inlinestatic Real registerExternalFunctionOutput(Type &v)
Register all active types of an aggregated type as external function outputs.