1.8 Nested Parallelism and Oversubscription
1.8.1 Motivation
You write a parallel map with four workers, then watch the CPU meter and find all sixteen cores busy. The extra threads come from nested parallelism: each R worker calls a library that runs its own thread pool, such as a multithreaded BLAS, an OpenMP loop or a database client. If each pool is sized to the whole machine, four workers put 64 threads on 16 cores. That is oversubscription, more runnable threads than cores, and it can make the job slower than it would be with fewer threads.
It is a common answer to “R is using more threads than I asked for”.
1.8.2 The structure: R-level parallelism and native-library parallelism
R parallelism happens at the R level. You call parallel::parLapply() or future::future(). R starts a task on a worker. The task is an R expression or function. That function then calls a C or Rust library.
If that library is single-threaded, the task runs alone on one core.
If that library is multithreaded (a BLAS implementation, OpenMP, or an Intel MKL matrix routine), it starts its own threads, for OpenMP and MKL at the first parallel call and for OpenBLAS when the library loads, and keeps them. Those threads run on additional cores. Now one R task occupies N CPU cores where N is the library’s thread count. Multiply by the R-level worker count and you get oversubscription.
R level: 4 R workers on a 16-core machine
Native level: Each worker calls MKL, starts 8 threads
Total threads: 4 workers × 8 threads = 32 threads
Physical cores: 16
Oversubscription: 32 ÷ 16 = 2x1.8.3 Causes of oversubscription
Multithreaded BLAS. When R is linked to a threaded BLAS (OpenBLAS, MKL, or Accelerate), large matrix multiplies, solve() and eigen() use many cores. The default pool usually covers the whole machine: OpenBLAS uses every CPU, and MKL one thread per physical core.
OpenMP pragmas in packages. C or C++ code using #pragma omp parallel starts an OpenMP thread pool. Without OMP_NUM_THREADS, GCC’s OpenMP runtime uses one thread per CPU. A package author writes efficient OpenMP code, and then a user runs eight R workers on an eight-core machine, each with its own pool.
Database and network clients. Some database drivers start thread pools for connection pooling or asynchronous queries. The pool size is independent of how many worker processes the user created.
Spark, TensorFlow, and GPU libraries. These libraries assume they have the machine to themselves. They start thread pools and lock in the core count at initialization time.
1.8.4 The cost of context switching
The kernel scheduler has limited time on each core. When 32 threads run on 16 cores, each thread gets half the core time. The scheduler switches threads constantly. Between each pair of switches:
- The scheduler saves registers and the program counter from the old thread.
- It restores registers and the program counter for the new thread.
- The CPU cache was warm for the old thread. The new thread’s data is cold.
- The TLB (the CPU’s cache of page translations) was warm. It must refill.
That is the cost of a context switch. For BLAS and OpenMP code the switches themselves are not the main loss. Their threads work in teams that meet at barriers, and a thread that reaches a barrier early waits for the others, often by spinning on the core for a while before it sleeps. When one member of the team has been descheduled, the whole team waits for it, and the spinning threads burn core time the late thread needed. Throughput can therefore drop sharply even at modest oversubscription. Measure your own case before relying on any ratio.
1.8.5 Environment variables that control thread pools
Each library reads its own environment variable, usually once, when it loads:
OMP_NUM_THREADS: OpenMP pool size. The standard leaves the default to the implementation, and GCC’s runtime uses one thread per CPU.MKL_NUM_THREADS: Intel MKL (threaded BLAS) pool size. It takes precedence overOMP_NUM_THREADS.OPENBLAS_NUM_THREADS: OpenBLAS pool size. An OpenBLAS built with OpenMP followsOMP_NUM_THREADSinstead.VECLIB_MAXIMUM_THREADS: Accelerate (macOS) pool size.NUMEXPR_NUM_THREADS: Numexpr (Python) pool size.POLARS_MAX_THREADS: Polars (Rust/Python dataframe library) pool size, read before Polars loads and fixed afterwards.
The names do not follow one pattern, and no single variable covers them all.
1.8.6 Preventing oversubscription
Method 1: Reduce native thread pool size to one.
Tell the nested libraries to use only one thread per R worker.
# Before starting parallel work
Sys.setenv(OMP_NUM_THREADS = 1)
Sys.setenv(MKL_NUM_THREADS = 1)
Sys.setenv(OPENBLAS_NUM_THREADS = 1)
cl <- parallel::makePSOCKcluster(4)
# The workers inherit these variables, so each BLAS and OpenMP pool has one threadThis works for spawned workers because they start after the variables are set and inherit them. It does not change the session that set them if its BLAS has already loaded, and a forked child inherits a library that has already read its settings. In those cases set the thread count from inside R: RhpcBLASctl::blas_set_num_threads(1) and RhpcBLASctl::omp_set_num_threads(1), run in each worker. data.table needs no help under fork, since it switches itself to one thread in a forked child. In a PSOCK worker it also obeys the inherited OMP_NUM_THREADS; without that variable it takes half the logical CPUs unless you call data.table::setDTthreads(1).
The cost is that each worker’s linear algebra runs on one thread. When the R workers already fill the cores, that loses little.
Method 2: Reduce R-level parallelism to match library thread pools.
If you have a fast multithreaded BLAS and 16 cores, start four R workers, each with its own four-thread BLAS pool. In general, give each worker the number of cores divided by the number of workers.
cores <- 16
workers <- 4
per_worker <- cores / workers # 4
Sys.setenv(OMP_NUM_THREADS = per_worker)
Sys.setenv(OPENBLAS_NUM_THREADS = per_worker)
Sys.setenv(MKL_NUM_THREADS = per_worker)
cl <- parallel::makePSOCKcluster(workers)
# 4 workers x 4 threads = 16 threads on 16 coresEach worker then runs its own BLAS calls on four threads, independently of the other three. The thread count matches the core count, so nothing is oversubscribed.
The cost: the scaling benefit of parallel R diminishes. You trade parallelism at the R level for parallelism at the library level.
Method 3: Use thread-aware task scheduling.
Cluster schedulers such as Slurm let a job request a number of cores per task and confine the task’s threads to those cores, so one task cannot spread across the whole node. The thread pools inside the task still need to be sized to match.
1.8.7 The measurement challenge
Finding oversubscription in your code requires visibility into thread count. Several tools help:
htop(Linux) orActivity Monitor(macOS) shows thread count per process.ps -o nlwp= -p <pid>on Linux prints the thread count of one process.top -Hon Linux shows individual threads.parallelly::availableCores()reports how many cores the session may use, not how many threads are running.
If your four workers hold 32 threads between them on a 16-core machine, you have oversubscription.
1.8.8 Diagnosis
- Start a parallel task and let it run.
- Get the worker process IDs from R, for example
unlist(parallel::clusterCall(cl, Sys.getpid)). - In another terminal, count each worker’s threads with
ps -o nlwp= -p <pid>. If a worker holds more threads than the cores it has to itself, you have nested parallelism. - Measure runtime. If it increases when you increase worker count, oversubscription is the likely cause.
1.8.9 Current practice in R packages
data.table sizes its OpenMP pool to half the logical CPUs by default and switches to one thread inside a forked child, but a PSOCK worker gets the full default, so four PSOCK workers on a 16-thread machine run 32 data.table threads. In xgboost 3.2, xgboost() takes an nthreads argument whose default is parallel::detectCores(), every core.
sunlight clamps each worker’s BLAS, OpenMP and data.table pools by default when hatch() creates a pool, to a fair share of floor(cores / workers) threads and never fewer than one, and govern() changes the shares.
threaded-r runs its interpreters as threads of one process, so they share one BLAS library and one OpenMP runtime. Settings such as OpenBLAS’s thread count are process-wide, so a limit set in one worker would apply to all of them, and the remedies above would have to be sized for the process as a whole. I have not measured how threaded-r’s workers and a threaded BLAS interact.
1.8.10 The hard part
The problem is architectural. A library writer assumes they have the machine. They start a thread pool sized for all cores. An R user assumes their parallel code will use all cores. The two assumptions collide when the library is called from R workers.
No single setting fixes this. Different libraries use different environment variables, and some libraries do not read any. GPUs and accelerators add another layer: a worker must not only reduce CPU threads but also partition GPU memory and compute resources.
Further reading
- OpenMP Architecture Review Board, OpenMP 5.0 specification, chapter 6: Environment Variables <https://www.openmp.org/spec-html/5.0/openmpch6.html>, including
OMP_NUM_THREADS. - GCC, libgomp: OMP_NUM_THREADS <https://gcc.gnu.org/onlinedocs/libgomp/OMP_005fNUM_005fTHREADS.html>, the default GCC’s runtime uses.
- Intel, oneMKL Developer Guide for Linux: Techniques to Set the Number of Threads <https://www.intel.com/content/www/us/en/docs/onemkl/developer-guide-linux/2025-2/techniques-to-set-the-number-of-threads.html>.
- OpenBLAS runtime variables <https://www.openmathlib.org/OpenBLAS/docs/runtime_variables/>.
- RhpcBLASctl <https://cran.r-project.org/package=RhpcBLASctl>, the CRAN package for setting BLAS and OpenMP thread counts from inside R.
- Why R Is Difficult to Parallelize.