| Title: | Parallel Future Backend using 'mirai' |
|---|---|
| Description: | Implementation of the Future API <doi:10.32614/RJ-2021-048> on top of the 'mirai' package <doi:10.5281/zenodo.7912722>. The backend is fully compliant with the Future API, as validated by the 'future.tests' conformance test suite, meaning that code using futures gives identical results and behaves the same regardless of which future backend is used. Futures are resolved in parallel by 'mirai' daemons on the local machine or across remote machines, and, contrary to backends relying on the 'parallel' package and socket connections, it can run more than 125 parallel R processes. A common use is to parallelize existing map-reduce calls via the 'futurize' package <doi:10.48550/arXiv.2601.17578>, e.g. 'plan(future.mirai::mirai_multisession)' followed by 'lapply(xs, fcn) |> futurize()' or 'purrr::map(xs, fcn) |> futurize()'. |
| Authors: | Henrik Bengtsson [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-7579-5165>), Charlie Gao [ctb] (ORCID: <https://orcid.org/0000-0002-0750-061X>, note: For 'mirai'-related patches and implementing feature requests in 'mirai') |
| Maintainer: | Henrik Bengtsson <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 1.1.0-9013 |
| Built: | 2026-11-22 23:37:25 UTC |
| Source: | https://github.com/futureverse/future.mirai |
The future.mirai package implements the Future API using the mirai package. The Future API is defined by the future package.
To use mirai futures, attach future, and
select the type of future you wish to use, e.g.
plan(future.mirai::mirai_multisession).
library(futurize) # also attaches 'future'
plan(future.mirai::mirai_multisession) # parallelize on the current machine
# A slow function
slow_fcn <- function(x) {
Sys.sleep(1.0)
x^2
}
# Example data
xs <- 1:10
# Sequential and parallel base R apply
ys <- lapply(xs, slow_fcn)
ys <- lapply(xs, slow_fcn) |> futurize()
# Sequential and parallel purrr map
library(purrr)
ys <- xs |> map(slow_fcn)
ys <- xs |> map(slow_fcn) |> futurize()
# Sequential and parallel foreach calls
library(foreach)
ys <- foreach(x = xs) %do% slow_fcn(x)
ys <- foreach(x = xs) %do% slow_fcn(x) |> futurize()
Maintainer: Henrik Bengtsson [email protected] (ORCID) [copyright holder]
Authors:
Henrik Bengtsson [email protected] (ORCID) [copyright holder]
Other contributors:
Charlie Gao [email protected] (ORCID) (note: For 'mirai'-related patches and implementing feature requests in 'mirai') [contributor]
Useful links:
Report bugs at https://github.com/futureverse/future.mirai/issues
WARNING: This function must never be called.
It may only be used with future::plan()
mirai_cluster(..., envir = parent.frame())mirai_cluster(..., envir = parent.frame())
envir |
The environment from where global objects should be identified. |
... |
Not used. |
Nothing.
If you have access to a high-performance-compute (HPC) environment
with a job scheduler, you can use future.mirai and mirai
to run parallel workers distributed on the computer cluster.
How to set these workers up is explained in mirai::cluster_config(),
which should work for our most common job schedulers, e.g. Slurm,
Sun/Son of/Oracle/Univa/Altair Grid Engine (SGE), OpenLava,
Load Sharing Facility (LSF), and TORQUE/PBS.
Note: Not all compute clusters support running mirai workers
this way. This is because mirai workers need to establish a
TCP connection back to the machine that launched the workers, but some
systems have security policies disallowing such connections from
being established. This is often configured in the firewall and
can only be controlled by the admins. If your system has such
rules, you will find that the mirai jobs are launched and running
on the scheduler, but we will wait forever for the mirai workers
to connect back, i.e. mirai::info()[["connections"]] is always
zero in the below example.
Briefly, to launch a cluster of mirai workers on an HPC cluster, we need to:
configure mirai::cluster_config() for the job scheduler,
use configuration to launch workers using mirai::daemons().
The first step is specific to each job scheduler and this is where you control things such as how much memory each worker gets, for how long they may run, and which environment modules to load, if any. The second step is the same regardless of job scheduler. Here is an example for how to run parallel mirai workers on a Slurm scheduler and then use these in futureverse.
# Here we give each worker 200 MiB of RAM and a maximum of one hour
# to run. Unless we specify '--cpus-per-task=N', each mirai worker
# is allotted one CPU core, which prevents nested parallelization.
# R is provided via environment module 'r' on this cluster.
config <- mirai::cluster_config(command = "sbatch", options = "
#SBATCH --job-name=mirai
#SBATCH --time=01:00:00
#SBATCH --mem=200M
module load r
")
# -------------------------------------------------------------------
# Launch eight mirai workers, via equally many jobs, wait for all of
# them to become available, and use them in futureverse
# -------------------------------------------------------------------
workers <- 8
mirai::daemons(n = workers, url = mirai::host_url(), remote = config)
while(mirai::info()[["connections"]] < workers) Sys.sleep(1.0)
plan(future.mirai::mirai_cluster)
# Verify that futures are resolved on a compute node
f <- future({
data.frame(
hostname = Sys.info()[["nodename"]],
os = Sys.info()[["sysname"]],
osVersion = utils::osVersion,
cores = unname(parallelly::availableCores()),
modules = Sys.getenv("LOADEDMODULES")
)
})
info <- value(f)
print(info)
#> hostname os osVersion cores modules
#> 1 n12 Linux Linux Ubuntu 24.04.3 LTS 1 r/4.5.2
# Shut down parallel workers
plan(sequential)
mirai::daemons(0)
If you are on SGE, you can use the following configuration:
config <- mirai::cluster_config(command = "qsub", options = " #$ -N mirai #$ -j y #$ -cwd #$ -l h_rt=01:00:00 #$ -l mem_free=200M module load r ")
Everything else is the same.
Comment: mirai::cluster_config() configures the jobs to run vanilla
POSIX shells, i.e. /bin/sh. This might be too strict for some users.
If your setup requires your jobs to be run using Bash (/bin/bash), you
can tweak the configuration config object manually to do so;
config$command <- "/bin/bash"
config$args <- sub("/bin/sh", config$command, config$args)
library(future) # Manually launch mirai workers mirai::daemons(parallelly::availableCores()) plan(future.mirai::mirai_cluster) # A function that returns a future # (note that N is a global variable) f <- function() future({ 4 * sum((runif(N) ^ 2 + runif(N) ^ 2) < 1) / N }, seed = TRUE) # Run a simple sampling approximation of pi in parallel using M * N points: N <- 1e6 # samples per worker M <- 10 # iterations pi_est <- Reduce(sum, Map(value, replicate(M, f()))) / M print(pi_est) ## Switch back to sequential processing plan(sequential) ## Shut down manually launched mirai workers invisible(mirai::daemons(0))library(future) # Manually launch mirai workers mirai::daemons(parallelly::availableCores()) plan(future.mirai::mirai_cluster) # A function that returns a future # (note that N is a global variable) f <- function() future({ 4 * sum((runif(N) ^ 2 + runif(N) ^ 2) < 1) / N }, seed = TRUE) # Run a simple sampling approximation of pi in parallel using M * N points: N <- 1e6 # samples per worker M <- 10 # iterations pi_est <- Reduce(sum, Map(value, replicate(M, f()))) / M print(pi_est) ## Switch back to sequential processing plan(sequential) ## Shut down manually launched mirai workers invisible(mirai::daemons(0))
WARNING: This function must never be called.
It may only be used with future::plan()
mirai_multisession(..., workers = availableCores(), envir = parent.frame())mirai_multisession(..., workers = availableCores(), envir = parent.frame())
workers |
The number of parallel processes to use.
If a function, it is called without arguments when the future
is created and its value is used to configure the workers.
If |
envir |
The environment from where global objects should be identified. |
... |
Not used. |
Nothing.
library(future) plan(future.mirai::mirai_multisession) # A function that returns a future # (note that N is a global variable) f <- function() future({ 4 * sum((runif(N) ^ 2 + runif(N) ^ 2) < 1) / N }, seed = TRUE) # Run a simple sampling approximation of pi in parallel using M * N points: N <- 1e6 # samples per worker M <- 10 # iterations pi_est <- Reduce(sum, Map(value, replicate(M, f()))) / M print(pi_est) ## Switch back to sequential processing, which also ## shuts down the automatically launched mirai workers plan(sequential)library(future) plan(future.mirai::mirai_multisession) # A function that returns a future # (note that N is a global variable) f <- function() future({ 4 * sum((runif(N) ^ 2 + runif(N) ^ 2) < 1) / N }, seed = TRUE) # Run a simple sampling approximation of pi in parallel using M * N points: N <- 1e6 # samples per worker M <- 10 # iterations pi_est <- Reduce(sum, Map(value, replicate(M, f()))) / M print(pi_est) ## Switch back to sequential processing, which also ## shuts down the automatically launched mirai workers plan(sequential)
Set up the future parameters.
MiraiFutureBackend(...)MiraiFutureBackend(...)
... |
Additional arguments passed to |
An object of class MiraiFutureBackend.