Peano
Loading...
Searching...
No Matches
Task.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3
4import enum
5from typing import Iterable, List, Optional, Set
6
7
8class Task:
9 """!
10
11 One task within a peano4.api.taskgraph.Graph
12
13 Every task works on exactly one kind of mesh entity - a vertex, a face
14 or a cell - which has to be specified through entity when the task is
15 constructed. GLOBAL tasks are the exception: they do not work on any
16 mesh entity (@ref peano4.api.taskgraph.Task.Task.GridEntity).
17
18 ## Task dependencies
19
20 Every task also holds dependencies, i.e. other Task instances that have
21 to be completed before this task can run. This sequence is empty by
22 default; use add_dependency() to add further tasks to it.
23
24 ## Task action sets
25
26 Every task furthermore holds an arbitrary number of Peano 4 action sets
27 (peano4.api.solversteps.ActionSet), i.e. the actual grid-sweep logic the
28 task realises once it is compiled into an Observer. This sequence is
29 empty by default; use add_action_set() to add further action sets to it.
30 Their order matters - see peano4.api.solversteps.Step.add_action_set().
31
32 ## Task data usage
33
34 A task's action sets are handed mesh data (fineGridCell..., fineGridFace...,
35 fineGridVertices...) as arguments once they are compiled into an Observer -
36 but only for the vertex/face/cell submodels the surrounding
37 peano4.api.solversteps.Step was told about via its own use_cell()/
38 use_face()/use_vertex(). An action set cannot make that declaration
39 itself (several tasks, and hence several Steps, can share the same
40 action set), so every task has to declare which submodels it needs
41 through use_cell()/use_face()/use_vertex() below. The task graph
42 compiler then forwards these declarations to the Step(s) it compiles
43 this task into.
44
45 ## Task repetitions
46
47 Finally, a task can be marked as repeating via task_has_to_repeat. By
48 default (None), a task runs once. Passing a non-None string works if
49 and only if it is a valid boolean C++ expression. Peano will evaluate
50 it on the global runner after the task has finished and then decide if
51 to rerun this step. An all time classic is the following expression
52
53 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
54 not peano4::grid::isGridStationary( peano4::parallel::SpacetreeSet::getInstance().getGridStatistics() )
55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
57 which means this task will repeat until the mesh becomes stationary.
58
59 ## Background tasks
60
61 Some task graph compilers can run parts of a task in the background,
62 i.e. as tasks that outlive the mesh traversal event which spawns them
63 (@ref peano4.api.taskgraph.compiler.CellTaskProducerConsumer.translate_with_sequential(), @ref peano4.api.taskgraph.compiler.CellTaskProducerConsumer.translate_with_fusion()).
64 Such compilers check whether this is safe, and leave the task as it is
65 if they cannot be sure. The flag may_spawn_background_tasks steers this
66 behaviour:
67
68 - None (default): The compilers decide whether running the task in the
69 background is safe.
70 - True: You know that running the task in the background is fine. The
71 compilers skip their checks.
72 - False: The task must never run in the background, e.g. as it
73 reduces data in unprepareTraversal() such as a global CFL condition.
74 The compilers leave the task as it is without any further check or
75 warning.
76
77 ## Task tags
78
79 Every task carries a set of tags, i.e. of plain strings. A task can carry
80 arbitrarily many tags at the same time. The task graph compilers ignore
81 them. Tags allow the code which creates a task graph to tell other code
82 what a task does, without that code having to know how the graph is
83 built. Tags are global: Add a tag via task.tags.add(tag) and find
84 all tasks with a tag via peano4.api.taskgraph.Graph.find_tasks().
85 ExaHyPE's numerical schemes, for example, tag all tasks which produce a
86 new solution with exahype2.api.Solver.Solver.SolutionUpdateTaskTag, such
87 that others can add postprocessing steps after them (@ref
88 page_exahype2_solver_manipulate_solver_steps). Use names with a prefix,
89 e.g. the name of the defining class, so tags of different parties
90 cannot clash by accident.
91
92 ## Task processing
93
94 The tasks will later on be taken by the graph compiler which maps them
95 onto a sequence of @ref peano4.api.solversteps.Step instances. How many
96 of these logical steps (grid traversals) are required depends on the
97 structure of the task graph but also the type of compiler chosen.
98 Contraray, each action set that is added to a task will remain to be a
99 real @ref peano4.api.solversteps.ActionSet, and subsequently be translated
100 into exactly one @ref peano4.api.output.ActionSet, i.e. one pair of header
101 and implementation file called MyFancyObserverOfAGridTraversal2ThisActionSet.
102
103 ## Things to read to get the full picture:
104
105 - @ref peano4.api.taskgraph This is the overall description of the task
106 graph API and should be the starting point for understanding how to use it.
107 It notably describes some design decisions re dependencies, grid
108 entities and action sets.
109 - @ref page_peano_action_sets
110 - @ref peano4.api.taskgraph
111
112 """
113
114 class GridEntity(enum.Enum):
115 """!
116
117 Which kind of mesh entity a Task works on.
118
119 GLOBAL tasks do not work on any mesh entity at all. They only plug
120 into prepareTraversal()/unprepareTraversal(), i.e. they realise
121 global (per rank) activities such as triggering the load balancing.
122 They therefore may not declare any cell, face or vertex data, may
123 not repeat. Task graph compilers which fuse tasks append a GLOBAL
124 task to the traversal holding its last dependency
125 (@ref peano4.api.taskgraph.compiler.FuseSteps.translate()). Other
126 tasks may depend on a GLOBAL task. The compiler then does not fuse
127 them into the traversal of the GLOBAL task, as the latter acts in
128 the prepare and unprepare events of this traversal: they run in a
129 later mesh sweep.
130
131 GLOBAL_START_OF_STEP and GLOBAL_END_OF_STEP are GLOBAL tasks that are
132 tied to the beginning and the end of the whole task graph rather than
133 to the traversal of their dependencies. Both do not require a mesh
134 traversal of their own:
135
136 - A GLOBAL_START_OF_STEP task runs first, i.e. its action sets are the
137 first ones of the first traversal. It may not depend on any task.
138 Other tasks may depend on it, but this is trivial.
139 - A GLOBAL_END_OF_STEP task runs last, i.e. its action sets are the
140 last ones of the last traversal, after all other tasks including
141 the other GLOBAL tasks. Other GLOBAL_END_OF_STEP tasks may depend
142 on it, which orders them among each other. A typical
143 example is the global CFL condition, which reduces data in
144 unprepareTraversal() only. If a task that is not an end-of-step
145 task depends on it, it cannot run at the very end of the step.
146 It then ends the traversal of its last dependency, as a GLOBAL
147 task does, and the dependent task runs in a later traversal
148 (@ref peano4.api.taskgraph.Graph.Graph.anchored_end_of_step_tasks()).
149
150 Task graph compilers that do not fuse tasks guarantee the same order
151 (@ref peano4.api.taskgraph.Graph.Graph.ordered_tasks()).
152
153 """
154
155 VERTEX = 1
156 FACE = 2
157 CELL = 3
158 GLOBAL = 4
159 GLOBAL_START_OF_STEP = 5
160 GLOBAL_END_OF_STEP = 6
161
162 @property
163 def is_global(self) -> bool:
164 """! True for GLOBAL, GLOBAL_START_OF_STEP and GLOBAL_END_OF_STEP """
165 return self in (
166 Task.GridEntity.GLOBAL,
167 Task.GridEntity.GLOBAL_START_OF_STEP,
168 Task.GridEntity.GLOBAL_END_OF_STEP,
169 )
170
171 class DataAccess(enum.Enum):
172 """!
173
174 How a task's action sets actually access a submodel it declares via
175 use_cell()/use_face()/use_vertex() - i.e. the qualifier that
176 accompanies every such declaration.
177
178 - READ_AND_DISCARD: the action set only reads the submodel, never
179 writes it, and does not need its value kept around afterwards -
180 this task is a final consumer, not a link in a longer chain.
181 The submodel is loaded before the step, but it is not stored
182 after the step: Its value is lost unless another task of the
183 same step writes it.
184 - WRITE: the action sets only write the submodel (e.g. fill in an
185 initial condition), without relying on whatever value it held
186 before. The submodel is stored after the step, but it is not
187 loaded before the step: The action sets see uninitialised data,
188 not the value the submodel held before.
189 - READ_AND_WRITE: the action sets both read the submodel's
190 existing value and write a new one (e.g. an in-place update).
191 The WRITE component doesn't mean necessarily that we alter the
192 content. It simply tells the task that the data should not be
193 discarded afterwards. The submodel is loaded before and stored
194 after the step.
195 - TEMPORARY: the submodel is neither meaningfully read nor written
196 by this task for its own sake - available for a task graph
197 author to say so explicitly, though nothing currently derives
198 this qualifier automatically. The submodel is neither loaded nor
199 stored.
200
201 The qualifiers matter at two levels. On the task-graph level, WRITE
202 and READ_AND_WRITE are treated identically by
203 peano4.api.taskgraph.compiler.PropagateUseStatements.propagate_use_statements():
204 both mean this task produces a genuinely new value for the
205 submodel, which someone downstream then has to actually pick up
206 (read) - see that function's documentation for the precise rule,
207 and note that the READ_AND_WRITE declarations *it* inserts to
208 satisfy that rule are indistinguishable from ones a task graph
209 author wrote by hand. READ_AND_DISCARD and TEMPORARY impose no
210 such obligation on whatever comes after.
211
212 On the level of the generated code, every qualifier translates
213 into the same use_cell()/use_face()/use_vertex() call on the
214 peano4.api.solversteps.Step this task compiles into (@ref
215 peano4.api.taskgraph.compiler.Sequential.translate()). However,
216 the qualifiers decide whether the generated code loads the
217 submodel before and stores it after a step: @ref
218 peano4.api.taskgraph.compiler.DeriveLoadStoreComputeFlag.derive_load_store_compute_flag()
219 derives these flags from the qualifiers as described per qualifier
220 above, and exahype2.api.Project.generate_Peano4_project() applies
221 it to all steps. If several tasks share a step,
222 peano4.api.taskgraph.compiler.FuseSteps merges their qualifiers
223 first.
224
225 Pick the qualifiers with care if a submodel's value has to survive
226 several steps: A task which reads data that an earlier task of
227 another step has declared READ_AND_DISCARD, or a task declaring
228 WRITE which still reads the previous value, reads garbage. The
229 Sommerfeld boundary conditions of exahype2.api are an example
230 (@ref exahype2.api.blockstructured.BoundaryConditions.get_access_to_persistent_face_data()).
231
232 """
233
234 READ_AND_DISCARD = 1
235 WRITE = 2
236 READ_AND_WRITE = 3
237 TEMPORARY = 4
238
240 self,
241 name: str,
242 entity: "Task.GridEntity",
243 task_has_to_repeat: Optional[str] = None,
244 may_spawn_background_tasks: Optional[bool] = None,
245 tags: Optional[Iterable[str]] = None,
246 ):
247 self.name = name
248 self.tags: Set[str] = set(tags) if tags is not None else set()
249 self.entity = entity
250 self.dependencies: List["Task"] = []
251 self.action_sets: List["peano4.api.solversteps.ActionSet"] = []
252 self.cell_data: List["peano4.api.datamodel.DoF"] = []
253 self.face_data: List["peano4.api.datamodel.DoF"] = []
254 self.vertex_data: List["peano4.api.datamodel.DoF"] = []
255 self.cell_data_access: dict = {}
256 self.face_data_access: dict = {}
257 self.vertex_data_access: dict = {}
258 self.has_to_repeat = task_has_to_repeat
259 self.may_spawn_background_tasks = may_spawn_background_tasks
260 assert not (entity.is_global and task_has_to_repeat is not None), (
261 f"Task {name} is global and hence may not repeat"
262 )
263
264 def add_dependency(self, task: "Task"):
265 self.dependencies.append(task)
266
267 def use_cell(self, submodel: "peano4.api.datamodel.DoF", qualifier: "Task.DataAccess"):
268 """!
269
270 Declare that this task's action sets access a cell submodel (e.g. a
271 peano4.api.datamodel.Patch registered via
272 peano_project.datamodel.add_cell()), and how - @see Task.DataAccess
273 for what qualifier to pass. Forwarded to the
274 peano4.api.solversteps.Step this task is compiled into, i.e. to its
275 own use_cell(), so the generated Observer passes fineGridCell<name>
276 into every touch-cell/create-cell/... operation.
277
278 """
279 assert not self.entity.is_global, f"Task {self.name} is global and hence may not use cell data"
280 self.cell_data.append(submodel)
281 self.cell_data_access[submodel] = qualifier
282
283 def use_face(self, submodel: "peano4.api.datamodel.DoF", qualifier: "Task.DataAccess"):
284 """!
285
286 Declare that this task's action sets access a face submodel, and
287 how. @see use_cell() for the general mechanism and Task.DataAccess
288 for what qualifier to pass.
289
290 """
291 assert not self.entity.is_global, f"Task {self.name} is global and hence may not use face data"
292 self.face_data.append(submodel)
293 self.face_data_access[submodel] = qualifier
294
295 def use_vertex(self, submodel: "peano4.api.datamodel.DoF", qualifier: "Task.DataAccess"):
296 """!
297
298 Declare that this task's action sets access a vertex submodel, and
299 how. @see use_cell() for the general mechanism and Task.DataAccess
300 for what qualifier to pass.
301
302 """
303 assert not self.entity.is_global, f"Task {self.name} is global and hence may not use vertex data"
304 self.vertex_data.append(submodel)
305 self.vertex_data_access[submodel] = qualifier
306
307 def add_action_set(self, action_set: "peano4.api.solversteps.ActionSet"):
308 """!
309
310 A task can hold an arbitrary number of Peano 4 action sets. Eventually
311 the overarching task graph will be mapped onto a sequence of steps
312 (observers) and those steps then grab those action sets from the tasks and execute them.
313
314 It is important to note that the action set passed in can define
315 actions for multiple grid entities, while the task is specified over
316 a particular mesh entity. This seems to be contradiction but is
317 actually explained in @ref peano4.api.taskgraph.
318
319 """
320 self.action_sets.append(action_set)
How a task's action sets actually access a submodel it declares via use_cell()/use_face()/use_vertex(...
Definition Task.py:171
Which kind of mesh entity a Task works on.
Definition Task.py:114
bool is_global(self)
True for GLOBAL, GLOBAL_START_OF_STEP and GLOBAL_END_OF_STEP.
Definition Task.py:163
One task within a peano4.api.taskgraph.Graph.
Definition Task.py:8
use_vertex(self, "peano4.api.datamodel.DoF" submodel, "Task.DataAccess" qualifier)
Declare that this task's action sets access a vertex submodel, and how.
Definition Task.py:295
__init__(self, str name, "Task.GridEntity" entity, Optional[str] task_has_to_repeat=None, Optional[bool] may_spawn_background_tasks=None, Optional[Iterable[str]] tags=None)
Definition Task.py:246
add_dependency(self, "Task" task)
Definition Task.py:264
add_action_set(self, "peano4.api.solversteps.ActionSet" action_set)
A task can hold an arbitrary number of Peano 4 action sets.
Definition Task.py:307
use_face(self, "peano4.api.datamodel.DoF" submodel, "Task.DataAccess" qualifier)
Declare that this task's action sets access a face submodel, and how.
Definition Task.py:283
use_cell(self, "peano4.api.datamodel.DoF" submodel, "Task.DataAccess" qualifier)
Declare that this task's action sets access a cell submodel (e.g.
Definition Task.py:267