Peano
Loading...
Searching...
No Matches
Sequential.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
3from typing import List
4
5import re
6import peano4.api
8
9from ..Graph import Graph
10from ..Task import Task
11from .ReportCompiledStepSequence import report_compiled_step_sequence
12
13
14def translate(graph: Graph, identifier: str, peano_project: "peano4.api.Project") -> List["peano4.api.solversteps.Step"]:
15 """!
16
17 Simplest possible task graph compiler
18
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
24 against.
25
26 ## Implementation
27
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().
34
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.
43
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.
50
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.
54
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.
61
62 @param peano_project: peano4.api.Project
63 Not used by this compiler. @ref peano4.api.taskgraph.compiler
64
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
72 reporting (@see
73 peano4.api.taskgraph.compiler.ReportCompiledStepSequence) - nothing
74 about the generated Observer depends on it.
75
76 """
77 order = graph.ordered_tasks()
78
79 steps = []
80 for task in order:
81 # Turn every whitespace-separated word into its own name segment,
82 # capitalising only its first letter - a blind .title() would also
83 # lower-case the rest of the word, which mangles existing camel
84 # case and acronyms (e.g. "EulerFVSmooth" would become
85 # "Eulerfvsmooth", "CFL" would become "Cfl").
86 # Words are separated by anything that C++ identifiers do not allow, too
87 task_name = "_".join(
88 word[:1].upper() + word[1:]
89 for word in re.split(r"[^0-9A-Za-z_]+", task.name)
90 if word
91 )
93 name=f"{identifier}_{task_name}",
94 add_user_defined_actions=False,
95 )
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)
108 steps.append(step)
109
110 peano4.api.Logger().new_section(f"Compile task graph '{identifier}'")
111 report_compiled_step_sequence(steps, title=f"Compiled task graph for {identifier}")
112
113 return steps
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
The Python-side model of "what happens during one mesh sweep".
Definition __init__.py:1
List["peano4.api.solversteps.Step"] translate(Graph graph, str identifier, "peano4.api.Project" peano_project)
Simplest possible task graph compiler.
Definition Sequential.py:14