1.3 When Parallelism Helps and When It Does Not
1.3.1 Parallel slowdowns
Twice the cores should mean twice the speed, yet parallel jobs often finish later than the serial version. The cause is overhead. Every parallel system pays fixed costs before it computes anything: starting workers, moving data, scheduling tasks and collecting results. A long computation amortizes them. A short one does not, and you can pay for sixteen workers and still finish after one core would have.
1.3.2 Fixed overhead per worker
Every parallel system pays costs that do not scale with the problem size.
Worker startup
Threads start in microseconds and forked R processes in milliseconds. A spawned R process takes a fraction of a second to start bare, and seconds once it has loaded a large package stack.
Example: On my desktop (8-core Ryzen 7 7700X, Windows 11, R 4.6.1), makePSOCKcluster(4) took about 0.15 seconds with base R only, and makePSOCKcluster(16) about 0.25 seconds. Every package the workers load adds its load time on each worker, and that is usually the larger cost.
Take one hundred small tasks at fifty milliseconds each. That is five seconds of work on one core. Suppose starting sixteen workers and loading the packages they need also costs about five seconds. Even if the tasks split perfectly, the work itself now finishes in roughly three hundred milliseconds, so the job takes about five point three seconds against five seconds serial. You paid for sixteen workers and finished later.
Package loading and initialization
A spawned worker starts empty and must load every package itself. A forked worker inherits the packages its parent had already loaded.
sunlight’s Day 7 benchmark started 32 workers with Seurat (a heavy single-cell genomics package) and timed the run to a first result. The fastest spawned-worker framework, mirai, took 14.87 seconds. sunlight, which forks the workers from a template that loads Seurat once, took 4.50 seconds with the template already running and 4.82 seconds from a cold start. Most of the gap is package loading, which every spawned worker does for itself. The run favored sunlight: all frameworks ran in one process in a fixed order with sunlight last, against a warm file cache, and on WSL2, not bare-metal Linux.
Data movement startup cost
Before a worker can run, it must receive the input data. For PSOCK, this means serialization and socket transfer. On my desktop, serializing a 400 MB matrix into memory took about 0.7 seconds, and unserializing it about 0.2 seconds.
Example: Sending that matrix to four PSOCK workers with clusterExport() took about 1.7 seconds. That is small next to a task that runs for minutes, and about twenty times longer than sum() over the same matrix on one core (0.08 seconds).
1.3.3 Task granularity
Task granularity is the amount of computation per task. Fine-grained means small tasks. Coarse-grained means large tasks.
Parallelism has overhead per task: scheduling, context switching, and result collection. Small tasks do not justify that overhead.
Concrete example
Suppose you have one thousand tasks. Each task takes ten microseconds on a single core. Total time is ten milliseconds.
With eight workers and perfect load balancing, each worker gets one hundred twenty-five tasks, which is one point two five milliseconds of work. Scheduling costs at least a microsecond per task before any real work happens, so the thousand tasks carry about a millisecond of scheduling, and most of it is serial. The critical path is therefore closer to two point two five milliseconds than to one point two five, and the eightfold speedup you hoped for is about four and a half.
That is still a gain. It stops being one as soon as the per-task overhead approaches the per-task work, and at ten microseconds a task there is very little headroom.
A microsecond is the order of what a thread pool pays per task. An R worker process pays much more. On my desktop, clusterApply() sending 2,000 empty tasks one at a time to four PSOCK workers took about 40 microseconds per task, four times the task in this example. parLapply() avoids most of that cost by sending each worker one chunk of tasks.
Granularity rule of thumb
With R worker processes, a task, or a chunk of tasks, should run for at least a millisecond and preferably much longer. Batch shorter tasks into coarser units.
1.3.4 Data transfer cost
Every piece of data a worker uses must travel from the parent to the worker. The cost depends on the execution model.
PSOCK serialization cost
PSOCK serializes every object it sends. The parent encodes the object into bytes, the socket carries the bytes, and the worker decodes them into a new object, so each step passes over the whole object. In the 400 MB measurement above, encoding alone took about 0.7 seconds.
A job that moves tens of gigabytes between the parent and its workers spends real time on this. When the data crosses a network link that is already saturated, more workers make the transfer slower.
Fork copy-on-write cost
Fork shares memory at first. A write by the worker copies a page, and so does a garbage collection, which writes a mark bit into each object it reaches. The first collection is therefore the expensive one, and fork, copy-on-write, and the first GC measures the damage.
In sunlight’s Day 7 benchmark, a forked pool came out far short of the several-fold memory reduction that shared pages suggest. Each privatized page also costs a page fault and a 4 KB copy the first time the child writes it.
1.3.5 Load imbalance
If tasks do not take the same time, some workers finish early and idle while others work.
Example: You have four workers and four tasks. Task one takes one second. Tasks two, three, and four take ten milliseconds each. All four start together. Three of them are done after ten milliseconds and then sit idle, while the job waits on task one and finishes at one second. Serial time would have been one point zero three seconds. Four workers bought you thirty milliseconds, and three of them spent ninety-nine percent of the job doing nothing.
If tasks have unpredictable duration (like search algorithms with pruning), static assignment is unfair. Dynamic work queues or work stealing help. But that adds overhead.
1.3.6 The serial fraction and Amdahl’s law
Not all code parallelizes. Some parts must run in order. Some computations are irreducibly sequential. The serial fraction is the proportion of total time that cannot be parallelized.
Amdahl’s law
If serial fraction is \(f\) and parallel fraction is \(1 - f\), and you have \(n\) workers, the maximum speedup is:
\[\text{speedup} = \frac{1}{f + \frac{1-f}{n}}\]
As \(n\) goes to infinity, speedup approaches \(1/f\). You cannot exceed this limit no matter how many cores you add.
Concrete example with R
Suppose you write a parallel matrix computation. Here is what happens:
- Load data (one second): serial.
- Distribute data to workers (two seconds): serial at the dispatcher.
- Each worker computes a chunk (ten seconds with one worker, one second with ten workers): parallel.
- Gather and combine results (one second): serial.
Total time for one worker: one plus two plus ten plus one equals fourteen seconds.
Serial fraction: (one plus two plus one) divided by 14 equals 4/14, about 29 percent.
With ten workers the job takes 1 + 2 + 1 + 1 = 5 seconds, a speedup of 14 / 5 = 2.8x. The formula gives the same: 1 / (4/14 + (10/14)/10) = 2.8.
Do not expect 10x speedup. The four serial seconds limit you to 14 / 4 = 3.5x, however many workers you add.
Measured speedups usually fall below this calculation. The real serial fraction is often larger (file I/O, input checks, formatting the results), and data movement adds costs the model leaves out.
Amdahl’s law. The serial fraction sets a ceiling that no worker count can pass.
1.3.7 Contention
When multiple threads or processes access shared resources (disk, network, memory bus), they compete. Contention reduces the parallelism benefit.
1.3.8 Memory bandwidth saturation
A core can often compute faster than memory supplies bytes. When all the cores are waiting on the same memory bus, adding cores does not help.
Example: On my 8-core desktop I ran two operations over a 400 MB vector in one to eight PSOCK workers at the same time, each worker with its own vector. sum(x) scaled almost perfectly: eight workers got through eight times the work of one. R’s sum() adds one element at a time into an extended-precision accumulator, and on this machine it runs out of additions before memory runs out of bytes. y <- x * 2 reads 400 MB and writes 400 MB, and eight workers got through only about 2.3 times the work of one. Both look like one pass over memory, and only the second is bandwidth-bound.
1.3.9 When parallelism fails
Parallelism fails to pay when:
- Task granularity is fine (microseconds): overhead exceeds parallelism benefit.
- Startup cost is large (seconds) and tasks are short: amortization fails.
- Data transfer is expensive: serialization or network bandwidth dominates.
- Bottleneck is memory bandwidth, not CPU: adding cores increases contention.
- Serial fraction is large: at fifty percent serial, Amdahl’s law caps the speedup at 2x.
- Load is unbalanced and task cost is unpredictable: workers idle.
1.3.10 Honest assessment
Before you parallelize, profile the serial version. If one core is not at 100 percent, the job is waiting on something, and more workers help only while that resource has capacity to spare, such as a disk or network link that is not yet saturated. If the serial fraction is large, Amdahl’s law caps your speedup.
Further reading
- A Map of Parallel R. Startup costs for each execution model.
- Classifying a Scientific Workload. Identify your bottleneck before parallelizing.
- Choosing a Parallel Execution Model. Match your workload to a model.