DUNE-ACFEM (unstable)

optimizationbase.hh
1#ifndef __DUNE_ACFEM_EXPRESSIONS_OPTIMIZATIONBASE_HH__
2#define __DUNE_ACFEM_EXPRESSIONS_OPTIMIZATIONBASE_HH__
3
4#include "policy.hh"
5#include "storage.hh"
6#include "operationtraits.hh"
7#include "optimizationprofile.hh"
8
9namespace Dune
10{
11
12 namespace ACFem
13 {
14
15 namespace Expressions
16 {
17
39 template<std::size_t N>
41 : OptimizeTag<N-1>
42 {
43 static constexpr std::size_t level_ = N;
44 };
45
46 template<>
47 struct OptimizeTag<0>
48 {
49 static constexpr std::size_t level_ = 0;
50 };
51
52 template<class Tag, std::size_t Incr = 1>
53 using OptimizeNext = OptimizeTag<Tag::level_ - Incr>;
54
55 template<class Tag, std::size_t Incr = 1>
56 using OptimizePrior = OptimizeTag<Tag::level_ + Incr>;
57
60 using Optimize2nd = OptimizeTag<Policy::OptimizationLevelMax::value-1>;
61 using Optimize3rd = OptimizeTag<Policy::OptimizationLevelMax::value-2>;
62
68 //using OptimizeBlock = OptimizeTop;
69
72
75
76/*
77
78Terminal
79Einsum
80Distributivity
81Factorize
82AllAll
83
841->2, Terminal
852->1 ScalarEinsum, *
86 Optimizations are tried with OptimizeTop
873->5 DistributiveOperation *
88 f() is called with OptimizeTop
89
90AllAll
91Explode
92
93
944->5 Factorize. +
95 Does a full explode and computes a "good" factorization.
96
97 Final step of sumUp: should avoid factorize.
985->6 SumAllAll, +
99
100 Must avoid to recurse into Factorize. ScalarEinsum and
101 Distributivity optimization do not play a role
102
103 Final step of sumUp: Could recurse into Factorize, if so:
104 Explode-Tag must come afterwards
105
1066->7 Terminal again?
1077->8 generic
1088...1 Other
1090 Just operate
110
111*/
112
129
130 template<std::size_t N>
131 constexpr auto optimizeNext(OptimizeTag<N> = OptimizeTag<N>{})
132 {
133 return OptimizeTag<N-1>{};
134 }
135
142#if 0
143 template<class F, class... T, std::enable_if_t<IsFunctor<F>::value, int> = 0>
144 constexpr decltype(auto) operate(DontOptimize, F&& f, T&&... t)
145 {
146 static_assert(IsFunctor<F>::value, "F is not an expression functor.");
147 return std::forward<F>(f)(std::forward<T>(t)...);
148 }
149
150 template<class Operation, class... T>
151 constexpr decltype(auto) operate(DontOptimize, T&&... t)
152 {
153 return F<Operation>{}(std::forward<T>(t)...);
154 }
155#endif
156
157 template<class T>
158 constexpr decltype(auto) operate(DontOptimize, OperationTraits<IdentityOperation>, T&& t)
159 {
160 DUNE_ACFEM_RECORD_OPTIMIZATION;
161
162 return forwardReturnValue<T>(t);
163 }
164
165 template<class T>
166 constexpr decltype(auto) operate(DontOptimize, OperationTraits<PlusOperation>, T&& t)
167 {
168 DUNE_ACFEM_RECORD_OPTIMIZATION;
169
170 return forwardReturnValue<T>(t);
171 }
172
176 template<class F, class... T, std::enable_if_t<IsFunctor<F>::value, int> = 0>
177 constexpr decltype(auto) operate(F&& f, T&&... t)
178 {
179#if DUNE_ACFEM_TRACE_OPTIMIZATION
180 std::clog << "<<<<<<<< OptmizeTop <<<<<<<<<" << std::endl;
181#endif
182 return operate(OptimizeTop{}, std::forward<F>(f), std::forward<T>(t)...);
183 }
184
185 template<class Operation, class... T>
186 constexpr decltype(auto) operate(T&&... t)
187 {
188#if DUNE_ACFEM_TRACE_OPTIMIZATION
189 std::clog << "<<<<<<<< OptmizeTop <<<<<<<<<" << std::endl;
190#endif
191 return operate(OptimizeTop{}, F<Operation>{}, std::forward<T>(t)...);
192 }
193
194 namespace {
195
196 template<class Optimize, class T, std::size_t... I>
197 constexpr decltype(auto) evaluateExpander(Optimize, T&& t, IndexSequence<I...>)
198 {
199 return operate(Optimize{}, std::forward<T>(t).operation(), std::forward<T>(t).template operand<I>()...);
200 }
201
202 } // anonymous::
203
205 template<class Optimize, class T>
206 constexpr decltype(auto) evaluate(Optimize, T&& t)
207 {
208 return evaluateExpander(Optimize{}, std::forward<T>(t), MakeIndexSequence<arity<T>()>{});
209 }
210
212 template<class T>
213 constexpr decltype(auto) evaluate(T&& t)
214 {
215 return evaluate(OptimizeTop{}, std::forward<T>(t));
216 }
217
218 template<std::size_t N, class F, class... T>
219 constexpr decltype(auto) finalize(OptimizeTag<N>, F&& f, T&&... t)
220 {
221 return expressionClosure(
222 operate(
223 OptimizeTag<N>{}, std::forward<F>(f), std::forward<T>(t)...
224 )
225 );
226 }
227
228 template<class F, class... T,
229 std::enable_if_t<IsFunctor<F>::value, int> = 0>
230 constexpr decltype(auto) finalize(F&& f, T&&... t)
231 {
232 return expressionClosure(
233 operate(
234 std::forward<F>(f), std::forward<T>(t)...
235 )
236 );
237 }
238
239 template<class Operation, class... T>
240 constexpr decltype(auto) finalize(T&&... t)
241 {
242 return expressionClosure(
243 operate<Operation>(std::forward<T>(t)...)
244 );
245 }
246
254 template<class OptOrF, class... Rest>
256 decltype(operate(std::declval<OptOrF>(), std::declval<Rest>()...));
257
259 template<class T>
261 decltype(evaluate(std::declval<T>()));
262
264
266
267 } // Expressions
268
270
271 } // ACFem
272
273} // Dune
274
275#endif // __DUNE_ACFEM_EXPRESSIONS_OPTIMIZATIONBASE_HH__
constexpr decltype(auto) expressionClosure(T &&t)
Do-nothing default implementation for pathologic cases.
Definition: interface.hh:93
OptimizeTag< Policy::OptimizationLevelMax::value > OptimizeTop
The top-level optmization tag.
Definition: optimizationbase.hh:59
decltype(evaluate(std::declval< T >())) EvaluatedType
(Re-)evaluate a given expression or Expressions::Storage.
Definition: optimizationbase.hh:261
constexpr decltype(auto) evaluate(T &&t)
Evaluate an expression or expression Expressions::Storage container.
Definition: optimizationbase.hh:213
decltype(operate(std::declval< OptOrF >(), std::declval< Rest >()...)) ExpressionType
Generate the type of an expression by calling operate().
Definition: optimizationbase.hh:256
OptimizeTag< 0 > DontOptimize
Bottom level is overloaded to do nothing.
Definition: optimizationbase.hh:74
MakeSequence< std::size_t, N, Offset, Stride, Repeat > MakeIndexSequence
Make a sequence of std::size_t elements.
Definition: generators.hh:34
Constant< typename Const::value_type, Const::value+1 > Incr
Increment an integral_constant.
Definition: types.hh:79
Optimization pattern disambiguation struct.
Definition: optimizationbase.hh:42
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)