CoDiPack  3.1.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-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 "../../misc/macros.hpp"
39#include "../../tapes/interfaces/reverseTapeInterface.hpp"
40#include "../../traits/expressionTraits.hpp"
41#include "../computeExpression.hpp"
42#include "../constantExpression.hpp"
43#include "../emptyExpression.hpp"
44#include "../expressionInterface.hpp"
45#include "../static/staticContextActiveType.hpp"
46#include "nodeInterface.hpp"
47
49namespace codi {
50
69 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset, typename = void>
71 public:
72
74 using Tape = CODI_DD(
76 static constexpr size_t primalValueOffset =
77 CODI_DD(T_primalValueOffset, 0);
78 static constexpr size_t constantValueOffset =
79 CODI_DD(T_constantValueOffset, 0);
80
81 using Real = typename Tape::Real;
82 using Identifier = typename Tape::Identifier;
83 using PassiveReal = typename Tape::PassiveReal;
84
87
93 static ResultType construct(Real* primalVector, Identifier const* const identifiers,
94 PassiveReal const* const constantData);
95 };
96
97#ifndef DOXYGEN_DISABLE
98
99 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset>
100 struct ConstructStaticContextLogic<T_Rhs, T_Tape, T_primalValueOffset, T_constantValueOffset,
101 ExpressionTraits::EnableIfLhsExpression<T_Rhs>> {
102 public:
103
104 using Rhs = T_Rhs;
105 using Tape = T_Tape;
106 static constexpr size_t primalValueOffset = T_primalValueOffset;
107 static constexpr size_t constantValueOffset = T_constantValueOffset;
108
109 using Real = typename Tape::Real;
110 using Identifier = typename Tape::Identifier;
111 using PassiveReal = typename Tape::PassiveReal;
112
115
117 CODI_INLINE static ResultType construct(Real* primalVector, Identifier const* const identifiers,
118 PassiveReal const* const constantData) {
119 CODI_UNUSED(constantData);
120
121 Identifier const identifier = identifiers[primalValueOffset];
122 Real const primal = primalVector[identifier];
123
124 return ResultType(primal, identifier);
125 }
126 };
127
128 template<typename T_Rhs, typename T_Tape, size_t T_primalValueOffset, size_t T_constantValueOffset>
129 struct ConstructStaticContextLogic<T_Rhs, T_Tape, T_primalValueOffset, T_constantValueOffset,
131 public:
132
133 using Rhs = T_Rhs;
134 using Tape = T_Tape;
135 static constexpr size_t primalValueOffset = T_primalValueOffset;
136 static constexpr size_t constantValueOffset = T_constantValueOffset;
137
138 using Real = typename Rhs::Real;
139 using InnerReal = typename Tape::Real;
140 using Identifier = typename Tape::Identifier;
141 using PassiveInnerReal = typename Tape::PassiveReal;
142
144 using ResultType = ConstantExpression<Real>;
145
147 CODI_INLINE static ResultType construct(InnerReal* primalVector, Identifier const* const identifiers,
148 PassiveInnerReal const* const constantData) {
149 CODI_UNUSED(primalVector, identifiers);
150
151 using ConversionOperator = typename Rhs::template ConversionOperator<PassiveInnerReal>;
152 using AggregateTraits = RealTraits::AggregatedTypeTraits<Real>;
153
154 Real value{};
156 AggregateTraits::template arrayAccess<i.value>(value) =
157 ConversionOperator::fromDataStore(constantData[constantValueOffset + i.value]);
158 });
159
160 return ResultType(value);
161 }
162 };
163
164 template<typename T_Real, template<typename> class T_Operation, typename T_Tape, size_t T_primalValueOffset,
165 size_t T_constantValueOffset, typename... T_Args>
166 struct ConstructStaticContextLogic<ComputeExpression<T_Real, T_Operation, T_Args...>, T_Tape, T_primalValueOffset,
167 T_constantValueOffset> {
168 public:
169
170 using OpReal = T_Real;
171 template<typename T>
172 using Operation = T_Operation<T>;
173 using Tape = T_Tape;
174 static constexpr size_t primalValueOffset = T_primalValueOffset;
175 static constexpr size_t constantValueOffset = T_constantValueOffset;
176 using Args = std::tuple<T_Args...>;
177
178 using Real = typename Tape::Real;
179 using Identifier = typename Tape::Identifier;
180 using PassiveReal = typename Tape::PassiveReal;
181
182 // Recursively compute the primal value offset. The template parameter T prevents a full template
183 // specialization.
184 template<typename T, size_t ArgNum>
185 struct PrimalValueOffsetForArg
186 : public std::integral_constant<size_t, PrimalValueOffsetForArg<T, ArgNum - 1>::value +
187 ExpressionTraits::NumberOfActiveTypeArguments<
188 typename std::tuple_element<ArgNum - 1, Args>::type>::value> {
189 };
190
191 // Primal value offset for the first argument. Here we return the global offset of the construction logic.
192 template<typename T>
193 struct PrimalValueOffsetForArg<T, 0> : public std::integral_constant<size_t, primalValueOffset> {};
194
195 // Recursively compute the constant value offset. The template parameter T prevents a full template
196 // specialization.
197 template<typename T, size_t ArgNum>
198 struct ConstantValueOffsetForArg
199 : public std::integral_constant<size_t, ConstantValueOffsetForArg<T, ArgNum - 1>::value +
200 ExpressionTraits::NumberOfConstantTypeArguments<
201 typename std::tuple_element<ArgNum - 1, Args>::type>::value> {
202 };
203
204 // Constant value offset for the first argument. Here we return the global offset of the construction logic.
205 template<typename T>
206 struct ConstantValueOffsetForArg<T, 0> : public std::integral_constant<size_t, constantValueOffset> {};
207
208 // Static context constructor for the given argument.
209 template<size_t ArgNum>
210 using ArgConstructor = ConstructStaticContextLogic<std::tuple_element_t<ArgNum, Args>, Tape,
211 PrimalValueOffsetForArg<double, ArgNum>::value,
212 ConstantValueOffsetForArg<double, ArgNum>::value>;
213
214 // Modified argument type for the result expression.
215 template<size_t ArgNum>
216 using ArgMod = typename ArgConstructor<ArgNum>::ResultType;
217
218 // Helper for ResultType definition. This allows us to expand on the index sequence Is.
219 template<size_t... Is>
220 static ComputeExpression<OpReal, T_Operation /* Make clang happy */, ArgMod<Is>...> ResultTypeHelper(
221 std::index_sequence<Is...>);
222
223 // Definition of the return type as the return value of ResultTypeHelper.
224 using ResultType = remove_all<decltype(ResultTypeHelper(std::index_sequence_for<T_Args...>()))>;
225
226 // Helper for construct definition. This allows us to expand on the index sequence Is.
227 template<size_t... Is>
228 CODI_INLINE static ResultType constructHelper(Real* primalVector, Identifier const* const identifiers,
229 PassiveReal const* const constantData, std::index_sequence<Is...>) {
230 CODI_UNUSED(primalVector, identifiers, constantData); // Required for empty sequence Is
231 return ResultType(ArgConstructor<Is>::construct(primalVector, identifiers, constantData)...);
232 }
233
234 CODI_INLINE static ResultType construct(Real* primalVector, Identifier const* const identifiers,
235 PassiveReal const* const constantData) {
236 return constructHelper(primalVector, identifiers, constantData, std::index_sequence_for<T_Args...>());
237 }
238 };
239#endif
240}
#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:97
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
Traits for everything that can be an expression e.g. codi::RealReverse, a + b, etc....
Definition expressionTraits.hpp:71
typename std::enable_if< IsConstantExpression< Expr >::value, T >::type EnableIfConstantExpression
Enable if wrapper for IsConstantExpression.
Definition expressionTraits.hpp:215
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
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:55
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:70
T_Rhs ResultType
The resulting expression type after all nodes are replaced.
Definition constructStaticContext.hpp:86
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:83
T_Rhs Rhs
See ConstructStaticContextLogic.
Definition constructStaticContext.hpp:73
typename Tape::Real Real
See TapeTypesInterface.
Definition constructStaticContext.hpp:81
static constexpr size_t constantValueOffset
Definition constructStaticContext.hpp:78
typename Tape::Identifier Identifier
See TapeTypesInterface.
Definition constructStaticContext.hpp:82
static constexpr size_t primalValueOffset
Definition constructStaticContext.hpp:76
T_Tape Tape
See ConstructStaticContextLogic.
Definition constructStaticContext.hpp:74
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