1.2 Classifying a Scientific Workload
1.2.1 Motivation
Before you choose an execution model, look at your workload along two independent axes. One is the bottleneck, the resource that stops the job from running faster. The other is the task structure, the dependencies that connect one piece of work to the next.
A CPU-bound job can have any structure, and an embarrassingly parallel job can be held back by the CPU, by memory or by I/O. The tables below help you place your job on both axes.
1.2.2 The limiting resource
| Bottleneck | What it looks like | Typical R example | Gain from parallelism |
|---|---|---|---|
| CPU-bound | The core is busy with arithmetic. A faster CPU would finish sooner. | A simulation that draws random numbers, transforms them and runs a test on each replicate. | High, up to the number of cores. |
| Memory-bandwidth-bound | The core waits for RAM, although the CPU meter still reads 100 percent. | Element-wise arithmetic such as y <- x * 2 on vectors far larger than the cache. |
Low past a few cores. On my 8-core desktop, eight workers each running x * 2 on their own 400 MB vector got through about 2.3 times the work of one worker, not 8 times. |
| Memory-capacity-bound | The data barely fits in RAM, and a larger problem would not fit at all. | Loading a 100 GB dataset into one matrix on a 256 GB machine to compute correlations. | Low on one machine. PSOCK workers each need their own copy. Forked workers can share a large numeric matrix that nobody writes. Splitting the data across machines can help. |
| I/O-bound | The CPU waits on disk or network. | Reading a thousand small files from a network share before fitting anything. | High while the storage has bandwidth to spare, then none. |
| Dependency-bound | Each step needs the result of the step before it. | An optimizer where iteration 3 cannot start until iteration 2 has finished. | None within one chain. Independent restarts or search branches can run in parallel. |
1.2.3 The shape of the work
| Shape | What it looks like | Example | Gain from parallelism |
|---|---|---|---|
| Embarrassingly parallel | Tasks run independently and meet only when the results are collected. | Fitting one model to each of one hundred datasets. | Best case. Any parallel system handles it. |
| Reductions | Each task produces a partial result, and an associative operation combines the partial results in any grouping. | Split a vector into segments, sum each segment on a worker, then add the sums. | Good, if combining is cheap next to the work. |
| Pipelines | The output of one stage is the input of the next, and different workers run different stages. | Read, filter, transform, fit, collect. | Limited. The slowest stage sets the pace, and the stages need buffers between them. |
| Iterative with shared state | Every iteration updates state that the next iteration reads. | Stochastic gradient descent, where each step updates the model that the next step uses. | Hard. Workers that update the same state race unless they lock it (contention) or keep local copies and merge them (communication). |
| Irregular task graphs | Tasks depend on other tasks, spawn new ones, and vary in size. | A tree search that prunes branches as good solutions turn up. | Uneven: high if load balancing works, unpredictable if it does not. |
1.2.4 Diagnosis
You need both axes, because they are independent. A bandwidth-bound job can be embarrassingly parallel and still gain little, since every task competes for the same memory bus.
A common misdiagnosis happens here: your CPU meter is at 100 percent, so you assume you are CPU-bound. A bandwidth-bound job pins the meter just as hard. The core is not idle while it waits for memory; it is stalled inside the pipeline, and a stalled core still reads as busy. Rprof() cannot separate the two, because it records which function is running and not why it is slow. Hardware counters can, through a tool such as Linux perf.
Without a profiler there is a cheap test. Run the same work on twice as many cores. CPU-bound work gets close to twice as fast. Bandwidth-bound work barely moves, because you added consumers to a bus that was already full.
Start with these questions, then look back at the tables to match your answers.
- Where is the time spent?
- If computation: likely CPU-bound.
- If waiting for data or I/O: I/O-bound or memory-bandwidth-bound.
- Can you partition the work into independent tasks?
- Yes: embarrassingly parallel. Good for most systems.
- No: either dependent tasks or shared state. More difficult.
- Do tasks share mutable state?
- No: ideal for process-based parallelism.
- Yes: give each worker a local copy and merge the copies at the end.
- How long does each task take?
- Seconds or more: the overhead of any model is small next to the work.
- Milliseconds or less: the cost of dispatching each task starts to dominate. Group tasks into chunks so that each message carries enough work.
- How much data moves between tasks?
- Small (kilobytes): serialization overhead is acceptable.
- Large (megabytes or gigabytes): split the data before sending it, have each worker read its own part from disk, or on Unix fork the workers after the data is loaded.
- Does load balance matter?
- Even workload across tasks: static assignment is fine.
- Uneven workload: dynamic load balancing is necessary. Work queues or work stealing help.
If you cannot answer these questions from observation, measure. See Profiling an R Workload Before Parallelizing It for how.
1.2.5 Connecting workloads to execution models
Embarrassingly-parallel, CPU-bound workloads with moderate data per task work well with any model: fork on Unix, PSOCK anywhere.
When the same kind of job is I/O-bound with long waits, it can run more workers than there are cores, because most of the workers are waiting at any moment.
Microsecond tasks lose most of their time to dispatch overhead in every process-based model, so batch them into larger chunks first. GNU R cannot run R code on several threads, which rules threads out for the R part of the work.
Irregular task graphs and variable load need dynamic load balancing from a work queue or a work-stealing scheduler. With forked or spawned workers, every dispatch from a central queue costs a message round trip.
Shared-state iterative algorithms are difficult in all models. Process isolation prevents data races but forces aggregation or message passing. Shared-memory models require locking or atomic operations. Distributed models need careful synchronization.
Memory-bandwidth-bound workloads stop gaining after a few workers. Measure the speedup at two and four workers before you add more.
Further reading
- A Map of Parallel R. The execution models in detail.
- When Parallelism Helps and When It Does Not. Costs that determine whether parallelism pays.
- Choosing a Parallel Execution Model. Decision table for your workload.