This function initializes a persistent parallel cluster (either a fork cluster on Unix-like systems or a mirai cluster on others) to be used by all Design and Inference objects. This avoids the overhead of creating clusters repeatedly.
Details
set_num_cores() sets a global upper bound for parallel work. It does
not guarantee that every inference routine will use all requested workers.
EDI's inference dispatcher applies a blocklist-first heuristic informed by
package benchmarks: workloads that have shown consistent multicore slowdowns
are forced to run serially, while the remaining workloads are allowed to use
their method-specific warmup heuristics and native thread caps.
The default forced-serial blocklist covers incidence randomization confidence intervals, bootstrap for non-regression KK Wilcoxon inference, bootstrap for non-KK survival procedures, and bootstrap for incidence procedures. Do not expect a universal "more cores is faster" rule.
On the mirai backend, inference stops the workers and fails with an
error if no chunk of work finishes for six hours, and so does
SimulationFramework, so a worker that has stopped responding cannot
hang the session forever. Change that limit, in seconds,
with options(EDI.mirai_stall_timeout_secs = ), or, when the option is
unset, with the EDI_MIRAI_STALL_TIMEOUT_SECS environment variable.
If you want to change the default policy, use
set_parallel_dispatch_policy().
Which design draws use the workers. set_num_cores() always
parallelizes the downstream statistic (randomization/bootstrap loops in
Inference objects) where the dispatcher allows it, but only some designs
parallelize the null assignment draws themselves. Draws that run in
native OpenMP kernels use the cores:
DesignFixedGreedy, DesignFixedMatchingGreedyPairSwitching,
DesignFixedBinaryMatch, DesignFixedRerandomization (balanced,
even-n rejection sampler) and the optimal-design searches
(DesignFixedGreedyDOptimal, DesignFixedOptimal). Draws made in
vectorized R in the main process do not: DesignFixedBlocking,
DesignFixedOptimalBlocks, DesignFixedCluster and
DesignFixedBlockedCluster generate all r assignments in one
batched pass whose result depends only on the seed, so extra cores neither
speed up nor change those draws. The no-covariate fallbacks of the matching,
greedy and rerandomization designs (plain complete randomization) are also
serial R.
Examples
set_num_cores(2)
unset_num_cores()
