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"]
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"]
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"]
125def test_independent_tasks_form_one_parallel_node():
127 first, second = Task("a", VERTEX), Task("b", CELL)
128 for task in (first, second):
129 task.add_action_set(_FakeActionSet(task.name))
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)
137def test_independent_tasks_with_conflicting_data_are_not_parallel():
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))
146 steps = FuseSteps.translate(graph, "S", None)
147 assert [step.name for step in steps] == ["S_A", "S_B"]
149 action_set.parallel for step in steps for action_set in step.action_sets
153def test_independent_tasks_with_conflicting_cell_data_are_fused_but_not_parallel():
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))
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)
169def test_independent_tasks_reading_the_same_cell_data_are_parallel():
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))
178 steps = FuseSteps.translate(graph, "S", None)
179 assert _groups(steps[0]) == [["a", "b"]]
182def test_adjacent_parallel_nodes_are_separated_by_a_synchronisation_marker():
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))
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
200 assert isinstance(steps[0].action_sets[2], SynchroniseActionSets)
201 assert _groups(steps[0]) == [["v1", "v2"], ["c1", "c2"]]
204def test_task_with_several_action_sets_is_never_part_of_a_parallel_node():
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"]]
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"]
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"]
235def test_face_tasks_with_conflicting_face_data_are_fused_but_not_parallel():
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))
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)
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"]
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"]
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"]
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)
304 # No traversal of their own
305 assert _action_sets_per_step(graph) == [["start", "c1", "c2", "end"]]
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)
317 assert _action_sets_per_step(graph) == [["c", "g", "end1", "end2"]]