TExcelTAD
Home › Selected Topics: FFT, Strings, Parallelism & NP-Completeness › Greedy Scheduling and Brent's Bound

Greedy Scheduling and Brent's Bound

The work and span laws say that no scheduler can beat max⁡(T1/P,T∞)\max(T_1/P, T_\infty). 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 T1/P+T∞T_1/P + T_\infty, 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 cc can be treated as a chain of cc 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 PP strands are ready it runs some PP of them, and otherwise it runs all of them.

Call a step complete if PP strands ran in it, and incomplete if fewer did. A greedy scheduler may choose which PP ready strands to run in a complete step arbitrarily; the bound below holds for every choice.

text
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 t

Brent's bound

Theorem (greedy scheduling). On an ideal machine with PP processors, a greedy scheduler executes a computation with work T1T_1 and span T∞T_\infty in time

TP≤T1P+T∞.T_P \le \frac{T_1}{P} + T_\infty.

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 PP strands. If there were at least ⌊T1/P⌋+1\lfloor T_1/P \rfloor + 1 of them, they would run at least P(⌊T1/P⌋+1)>T1P(\lfloor T_1/P \rfloor + 1) > T_1 strands, more than the DAG has. So there are at most ⌊T1/P⌋\lfloor T_1/P \rfloor complete steps.

Incomplete steps. Let G′G' be the sub-DAG of strands not yet executed at the start of an incomplete step. Every strand with no predecessor in G′G' is ready. In an incomplete step the scheduler runs all ready strands, so it runs the first strand of every longest path of G′G', and the span of what remains drops by exactly 1. The span starts at T∞T_\infty, so there are at most T∞T_\infty incomplete steps.

Adding the two counts gives TP≤⌊T1/P⌋+T∞≤T1/P+T∞T_P \le \lfloor T_1/P \rfloor + T_\infty \le T_1/P + T_\infty.

Two consequences:

  • Factor 2 of optimal. Every scheduler needs TP∗≥max⁡(T1/P,T∞)T_P^* \ge \max(T_1/P, T_\infty), and T1/P+T∞≤2max⁡(T1/P,T∞)T_1/P + T_\infty \le 2\max(T_1/P, T_\infty). So any greedy schedule is at most twice as long as the best possible schedule.
  • Near-perfect speedup with slackness. Write the slackness as s=T1/(P T∞)s = T_1/(P\,T_\infty). Then T∞=T1/(sP)T_\infty = T_1/(sP) and the bound becomes TP≤(T1/P)(1+1/s)T_P \le (T_1/P)(1 + 1/s). 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 (T1=190T_1 = 190, T∞=13T_\infty = 13), choosing the ready strands with the smallest numbers first, and counts the complete and incomplete steps.

python · try it yourself
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: T1=82T_1 = 82 and T∞=22T_\infty = 22. 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.

python · try it yourself
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 T1/P+O(T∞)T_1/P + O(T_\infty), 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 (T1=3070T_1 = 3070, T∞=21T_\infty = 21). The ready list is kept sorted with a std::set, so the scheduler always runs the smallest-numbered ready strands.

cpp · try it yourself
#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 T1/16≈192T_1/16 \approx 192, a speedup of almost 15: the parallelism, about 146, is nine times the number of processors.

Test yourself

A computation has T1=10 000T_1 = 10\,000 and T∞=100T_\infty = 100. What does Brent's bound guarantee for a greedy scheduler on 50 processors?

  1. T50≤100T_{50} \le 100
  2. T50≤200T_{50} \le 200
  3. T50≤300T_{50} \le 300
  4. T50≤10 100T_{50} \le 10\,100

In the proof of Brent's bound, why are there at most T∞T_\infty incomplete steps?

  1. Each incomplete step runs all ready strands, which shortens the remaining longest path by one
  2. Incomplete steps only happen at the end
  3. Each incomplete step runs at least one strand
  4. Because T∞≤T1/PT_\infty \le T_1/P

Why is every greedy schedule within a factor of 2 of an optimal schedule?

  1. Because greedy schedulers are randomized
  2. Because T1/P+T∞≤2max⁡(T1/P,T∞)T_1/P + T_\infty \le 2\max(T_1/P, T_\infty) and every schedule needs at least max⁡(T1/P,T∞)\max(T_1/P, T_\infty)
  3. Because the span is at most half the work
  4. Because it runs the critical path first

With parallel slackness s=T1/(P T∞)=20s = T_1/(P\,T_\infty) = 20, Brent's bound is within what factor of perfect linear speedup?

  1. 1.051.05
  2. 1.21.2
  3. 22
  4. 2020