# How many cores can R use on this computer?
parallelly::availableCores()
data_roc <-
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(RRatepol::example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(RRatepol::example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(RRatepol::example_data, "age_uncertainty", 1),
rand = 10000,
use_parallel = 4, # number of cores
seed = 900723 # for reproducible results
)Robust rate-of-change (RoC) estimates need many randomisation runs (rand), and these can take minutes per record. This article explains when running estimate_roc() on several cores saves time, how to choose use_parallel and the number of runs, how to keep parallel results reproducible, and how to run RRatepol on a high-performance computing (HPC) cluster.
Why use parallel computation
The processor of a computer has several cores, and each core can carry out one calculation at a time. By default, R uses only one of them, so most of the computer stays idle while RRatepol works.
estimate_roc() repeats the same estimation many times, each time with a different random draw of sample ages and pollen counts, and then summarises all repetitions. These randomisation runs do not depend on each other, so they can be shared out among several cores, much like dividing a pile of samples to be counted among several people. The summary at the end is the same as with one core; only the waiting time is shorter.
This matters because reliable estimates need many randomisation runs: the workflow example recommends 10 000, and Choosing the number of runs shows how to find out how many a record needs. On a single core, this can keep the computer busy for a long time for each record, and the time multiplies with the number of records. With several cores, the same work finishes in a fraction of that time (see the timing table below).
A typical call looks like this:
What to watch for
-
Test first, then go parallel. Try out settings with a small
randon a single core, where errors show up quickly and are easier to read. Switch onuse_parallelfor the final estimate with many runs. -
Check how many runs the record needs. Estimate RoC with 100 runs and let
check_rand()recommend the number of runs for the final estimate (see Choosing the number of runs). -
Leave some cores free. While RRatepol uses all cores, the computer can become slow, the fans loud, and a laptop battery drains fast. On a computer you also use for other work, give a number of cores (for example
use_parallel = 4) rather thanuse_parallel = TRUE. - Watch the memory. Each core works on its own copy of the data. If the computer runs out of memory or slows down sharply, use fewer cores.
-
Set a seed to make results repeatable. With a seed (for example
seed = 900723inestimate_roc()), the results are identical whether you use one core or many. Without a seed, every call gives slightly different values, as expected from new randomisation runs. Report the seed,rand, and the RRatepol version with your results (see Reproducible parallel results). - Few runs gain little. Starting the cores takes time, so with only a few randomisation runs, parallel computation saves little or nothing.
- Shared computers and HPC clusters have rules. On a server or cluster shared with others, use only the cores you have been given. On an HPC cluster, run RRatepol inside a job, never on the login node (see Running RRatepol on an HPC cluster).
How parallel computation works in RRatepol
estimate_roc() repeats the whole estimation rand times. In each randomisation run, it draws one age sequence from age_uncertainty (if supplied) and, with standardise = TRUE, a random subset of n_individuals from each working unit. The runs are independent of each other, so they can be spread over several cores.
The argument use_parallel controls this:
-
FALSE(default) or1: all runs on a single core. - A whole number above 1: that many cores.
-
TRUE: all cores available to the R session, as reported byparallelly::availableCores().
With more than one core, estimate_roc() starts a local cluster of R worker processes on the same computer, sends the runs to the workers, and stops the cluster when the runs are finished, also when a run fails.
When parallel computation pays off
Starting the workers and sending them the data takes time, so the time saved is small with few runs and grows with the number of runs. The table below compares elapsed times for the first record of example_data with age uncertainty, Shepard’s 5-term filter, moving-window binning, and taxa standardisation.
Show the benchmark settings
library(RRatepol)
n_cores_available <-
base::as.integer(parallelly::availableCores())
vec_cores <-
c(1, 2, 4, 8)
vec_cores_tested <-
vec_cores[vec_cores <= n_cores_available]
vec_rand <-
c(10, 100, 500)
# Re-run the cached benchmark for a new package version or another number
# of available cores
list_cache_key <-
list(
base::as.character(utils::packageVersion("RRatepol")),
n_cores_available
)Show the benchmark code
data_benchmark <-
tidyr::expand_grid(
rand = vec_rand,
n_cores = vec_cores_tested
) %>%
dplyr::mutate(
elapsed = purrr::map2_dbl(
.x = rand,
.y = n_cores,
.f = ~ {
base::set.seed(900723)
time_run <-
base::system.time(
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", 1),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = .x,
use_parallel = .y,
silent = TRUE
)
)
purrr::chuck(time_run, "elapsed")
}
)
)
benchmark_r_version <-
base::R.version.string
benchmark_os <-
purrr::chuck(base::Sys.info(), "sysname")Show the table code
data_benchmark_wide <-
data_benchmark %>%
dplyr::mutate(
n_cores = base::ifelse(
test = n_cores == 1,
yes = "1 core",
no = base::paste(n_cores, "cores")
),
elapsed = base::round(elapsed, 1)
) %>%
tidyr::pivot_wider(
names_from = n_cores,
values_from = elapsed
)
knitr::kable(
data_benchmark_wide,
caption = "Elapsed time (s) of estimate_roc() by number of cores"
)| rand | 1 core | 2 cores | 4 cores | 8 cores |
|---|---|---|---|---|
| 10 | 2.2 | 2.0 | 1.8 | 1.6 |
| 100 | 17.0 | 9.5 | 5.4 | 3.9 |
| 500 | 78.8 | 42.8 | 25.2 | 16.6 |
The times were measured on Windows with R version 4.6.2 (2026-10-29 ucrt) and 32 available cores; they depend on the computer and on its load at the time. With 10 runs, 8 cores needed 71 % of the single-core time. With 500 runs, they needed 21 %. Adding cores shortens the time less than proportionally, because starting the workers and collecting their results do not get faster with more cores.
In practice:
- For quick checks with few runs, the default
use_parallel = FALSEsaves little time compared with several cores. - For final estimates with hundreds or thousands of runs, use several cores.
- More cores give diminishing returns; leave some cores free for other work on a shared computer.
Choosing use_parallel
use_parallel = TRUE uses all cores available to the R session. parallelly::availableCores() respects the cores allocated to a job by an HPC scheduler (for example SLURM, PBS, or SGE), the limits of a container, and the option mc.cores, so RRatepol does not start more workers than the session may use. To see how many cores use_parallel = TRUE would use, run:
parallelly::availableCores()A number, such as use_parallel = 4, sets the number of cores explicitly. This is useful on a shared computer, where use_parallel = TRUE would take all available cores. If only one core is available, use_parallel = TRUE runs on a single core without starting a cluster.
Choosing the number of runs
How many randomisation runs are enough depends on the record and the settings. The summaries of the runs (ROC, ROC_up, and ROC_dw) have a Monte Carlo error: with another seed, they would come out slightly different. This error falls with the square root of the number of runs, so halving it takes four times more runs. estimate_roc() reports it for each working unit in the columns ROC_mc_error, ROC_up_mc_error, and ROC_dw_mc_error, together with the number of runs behind each working unit (n_runs). The bounds of the interval (ROC_up and ROC_dw) need more runs than the median, because they rest on the few most extreme runs.
check_rand() turns these errors into a recommended number of runs: the number at which the error is below 10 % of the width of the 95 % interval in 90 % of the working units. A practical approach has three steps: estimate RoC with 100 runs as a pilot, check the pilot, and repeat the estimation with the recommended number of runs.
First, the pilot with 100 runs. With silent = FALSE, estimate_roc() warns when the Monte Carlo error is too large and gives the recommended number of runs:
data_roc_pilot <-
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", 1),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = 100,
seed = 900723,
silent = FALSE
)
#>
#> ── RRatepol started 2026-12-04 19:03:42 ────────────────────────────────────────
#> ℹ Age uncertainty: each run uses one age sequence drawn from `age_uncertainty`.
#> ℹ Working Units: moving-window binning with 500-year bins and 5 shifts of the
#> window.
#> ℹ Bin selection: one sample at random in each bin.
#> ℹ Time standardisation: RoC is reported as dissimilarity per 500 years.
#> ℹ Standardisation: each Working Unit is rarefied to 150 individuals, or to the
#> smallest count in the data if that is lower.
#> Warning: ! The Monte Carlo error of the RoC estimates exceeds 10 % of the 95 % interval
#> width in at least 10 % of the assessed Working Units.
#> ℹ About 400 randomisation runs (`rand`) are recommended; see `check_rand()`.
#> ✔ RRatepol finished 2026-12-04 19:03:59 [17.04 secs]Second, check_rand() shows how many working units were assessed, in how many of them the error is too large, and the recommended number of runs:
list_rand <-
RRatepol::check_rand(data_source = data_roc_pilot)
#> ℹ Monte Carlo error assessed in 80 of 84 Working Units. With 100 runs, it
#> exceeds 10 % of the 95 % interval width in 36 of them. Recommended number of
#> randomisation runs (`rand`): 400.For this record and these settings, check_rand() recommends 400 runs. The tolerance argument of check_rand() sets another precision; a tolerance of 0.05 needs about four times more runs than the default 0.10.
Third, the final estimate with the recommended number of runs, spread over all available cores:
rand_final <-
purrr::chuck(list_rand, "rand_recommended")
data_roc_final <-
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", 1),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = rand_final,
use_parallel = TRUE,
seed = 900723,
silent = FALSE
)
#>
#> ── RRatepol started 2026-12-04 19:03:59 ────────────────────────────────────────
#> ℹ Age uncertainty: each run uses one age sequence drawn from `age_uncertainty`.
#> ℹ Working Units: moving-window binning with 500-year bins and 5 shifts of the
#> window.
#> ℹ Bin selection: one sample at random in each bin.
#> ℹ Time standardisation: RoC is reported as dissimilarity per 500 years.
#> ℹ Standardisation: each Working Unit is rarefied to 150 individuals, or to the
#> smallest count in the data if that is lower.
#> ✔ RRatepol finished 2026-12-04 19:04:07 [7.66 secs]If the final estimate still warns, repeat it with the new recommendation. The estimate of the error is itself uncertain, so the recommendation varies somewhat between seeds; it is meant as a guide, not an exact number.
Reproducible parallel results
The age sequences drawn from age_uncertainty are drawn in the main R session before the runs start. When the runs themselves draw random numbers (random bin selection or standardise = TRUE), each run has its own random-number stream, also created in the main session before the runs start. A run draws the same random numbers whether it is computed in the main session or on any worker.
There are two ways to make the results reproducible:
-
The
seedargument. Withseed = 900723, for example, all random draws of the call come from a random-number generator seeded with that number. The call gives the same results in any R session, whatever random draws came before it, and it leaves the random-number generator of the session unchanged. -
set.seed()right beforeestimate_roc(), with the defaultseed = NULL. The call then draws from the random-number generator of the session, so any random draw betweenset.seed()and the call changes the results. In a loop over several records with oneset.seed()before the loop, for example, the results of each record depend on the records before it.
In a session with R’s default random-number generator, seed = 900723 and set.seed(900723) before the call give the same results. Either way, the results are identical on a single core and on any number of cores, with or without progress output (silent). The chunk below runs the same estimation on one core and on two cores:
list_roc_parallel <-
purrr::map(
.x = list(FALSE, 2),
.f = ~ RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", 1),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = 10,
use_parallel = .x,
seed = 900723,
silent = TRUE
)
)
base::identical(
purrr::chuck(list_roc_parallel, 1),
purrr::chuck(list_roc_parallel, 2)
)
#> [1] TRUEFor results that others should reproduce exactly, report the seed, rand, and the RRatepol version. The number of cores does not need to be reported, because it does not change the results. With random bin selection or standardise = TRUE, versions before 1.4.0 gave different random draws after the same set.seed(); to reproduce results of an earlier version, install that version.
Memory
Each worker is a separate R process that loads RRatepol and receives its own copy of the data for the runs it computes. Memory use therefore grows with the number of cores. For large records or many cores, check the memory available to the session (on an HPC cluster, the memory requested for the job) before increasing use_parallel.
Running RRatepol on an HPC cluster
The cluster that estimate_roc() starts runs on one computer. On an HPC cluster, this means one node: request several cores on a single node for one job. The examples below use the SLURM scheduler; the directives differ for other schedulers, and the module names, partitions, and resource limits depend on the site. Before a long job, choose rand with a short pilot (see Choosing the number of runs).
One record on one node
An R script estimate_roc_record.R:
library(RRatepol)
data_roc <-
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", 1),
data_source_age =
purrr::chuck(example_data, "sample_age", 1),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", 1),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = 10000,
use_parallel = TRUE,
seed = 900723,
silent = TRUE
)
base::saveRDS(
object = data_roc,
file = "roc_record_1.rds"
)A job script estimate_roc_record.sh, submitted with sbatch estimate_roc_record.sh:
#!/bin/bash
#SBATCH --job-name=rratepol
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=8
#SBATCH --mem=8G
#SBATCH --time=02:00:00
# Load R as provided on your cluster
module load R
Rscript estimate_roc_record.R--cpus-per-task sets the number of cores of the job, and use_parallel = TRUE uses exactly these cores. Adjust the memory and time requests to your data and the rules of your cluster.
Many records with a job array
For many records, it is usually more efficient to run one record per job on a single core and let the scheduler run many jobs at the same time. A job array starts the same script many times, each with a different task number.
An R script estimate_roc_array.R that selects the record by the task number:
library(RRatepol)
index_record <-
base::as.integer(base::Sys.getenv("SLURM_ARRAY_TASK_ID"))
data_roc <-
RRatepol::estimate_roc(
data_source_community =
purrr::chuck(example_data, "pollen_data", index_record),
data_source_age =
purrr::chuck(example_data, "sample_age", index_record),
age_uncertainty =
purrr::chuck(example_data, "age_uncertainty", index_record),
smooth_method = "shep",
working_units = "MW",
bin_size = 500,
number_of_shifts = 5,
standardise = TRUE,
n_individuals = 150,
rand = 10000,
use_parallel = FALSE,
seed = 900723,
silent = TRUE
)
base::saveRDS(
object = data_roc,
file = base::paste0("roc_record_", index_record, ".rds")
)A job script estimate_roc_array.sh with one task per record of example_data:
#!/bin/bash
#SBATCH --job-name=rratepol-array
#SBATCH --array=1-4
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --mem=2G
#SBATCH --time=08:00:00
module load R
Rscript estimate_roc_array.RWith your own data, set --array to the number of records and read each record from a file in the R script. Each task uses the same seed; because the tasks process different records, the results do not depend on each other. If single jobs take too long, combine both approaches: a job array with a few cores per task and use_parallel = TRUE.
