Learn R Programming

mirai (version 2.4.1)

everywhere: Evaluate Everywhere

Description

Evaluate an expression 'everywhere' on all connected daemons for the specified compute profile - this must be set prior to calling this function. Performs operations across daemons such as loading packages or exporting common data. Resultant changes to the global environment, loaded packages and options are persisted regardless of a daemon's cleanup setting.

Usage

everywhere(.expr, ..., .args = list(), .compute = NULL)

Value

A 'mirai_map' (list of 'mirai' objects).

Arguments

.expr

an expression to evaluate asynchronously (of arbitrary length, wrapped in { } where necessary), or else a pre-constructed language object.

...

(optional) either named arguments (name = value pairs) specifying objects referenced, but not defined, in .expr, or an environment containing such objects. See 'evaluation' section below.

.args

(optional) either a named list specifying objects referenced, but not defined, in .expr, or an environment containing such objects. These objects will remain local to the evaluation environment as opposed to those supplied in ... above - see 'evaluation' section below.

.compute

[default NULL] character value for the compute profile to use (each has its own independent set of daemons), or NULL to use the 'default' profile.

Evaluation

The expression .expr will be evaluated in a separate R process in a clean environment (not the global environment), consisting only of the objects supplied to .args, with the objects passed as ... assigned to the global environment of that process.

As evaluation occurs in a clean environment, all undefined objects must be supplied through ... and/or .args, including self-defined functions. Functions from a package should use namespaced calls such as mirai::mirai(), or else the package should be loaded beforehand as part of .expr.

For evaluation to occur as if in your global environment, supply objects to ... rather than .args, e.g. for non-local variables or helper functions required by other functions, as scoping rules may otherwise prevent them from being found.

Details

If using dispatcher, this function forces a synchronization point at dispatcher, whereby the everywhere() call must have been evaluated on all daemons prior to subsequent evaluations taking place. It is an error to call everywhere() successively without at least one mirai() call in between, as an ordinary mirai call is required to exit each synchronization point.

Examples

Run this code
if (FALSE) { # interactive()
daemons(1)
# export common data by a super-assignment expression:
everywhere(y <<- 3)
# '...' variables are assigned to the global environment
# '.expr' may be specified as an empty {} in such cases:
everywhere({}, a = 1, b = 2)
m <- mirai(a + b - y == 0L)
m[]
# everywhere() returns a list of mirai which may be waited for and inspected
mlist <- everywhere("just a normal operation")
collect_mirai(mlist)
mlist <- everywhere(stop("error"))
collect_mirai(mlist)
daemons(0)

# loading a package on all daemons
daemons(1, dispatcher = FALSE)
everywhere(library(parallel))
m <- mirai("package:parallel" %in% search())
m[]
daemons(0)
}

Run the code above in your browser using DataLab