CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
compileTimeTraversalLogic.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://www.scicomp.uni-kl.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://www.scicomp.uni-kl.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 <utility>
38
39#include "../../config.h"
40#include "../../misc/macros.hpp"
41#include "nodeInterface.hpp"
42
44namespace codi {
45
56 template<typename T_ResultType, typename T_Impl>
58 public:
59
60 using ResultType = CODI_DD(T_ResultType, size_t);
62
63 static ResultType constexpr NeutralElement = {};
64
68 template<typename Node, typename... Args>
69 CODI_INLINE static ResultType constexpr eval(Args&&... args) {
70 return toNode<Node>(std::forward<Args>(args)...);
71 }
72
73 /*******************************************************************************/
76
85 return a + b;
86 }
87
97 template<typename Node, typename... Args>
98 CODI_INLINE static ResultType constexpr node(Args&&... args) {
99 // Default logic forwards to all links.
100 return toLinks<Node>(std::forward<Args>(args)...);
101 }
102
110 template<typename Node, typename... Args>
111 CODI_INLINE static ResultType constexpr leaf(Args&&... CODI_UNUSED_ARG(args)) {
112 // Default logic does nothing.
113 return Impl::NeutralElement;
114 }
115
125 template<size_t ChildNumber, typename Child, typename Root, typename... Args>
126 CODI_INLINE static ResultType constexpr link(Args&&... args) {
127 // Default logic forwards to node evaluation.
128 return toNode<Child>(std::forward<Args>(args)...);
129 }
130
132
133 protected:
134
135#ifndef DOXYGEN_DISABLE
136 template<typename TraversalImpl, bool endPoint = false>
137 struct CallSwitch {
138 public:
139 template<typename Node, typename... Args>
140 CODI_INLINE static ResultType constexpr call(Args&&... args) {
141 return TraversalImpl::template node<Node>(std::forward<Args>(args)...);
142 }
143 };
144
145 template<typename TraversalImpl>
146 struct CallSwitch<TraversalImpl, true> {
147 public:
148 template<typename Node, typename... Args>
149 CODI_INLINE static ResultType constexpr call(Args&&... args) {
150 return TraversalImpl::template leaf<Node>(std::forward<Args>(args)...);
151 }
152 };
153#endif
154
156 template<typename Node, typename... Args>
157 CODI_INLINE static ResultType constexpr toNode(Args&&... args) {
158 return CallSwitch<Impl, Node::EndPoint>::template call<Node>(std::forward<Args>(args)...);
159 }
160
162 template<typename Node, typename... Args>
163 CODI_INLINE static ResultType constexpr toLinks(Args&&... args) {
164 return Node::template forEachLinkConstExpr<Impl>(std::forward<Args>(args)...);
165 }
166 };
167}
#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_UNUSED_ARG(arg)
Used in a constexpr context, where using CODI_UNUSED spoils the constexpr.
Definition: macros.hpp:49
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
Traversal of CoDiPack expressions during compile time.
Definition: compileTimeTraversalLogic.hpp:57
static ResultType constexpr NeutralElement
Neutral element of the reduction.
Definition: compileTimeTraversalLogic.hpp:63
static ResultType constexpr link(Args &&... args)
Called for all links in the expression.
Definition: compileTimeTraversalLogic.hpp:126
static ResultType constexpr toLinks(Args &&... args)
Helper method which calls forEachLinkConstExpr on the node.
Definition: compileTimeTraversalLogic.hpp:163
static ResultType constexpr reduce(ResultType a, ResultType b)
Reduction operation for the results of two links.
Definition: compileTimeTraversalLogic.hpp:84
static ResultType constexpr leaf(Args &&...)
Called for all leaf nodes in the expression.
Definition: compileTimeTraversalLogic.hpp:111
static ResultType constexpr eval(Args &&... args)
Start the evaluation of the logic on the given expression.
Definition: compileTimeTraversalLogic.hpp:69
static ResultType constexpr toNode(Args &&... args)
Helper method to distinguish between leaf nodes and normal nodes.
Definition: compileTimeTraversalLogic.hpp:157
T_ResultType ResultType
See CompileTimeTraversalLogic.
Definition: compileTimeTraversalLogic.hpp:60
T_Impl Impl
See CompileTimeTraversalLogic.
Definition: compileTimeTraversalLogic.hpp:61
static ResultType constexpr node(Args &&... args)
Called for each node in the expression.
Definition: compileTimeTraversalLogic.hpp:98