Greedy Scheduling and Brent's Bound
The work and span laws say that no scheduler can beat . Is there a scheduler that comes close? Remarkably, the simplest imaginable one does: at every step, run as many ready strands as there are processors. This lesson proves that such a greedy scheduler is never worse than , which is within a factor of 2 of optimal, and simulates it on real computation DAGs.
Greedy schedulers
Think of time in unit steps, with every strand costing one step (a strand of cost can be treated as a chain of unit strands). A strand is ready when all its predecessors in the DAG have finished. A scheduler is greedy if it never leaves a processor idle while a strand is ready: in each step, if at least strands are ready it runs some of them, and otherwise it runs all of them.
Call a step complete if strands ran in it, and incomplete if fewer did. A greedy scheduler may choose which ready strands to run in a complete step arbitrarily; the bound below holds for every choice.
GREEDY-SCHEDULE(G, P) // G: a DAG of unit strands
1 ready = the strands of G with no predecessors
2 t = 0
3 while ready is not empty
4 run = any min(P, |ready|) strands of ready
5 execute the strands of run in step t; remove them from ready
6 add to ready every strand whose last predecessor was in run
7 t = t + 1
8 return tBrent's bound
Theorem (greedy scheduling). On an ideal machine with processors, a greedy scheduler executes a computation with work and span in time
The idea goes back to Brent (1974), with the general form for greedy schedules due to Graham's analysis of list scheduling.
Proof
Count the two kinds of steps separately.
Complete steps. Each runs strands. If there were at least of them, they would run at least strands, more than the DAG has. So there are at most complete steps.
Incomplete steps. Let be the sub-DAG of strands not yet executed at the start of an incomplete step. Every strand with no predecessor in is ready. In an incomplete step the scheduler runs all ready strands, so it runs the first strand of every longest path of , and the span of what remains drops by exactly 1. The span starts at , so there are at most incomplete steps.
Adding the two counts gives .
Two consequences:
- Factor 2 of optimal. Every scheduler needs , and . So any greedy schedule is at most twice as long as the best possible schedule.
- Near-perfect speedup with slackness. Write the slackness as . Then and the bound becomes . With a slackness of 10, a greedy scheduler is within 10% of perfect linear speedup. This is why parallel algorithms are designed for parallelism far above the number of processors.
A greedy scheduler at work
The program runs a greedy scheduler on the DAG of P-SUM for 64 numbers (, ), choosing the ready strands with the smallest numbers first, and counts the complete and incomplete steps.
def p_sum_dag(n):
edges, count = [], [0]
def rec(lo, hi):
if hi - lo == 1:
count[0] += 1
return count[0] - 1, count[0] - 1
u = count[0]; count[0] += 1
mid = (lo + hi) // 2
f1, l1 = rec(lo, mid)
f2, l2 = rec(mid, hi)
v = count[0]; count[0] += 1
edges.extend([(u, f1), (u, f2), (l1, v), (l2, v)])
return u, v
rec(0, n)
return count[0], edges
def span(n_strands, edges):
longest = [1] * n_strands
for u, v in sorted(edges): # every edge goes to a larger number
longest[v] = max(longest[v], longest[u] + 1)
return max(longest)
def greedy(n_strands, edges, P, key=lambda s: s):
succ = [[] for _ in range(n_strands)]
indeg = [0] * n_strands
for u, v in edges:
succ[u].append(v); indeg[v] += 1
ready = [s for s in range(n_strands) if indeg[s] == 0]
complete = incomplete = 0
while ready:
ready.sort(key=key)
run, ready = ready[:P], ready[P:]
if len(run) == P: complete += 1
else: incomplete += 1
for s in run:
for w in succ[s]:
indeg[w] -= 1
if indeg[w] == 0:
ready.append(w)
return complete + incomplete, complete, incomplete
n_strands, edges = p_sum_dag(64)
T1, Tinf = n_strands, span(n_strands, edges)
print(f"T1 = {T1}, Tinf = {Tinf}")
print(" P T_P complete incomplete max(T1/P, Tinf) T1/P + Tinf")
for P in [1, 2, 4, 8, 16, 32, 64]:
TP, c, i = greedy(n_strands, edges, P)
print(f"{P:3} {TP:5} {c:9} {i:11} {max(T1 / P, Tinf):17.1f} {T1 / P + Tinf:13.1f}")T1 = 190, Tinf = 13
P T_P complete incomplete max(T1/P, Tinf) T1/P + Tinf
1 190 190 0 190.0 203.0
2 99 91 8 95.0 108.0
4 54 44 10 47.5 60.5
8 32 20 12 23.8 36.8
16 21 9 12 13.0 24.9
32 15 4 11 13.0 18.9
64 13 1 12 13.0 16.0
Every row lies between the lower bound and Brent's bound, and the incomplete steps never exceed 13, the span. With few processors almost every step is complete and the speedup is nearly perfect; with 32 or 64 processors the span dominates and most steps are incomplete.
Which strands to run first?
The bound allows any choice among the ready strands, and the choice does matter, though never by more than the factor of 2. In the DAG below a source strand releases a chain of 20 strands and 60 independent strands, and a sink waits for all of them: and . With 4 processors, a scheduler that favours the chain finishes in nearly the optimum, while one that favours the independent strands lets the critical path wait.
def greedy(n_strands, edges, P, key):
succ = [[] for _ in range(n_strands)]
indeg = [0] * n_strands
for u, v in edges:
succ[u].append(v); indeg[v] += 1
ready = [s for s in range(n_strands) if indeg[s] == 0]
steps = 0
while ready:
ready.sort(key=key)
run, ready = ready[:P], ready[P:]
steps += 1
for s in run:
for w in succ[s]:
indeg[w] -= 1
if indeg[w] == 0:
ready.append(w)
return steps
L, K = 20, 60
source, chain, free, sink = 0, list(range(1, L + 1)), list(range(L + 1, L + K + 1)), L + K + 1
edges = [(source, chain[0])] + list(zip(chain, chain[1:])) + [(chain[-1], sink)]
edges += [(source, f) for f in free] + [(f, sink) for f in free]
n = sink + 1
T1, Tinf, P = n, L + 2, 4
print(f"T1 = {T1}, Tinf = {Tinf}, P = {P}")
print("lower bound max(T1/P, Tinf):", max(T1 / P, Tinf))
print("greedy, chain first: ", greedy(n, edges, P, key=lambda s: s))
print("greedy, independent first:", greedy(n, edges, P, key=lambda s: -s))
print("Brent's bound T1/P + Tinf:", T1 / P + Tinf)T1 = 82, Tinf = 22, P = 4
lower bound max(T1/P, Tinf): 22
greedy, chain first: 22
greedy, independent first: 37
Brent's bound T1/P + Tinf: 42.5
Favouring strands on long paths is the idea behind critical-path scheduling, but finding an optimal schedule for a general DAG is NP-hard, and real runtimes do not know the DAG in advance anyway. They use randomized work stealing instead: each processor works on its own deque of spawned strands, and an idle processor steals from a random victim. Blumofe and Leiserson proved that work stealing runs in expected time , the same shape as Brent's bound, with low overhead in practice. This is how Cilk, oneTBB, Java's fork-join pool and Rust's Rayon schedule tasks.
In C++
The same greedy simulation in C++, on the P-SUM DAG for 1024 numbers (, ). The ready list is kept sorted with a std::set, so the scheduler always runs the smallest-numbered ready strands.
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
int strands = 0;
std::vector<std::pair<int, int>> edges;
std::pair<int, int> build(int lo, int hi) {
if (hi - lo == 1) { int s = strands++; return {s, s}; }
int u = strands++, mid = (lo + hi) / 2;
auto [f1, l1] = build(lo, mid);
auto [f2, l2] = build(mid, hi);
int v = strands++;
edges.insert(edges.end(), {{u, f1}, {u, f2}, {l1, v}, {l2, v}});
return {u, v};
}
int main() {
build(0, 1024);
std::vector<std::vector<int>> succ(strands);
std::vector<int> indeg0(strands, 0);
for (auto [u, v] : edges) { succ[u].push_back(v); indeg0[v]++; }
const int T1 = strands, Tinf = 21;
std::printf(" P T_P T1/P + Tinf\n");
for (int P : {1, 4, 16, 64, 256}) {
std::vector<int> indeg = indeg0;
std::set<int> ready;
for (int s = 0; s < strands; s++) if (indeg[s] == 0) ready.insert(s);
int steps = 0;
while (!ready.empty()) {
std::vector<int> run;
while (!ready.empty() && (int)run.size() < P) { run.push_back(*ready.begin()); ready.erase(ready.begin()); }
for (int s : run)
for (int w : succ[s]) if (--indeg[w] == 0) ready.insert(w);
steps++;
}
std::printf("%4d %6d %13.1f\n", P, steps, (double)T1 / P + Tinf);
}
} P T_P T1/P + Tinf
1 3070 3091.0
4 777 788.5
16 205 212.9
64 63 69.0
256 29 33.0
With 16 processors the schedule takes 205 steps, 13 more than , a speedup of almost 15: the parallelism, about 146, is nine times the number of processors.
Test yourself
A computation has and . What does Brent's bound guarantee for a greedy scheduler on 50 processors?
In the proof of Brent's bound, why are there at most incomplete steps?
- Each incomplete step runs all ready strands, which shortens the remaining longest path by one
- Incomplete steps only happen at the end
- Each incomplete step runs at least one strand
- Because
Why is every greedy schedule within a factor of 2 of an optimal schedule?
- Because greedy schedulers are randomized
- Because and every schedule needs at least
- Because the span is at most half the work
- Because it runs the critical path first
With parallel slackness , Brent's bound is within what factor of perfect linear speedup?