Package 'future.mizu'

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

Help Index


future.mizu: Parallel Future Backend using 'mizu'

Description

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.

Details

To use mizu futures, attach future, and select the type of future you wish to use, e.g. plan(future.mizu::mizu_multisession).

Example

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()

Author(s)

Maintainer: Henrik Bengtsson [email protected] (ORCID) [copyright holder]

Authors:

See Also

Useful links:


Mizu-based localhost multisession futures

Description

WARNING: This function must never be called. It may only be used with future::plan()

Usage

mizu_multisession(..., workers = availableCores(), envir = parent.frame())

Arguments

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 workers == 1, then all processing using done in the current/main R session and we therefore fall back to using a sequential future. To override this fallback, use workers = I(1).

envir

The environment from where global objects should be identified.

...

Not used.

Value

Nothing.

Examples

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)

A future backend based on the 'mizu' framework

Description

Set up the future parameters.

Usage

MizuFutureBackend(workers = availableCores(), ...)

Arguments

workers

The number of parallel workers (R processes) to launch, or a function returning such a number. A single worker, unless protected by base::I(), falls back to sequential processing in the current R process. At most 64 workers are supported by mizu.

...

Additional arguments passed to future::FutureBackend().

Value

An object of class MizuFutureBackend.

Limitations

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.