1.5 Data Movement Is Part of the Algorithm
1.5.1 Data movement shapes the bottleneck
In a sequential program the computation sets the runtime. In a parallel program the data movement counts as well: how much each worker receives and how much it sends back. On my desktop, serializing a 400 MB matrix took about 0.7 seconds before a single byte had moved, and a gigabit network link needs at least eight seconds to carry a gigabyte. Either cost can become the slowest phase of the job.
1.5.3 Environment chains and parent references
Environments have parent environments. A closure’s captured environment points to a parent, which points to its parent, up to the global environment or a package namespace.
outer <- function() {
big_data <- rnorm(1e6)
inner <- function() {
function(x) {
x + mean(big_data)
}
}
inner()
}
process_closure <- outer()
length(serialize(process_closure, NULL)) # about 8 MBThe innermost closure’s own environment is the frame of the inner() call, which is empty. Its parent is the frame of the outer() call, which holds big_data and inner. Serialization does not stop at the closure’s immediate environment: it walks the parent chain and writes every frame it meets, so process_closure serializes to about 8 MB, the size of big_data.
The walk stops at the global environment, at package namespaces and at the base environment. R writes those as references, and their contents do not travel. A function defined at top level therefore carries none of your global objects with it, which is why a PSOCK worker reports a missing object until you clusterExport() it. Inside the frames it does write, serialization cannot know which bindings the function uses, so it writes all of them.
1.5.4 Full copies in flight
When the main process serializes an object, the object exists in the main heap. Serialization converts it to bytes. Those bytes travel to the worker. Deserialization allocates new memory on the worker heap and rebuilds the object as a full copy. The object now exists twice: main heap and worker heap. With N workers receiving the same data, the object exists N+1 times simultaneously.
big_matrix <- matrix(rnorm(1e7), nrow=1000) # ~80 MB
results <- parallel_map(1:32, function(i) {
use_matrix(big_matrix, i) # matrix serialized to all 32 workers
})For this pattern, peak memory includes the original on the main process plus 32 copies on workers. That is 33 copies of 80 MB, or 2.6 gigabytes, for a matrix that only 32 workers read in parallel.
1.5.6 Zero-copy claims and what they actually mean
Some parallel systems advertise zero-copy data movement: the data lives in memory once, and a worker receives an address or a handle instead of a byte stream. The object’s bytes stay in place and only the address moves.
That is not free. The receiver still needs a message saying where the data is and that it is safe to read, and between processes it needs a shared mapping that the operating system sets up. Zero-copy also does not survive writes. Shared pages stay shared only while nobody writes them, and in a forked R worker the garbage collector writes object headers, so pages meant to be shared become private copies.
sunlight preloads packages in a template process and forks its workers from it, so the packages are never serialized and no worker loads them again. The memory does not stay shared: sunlight’s measurements found that a worker’s first garbage collection privatizes the inherited pages, so before its first task a forked worker holds a private copy of most of the template’s heap (fork, copy-on-write, and the first GC). The task function and its arguments are still serialized and sent with every chunk of work.
threaded-r’s THREAD cluster serializes the objects it passes between interpreters, as PSOCK does, but through memory instead of a socket. Each interpreter owns its own heap.
1.5.7 Moving less data
Partition early. Split the input before starting tasks, so each worker receives only its slice and no large object goes to every worker.
Preload what workers need. On Unix, load reference data (lookup tables, model coefficients, geographic boundaries) in the parent before forking, and the workers inherit it without serialization. On spawned workers, send it once per worker with clusterExport() or mirai’s everywhere(), not once per task.
Compute locally. If a worker can generate its own random data instead of receiving serialized data, the savings are immediate. One call to rnorm() beats deserializing a vector.
Return only summaries. Workers compute on huge datasets but return only small results. One worker processes a terabyte of genomic data and returns a p-value. The return trip costs microseconds instead of seconds.
Avoid implicit environment capture. Write functions that accept data as explicit arguments, and do not create the worker function inside a function whose frame holds large objects. The future package works differently from parallel here: it finds the global variables a function uses and exports them with each future, so a large global named in the function body travels as well.
1.5.8 The architecture signal from data movement costs
When data movement dominates your parallel runtime, it signals a mismatch between your parallelism strategy and your workload.
If serialization takes ten seconds and computation takes one second, you chose the wrong tool. The cost of moving data overwhelms the speedup.
If you send a gigabyte to each worker every iteration, ask whether that data changes. If it does not, send it once and keep it on the workers between iterations.
If the same large function, or the data its environment holds, travels with every task, the transmission is never amortized. Forking after the function exists, as mclapply() does, lets the children inherit it without serialization. With persistent workers, export it once with clusterExport() or mirai’s everywhere() and call it by name in each task.
Further reading
- R Core Team, Package ‘parallel’ <https://stat.ethz.ch/R-manual/R-devel/library/parallel/doc/parallel.pdf>, the package vignette, with worked examples that export data to workers.
- Serialization. How R turns objects into bytes.
- Closures. What a closure keeps alive.
- Environments. Frames and the parent chain.
Where this appears
- sunlight Packages preloaded in the template process are never serialized or reloaded by the forked workers.
- threaded-r Interpreters share one address space but not a heap, and the THREAD cluster still serializes what it passes between them.