Work, Span and Parallelism
How fast can a fork-join program run on 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 be the running time on processors with some scheduler.
- The work 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 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, processors execute at most steps of work, so
Span law. A machine with 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
Speedup and parallelism
The speedup on processors is . By the work law it is at most . When it is the speedup is linear, and when it equals it is perfect linear speedup.
The ratio
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, , 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 , perfect linear speedup is impossible.
The ratio of the parallelism to the number of processors, , 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 and run in series (every strand of before every strand of ), 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 |
|---|---|---|
| then | ||
| in parallel with |
For the parallel sum P-SUM of the previous lesson, a call on numbers is a split strand, then the two halves in parallel, then an add strand. With unit strands this gives
so , , and the parallelism is . The program computes the two measures with the composition rules and compares them with the closed forms.
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 elements. The work is still , but now the span recurrence is , which is , and the parallelism is a constant. Splitting at a fixed fraction other than one half is fine, though: a to split still has span .
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 . For small the work term dominates and good speedup is possible; once passes the parallelism, the span term takes over and the curve flattens. The program tabulates the lower bound for an algorithm with work and span (parallelism 1000).
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 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 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 shrinks, while the parallelism falls as grows. The C++ program computes both measures exactly with the composition rules.
#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 , 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 .
Test yourself
An algorithm has work and span . What is the best possible running time on processors?
- 40
- 120
- 300
- 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?
- 50
- 70
- 80
- 100
What does the parallelism bound?
- The number of strands
- The speedup on any number of processors
- The work on processors
- The number of spawns
A recursion that spawns one element and recurses on the other has which span?