Skip to content

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.

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.

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:

The steps of the build
The steps by level
0: generate2: lint1: compile3: compileTests4: docs5: test6: package7: sign8: publish
0: generate2: lint1: compile3: compileTests4: docs5: test6: package7: sign8: publishlevel 6level 5level 4level 3level 2level 1level 0
Input
  • Step asked about
Output
  • Level
TopOrderAndLevels levels = TopOrderAndLevels.of(steps);
// 3 - three steps run before it, one after the other
int 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]]
Full example
let levels = TopOrderAndLevels::of(&steps)?;
// 3 - three steps run before it, one after the other
let 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"]]
Full example
const levels = TopOrderAndLevels.of(steps);
// 3 - three steps run before it, one after the other
const 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]]
Full example
TopOrderAndLevels levels = TopOrderAndLevels.Of(steps);
// 3 - three steps run before it, one after the other
int 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]]
Full example

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.