CoDiPack  3.0.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
constructStaticContext.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/macros.hpp"
39#include "../../tapes/interfaces/reverseTapeInterface.hpp"
40#include "../../traits/expressionTraits.hpp"
41#include "../computeExpression.hpp"
42#include "../constantExpression.hpp"
43#include "../expressionInterface.hpp"
44#include "../static/staticContextActiveType.hpp"
45#include "nodeInterface.hpp"
46
48namespace codi {
49
68 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset, typename = void>
70 public:
71
73 using Tape = CODI_DD(
75 static constexpr size_t primalValueOffset =
76 CODI_DD(T_primalValueOffset, 0);
77 static constexpr size_t constantValueOffset =
78 CODI_DD(T_constantValueOffset, 0);
79
80 using Real = typename Tape::Real;
81 using Identifier = typename Tape::Identifier;
82 using PassiveReal = typename Tape::PassiveReal;
83
86
92 static ResultType construct(Real* primalVector, Identifier const* const identifiers,
93 PassiveReal const* const constantData);
94 };
95
96#ifndef DOXYGEN_DISABLE
97
98 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset>
99 struct ConstructStaticContextLogic<T_Rhs, T_Tape, T_primalValueOffset, T_constantValueOffset,
100 ExpressionTraits::EnableIfLhsExpression<T_Rhs>> {
101 public:
102
103 using Rhs = T_Rhs;
104 using Tape = T_Tape;
105 static constexpr size_t primalValueOffset = T_primalValueOffset;
106 static constexpr size_t constantValueOffset = T_constantValueOffset;
107
108 using Real = typename Tape::Real;
109 using Identifier = typename Tape::Identifier;
110 using PassiveReal = typename Tape::PassiveReal;
111
114
116 CODI_INLINE static ResultType construct(Real* primalVector, Identifier const* const identifiers,
117 PassiveReal const* const constantData) {
118 CODI_UNUSED(constantData);
119
120 Identifier const identifier = identifiers[primalValueOffset];
121 Real const primal = primalVector[identifier];
122
123 return ResultType(primal, identifier);
124 }
125 };
126
127 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset>
128 struct ConstructStaticContextLogic<T_Rhs, T_Tape, T_primalValueOffset, T_constantValueOffset,
130 public:
131
132 using Rhs = T_Rhs;
133 using Tape = T_Tape;
134 static constexpr size_t primalValueOffset = T_primalValueOffset;
135 static constexpr size_t constantValueOffset = T_constantValueOffset;
136
137 using Real = typename Rhs::Real;
138 using InnerReal = typename Tape::Real;
139 using Identifier = typename Tape::Identifier;
140 using PassiveInnerReal = typename Tape::PassiveReal;
141
143 using ResultType = ConstantExpression<Real>;
144
146 CODI_INLINE static ResultType construct(InnerReal* primalVector, Identifier const* const identifiers,
147 PassiveInnerReal const* const constantData) {
148 CODI_UNUSED(primalVector, identifiers);
149
150 using ConversionOperator = typename Rhs::template ConversionOperator<PassiveInnerReal>;
151 using AggregateTraits = RealTraits::AggregatedTypeTraits<Real>;
152
153 Real value{};
155 AggregateTraits::template arrayAccess<i.value>(value) =
156 ConversionOperator::fromDataStore(constantData[constantValueOffset + i.value]);
157 });
158
159 return ResultType(value);
160 }
161 };
162
163 template<typename T_Real, template<typename> class T_Operation, typename T_Tape, size_t T_primalValueOffset,
164 size_t T_constantValueOffset, typename... T_Args>
165 struct ConstructStaticContextLogic<ComputeExpression<T_Real, T_Operation, T_Args...>, T_Tape, T_primalValueOffset,
166 T_constantValueOffset> {
167 public:
168
169 using OpReal = T_Real;
170 template<typename T>
171 using Operation = T_Operation<T>;
172 using Tape = T_Tape;
173 static constexpr size_t primalValueOffset = T_primalValueOffset;
174 static constexpr size_t constantValueOffset = T_constantValueOffset;
175 using Args = std::tuple<T_Args...>;
176
177 using Real = typename Tape::Real;
178 using Identifier = typename Tape::Identifier;
179 using PassiveReal = typename Tape::PassiveReal;
180
181 // Recursively compute the primal value offset. The template parameter T prevents a full template
182 // specialization.
183 template<typename T, size_t ArgNum>
184 struct PrimalValueOffsetForArg
185 : public std::integral_constant<size_t, PrimalValueOffsetForArg<T, ArgNum - 1>::value +
186 ExpressionTraits::NumberOfActiveTypeArguments<
187 typename std::tuple_element<ArgNum - 1, Args>::type>::value> {
188 };
189
190 // Primal value offset for the first argument. Here we return the global offset of the construction logic.
191 template<typename T>
192 struct PrimalValueOffsetForArg<T, 0> : public std::integral_constant<size_t, primalValueOffset> {};
193
194 // Recursively compute the constant value offset. The template parameter T prevents a full template
195 // specialization.
196 template<typename T, size_t ArgNum>
197 struct ConstantValueOffsetForArg
198 : public std::integral_constant<size_t, ConstantValueOffsetForArg<T, ArgNum - 1>::value +
199 ExpressionTraits::NumberOfConstantTypeArguments<
200 typename std::tuple_element<ArgNum - 1, Args>::type>::value> {
201 };
202
203 // Constant value offset for the first argument. Here we return the global offset of the construction logic.
204 template<typename T>
205 struct ConstantValueOffsetForArg<T, 0> : public std::integral_constant<size_t, constantValueOffset> {};
206
207 // Static context constructor for the given argument.
208 template<size_t ArgNum>
209 using ArgConstructor = ConstructStaticContextLogic<std::tuple_element_t<ArgNum, Args>, Tape,
210 PrimalValueOffsetForArg<double, ArgNum>::value,
211 ConstantValueOffsetForArg<double, ArgNum>::value>;
212
213 // Modified argument type for the result expression.
214 template<size_t ArgNum>
215 using ArgMod = typename ArgConstructor<ArgNum>::ResultType;
216
217 // Helper for ResultType definition. This allows us to expand on the index sequence Is.
218 template<size_t... Is>
219 static ComputeExpression<OpReal, Operation, ArgMod<Is>...> ResultTypeHelper(std::index_sequence<Is...>);
220
221 // Definition of the return type as the return value of ResultTypeHelper.
222 using ResultType = remove_all<decltype(ResultTypeHelper(std::index_sequence_for<T_Args...>()))>;
223
224 // Helper for construct definition. This allows us to expand on the index sequence Is.
225 template<size_t... Is>
226 CODI_INLINE static ResultType constructHelper(Real* primalVector, Identifier const* const identifiers,
227 PassiveReal const* const constantData, std::index_sequence<Is...>) {
228 return ResultType(ArgConstructor<Is>::construct(primalVector, identifiers, constantData)...);
229 }
230
231 CODI_INLINE static ResultType construct(Real* primalVector, Identifier const* const identifiers,
232 PassiveReal const* const constantData) {
233 return constructHelper(primalVector, identifiers, constantData, std::index_sequence_for<T_Args...>());
234 }
235 };
236#endif
237}
#define CODI_LAMBDA_INLINE
See codi::Config::ForcedInlines.
Definition config.h:473
#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
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:116
Traits for everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:65
typename std::enable_if< IsConstantExpression< Expr >::value, T >::type EnableIfConstantExpression
Enable if wrapper for IsConstantExpression.
Definition expressionTraits.hpp:200
CoDiPack - Code Differentiation Package.
Definition codi.hpp:94
inlinevoid static_for(F func, Args &&... args)
Static for with i = 0 .. (N - 1). See CompileTimeLoop for details.
Definition compileTimeLoop.hpp:110
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:54
typename std::remove_cv< typename std::remove_reference< typename std::remove_cv< T >::type >::type >::type remove_all
Removes all reference, constant and volatile modifiers.
Definition removeAll.hpp:47
Represents an operator or function with an arbitrary number of arguments in the expression tree.
Definition computeExpression.hpp:371
Helper class for the construction of an expression in a different context.
Definition constructStaticContext.hpp:69
T_Rhs ResultType
The resulting expression type after all nodes are replaced.
Definition constructStaticContext.hpp:85
static ResultType construct(Real *primalVector, Identifier const *const identifiers, PassiveReal const *const constantData)
Perform the construction.
typename Tape::PassiveReal PassiveReal
Basic computation type.
Definition constructStaticContext.hpp:82
T_Rhs Rhs
See ConstructStaticContextLogic.
Definition constructStaticContext.hpp:72
typename Tape::Real Real
See TapeTypesInterface.
Definition constructStaticContext.hpp:80
static constexpr size_t constantValueOffset
Definition constructStaticContext.hpp:77
typename Tape::Identifier Identifier
See TapeTypesInterface.
Definition constructStaticContext.hpp:81
static constexpr size_t primalValueOffset
Definition constructStaticContext.hpp:75
T_Tape Tape
See ConstructStaticContextLogic.
Definition constructStaticContext.hpp:73
Base class for all CoDiPack expressions.
Definition expressionInterface.hpp:60
Minimum tape interface for a working reverse tape implementation.
Definition reverseTapeInterface.hpp:78
Replacement type of LhsExpressionInterface types in ConstructStaticContext.
Definition staticContextActiveType.hpp:56