TExcelTAD

Work, Span and Parallelism

How fast can a fork-join program run on PP processors? The answer depends on the scheduler, the machine and the input, but two numbers read off the computation DAG already bound it from both sides: the work, the total amount of computation, and the span, the length of the longest chain of dependences. This lesson defines them, proves the two laws they obey, and shows how to compute them for a recursive algorithm with two simple composition rules.

Two measures of a DAG

Give each strand of the computation DAG (The Fork-Join Model and Computation DAGs) its running time; in the examples every strand costs one step. Let TPT_P be the running time on PP processors with some scheduler.

  • The work T1T_1 is the total time of all strands. It is the running time on a single processor, which simply executes the strands one after another (the serial projection, plus a little bookkeeping).
  • The span T∞T_\infty is the largest total time along any path of the DAG, the critical path. It is the running time with an unlimited number of processors, each strand starting the moment its predecessors finish.

Two laws follow immediately.

Work law. In one time step, PP processors execute at most PP steps of work, so

TP≥T1P.T_P \ge \frac{T_1}{P}.

Span law. A machine with PP processors can do anything the idealized machine with unlimited processors can do in the same time, and the strands on a critical path run one after another whatever the number of processors, so

TP≥T∞.T_P \ge T_\infty.

Speedup and parallelism

The speedup on PP processors is T1/TPT_1 / T_P. By the work law it is at most PP. When it is Θ(P)\Theta(P) the speedup is linear, and when it equals PP it is perfect linear speedup.

The ratio

parallelism=T1T∞\text{parallelism} = \frac{T_1}{T_\infty}

is the most important single number of a parallel algorithm. Three readings of it:

  • It is the average amount of work available per step along the critical path.
  • By the span law, T1/TP≤T1/T∞T_1/T_P \le T_1/T_\infty, so it is an upper bound on the speedup, for any number of processors.
  • It is therefore the number of processors beyond which adding more cannot help: with P>T1/T∞P > T_1/T_\infty, perfect linear speedup is impossible.

The ratio of the parallelism to the number of processors, (T1/T∞)/P(T_1/T_\infty)/P, is called the parallel slackness. A slackness below 1 means some processors must idle; the next lesson shows that a slackness well above 1 lets a simple scheduler get near-perfect speedup.

Composition rules

Fork-join DAGs are built from two operations, so work and span follow from two rules. If sub-DAGs AA and BB run in series (every strand of AA before every strand of BB), the work and the span both add. If they run in parallel (no edges between them), the work adds but the span is the maximum:

Composition Work Span
AA then BB T1(A)+T1(B)T_1(A) + T_1(B) T∞(A)+T∞(B)T_\infty(A) + T_\infty(B)
AA in parallel with BB T1(A)+T1(B)T_1(A) + T_1(B) max⁡(T∞(A),T∞(B))\max(T_\infty(A), T_\infty(B))

For the parallel sum P-SUM of the previous lesson, a call on n≥2n \ge 2 numbers is a split strand, then the two halves in parallel, then an add strand. With unit strands this gives

W(n)=W(⌊n/2⌋)+W(⌈n/2⌉)+2,W(1)=1,S(n)=max⁡(S(⌊n/2⌋),S(⌈n/2⌉))+2,S(1)=1,\begin{aligned} W(n) &= W(\lfloor n/2 \rfloor) + W(\lceil n/2 \rceil) + 2, & W(1) &= 1, \\ S(n) &= \max\bigl(S(\lfloor n/2 \rfloor), S(\lceil n/2 \rceil)\bigr) + 2, & S(1) &= 1, \end{aligned}

so W(n)=3n−2=Θ(n)W(n) = 3n - 2 = \Theta(n), S(n)=2⌈lg⁡n⌉+1=Θ(lg⁡n)S(n) = 2\lceil \lg n \rceil + 1 = \Theta(\lg n), and the parallelism is Θ(n/lg⁡n)\Theta(n / \lg n). The program computes the two measures with the composition rules and compares them with the closed forms.

python · try it yourself
from functools import lru_cache
from math import ceil, log2

@lru_cache(maxsize=None)
def work_span(n):
    """work and span of P-SUM on n numbers, unit-cost strands"""
    if n == 1:
        return 1, 1
    w1, s1 = work_span(n // 2)
    w2, s2 = work_span(n - n // 2)
    return 1 + (w1 + w2) + 1, 1 + max(s1, s2) + 1     # split, halves in parallel, add

print("       n       work   span  parallelism   3n-2  2ceil(lg n)+1")
for n in [2, 10, 100, 1000, 10**6]:
    w, s = work_span(n)
    print(f"{n:8} {w:10} {s:6} {w / s:12.1f} {3 * n - 2:8} {2 * ceil(log2(n)) + 1:8}")
       n       work   span  parallelism   3n-2  2ceil(lg n)+1
       2          4      3          1.3        4        3
      10         28      9          3.1       28        9
     100        298     15         19.9      298       15
    1000       2998     21        142.8     2998       21
 1000000    2999998     41      73170.7  2999998       41

A million numbers have a span of only 41 steps, so in principle more than 73,000 processors could be kept busy. In practice the base case is coarsened (below), which lowers the parallelism by a constant factor but cuts the overhead.

A badly split recursion

The span depends on how the work is divided. Suppose the recursion peels off one element at a time: spawn the sum of A[lo] alone, and recurse on the remaining n−1n - 1 elements. The work is still Θ(n)\Theta(n), but now the span recurrence is S(n)=S(n−1)+2S(n) = S(n - 1) + 2, which is Θ(n)\Theta(n), and the parallelism is a constant. Splitting at a fixed fraction other than one half is fine, though: a 1/101/10 to 9/109/10 split still has span Θ(lg⁡n)\Theta(\lg n).

python · try it yourself
def work_span_table(n, split):
    """work and span of the sum for every size 1..n, bottom-up (no deep recursion)"""
    W, S = [0, 1], [0, 1]
    for m in range(2, n + 1):
        left = split(m)
        W.append(W[left] + W[m - left] + 2)
        S.append(max(S[left], S[m - left]) + 2)
    return W[n], S[n]

splits = {"halves": lambda n: n // 2,
          "1/10 : 9/10": lambda n: max(1, n // 10),
          "one at a time": lambda n: 1}
n = 900
for name, split in splits.items():
    w, s = work_span_table(n, split)
    print(f"{name:14} n = {n}: work {w}, span {s:4}, parallelism {w / s:6.1f}")
halves         n = 900: work 2698, span   21, parallelism  128.5
1/10 : 9/10    n = 900: work 2698, span  115, parallelism   23.5
one at a time  n = 900: work 2698, span 1799, parallelism    1.5

All three versions do exactly the same work, 2698 steps. The one-at-a-time split has a span of 1799 and a parallelism of 1.5: no number of processors makes it more than 1.5 times faster. The constant-fraction split loses a constant factor in span compared with halving, nothing more.

What the laws predict

Together the two laws say TP≥max⁡(T1/P,T∞)T_P \ge \max(T_1/P, T_\infty). For small PP the work term dominates and good speedup is possible; once PP passes the parallelism, the span term takes over and the curve flattens. The program tabulates the lower bound for an algorithm with work 10610^6 and span 10310^3 (parallelism 1000).

python · try it yourself
T1, Tinf = 10**6, 10**3
print("    P   lower bound on T_P   best possible speedup")
for P in [1, 10, 100, 500, 1000, 2000, 10000]:
    bound = max(T1 / P, Tinf)
    print(f"{P:5} {bound:16.0f} {T1 / bound:20.1f}")
    P   lower bound on T_P   best possible speedup
    1          1000000                  1.0
   10           100000                 10.0
  100            10000                100.0
  500             2000                500.0
 1000             1000               1000.0
 2000             1000               1000.0
10000             1000               1000.0

Up to 1000 processors, perfect linear speedup is not ruled out; from then on the best speedup stays at 1000. A parallel algorithm with parallelism Θ(n/lg⁡n)\Theta(n / \lg n) therefore scales to any fixed machine as the input grows, while one with constant parallelism never does.

In C++

Real implementations stop recursing below a grain size GG and sum small ranges serially. With strands that cost their number of additions plus one step for each split and add, the work grows only slightly as GG shrinks, while the parallelism falls as GG grows. The C++ program computes both measures exactly with the composition rules.

cpp · try it yourself
#include <algorithm>
#include <cstdio>
#include <utility>

// work and span of P-SUM on n numbers when ranges of at most g numbers are summed serially
std::pair<long long, long long> work_span(long long n, long long g) {
    if (n <= g) return {n, n};                     // a serial loop: n steps, all in series
    auto [w1, s1] = work_span(n / 2, g);
    auto [w2, s2] = work_span(n - n / 2, g);
    return {w1 + w2 + 2, std::max(s1, s2) + 2};    // split, halves in parallel, add
}

int main() {
    const long long n = 1 << 20;
    std::printf("   grain       work    span  parallelism\n");
    for (long long g : {1LL, 16LL, 256LL, 4096LL, 65536LL}) {
        auto [w, s] = work_span(n, g);
        std::printf("%8lld %10lld %7lld %12.1f\n", g, w, s, (double)w / s);
    }
}
   grain       work    span  parallelism
       1    3145726      41      76725.0
      16    1179646      48      24576.0
     256    1056766     280       3774.2
    4096    1049086    4112        255.1
   65536    1048606   65544         16.0

With a grain of 256 the work is less than 1% above nn, and the parallelism is still above 3000, far more than the cores of any desktop machine. That is the usual engineering trade-off: coarsen until the overhead disappears, as long as the parallelism stays well above PP.

Test yourself

An algorithm has work T1=12 000T_1 = 12\,000 and span T∞=40T_\infty = 40. What is the best possible running time on P=100P = 100 processors?

  1. 40
  2. 120
  3. 300
  4. 12 000

Two sub-computations with spans 30 and 50 run in parallel, followed in series by one with span 20. What is the total span?

  1. 50
  2. 70
  3. 80
  4. 100

What does the parallelism T1/T∞T_1/T_\infty bound?

  1. The number of strands
  2. The speedup on any number of processors
  3. The work on PP processors
  4. The number of spawns

A recursion that spawns one element and recurses on the other n−1n - 1 has which span?

  1. Θ(1)\Theta(1)
  2. Θ(lg⁡n)\Theta(\lg n)
  3. Θ(n)\Theta(n)
  4. Θ(nlg⁡n)\Theta(n \lg n)