| Title: | Parallel Future Backend using 'mizu' |
|---|---|
| Description: | Implementation of the Future API <doi:10.32614/RJ-2021-048> on top of the 'mizu' package <doi:10.32614/CRAN.package.mizu>. 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 on the local machine by R worker processes that exchange data via shared memory. A common use is to parallelize existing map-reduce calls via the 'futurize' package <doi:10.48550/arXiv.2601.17578>, e.g. 'plan(future.mizu::mizu_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>) |
| Maintainer: | Henrik Bengtsson <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 0.1.0 |
| Built: | 2026-11-23 15:18:17 UTC |
| Source: | https://github.com/futureverse/future.mizu |
The future.mizu package implements the Future API using the mizu package, which runs parallel R workers on the local machine communicating via shared memory. The Future API is defined by the future package.
To use mizu futures, attach future, and
select the type of future you wish to use, e.g.
plan(future.mizu::mizu_multisession).
library(futurize) # also attaches 'future'
plan(future.mizu::mizu_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]
Useful links:
Report bugs at https://github.com/futureverse/future.mizu/issues
WARNING: This function must never be called.
It may only be used with future::plan()
mizu_multisession(..., workers = availableCores(), envir = parent.frame())mizu_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.mizu::mizu_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 mizu workers plan(sequential)library(future) plan(future.mizu::mizu_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 mizu workers plan(sequential)
Set up the future parameters.
MizuFutureBackend(workers = availableCores(), ...)MizuFutureBackend(workers = availableCores(), ...)
workers |
The number of parallel workers (R processes) to launch,
or a function returning such a number. A single worker, unless
protected by |
... |
Additional arguments passed to |
An object of class MizuFutureBackend.
mizu workers always run on the local machine, and they communicate via shared memory. Cancelling a future is advisory: a future that is already running on a worker cannot be interrupted, and runs to completion with its result being discarded.