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"""!
4
5The task graph abstraction allows users to formalise their solver steps over small task graphs which are then lowered into a sequence of Peano mesh traversals.
6
7
8@tableofcontents
9
10
11## Example
12
13An ExaHyPE solver's generator, i.e. the part that translates the pyysical model into a numerical workflow, describes each algorithmic phase (setup, time stepping, plotting)
14as a @ref peano4.api.taskgraph.Graph of @ref peano4.api.taskgraph.Task instances.
15Each task defines computations over a mesh entity (vertex, face, cell, or global), subject to dependencies between the tasks.
16Global tasks do not touch the mesh. They are tied to a traversal, to the start of a time step or to its end
17(@ref peano4.api.taskgraph.Task.Task.GridEntity). The latter two never cost a mesh traversal of their own.
18Typical examples for a task at the end of the step are the global CFL condition or a synchronisation of several solvers.
19A simple task graph reads as follows:
20
211. Run over all cells and compute A.
222. Run over all faces and compute B, meanwhile do a global calculation. Both steps require A to have finished.
233. Run over all vertices once B has completed.
244. Wait for the third step and the global calculation and run over all cells again.
25
26The solvers use this formalism to set up graphs similar to this one:
27
28@image html task-graph-example.png
29
30This is obviously strongly simplified.
31A proper yet still simplistic example can be found in @ref exahype2.api.fv.SetupSimulationOverRegularGrid.
922. Give them a name, specify if they can repeat (off by default) and specify over which mesh entity they operate.
933. Add action sets to the tasks. Action sets are the actual code that is triggered by a task (and executed during a mesh traversal corresponding to this task later).
944. Add dependencies between the tasks.
955. Add task to task graph.
96
97
98Note the nice feature at the end of the script: The API comes along
99with a routine to visualise the arising task graph. This is the same
100feature used for the example image above.
101
102## Mapping the task graph onto Peano
103
104The idea behind the task graph feature of Peano is that there are different
105compilers which take the task graph and lower it onto grid traversals.
106The term "lower" is taken from the xDSL compiler project and highlights that
107we go from more abstract definitions close to the actual implementation.
108
109There are different compilers available, each producing a different type of
110a task graph implementation. You find them all in @ref peano4.api.taskgraph.compiler.
111The simples one if @ref @ref peano4.api.taskgraph.compiler.Sequential, which
112maps the task graph onto a total order preservering the dependencies. Each task
113of the rearranged total order (each step) is then mapped onto one mesh traversal.
114This is the simplest way to execute a task graph, through obviously might not
115be the most efficient one.
116
117Besides the lowering onto Peano mesh traversals, the compilers also yield
118brief functions which we can call from the main() function later to step
119through all the mesh traversals which make up one task graph realisation.
125The task graph implicitly models dependencies over touch-first events. This is,
126if a task A is formalised over faces and B depends on cells, then the task graph
127compiler might later conclude that A and B can be done in one mesh sweep, as
128all touchFaceFirstTime() complete before any touchCellFirstTime() is called on
129adjacent faces. These dependencies are not validated. Task A might as well host
130an action set which does something in touchFaceLastTime() which feeds into B.
131It is the user's responsibility to ensure that the tasks are formalised correctly.
132</div>
133
134
135### Task dependencies
136
137Each task can depend on an arbitrary number of predecessor tasks. There is
138no feature to model outgoing dependencies. They are implicitly given by other
139tasks referencing predecessors.
140
141The task graph does not have to have a unique root task. It can comprise
142multiple ready tasks and can have multiple terminal tasks.
143
144### Tasks and action sets
145
146Each task can hold an arbitrary number of Peano 4 action sets (@ref peano4.api.solversteps.ActionSet).
147Each action set can plug into all kinds of events. This seems to be a contradiction:
148The task is tied to a particular mesh entity, while the action set can define actions for multiple mesh entities.
149The task association defines dependencies, the action set what is done.
150If task A depends on B and B is tied to vertices, any operation over a cell in
151A will be executed after all operations over the cell's adjacent vertices in B
152have completed.
153The action sets of A still might also do something on cells and faces as well,
154but this is not subject to the dependency handling.
155
156### Task repetition
157
158By default, tasks in the task graph do not repeat. This means that they are
159executed once. But users can attach a boolean expression to a task which is
160evaluated at the end of the task's execution. If it evaluates to true, then the task is repeated. This is useful for tasks that need to be executed multiple times until a certain condition is met.
161Unfortunately, a task that can repeat can never be merged with any successor,
162so these repeating tasks kind of veto any optimisation.
163
164### Task tags
165
166Each task can carry arbitrarily many tags. They are plain strings, stored
167in the set tags of @ref peano4.api.taskgraph.Task.Task, so one task can be
168tagged by several independent parties at the same time. Tags are global: