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 is associative, a Fork/Join task that splits a range in two, solves each half (in parallel or not) and combines the results with returns , 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 split at , the induction hypothesis says that the two subtasks return and . By associativity, . 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.
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 of the sequential time can be parallelized perfectly and the rest, , cannot, the speedup with processors is
and it can never exceed .
Proof. With the sequential time equal to 1, the sequential part takes whatever the number of processors, and the parallel part, divided among processors, takes . The parallel time is their sum, and the speedup is the quotient . As , , so increases towards , and since for every finite , it never reaches it.
Gustafson's law
Claim. If, with processors, the parallel program runs in time 1, spending a fraction of it in the sequential part and in the parallel part, then the speedup compared with running the same (larger) problem on one processor is
Proof. On one processor, the sequential part still takes , and the parallel part, which kept processors busy for , takes . The sequential time is , and dividing by the parallel time, 1, gives the speedup.
The difference from Amdahl's law is the assumption. Amdahl fixes the problem and measures the fraction on the sequential execution; Gustafson fixes the time and measures on the parallel execution of a problem that grows with . They are not contradictory: they describe two different ways of using more processors.
The Karp-Flatt metric
Claim. If a program with processors reaches a measured speedup , the serial fraction that would explain it under Amdahl's law is
Proof. Amdahl's law with serial fraction says . Solving for : .
If the only loss came from a fixed sequential part, would be the same for every . A value of that grows with 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 be the work (the total number of operations, or the time with one processor) and the span (the longest chain of operations that depend on each other, or the time with unlimited processors). Let be the time with processors.
Claim 1 (lower bounds). Every schedule satisfies and .
Proof. In each unit of time, processors execute at most operations, so operations need at least units. And the operations of the longest chain must be executed one after another, whatever the number of processors, so they need at least units.
Claim 2 (Brent's bound). A greedy scheduler, one that never leaves a processor idle while there is a task ready to run, achieves
Proof. Divide the execution into steps of one unit. Call a step complete if all processors work in it, and incomplete otherwise. In each complete step, operations are executed, so there are at most 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 incomplete steps. Adding both kinds gives the bound.
Combining both claims, a greedy scheduler is always within a factor 2 of the best possible time, since . 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 , called the parallelism, is the number of processors beyond which adding more cannot help much: when , the term dominates. For the parallel sum, and , so the parallelism grows almost linearly with .
The span of the parallel sum is logarithmic
Claim. The Fork/Join sum with threshold has work and span .
Proof. The tree of tasks has leaves, each adding elements, and internal nodes, each doing a constant amount of work (creating two subtasks and adding two results). So . Along any path from the root to a leaf there are internal nodes, each of constant cost, and then one leaf of cost ; the two halves at each node run in parallel, so the span is the cost of one path: .
With a constant threshold the span is . A larger threshold increases the span but reduces the overhead of the tasks, which is the trade-off measured in the Fork/Join lesson.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.