Peano
Loading...
Searching...
No Matches
test_fuse_steps.py
Go to the documentation of this file.
1import pytest
2
3from peano4.api.taskgraph import Graph, Task
4from peano4.api.taskgraph.compiler import FuseSteps
5from peano4.api.solversteps import SynchroniseActionSets, get_task_groups
6
7
8VERTEX = Task.GridEntity.VERTEX
9FACE = Task.GridEntity.FACE
10CELL = Task.GridEntity.CELL
11
12READ_AND_DISCARD = Task.DataAccess.READ_AND_DISCARD
13WRITE = Task.DataAccess.WRITE
14READ_AND_WRITE = Task.DataAccess.READ_AND_WRITE
15TEMPORARY = Task.DataAccess.TEMPORARY
16
17
19 def __init__(self, name, descend_invocation_order=0):
20 self.name = name
21 self.descend_invocation_order = descend_invocation_order
22 self.parallel = False
23
25 return self.name
26
27
29 def __init__(self, name):
30 self.name = name
31
32
33def _chain(*entities):
34 """A chain of tasks t0 -> t1 -> ... with the given grid entities"""
35 graph = Graph()
36 tasks = [Task(name=f"t{i}", entity=entity) for i, entity in enumerate(entities)]
37 for previous_task, task in zip(tasks, tasks[1:]):
38 task.add_dependency(previous_task)
39 for task in tasks:
40 task.add_action_set(_FakeActionSet(task.name))
41 graph.add_task(task)
42 return graph, tasks
43
44
45def _groups(step):
46 """Names of the action sets per task group of the generated observer"""
47 return [
48 [action_set.name for action_set in group]
49 for group in get_task_groups(step.action_sets)
50 ]
51
52
53def _step_names(graph, non_fusable_task_pairs=None):
54 return [
55 step.name
56 for step in FuseSteps.translate(graph, "S", None, non_fusable_task_pairs)
57 ]
58
59
60def test_entity_order_fuses_vertex_face_cell_but_not_cell_vertex():
61 graph, _ = _chain(VERTEX, FACE, CELL, VERTEX, CELL)
62 assert _step_names(graph) == ["S_T0__T1__T2", "S_T3__T4"]
63
64
65def test_repeating_task_is_never_fused():
66 graph = Graph()
67 refine = Task("refine", CELL, task_has_to_repeat="condition")
68 initial_condition = Task("init", CELL)
69 initial_condition.add_dependency(refine)
70 graph.add_task(refine)
71 graph.add_task(initial_condition)
72 steps = FuseSteps.translate(graph, "S", None)
73 assert [step.name for step in steps] == ["S_Refine", "S_Init"]
74 assert steps[0].has_to_repeat == "condition"
75
76
77def test_non_fusable_pair_starts_new_traversal():
78 graph, tasks = _chain(CELL, CELL)
79 assert _step_names(graph) == ["S_T0__T1"]
80 assert _step_names(graph, [(tasks[1], tasks[0])]) == ["S_T0", "S_T1"]
81
82
83def test_cell_data_do_not_constrain_fusion():
84 graph, tasks = _chain(CELL, CELL)
85 patch = _FakeSubmodel("Q")
86 tasks[0].use_cell(patch, WRITE)
87 tasks[1].use_cell(patch, READ_AND_WRITE)
88 assert _step_names(graph) == ["S_T0__T1"]
89
90
91def test_face_data_written_then_read_prevents_fusion():
92 graph, tasks = _chain(CELL, CELL)
93 face_data = _FakeSubmodel("F")
94 tasks[0].use_face(face_data, WRITE)
95 tasks[1].use_face(face_data, READ_AND_DISCARD)
96 assert _step_names(graph) == ["S_T0", "S_T1"]
97
98
99def test_face_data_read_then_written_prevents_fusion():
100 """The reconstruction/projection pattern of a Finite Volume time step"""
101 graph, tasks = _chain(CELL, CELL, CELL)
102 face_data = _FakeSubmodel("F")
103 tasks[0].use_face(face_data, READ_AND_DISCARD)
104 tasks[2].use_face(face_data, WRITE)
105 assert _step_names(graph) == ["S_T0__T1", "S_T2"]
106
107
108def test_vertex_data_written_twice_prevents_fusion():
109 graph, tasks = _chain(CELL, CELL)
110 vertex_data = _FakeSubmodel("V")
111 tasks[0].use_vertex(vertex_data, WRITE)
112 tasks[1].use_vertex(vertex_data, WRITE)
113 assert _step_names(graph) == ["S_T0", "S_T1"]
114
115
116def test_face_data_read_twice_or_temporary_does_not_prevent_fusion():
117 graph, tasks = _chain(CELL, CELL, CELL)
118 face_data = _FakeSubmodel("F")
119 tasks[0].use_face(face_data, READ_AND_DISCARD)
120 tasks[1].use_face(face_data, READ_AND_DISCARD)
121 tasks[2].use_face(face_data, TEMPORARY)
122 assert _step_names(graph) == ["S_T0__T1__T2"]
123
124
125def test_independent_tasks_form_one_parallel_node():
126 graph = Graph()
127 first, second = Task("a", VERTEX), Task("b", CELL)
128 for task in (first, second):
129 task.add_action_set(_FakeActionSet(task.name))
130 graph.add_task(task)
131 steps = FuseSteps.translate(graph, "S", None)
132 assert [step.name for step in steps] == ["S_A__B"]
133 assert _groups(steps[0]) == [["a", "b"]]
134 assert all(action_set.parallel for action_set in steps[0].action_sets)
135
136
137def test_independent_tasks_with_conflicting_data_are_not_parallel():
138 graph = Graph()
139 first, second = Task("a", CELL), Task("b", CELL)
140 face_data = _FakeSubmodel("F")
141 first.use_face(face_data, WRITE)
142 second.use_face(face_data, READ_AND_DISCARD)
143 for task in (first, second):
144 task.add_action_set(_FakeActionSet(task.name))
145 graph.add_task(task)
146 steps = FuseSteps.translate(graph, "S", None)
147 assert [step.name for step in steps] == ["S_A", "S_B"]
148 assert not any(
149 action_set.parallel for step in steps for action_set in step.action_sets
150 )
151
152
153def test_independent_tasks_with_conflicting_cell_data_are_fused_but_not_parallel():
154 graph = Graph()
155 first, second, third = Task("a", CELL), Task("b", CELL), Task("c", CELL)
156 cell_data = _FakeSubmodel("Q")
157 first.use_cell(cell_data, READ_AND_WRITE)
158 second.use_cell(cell_data, READ_AND_WRITE)
159 third.use_cell(cell_data, READ_AND_DISCARD)
160 for task in (first, second, third):
161 task.add_action_set(_FakeActionSet(task.name))
162 graph.add_task(task)
163 steps = FuseSteps.translate(graph, "S", None)
164 assert [step.name for step in steps] == ["S_A__B__C"]
165 assert _groups(steps[0]) == [["a"], ["b"], ["c"]]
166 assert not any(action_set.parallel for action_set in steps[0].action_sets)
167
168
169def test_independent_tasks_reading_the_same_cell_data_are_parallel():
170 graph = Graph()
171 first, second = Task("a", CELL), Task("b", CELL)
172 cell_data = _FakeSubmodel("Q")
173 first.use_cell(cell_data, READ_AND_DISCARD)
174 second.use_cell(cell_data, READ_AND_DISCARD)
175 for task in (first, second):
176 task.add_action_set(_FakeActionSet(task.name))
177 graph.add_task(task)
178 steps = FuseSteps.translate(graph, "S", None)
179 assert _groups(steps[0]) == [["a", "b"]]
180
181
182def test_adjacent_parallel_nodes_are_separated_by_a_synchronisation_marker():
183 graph = Graph()
184 v1, v2, c1, c2 = (
185 Task("v1", VERTEX),
186 Task("v2", VERTEX),
187 Task("c1", CELL),
188 Task("c2", CELL),
189 )
190 c1.add_dependency(v1)
191 c2.add_dependency(v2)
192 for task in (v1, v2, c1, c2):
193 task.add_action_set(_FakeActionSet(task.name))
194 graph.add_task(task)
195 steps = FuseSteps.translate(graph, "S", None)
196 assert [step.name for step in steps] == ["S_V1__V2__C1__C2"]
197 assert [type(action_set) for action_set in steps[0].action_sets].count(
198 SynchroniseActionSets
199 ) == 1
200 assert isinstance(steps[0].action_sets[2], SynchroniseActionSets)
201 assert _groups(steps[0]) == [["v1", "v2"], ["c1", "c2"]]
202
203
204def test_task_with_several_action_sets_is_never_part_of_a_parallel_node():
205 graph = Graph()
206 first, second = Task("a", CELL), Task("b", CELL)
207 first.add_action_set(_FakeActionSet("a1"))
208 first.add_action_set(_FakeActionSet("a2"))
209 second.add_action_set(_FakeActionSet("b"))
210 graph.add_task(first)
211 graph.add_task(second)
212 steps = FuseSteps.translate(graph, "S", None)
213 assert [step.name for step in steps] == ["S_A__B"]
214 assert not any(action_set.parallel for action_set in steps[0].action_sets)
215 assert _groups(steps[0]) == [["a1"], ["a2"], ["b"]]
216
217
218def test_face_task_writing_face_data_fuses_with_subsequent_cell_task():
219 """Faces are touched for the first time before their adjacent cells, e.g. boundary conditions before a reconstruction"""
220 graph, tasks = _chain(FACE, CELL)
221 face_data = _FakeSubmodel("F")
222 tasks[0].use_face(face_data, READ_AND_WRITE)
223 tasks[1].use_face(face_data, READ_AND_DISCARD)
224 assert _step_names(graph) == ["S_T0__T1"]
225
226
227def test_face_task_reading_face_data_fuses_with_subsequent_cell_task_writing_it():
228 graph, tasks = _chain(FACE, CELL)
229 face_data = _FakeSubmodel("F")
230 tasks[0].use_face(face_data, READ_AND_DISCARD)
231 tasks[1].use_face(face_data, WRITE)
232 assert _step_names(graph) == ["S_T0__T1"]
233
234
235def test_face_tasks_with_conflicting_face_data_are_fused_but_not_parallel():
236 graph = Graph()
237 first, second = Task("a", FACE), Task("b", FACE)
238 face_data = _FakeSubmodel("F")
239 first.use_face(face_data, WRITE)
240 second.use_face(face_data, READ_AND_WRITE)
241 for task in (first, second):
242 task.add_action_set(_FakeActionSet(task.name))
243 graph.add_task(task)
244 steps = FuseSteps.translate(graph, "S", None)
245 assert [step.name for step in steps] == ["S_A__B"]
246 assert _groups(steps[0]) == [["a"], ["b"]]
247 assert not any(action_set.parallel for action_set in steps[0].action_sets)
248
249
250def test_cell_task_writing_face_data_is_not_fused_with_subsequent_face_task():
251 graph, tasks = _chain(CELL, FACE)
252 face_data = _FakeSubmodel("F")
253 tasks[0].use_face(face_data, WRITE)
254 tasks[1].use_face(face_data, READ_AND_DISCARD)
255 assert _step_names(graph) == ["S_T0", "S_T1"]
256
257
258def test_vertex_task_writing_vertex_data_fuses_with_subsequent_cell_task():
259 graph, tasks = _chain(VERTEX, CELL)
260 vertex_data = _FakeSubmodel("V")
261 tasks[0].use_vertex(vertex_data, WRITE)
262 tasks[1].use_vertex(vertex_data, READ_AND_DISCARD)
263 assert _step_names(graph) == ["S_T0__T1"]
264
265
266def test_face_task_writing_vertex_data_prevents_fusion():
267 """A vertex is adjacent to faces which are touched only after the cell"""
268 graph, tasks = _chain(FACE, CELL)
269 vertex_data = _FakeSubmodel("V")
270 tasks[0].use_vertex(vertex_data, WRITE)
271 tasks[1].use_vertex(vertex_data, READ_AND_DISCARD)
272 assert _step_names(graph) == ["S_T0", "S_T1"]
273
274
275START = Task.GridEntity.GLOBAL_START_OF_STEP
276END = Task.GridEntity.GLOBAL_END_OF_STEP
277
278
279def _graph_with_global_tasks(*tasks):
280 graph = Graph()
281 for task in tasks:
282 if not task.action_sets:
283 task.add_action_set(_FakeActionSet(task.name))
284 graph.add_task(task)
285 return graph
286
287
288def _action_sets_per_step(graph):
289 return [
290 [action_set.name for action_set in step.action_sets]
291 for step in FuseSteps.translate(graph, "S", None)
292 ]
293
294
295def test_start_and_end_of_step_tasks_open_and_close_the_first_and_last_traversal():
296 cell_1, cell_2 = Task("c1", CELL), Task("c2", CELL)
297 cell_2.add_dependency(cell_1)
298 # They are the last and first in the graph, but ought to be first and last
299 end = Task("end", END)
300 end.add_dependency(cell_2)
301 start = Task("start", START)
302 graph = _graph_with_global_tasks(end, cell_1, cell_2, start)
303
304 # No traversal of their own
305 assert _action_sets_per_step(graph) == [["start", "c1", "c2", "end"]]
306
307
308def test_end_of_step_tasks_come_after_other_global_tasks_and_follow_their_dependencies():
309 cell = Task("c", CELL)
310 global_task = Task("g", Task.GridEntity.GLOBAL)
311 global_task.add_dependency(cell)
312 first_end = Task("end1", END)
313 second_end = Task("end2", END)
314 second_end.add_dependency(first_end)
315 graph = _graph_with_global_tasks(second_end, first_end, cell, global_task)
316
317 assert _action_sets_per_step(graph) == [["c", "g", "end1", "end2"]]
318
319
320def test_global_tasks_of_the_step_do_not_pull_other_tasks_into_the_traversal():
321 """The start task is not a predecessor that delays: both cell tasks share a layer"""
322 start = Task("start", START)
323 cell_1, cell_2 = Task("c1", CELL), Task("c2", CELL)
324 cell_1.add_dependency(start)
325 cell_2.add_dependency(start)
326 graph = _graph_with_global_tasks(start, cell_1, cell_2)
327
328 assert _action_sets_per_step(graph) == [["start", "c1", "c2"]]
329 assert [step.action_sets[1].parallel for step in FuseSteps.translate(graph, "S", None)] == [True]
330
331
332def test_start_of_step_task_may_not_depend_on_anything():
333 cell = Task("c", CELL)
334 start = Task("start", START)
335 start.add_dependency(cell)
336 graph = _graph_with_global_tasks(cell, start)
337 with pytest.raises(AssertionError, match="may not depend"):
338 FuseSteps.translate(graph, "S", None)
339
340
341def test_task_depending_on_a_global_task_does_not_share_its_traversal():
342 first, second = Task("c1", CELL), Task("c2", CELL)
343 global_task = Task("g", Task.GridEntity.GLOBAL)
344 global_task.add_dependency(first)
345 second.add_dependency(global_task)
346 graph = _graph_with_global_tasks(first, global_task, second)
347 assert _action_sets_per_step(graph) == [["c1", "g"], ["c2"]]
348 # Without the global task, the two cell tasks run in one traversal
349 plain = _graph_with_global_tasks(first, second)
350 second.dependencies.remove(global_task)
351 second.add_dependency(first)
352 assert _action_sets_per_step(plain) == [["c1", "c2"]]
353
354
355def test_end_of_step_task_with_a_dependent_task_ends_its_traversal_instead_of_the_step():
356 first, second = Task("c1", CELL), Task("c2", CELL)
357 anchored_end, trailing_end = Task("end1", END), Task("end2", END)
358 anchored_end.add_dependency(first)
359 second.add_dependency(anchored_end)
360 trailing_end.add_dependency(second)
361 graph = _graph_with_global_tasks(trailing_end, second, anchored_end, first)
362 assert graph.anchored_end_of_step_tasks() == [anchored_end]
363 assert _action_sets_per_step(graph) == [["c1", "end1"], ["c2", "end2"]]
364 # The order stays topological
365 order = graph.ordered_tasks()
366 assert order.index(first) < order.index(anchored_end) < order.index(second) < order.index(trailing_end)
367
368
369def test_end_of_step_task_which_an_anchored_one_depends_on_is_anchored_too():
370 first, second = Task("c1", CELL), Task("c2", CELL)
371 inner_end, outer_end = Task("end1", END), Task("end2", END)
372 inner_end.add_dependency(first)
373 outer_end.add_dependency(inner_end)
374 second.add_dependency(outer_end)
375 graph = _graph_with_global_tasks(first, second, inner_end, outer_end)
376 assert set(graph.anchored_end_of_step_tasks()) == {inner_end, outer_end}
377 assert _action_sets_per_step(graph) == [["c1", "end1", "end2"], ["c2"]]
378
379
380def test_repeating_first_and_last_traversal_get_their_own_start_and_end_traversal():
381 start = Task("start", START)
382 refine = Task("refine", CELL, task_has_to_repeat="condition")
383 plot = Task("plot", CELL, task_has_to_repeat="again")
384 plot.add_dependency(refine)
385 end = Task("end", END)
386 end.add_dependency(plot)
387 graph = _graph_with_global_tasks(start, refine, plot, end)
388
389 # Otherwise, start and end would run in every repetition
390 assert _action_sets_per_step(graph) == [["start"], ["refine"], ["plot"], ["end"]]
391
392
393def test_start_and_end_of_step_tasks_without_other_tasks():
394 graph = _graph_with_global_tasks(Task("start", START), Task("end", END))
395 assert _action_sets_per_step(graph) == [["start", "end"]]
396
397
398def test_ordered_tasks_put_start_first_and_end_last():
399 cell = Task("c", CELL)
400 end = Task("end", END)
401 end.add_dependency(cell)
402 start = Task("start", START)
403 graph = Graph()
404 for task in [end, cell, start]:
405 graph.add_task(task)
406 assert [task.name for task in graph.ordered_tasks()] == ["start", "c", "end"]
407
408
409def test_global_tasks_may_not_hold_data_or_repeat():
410 for entity in [START, END]:
411 task = Task("t", entity)
412 with pytest.raises(AssertionError):
413 task.use_cell(_FakeSubmodel("Q"), WRITE)
414 with pytest.raises(AssertionError):
415 Task("t", entity, task_has_to_repeat="condition")
__init__(self, name, descend_invocation_order=0)
The Python-side model of "what happens during one mesh sweep".
Definition __init__.py:1
Compilers are simple functions.
Definition __init__.py:1
The task graph abstraction allows users to formalise their solver steps over small task graphs which ...
Definition __init__.py:1
_groups(step)
Names of the action sets per task group of the generated observer.
_chain(*entities)
A chain of tasks t0 -> t1 -> ... with the given grid entities.