Learn R Programming

future (version 1.26.1)

cluster: Create a cluster future whose value will be resolved asynchronously in a parallel process

Description

A cluster future is a future that uses cluster evaluation, which means that its value is computed and resolved in parallel in another process.

Usage

cluster(..., workers = availableWorkers(), envir = parent.frame())

Value

A ClusterFuture.

Arguments

...

Additional named elements passed to ClusterFuture().

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.

envir

The environment from where global objects should be identified.

Details

This function is not meant to be called directly. Instead, the typical usages are:

# Evaluate futures via a single background R process on the local machine
plan(cluster, workers = 1)

# Evaluate futures via two background R processes on the local machine plan(cluster, workers = 2)

# Evaluate futures via a single R process on another machine on on the # local area network (LAN) plan(cluster, workers = "raspberry-pi")

# Evaluate futures via a single R process running on a remote machine plan(cluster, workers = "pi.example.org")

# Evaluate futures via four R processes, one running on the local machine, # two running on LAN machine 'n1' and one on a remote machine plan(cluster, workers = c("localhost", "n1", "n1", "pi.example.org")

Examples

Run this code
# \donttest{

## Use cluster futures
cl <- parallel::makeCluster(2, timeout = 60)
plan(cluster, workers = cl)

## A global variable
a <- 0

## Create future (explicitly)
f <- future({
  b <- 3
  c <- 2
  a * b * c
})

## A cluster future is evaluated in a separate process.
## Regardless, changing the value of a global variable will
## not affect the result of the future.
a <- 7
print(a)

v <- value(f)
print(v)
stopifnot(v == 0)

## CLEANUP
parallel::stopCluster(cl)

# }

Run the code above in your browser using DataLab