| Context | R Documentation |
This class represents the base context for interacting with
Backend implementations via the BackendService
interface.
This class is a vanilla wrapper around a Backend implementation.
It registers a backend instance and forwards all BackendService
methods calls to the backend instance. Subclasses can override any of the
BackendService methods to decorate the backend instance with
additional functionality (e.g., see the ProgressTrackingContext
class for an example).
parabar::BackendService -> Context
backendThe Backend object registered with the
context.
new()Create a new Context object.
Context$new()
An object of class Context.
set_backend()Set the backend instance to be used by the context.
Context$set_backend(backend)
backendAn object of class Backend that
implements the BackendService interface.
start()Start the backend.
Context$start(specification)
specificationAn object of class Specification
that contains the backend configuration.
This method returns void. The resulting backend must be stored in the
.cluster private field on the Backend abstract class,
and accessible to any concrete backend implementations via the active
binding cluster.
stop()Stop the backend.
Context$stop()
This method returns void.
clear()Remove all objects from the backend. This function is equivalent to
calling rm(list = ls(all.names = TRUE)) on each node in the
backend.
Context$clear()
This method returns void.
peek()Inspect the backend for variables available in the .GlobalEnv.
Context$peek()
This method returns a list of character vectors, where each element
corresponds to a node in the backend. The character vectors contain
the names of the variables available in the .GlobalEnv on each
node.
export()Export variables from a given environment to the backend.
Context$export(variables, environment)
variablesA character vector of variable names to export.
environmentAn environment object from which to export the variables. Defaults to the parent frame.
This method returns void.
evaluate()Evaluate an arbitrary expression on the backend.
Context$evaluate(expression)
expressionAn unquoted expression to evaluate on the backend.
This method returns the result of the expression evaluation.
sapply()Run a task on the backend akin to parallel::parSapply().
Context$sapply(x, fun, ...)
xAn atomic vector or list to pass to the fun function.
funA function to apply to each element of x.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
lapply()Run a task on the backend akin to parallel::parLapply().
Context$lapply(x, fun, ...)
xAn atomic vector or list to pass to the fun function.
funA function to apply to each element of x.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
apply()Run a task on the backend akin to parallel::parApply().
Context$apply(x, margin, fun, ...)
xAn array to pass to the fun function.
marginA numeric vector indicating the dimensions of x the
fun function should be applied over. For example, for a matrix,
margin = 1 indicates applying fun rows-wise, margin = 2
indicates applying fun columns-wise, and margin = c(1, 2)
indicates applying fun element-wise. Named dimensions are also
possible depending on x. See parallel::parApply() and
base::apply() for more details.
funA function to apply to x according to the margin.
...Additional arguments to pass to the fun function.
This method returns void. The output of the task execution must be
stored in the private field .output on the Backend
abstract class, and is accessible via the get_output() method.
get_output()Get the output of the task execution.
Context$get_output(...)
...Additional arguments to pass to the backend registered
with the context. This is useful for backends that require additional
arguments to fetch the output (e.g., AsyncBackend$get_output(wait = TRUE)).
This method fetches the output of the task execution after calling
the sapply() method. It returns the output and immediately removes
it from the backend. Therefore, subsequent calls to this method are
not advised. This method should be called after the execution of a
task.
A vector, matrix, or list of the same length as x, containing the
results of the fun. The output format differs based on the specific
operation employed. Check out the documentation for the apply
operations of parallel::parallel for more information.
clone()The objects of this class are cloneable with this method.
Context$clone(deep = FALSE)
deepWhether to make a deep clone.
ProgressTrackingContext, BackendService,
Backend, and SyncBackend.
# Define a task to run in parallel.
task <- function(x, y) {
# Sleep a bit.
Sys.sleep(0.25)
# Return the result of a computation.
return(x + y)
}
# Create a specification object.
specification <- Specification$new()
# Set the number of cores.
specification$set_cores(cores = 2)
# Set the cluster type.
specification$set_type(type = "psock")
# Create a backend factory.
backend_factory <- BackendFactory$new()
# Get a synchronous backend instance.
backend <- backend_factory$get("sync")
# Create a base context object.
context <- Context$new()
# Register the backend with the context.
context$set_backend(backend)
# From now all, all backend operations are intercepted by the context.
# Start the backend.
context$start(specification)
# Run a task in parallel (i.e., approx. 1.25 seconds).
context$sapply(x = 1:10, fun = task, y = 10)
# Get the task output.
context$get_output()
# Close the backend.
context$stop()
# Get an asynchronous backend instance.
backend <- backend_factory$get("async")
# Register the backend with the same context object.
context$set_backend(backend)
# Start the backend reusing the specification object.
context$start(specification)
# Run a task in parallel (i.e., approx. 1.25 seconds).
context$sapply(x = 1:10, fun = task, y = 10)
# Get the task output.
backend$get_output(wait = TRUE)
# Close the backend.
context$stop()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.