2.3 Find which nodes can be processed in parallel
You turn a DAG (directed acyclic graph) into the stages of a schedule: you get the level of each node and a topological order level by level.
Problem
Section titled “Problem”You have several workers, such as threads, machines or people. You want to run tasks at the same time wherever they do not depend on each other. A topological order puts the tasks in a single line. You want stages instead: all tasks of a stage can run together, and a stage starts when the stage before it is done. You need this for:
- a build or a test run spread over several threads or machines;
- a computation over a DAG in which each node needs the results of its in-nodes;
- a plan for a team, in which tasks that do not depend on each other go to different people.
Example
Section titled “Example”The example graph steps models the steps that build and publish a small program. It has an edge
a -> b if step b may start only after step a. The array names holds the names of the steps.
generate and lint need nothing and can start together. compileTests and docs both wait only
for compile. Which steps can run at the same time, and on which level is test? The picture after
shows the steps in their levels, with one frame per level:
- Step asked about
- Level
Solution
Section titled “Solution”TopOrderAndLevels levels = TopOrderAndLevels.of(steps);// 3 - three steps run before it, one after the otherint levelOfTest = levels.levelOf(test);int levelCount = levels.levelCount(); // 7
// One stage per level: the steps of a stage have no edge between them.List<List<String>> stages = new ArrayList<>();for (int level = 0; level < levelCount; level++) { stages.add(new ArrayList<>());}for (int step : levels.order()) { stages.get(levels.levelOf(step)).add(names[step]);}// stages: [[generate, lint], [compile], [compileTests, docs], [test], [package],// [sign], [publish]]let levels = TopOrderAndLevels::of(&steps)?;// 3 - three steps run before it, one after the otherlet level_of_test = levels.level_of(test);let level_count = levels.level_count(); // 7
// One stage per level: the steps of a stage have no edge between them.let mut stages = vec![Vec::new(); level_count as usize];for &step in levels.order() { stages[levels.level_of(step) as usize].push(names[step as usize]);}// stages: [["generate", "lint"], ["compile"], ["compileTests", "docs"], ["test"],// ["package"], ["sign"], ["publish"]]const levels = TopOrderAndLevels.of(steps);// 3 - three steps run before it, one after the otherconst levelOfTest = levels.levelOf(test);const levelCount = levels.levelCount; // 7
// One stage per level: the steps of a stage have no edge between them.const stages: string[][] = [];for (let level = 0; level < levelCount; level++) { stages.push([]);}for (const step of levels.order) { stages[levels.levelOf(step)].push(names[step]);}// stages: [[generate, lint], [compile], [compileTests, docs], [test], [package],// [sign], [publish]]TopOrderAndLevels levels = TopOrderAndLevels.Of(steps);// 3 - three steps run before it, one after the otherint levelOfTest = levels.LevelOf(test);int levelCount = levels.LevelCount; // 7
// One stage per level: the steps of a stage have no edge between them.List<List<string>> stages = [];for (int level = 0; level < levelCount; level++){ stages.Add([]);}foreach (int step in levels.Order){ stages[levels.LevelOf(step)].Add(names[step]);}// stages: [[generate, lint], [compile], [compileTests, docs], [test], [package],// [sign], [publish]]A root node is a node without in-nodes. The level of a node is the number of edges on a longest path to it from a root node. So no edge and no path connects two nodes of the same level. See Find the starting points of a graph for how to find the root nodes.
TopOrderAndLevels.levelOfTopOrderAndLevels::level_ofTopOrderAndLevels.levelOfTopOrderAndLevels.LevelOf returns the level of a node, and
TopOrderAndLevels.levelCountTopOrderAndLevels::level_countTopOrderAndLevels.levelCountTopOrderAndLevels.LevelCount returns the number of levels. The code collects the
steps of each level into a stage. The build runs in seven stages, and two of them have two steps
each.
Good to know
Section titled “Good to know”- The longest path counts.
packageneedscompile, which is on level 1, butpackageis on level 4. It also waits fortest, which is on level 3. The number of levels is the length of the longest path plus one. That is the least number of stages the work needs, however many workers there are. - An order as well.
TopOrderAndLevels.orderTopOrderAndLevels::orderTopOrderAndLevels.orderTopOrderAndLevels.Orderis a topological order that lists the nodes level by level.TopOrderAndLevels.levelsTopOrderAndLevels::levelsTopOrderAndLevels.levelsTopOrderAndLevels.Levelsreturns the level of every node as a list. For the order alone,TopOrder.oftop_order::ofTopOrder.ofTopOrder.Ofneeds less memory. - Cycles. Like any topological order, the levels exist only for a DAG.
TopOrderAndLevels.ofTopOrderAndLevels::ofTopOrderAndLevels.ofTopOrderAndLevels.Ofrejects a graph with a cycle. - Steps of different levels can overlap.
docsis on level 2 andtestis on level 3, but no path connects them, so they can run at the same time as well. The levels put each node into the earliest stage its in-nodes allow. To keep every worker busy, start each step as soon as its last in-node has finished.
API used
Section titled “API used”TopOrderAndLevels.of(Graph)TopOrderAndLevels::ofTopOrderAndLevels.ofTopOrderAndLevels.OfTopOrderAndLevels.levelOf(int)TopOrderAndLevels::level_ofTopOrderAndLevels.levelOfTopOrderAndLevels.LevelOfTopOrderAndLevels.levels()TopOrderAndLevels::levelsTopOrderAndLevels.levelsTopOrderAndLevels.LevelsTopOrderAndLevels.levelCount()TopOrderAndLevels::level_countTopOrderAndLevels.levelCountTopOrderAndLevels.LevelCountTopOrderAndLevels.order()TopOrderAndLevels::orderTopOrderAndLevels.orderTopOrderAndLevels.OrderTopOrder.of(Graph)top_order::ofTopOrder.ofTopOrder.Of