Mirai-based cluster futures

mirai_cluster(
  expr,
  substitute = TRUE,
  envir = parent.frame(),
  ...,
  workers = availableWorkers()
)

Arguments

expr

An R expression.

substitute

If TRUE, argument expr is substitute():ed, otherwise not.

envir

The environment from where global objects should be identified.

...

Additional named elements of the future.

workers

A cluster object, a character vector of host names, a positive numeric scalar, or a function. If a character vector or a numeric scalar, a cluster object is created using makeClusterPSOCK(workers). If a function, it is called without arguments when the future is created and its value is used to configure the workers. The function should return any of the above types.

Value

An object of class MiraiFuture.

Examples

mirai::daemons(parallelly::availableCores())
#> [1] 8
plan(mirai_cluster, workers = NULL)

# A function that returns a future, note that N uses lexical scoping...
f <- \() 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)
#> [1] 3.141063