Peano
Loading...
Searching...
No Matches
AlgorithmStep.py
Go to the documentation of this file.
1# This file is part of the ExaParty project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3
4from enum import Enum, IntEnum
5
6from abc import abstractmethod
7
8import peano4.api
9
10
11class AlgorithmStep(object):
12 """!
13
14 Defines the meta data around one algorithmic step per particle
15
16 A sequence of these steps describes the particle lifecycle per time step. It
17 is Peano's/ExaParty's responsibility to arrange these steps in grid sweeps and
18 to identify concurrency.
19
20 """
21
22 class Dependencies(Enum):
23 """
24
25 An algorithmic step for one particle can depend on different
26 pre-conditions:
27
28 - It can only depend on its own previous step (SELF);
29 - It can depend on the neighbours within a particle's interaction radius
30 (NEIGHBOURS);
31
32 """
33
34 SELF = 0
35 NEIGHBOURS = 1
36
37 class Effect(Enum):
38 """
39
40 What happens in this algorithmic step per particle:
41
42 - ALTER_GLOBAL_STATE is the most generic baseline routine. No local data
43 are changed, but the code (might) alter some global properties. Typical
44 examples are the reduction of the global admissible time step size.
45 - ALTER_LOCAL_STATE implies that only the local state of a particle is
46 changed. The code might also modify the global state, but it may not
47 change a particle's position or its search radius, i.e. the spatial
48 topology between particles.
49 - CHANGE_POSITION_OR_INTERACTION_RADIUS is the most general update which
50 means that literally any global or local attribute might be modified.
51 However, the graph compilers have to know whether you might ask it to
52 rerun this step. This is important, as this step might change the
53 topology.
54
55 Please note that any update of a particle position should happen within
56 touch vertex last time. Otherwise, we'd destroy the particle topology.
57
58 By default, any algorithm step runs only once. However, there are
59 variants of each step which can rerun. But the user has to flag this
60 possibility explicitly.
61
62 """
63
64 ALTER_GLOBAL_STATE = 0
65 ALTER_GLOBAL_STATE_AND_MIGHT_RERUN = 1
66 ALTER_LOCAL_STATE = 2
67 ALTER_LOCAL_STATE_AND_MIGHT_RERUN = 3
68 CHANGE_POSITION_OR_INTERACTION_RADIUS = 4
69 CHANGE_POSITION_OR_INTERACTION_RADIUS_AND_MIGHT_RERUN = 5
70
71 @staticmethod
72 def may_trigger_rerun(effect: Effect):
73 """!
74
75 Return True if the effect object may trigger a rerun
76
77 If an algorithm step can trigger a rerun, this implies that
78 any graph compiler, i.e. any translation into a sequence of Peano
79 observers, has to be aware that the step might be executed multiple times.
80 Hence,it cannot fuse it with other steps arbitrarily.
81
82 Technically, the possibility of a rerun has to be captured in the
83 runner of an ExaParty project. After each step, it queries
84 repositories::rerunPreviousGridSweep() to find out if we should
85 stick with the current observer.
86 @see ExaPartyMain.template.cpp
87
88 The decision of a rerun is tied to the species, so each species has
89 a flag and this flag is reduced by repositories::rerunPreviousGridSweep().
90
91 @todo Might change with new Peano task graph API
92
93
94 @param effect: Effect
95 The effect of an AlgorithmStep to check.
96
97 @return may_trigger_rerun: Bool
98 True if effect is one of the *_AND_MIGHT_RERUN variants of Effect.
99
100 """
101 if effect in [
102 AlgorithmStep.Effect.ALTER_GLOBAL_STATE_AND_MIGHT_RERUN,
103 AlgorithmStep.Effect.ALTER_LOCAL_STATE_AND_MIGHT_RERUN,
104 AlgorithmStep.Effect.CHANGE_POSITION_OR_INTERACTION_RADIUS_AND_MIGHT_RERUN,
105 ]:
106 return True
107 else:
108 return False
109
110 class PeanoEventUsedBySwift(IntEnum):
111 """!
112
113 Enumeration of different @ref page_peano_action_sets "Peano events" during a
114 grid traversal into which ExaParty plugs in. We don't use all events.
115 Therefore, this is a subset of the operations in
116 peano4.api.solversteps.ActionSet. The enumeration is based upon integers,
117 as we need to know the order of the enumerations.
118
119 Should we ever pick up more events, make sure to also update the dict
120 containing the names of the stages in
121 """
122
123 TOUCH_VERTEX_FIRST_TIME = 0
124 CELL_KERNEL = 1
125 TOUCH_VERTEX_LAST_TIME = 2
126 EVENT_COUNT = 3
127
128 @staticmethod
129 def get_event_name(stage: PeanoEventUsedBySwift):
130 """!
131
132 Get the name as a string of a sweep stage for a given PeanoEventUsedBySwift enum.
133
134 """
135 Results = {
136 AlgorithmStep.PeanoEventUsedBySwift.TOUCH_VERTEX_FIRST_TIME: peano4.api.solversteps.ActionSet.OPERATION_TOUCH_VERTEX_FIRST_TIME,
137 AlgorithmStep.PeanoEventUsedBySwift.CELL_KERNEL: "cellKernel",
138 AlgorithmStep.PeanoEventUsedBySwift.TOUCH_VERTEX_LAST_TIME: peano4.api.solversteps.ActionSet.OPERATION_TOUCH_VERTEX_LAST_TIME,
139 AlgorithmStep.PeanoEventUsedBySwift.EVENT_COUNT: "count",
140 }
141 return Results[stage]
142
144 self,
145 name,
146 dependencies: Dependencies,
147 effect: Effect,
148 cell_kernel=None,
149 touch_vertex_first_time_kernel=None,
150 touch_vertex_last_time_kernel=None,
151 prepare_traversal_kernel="",
152 unprepare_traversal_kernel="",
153 input_particles=None,
154 includes="",
155 cell_kernel_dependency_policy=None,
156 touch_vertex_first_time_dependency_policy=None,
157 touch_vertex_last_time_dependency_policy=None,
158 ):
159 """!
160
161 The algorithmic step description is a meta data object, i.e. it only
162 holds properties.
163
164 ## Parameters/attributes
165
166 @param cell_kernel: C++ code (string)
167 Kernel invocation what happens in a cell. Set to None if nothing
168 is to be done per cell.
169
170 @param touch_vertex_first_time_kernel: C++ code (string)
171 Kernel invocation what happens when we read a vertex and its particles
172 for the first time. Set to None if nothing is to be done.
173
174 @param touch_vertex_last_time_kernel: C++ code (string)
175 Kernel invocation what happens when we read a vertex and its particles
176 for the last time. Set to None if nothing is to be done.
177
178 @param input_particles: ParticleSet
179 Switch to None in most cases. This fragment gives you the opportunity
180 to couple the particles of one step with another particle species. If
181 you hand in None, then we assume that the algorithm step updates the
182 particles as input function from the particles.
183
184 @param includes: String
185 Additional include statements
186
187 prepare_traversals_kernel = None,
188
189 @param prepare_traversal_kernel: String (C++ code snippet)
190 Whenever Peano runs through the mesh, it creates one instance of the
191 mesh observer and then runs through all subpartitions (trees) in
192 parallel. Each replica of the observer issues its own touchVertexFirstTime(),
193 touchCellLastTime(), ... events, and each replica starts its traversal
194 with a call to beginTraversal() and endTraversal(). If you want to
195 plug into the phase such before these parallel instances are created,
196 you can use prepareTraversal() - the C++ code snippet passed to this
197 attribute is copied into prepareTraversal(). The overview over
198 @ref page_peano_action_sets "action sets" provides more details. Note that
199 no action set (or observer object) is created at this point, i.e. the
200 C++ code snippet will be injected into a static routine and can
201 therefore only access static information. Often, this routine is used
202 for some MPI communication (global data exchange), e.g., or to update
203 some other static properties for whole sets of objects.
204
205 @param unprepare_traversal_kernel: String (C++ code snippet)
206 Counterpart of prepare_traversal_kernel. Consult
207 @ref page_peano_action_sets "action sets" for more context.
208
209 @param cell_kernel_dependency_policy: String or None
210 If it is a string, it should identify one of the variants of
211 exaparty::dependencychecks::Invariant. If you pick None, ExaParty
212 will add default dependencies. They might be too strict.
213
214 @param touch_vertex_first_time_dependency_policy: String or None
215 If it is a string, it should identify one of the variants of
216 exaparty::dependencychecks::Invariant. If you pick None, ExaParty
217 will add default dependencies. They might be too strict.
218
219 @param touch_vertex_last_time_dependency_policy: String or None
220 If it is a string, it should identify one of the variants of
221 exaparty::dependencychecks::Invariant. If you pick None, ExaParty
222 will add default dependencies. They might be too strict.
223
224 """
225 self.name = name
226 self.dependencies = dependencies
227 self.effect = effect
228
229 self.cell_kernel = cell_kernel
230 self.touch_vertex_first_time_kernel = touch_vertex_first_time_kernel
231 self.touch_vertex_last_time_kernel = touch_vertex_last_time_kernel
232 self.prepare_traversal_kernel = prepare_traversal_kernel
233 self.unprepare_traversal_kernel = unprepare_traversal_kernel
234
235 self.input_particles = input_particles
236 self.includes = (
237 """
238#include "Constants.h"
239#include "exaparty/kernels/ParticleUpdatePredicates.h"
240
241#include "exaparty/dependencychecks/DependencyChecks.h"
242#include "toolbox/particles/assignmentchecks/TracingAPI.h"
243 """
244 + includes +
245"""
246#include "exaparty/kernels/ParticleSetIterators.h"
247"""
248 # Since the extended LLVM compiler inlines particle iterator callbacks,
249 # callback definitions must appear before the iterators.
250 # Moving the include as far down as possible tries to guarantee this.
251 )
252
253 self.cell_kernel_dependency_policy = cell_kernel_dependency_policy
255 touch_vertex_first_time_dependency_policy
256 )
258 touch_vertex_last_time_dependency_policy
259 )
260
261 def __str__(self):
262 return "({},{},{})".format(self.name, self.dependencies, self.effect)
An algorithmic step for one particle can depend on different pre-conditions:
What happens in this algorithmic step per particle:
Enumeration of different Peano events during a grid traversal into which ExaParty plugs in.
Defines the meta data around one algorithmic step per particle.
may_trigger_rerun(Effect effect)
Return True if the effect object may trigger a rerun.
get_event_name(PeanoEventUsedBySwift stage)
Get the name as a string of a sweep stage for a given PeanoEventUsedBySwift enum.
__init__(self, name, Dependencies dependencies, Effect effect, cell_kernel=None, touch_vertex_first_time_kernel=None, touch_vertex_last_time_kernel=None, prepare_traversal_kernel="", unprepare_traversal_kernel="", input_particles=None, includes="", cell_kernel_dependency_policy=None, touch_vertex_first_time_dependency_policy=None, touch_vertex_last_time_dependency_policy=None)
The algorithmic step description is a meta data object, i.e.