A great follow-up question!
Congratulations on performing a topological sorting on your targets with dependencies. Now, you're ready to take advantage of parallelism. Here's the next step:
After topological sorting, group targets by their "level" or "rank"
Recall that topological sorting produces a linear ordering of targets, e.g., [T1, T2, T3, T4, T5, ...], such that for every dependency A -> B, A comes before B in the ordering.
Now, assign a level or rank to each target:
- Initialize the level of all targets with no dependencies (i.e., no incoming edges) to 0. These are your "root" targets.
- Iterate through the sorted list: for each target
T, set its level to max(level(P)) + 1, where P is the set of all direct predecessors of T (i.e., targets that T depends on).
Here's a simple example:
Suppose your dependencies are:
T1 -> T3
T2 -> T3
T2 -> T4
T3 -> T5
T4 -> T5
Topological sorting yields: [T1, T2, T3, T4, T5]
Now, assign levels:
T1 and T2 have no dependencies, so level(T1) = level(T2) = 0.
T3 depends on T1 and T2, so level(T3) = max(level(T1), level(T2)) + 1 = max(0, 0) + 1 = 1.
T4 depends only on T2, so level(T4) = level(T2) + 1 = 0 + 1 = 1.
T5 depends on T3 and T4, so level(T5) = max(level(T3), level(T4)) + 1 = max(1, 1) + 1 = 2.
The resulting levels are:
Level 0: [T1, T2] # no dependencies
Level 1: [T3, T4] # depend on Level 0
Level 2: [T5] # depends on Level 1
Here's the magic part:
All targets at the same level can be built in parallel!
Why? Because, by construction:
- Targets at level
L only depend on targets at levels < L.
- There's no dependency between targets at the same level
L (otherwise, the topological sorting would have ordered them differently).
In our example:
- Build
T1 and T2 in parallel (Level 0). No dependencies to worry about.
- Once
T1 and T2 are done, build T3 and T4 in parallel (Level 1). Both depend on Level 0, but not on each other.
- After
T3 and T4 are complete, build T5 (Level 2). It depends on both T3 and T4, but they are guaranteed to be finished.
To summarize:
- Topological sorting orders targets by dependencies.
- Assign levels based on the maximum level of direct predecessors.
- Group targets by their levels.
- Build all targets at the same level in parallel.
Some additional tips:
- You can use a queue data structure to manage the levels: start with Level 0 targets in the queue. When all targets at Level
L are processed (i.e., built), move to the next level L+1 and enqueue those targets.
- If you're using a build system like Make, Ninja, or Bazel, they often have built-in support for parallelism. You can usually specify the number of concurrent jobs (
-j flag in Make, --jobs in Ninja, etc.). These systems will automatically handle the level-based parallelism for you.
- When there's a diamond dependency pattern (e.g.,
T1 -> T2, T1 -> T3, T2 -> T4, T3 -> T4), your levels will correctly handle the convergence: T2 and T3 can run in parallel (same level), and T4 waits for both (next level).
Now, go parallelize those builds!