14def translate(graph: Graph, identifier: str, peano_project:
"peano4.api.Project") -> List[
"peano4.api.solversteps.Step"]:
17 Simplest possible task graph compiler
19 Linearises the task graph into one mesh traversal (Observer) per task, in
20 an order that respects every dependency: a task's Observer always comes
21 after the Observers of all tasks it depends on. This throws away any
22 parallelism the task graph might encode - it is the "no fusion, no
23 reordering" baseline compiler that smarter compilers can be measured
28 A straightforward, direct mapping: graph.ordered_tasks() linearises the
29 graph's tasks into a valid topological order (a task always comes after
30 every task it depends on - @see Graph.ordered_tasks() for how), and
31 every task in that order becomes exactly one Observer, populated with
32 that task's action sets and the cell/face/vertex data it declared via
33 use_cell()/use_face()/use_vertex().
35 Note that this function no longer derives any *further* use_cell()/
36 use_face()/use_vertex() declarations itself - that is the job of
37 @ref peano4.api.taskgraph.compiler.PropagateUseStatements.propagate_use_statements(),
38 which has to run *before* translate() (it reasons about tasks, not the
39 Steps translate() produces here, and about the composability of
40 several graphs at once, which a single graph's own translate() call
41 cannot see). By the time translate() runs, every task already carries
42 every use_cell()/use_face()/use_vertex() declaration it will ever need.
44 A peano4.api.Logger().new_section() call brackets the translation, and
45 @ref peano4.api.taskgraph.compiler.ReportCompiledStepSequence.report_compiled_step_sequence()
46 reports its outcome right after - a textual listing of the resulting
47 sequence and its action sets under LoggerVerbosity.TEXT, or a flow chart
48 in the same style as Graph.visualise() under
49 LoggerVerbosity.VISUALISATION.
51 @param graph: peano4.api.taskgraph.Graph
52 The task graph to linearise. Assumed to be acyclic - translate() does
53 not check for cycles and will recurse indefinitely on one.
55 @param identifier: str
56 Identifies which of the surrounding project's algorithmic phases this
57 graph belongs to (e.g. "SetupSimulation", "TimeStep", "Plot"). Every
58 generated Observer's name is prefixed with this identifier, joined by
59 an underscore, so Observers coming from different phases never clash
60 even if their tasks happen to share a name.
62 @param peano_project: peano4.api.Project
63 Not used by this compiler. @ref peano4.api.taskgraph.compiler
65 @return [peano4.api.solversteps.Step]
66 One Observer per task, ordered so that every task's Observer comes
67 after the Observers of all tasks it depends on, carrying that task's
68 action_sets (in the order they were added to the Task) plus the
69 cell/face/vertex data use_cell()/use_face()/use_vertex() determined.
70 Each Step's cell_data_access/face_data_access/vertex_data_access is
71 also populated as a copy of the originating Task's, purely for
73 peano4.api.taskgraph.compiler.ReportCompiledStepSequence) - nothing
74 about the generated Observer depends on it.
77 order = graph.ordered_tasks()
88 word[:1].upper() + word[1:]
89 for word
in re.split(
r"[^0-9A-Za-z_]+", task.name)
93 name=f
"{identifier}_{task_name}",
94 add_user_defined_actions=
False,
96 step.has_to_repeat = task.has_to_repeat
97 for cell_data
in task.cell_data:
98 step.use_cell(cell_data)
99 step.cell_data_access[cell_data] = task.cell_data_access[cell_data]
100 for face_data
in task.face_data:
101 step.use_face(face_data)
102 step.face_data_access[face_data] = task.face_data_access[face_data]
103 for vertex_data
in task.vertex_data:
104 step.use_vertex(vertex_data)
105 step.vertex_data_access[vertex_data] = task.vertex_data_access[vertex_data]
106 for action_set
in task.action_sets:
107 step.add_action_set(action_set)
111 report_compiled_step_sequence(steps, title=f
"Compiled task graph for {identifier}")