1.1 A Map of Parallel R
1.1.1 Motivation
Parallel R covers several execution models. At one end, separate R processes share nothing and exchange copies of every object. At the other, threads inside a C library work directly on the memory of one R session. The choice fixes what you pay for startup, data movement, memory and safety, and the sections below price each model in those terms.
1.1.2 The models
Sequential vectorized execution
Your R code calls a linear algebra function such as %*%, crossprod() or solve(), and R hands the work to its BLAS (Basic Linear Algebra Subroutines) and LAPACK libraries. If R is linked to a multithreaded BLAS such as OpenBLAS or Intel MKL, one large matrix multiply runs on many cores. The reference BLAS that ships with R is single-threaded, so a default build gets no parallelism from this model.
What is shared: The matrices. The BLAS threads read the inputs and write the result directly in R’s memory, while the interpreter’s own thread waits for the call to return.
What is copied: Nothing. The BLAS routine works on the vectors R already allocated.
Where code runs: Your R expression runs in the main thread. The C code inside BLAS runs in the library’s own threads.
Startup cost: Close to none. OpenBLAS and MKL keep their threads alive between calls, so each call only hands its work to them.
Limits: Only linear algebra that reaches the BLAS or LAPACK. Your own loops stay serial.
Threads inside native libraries
You call a function from a native package, and its C or C++ code runs on several OpenMP threads or pthreads. data.table does this in fread(), fwrite(), sorting, and grouped summaries such as DT[, mean(x), by = g], which its GForce optimization computes in C across groups. Arbitrary R code inside j still runs on the one interpreter thread.
What is shared: The data inside R vectors. The package’s threads read and write those arrays through C pointers. Writing R Extensions strongly discourages calling the R API from those threads, because many of its functions modify R’s internal data structures.
What is copied: Nothing.
Where code runs: Your R code runs in the main interpreter. The C code’s threads run in the package binary.
Startup cost: None visible. The OpenMP runtime creates its threads on first use and keeps them for later calls.
Limits: Only functions that package authors wrote with parallel code. If the package does not have parallel code for a specific operation, you cannot add it from R.
R processes with socket connections (PSOCK)
The parallel package or future package starts separate R processes. Each worker is a complete R runtime. They communicate through sockets.
What is shared: Nothing. Each process has its own interpreter, heap, and symbol table.
What is copied: Data must serialize to bytes, travel over a socket, and unserialize on arrival. The bytes move, and a new R object reconstructs in the worker heap.
Where code runs: The function and its arguments are serialized as R objects, not as source text. The worker unserializes them and evaluates the call in its own interpreter.
Startup cost: From a fraction of a second to many seconds. Each worker starts a fresh R and then loads every package its tasks need. On my desktop (8-core Ryzen 7 7700X, Windows 11, R 4.6.1), makePSOCKcluster(16) took about 0.25 seconds with base R only. A heavy package stack loaded on every worker takes far longer.
Limits: Data that serializes and fits in memory. Large datasets and rapid task dispatch have high overhead.
Forked workers
Unix systems (Linux, macOS) use fork() to duplicate the parent process. The parallel::mclapply() function and future::plan(multicore) use this.
What is shared: Everything initially. The child inherits the parent’s memory through copy-on-write. Pages stay shared until a write causes the kernel to copy them.
What is copied: Nothing at first. On first write, the kernel copies that page. Garbage collection writes mark bits, triggering many copies, so most of the sharing is gone once the child has collected.
Where code runs: The child process runs the same R code in its own interpreter.
Startup cost: Milliseconds. The kernel duplicates page tables, not memory.
Limits: Unix only. Windows has no fork call, and mclapply() there refuses mc.cores > 1. R’s own documentation strongly discourages forking from a GUI, and future’s multicore plan falls back to sequential processing inside RStudio. The memory saving is also much smaller than copy-on-write suggests (see “Internal mechanics” below).
Spawned workers with cluster managers
External cluster managers (SLURM, PBS, Kubernetes) or the parallel package with explicit nodes start fresh R processes on machines across a network.
What is shared: Nothing. Each worker is a process with its own memory space, often on a different machine.
What is copied: Data serializes, travels over the network, and unserializes. Round-trip communication passes through the network stack.
Where code runs: The manager distributes tasks. Each worker unserializes and evaluates its assignment independently.
Startup cost: Seconds to minutes. Each worker boots from scratch, loads R and its packages, and a batch scheduler can add queue time before the job starts at all.
Limits: Large data transfers tax the network. Task dispatch and result collection add latency.
Experimental: Multiple interpreters in one process
The apartment model runs many interpreters in one process. Each interpreter owns its heap and runtime state. Code runs in one interpreter per task.
What is shared: The address space and the loaded native code. Passing R objects between interpreters without a copy is a design goal, not a shipped feature.
What is copied: In threaded-r today, every object a task sends or returns. Its THREAD cluster serializes data the way PSOCK does, but through memory instead of a socket.
Where code runs: Tasks run in different interpreters inside one process.
Startup cost: Lower than PSOCK. threaded-r’s own demo at two workers reports 1.8 times faster startup and 2.1 times less memory than PSOCK. That is the project’s own measurement at a small N, not an independent replication.
Limits: Experimental. Compiled packages must be rebuilt for it, and native code that keeps process-global state is not yet safe in more than one interpreter. See threaded-r.
What each execution model shares, how R objects cross a worker boundary, and what each costs to start.
1.1.3 Comparison table
This table summarizes the tradeoffs of each model.
| Model | Startup | Data in/out | Memory | Shared objects | Platform | Process isolation |
|---|---|---|---|---|---|---|
| BLAS/OpenMP | none | in heap | 1x | yes, in place | all | no |
| Native library threads | none | in pointers | 1x | yes, via C pointers | varies | no |
| PSOCK | under a second to seconds | serialize | Nx | no | all | yes |
| Fork | milliseconds | copy-on-write | close to Nx after GC | yes, until GC | Unix | yes |
| Spawned cluster | seconds-minutes | serialize + network | Nx | no | all | yes |
| Apartment (experimental) | below PSOCK | serialize, in memory | below Nx | not yet | Linux, Windows | partial |
Here N is the number of workers.
1.1.4 Out of scope
The sections above describe the models without recommending one, because the right choice depends on what limits your workload.
The next page in this section, Classifying a Scientific Workload, teaches you to identify what limits your job. Choosing a Parallel Execution Model connects workload shapes and constraints to these execution models.
1.1.5 Internal mechanics
The apparent memory saving of fork
Fork uses copy-on-write. The kernel marks shared pages read-only in both processes. When either process writes, the CPU raises a page fault. The kernel copies that page (typically four kilobytes) and resumes the write. Pages that nobody writes stay shared for as long as both processes live.
On paper that is nearly free, and sunlight’s measurements found otherwise.
sunlight forks its workers from a template process that has already loaded the user’s packages. Its benchmarks found that the sharing does not survive garbage collection in the worker: R’s collector writes a mark bit into the header of each object it reaches, and each write to a shared page makes the kernel copy that page. In the Day 7 run, the workers’ combined PSS was only 1.28 times lower than the combined RSS of the best spawned-worker setup at 8 workers with a tidyverse-class package set, and 1.10 times lower at 32 workers with Seurat. Setting PSS against RSS favors sunlight, and the run was on WSL2, not bare-metal Linux. The comparison leaves out crew, which did not start all its workers. See Fork, Copy-on-Write, and the First GC.
Reading an R object can still write to its page. Every SEXP header carries the collector’s bookkeeping (the mark bit, the generation, and the links that chain the object into the collector’s lists), and a collection rewrites it while the program itself only reads the data.
PSOCK: portable but slow
PSOCK serializes every object that crosses process boundaries. Serialization converts an R data structure into a byte string. On the other side, unserialize() reconstructs the object in the worker’s heap.
A byte stream works on every platform, which is why PSOCK runs on Windows. Its cost grows with the object. On my desktop, clusterExport() of a 400 MB matrix to four PSOCK workers took about 1.7 seconds, and the four copies then held 1.6 GB of worker memory on top of the original. For fine-grained work with rapid task dispatch, the fixed cost of each message adds up as well.
Further reading
- Processes vs Threads. Detailed cost analysis of process vs thread tradeoffs.
- Serialization. How data moves between processes.
- When Parallelism Helps and When It Does Not. Practical costs in real workloads.