Skip to main content

Proofs: Parallel Algorithms

This page derives the formulas of the performance lesson and proves that the Fork/Join scheme computes the same result as the sequential algorithm. Times are measured relative to the sequential time, which we take as 1 unless stated otherwise.


Fork/Join computes the same result as the sequential algorithm​

Claim. If the combination operation ⊕\oplus is associative, a Fork/Join task that splits a range in two, solves each half (in parallel or not) and combines the results with ⊕\oplus returns a0⊕a1⊕⋯⊕an−1a_0 \oplus a_1 \oplus \dots \oplus a_{n-1}, the same value as a sequential loop.

Proof. By strong induction on the size of the range. A range below the threshold is processed by the sequential loop, which returns its combination by definition. For a larger range [s,e)[s, e) split at mm, the induction hypothesis says that the two subtasks return L=as⊕⋯⊕am−1L = a_s \oplus \dots \oplus a_{m-1} and R=am⊕⋯⊕ae−1R = a_m \oplus \dots \oplus a_{e-1}. By associativity, L⊕R=as⊕⋯⊕ae−1L \oplus R = a_s \oplus \dots \oplus a_{e-1}. The order in which the subtasks run does not matter, because each one only reads its own part of the input and writes nothing shared; only the order in which the results are combined matters, and it is always left before right. ■\blacksquare

Associativity is what allows the split. Sum, product, maximum, minimum and concatenation are associative. Floating-point addition is associative only approximately: summing double values in a different grouping can change the last digits of the result, which is why a parallel sum of double numbers may differ slightly from the sequential one.


Amdahl's law​

Claim. If a fraction ff of the sequential time can be parallelized perfectly and the rest, 1−f1 - f, cannot, the speedup with pp processors is

S(p)=1(1−f)+fpS(p) = \frac{1}{(1 - f) + \dfrac{f}{p}}

and it can never exceed 11−f\dfrac{1}{1 - f}.

Proof. With the sequential time equal to 1, the sequential part takes 1−f1 - f whatever the number of processors, and the parallel part, divided among pp processors, takes f/pf / p. The parallel time is their sum, and the speedup is the quotient 1/Tp1 / T_p. As p→∞p \to \infty, f/p→0f / p \to 0, so S(p)S(p) increases towards 1/(1−f)1 / (1 - f), and since f/p>0f / p \gt 0 for every finite pp, it never reaches it. ■\blacksquare


Gustafson's law​

Claim. If, with pp processors, the parallel program runs in time 1, spending a fraction ss of it in the sequential part and 1−s1 - s in the parallel part, then the speedup compared with running the same (larger) problem on one processor is

S(p)=s+p(1−s)=p−s(p−1)S(p) = s + p (1 - s) = p - s (p - 1)

Proof. On one processor, the sequential part still takes ss, and the parallel part, which kept pp processors busy for 1−s1 - s, takes p(1−s)p (1 - s). The sequential time is s+p(1−s)s + p(1 - s), and dividing by the parallel time, 1, gives the speedup. ■\blacksquare

The difference from Amdahl's law is the assumption. Amdahl fixes the problem and measures the fraction ff on the sequential execution; Gustafson fixes the time and measures ss on the parallel execution of a problem that grows with pp. They are not contradictory: they describe two different ways of using more processors.


The Karp-Flatt metric​

Claim. If a program with pp processors reaches a measured speedup SS, the serial fraction ee that would explain it under Amdahl's law is

e=1/S−1/p1−1/pe = \frac{1/S - 1/p}{1 - 1/p}

Proof. Amdahl's law with serial fraction e=1−fe = 1 - f says 1S=e+1−ep\frac{1}{S} = e + \frac{1 - e}{p}. Solving for ee: 1S−1p=e(1−1p)\frac{1}{S} - \frac{1}{p} = e \left( 1 - \frac{1}{p} \right). ■\blacksquare

If the only loss came from a fixed sequential part, ee would be the same for every pp. A value of ee that grows with pp therefore reveals overheads that grow with the number of processors: synchronization, task creation, or contention for memory.


Work, span and the time with p processors​

Let WW be the work (the total number of operations, or the time with one processor) and S∞S_\infty the span (the longest chain of operations that depend on each other, or the time with unlimited processors). Let TpT_p be the time with pp processors.

Claim 1 (lower bounds). Every schedule satisfies Tp≥W/pT_p \ge W / p and Tp≥S∞T_p \ge S_\infty.

Proof. In each unit of time, pp processors execute at most pp operations, so WW operations need at least W/pW / p units. And the operations of the longest chain must be executed one after another, whatever the number of processors, so they need at least S∞S_\infty units. ■\blacksquare

Claim 2 (Brent's bound). A greedy scheduler, one that never leaves a processor idle while there is a task ready to run, achieves

Tp≤Wp+S∞T_p \le \frac{W}{p} + S_\infty

Proof. Divide the execution into steps of one unit. Call a step complete if all pp processors work in it, and incomplete otherwise. In each complete step, pp operations are executed, so there are at most W/pW / p complete steps. In an incomplete step, the scheduler runs every ready operation (otherwise a processor would be idle with work available). In particular, it runs the first operation of every longest remaining chain, so each incomplete step reduces the length of the longest remaining chain by one. There are therefore at most S∞S_\infty incomplete steps. Adding both kinds gives the bound. ■\blacksquare

Combining both claims, a greedy scheduler is always within a factor 2 of the best possible time, since Wp+S∞≤2max⁡(Wp,S∞)\frac{W}{p} + S_\infty \le 2 \max\left(\frac{W}{p}, S_\infty\right). Fork/Join's work stealing is not exactly greedy, but it achieves the same bound in expectation, plus an overhead proportional to the span (Blumofe and Leiserson, 1999).

Consequence. The ratio W/S∞W / S_\infty, called the parallelism, is the number of processors beyond which adding more cannot help much: when p>W/S∞p \gt W / S_\infty, the term S∞S_\infty dominates. For the parallel sum, W∈O(n)W \in O(n) and S∞∈O(log⁡n)S_\infty \in O(\log n), so the parallelism grows almost linearly with nn.


The span of the parallel sum is logarithmic​

Claim. The Fork/Join sum with threshold tt has work O(n)O(n) and span O(t+log⁡(n/t))O(t + \log(n/t)).

Proof. The tree of tasks has n/tn / t leaves, each adding tt elements, and n/t−1n/t - 1 internal nodes, each doing a constant amount of work (creating two subtasks and adding two results). So W=O(n)W = O(n). Along any path from the root to a leaf there are log⁡2(n/t)\log_2 (n / t) internal nodes, each of constant cost, and then one leaf of cost O(t)O(t); the two halves at each node run in parallel, so the span is the cost of one path: S∞=O(log⁡(n/t)+t)S_\infty = O(\log (n/t) + t). ■\blacksquare

With a constant threshold the span is O(log⁡n)O(\log n). A larger threshold increases the span but reduces the overhead of the tasks, which is the trade-off measured in the Fork/Join lesson.

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.