ThunderEgg  1.0.0
Loops.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ThunderEgg, a library for solvers on adaptively refined block-structured
3  * Cartesian grids.
4  *
5  * Copyright (c) 2019-2021 Scott Aiton
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  ***************************************************************************/
20 
21 #ifndef THUNDEREGG_LOOP_H
22 #define THUNDEREGG_LOOP_H
23 
29 #include <array>
30 
31 namespace ThunderEgg {
35 class Loop
36 {
37 private:
38  template<int start, int stop, typename T>
39  class ULoop
40  {
41  public:
42  static void inline loop_loop(T lambda)
43  {
44  lambda(start);
45  ULoop<start + 1, stop, T>::loop_loop(lambda);
46  }
47  };
48 
49  template<int start, typename T>
50  class ULoop<start, start, T>
51  {
52  public:
53  static void inline loop_loop(T lambda) { lambda(start); }
54  };
55  template<typename T>
56  class ULoop<0, -1, T>
57  {
58  public:
59  static void inline loop_loop(T lambda) {}
60  };
61 
62 public:
71  template<int start, int stop, typename T>
72  static inline void Unroll(T lambda)
73  {
74  ULoop<start, stop, T>::loop_loop(lambda);
75  }
76 
77 private:
83  template<int D, int Dir, typename T, typename A>
84  class NestedLoop
85  {
86  public:
87  static void inline nested_loop_loop(A coord, A start, A end, T lambda)
88  {
89  for (coord[Dir] = start[Dir]; coord[Dir] <= end[Dir]; coord[Dir]++) {
90  NestedLoop<D, Dir - 1, T, A>::nested_loop_loop(coord, start, end, lambda);
91  }
92  }
93  };
94 
95  template<int D, typename T, typename A>
96  class NestedLoop<D, 0, T, A>
97  {
98  public:
99  static void inline nested_loop_loop(A coord, A start, A end, T lambda)
100  {
101  for (coord[0] = start[0]; coord[0] <= end[0]; coord[0]++) {
102  lambda(coord);
103  }
104  }
105  };
106  template<typename T, typename A>
107  class NestedLoop<0, -1, T, A>
108  {
109  public:
110  static void inline nested_loop_loop(A coord, A, A, T lambda) { lambda(coord); }
111  };
112 
113 public:
124  template<int D, typename T, typename A>
125  static inline void Nested(A start, A end, T lambda)
126  {
127  A coord = start;
128  NestedLoop<D, D - 1, T, A>::nested_loop_loop(coord, start, end, lambda);
129  }
139  template<int D, typename V, typename T>
140  static inline void OverInteriorIndexes(const V& view, T lambda)
141  {
142  Nested<D>(view.getStart(), view.getEnd(), lambda);
143  }
153  template<int D, typename V, typename T>
154  static inline void OverAllIndexes(const V& view, T lambda)
155  {
156  Nested<D>(view.getGhostStart(), view.getGhostEnd(), lambda);
157  }
158 };
159 } // namespace ThunderEgg
160 #endif
ThunderEgg::Loop::Nested
static void Nested(A start, A end, T lambda)
Loop over a range of integer coordinates.
Definition: Loops.h:125
ThunderEgg::Loop::OverAllIndexes
static void OverAllIndexes(const V &view, T lambda)
Loop over all the coordinates of a view, including ghost cells.
Definition: Loops.h:154
ThunderEgg::Loop::Unroll
static void Unroll(T lambda)
Unroll a fixed-length loop.
Definition: Loops.h:72
ThunderEgg::Loop
Dimension templated loops.
Definition: Loops.h:35
ThunderEgg
The ThunderEgg namespace.
Definition: BiLinearGhostFiller.h:31
ThunderEgg::Loop::OverInteriorIndexes
static void OverInteriorIndexes(const V &view, T lambda)
Loop over all the interior coordinates of a view.
Definition: Loops.h:140