SyncBackend | R Documentation |
This is a concrete implementation of the abstract class Backend
that implements the BackendService
interface. This backend
executes tasks in parallel on a parallel::makeCluster()
cluster
synchronously (i.e., blocking the main R
session).
parabar::BackendService
-> parabar::Backend
-> SyncBackend
new()
Create a new SyncBackend
object.
SyncBackend$new()
An object of class SyncBackend
.
start()
Start the backend.
SyncBackend$start(specification)
specification
An 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.
SyncBackend$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.
SyncBackend$clear()
This method returns void.
peek()
Inspect the backend for variables available in the .GlobalEnv
.
SyncBackend$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.
SyncBackend$export(variables, environment)
variables
A character vector of variable names to export.
environment
An 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.
SyncBackend$evaluate(expression)
expression
An 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()
.
SyncBackend$sapply(x, fun, ...)
x
An atomic vector or list to pass to the fun
function.
fun
A 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()
.
SyncBackend$lapply(x, fun, ...)
x
An atomic vector or list to pass to the fun
function.
fun
A 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()
.
SyncBackend$apply(x, margin, fun, ...)
x
An array to pass to the fun
function.
margin
A 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.
fun
A 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.
SyncBackend$get_output(...)
...
Additional arguments currently not in use.
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 will
return NULL
. 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.
SyncBackend$clone(deep = FALSE)
deep
Whether to make a deep clone.
BackendService
, Backend
, AsyncBackend
,
and Context
.
# 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 synchronous backend object.
backend <- SyncBackend$new()
# Start the cluster on the backend.
backend$start(specification)
# Check if there is anything on the backend.
backend$peek()
# Create a dummy variable.
name <- "parabar"
# Export the variable from the current environment to the backend.
backend$export("name", environment())
# Remove variable from current environment.
rm(name)
# Run an expression on the backend, using the exported variable `name`.
backend$evaluate({
# Print the name.
print(paste0("Hello, ", name, "!"))
})
# Run a task in parallel (i.e., approx. 1.25 seconds).
backend$sapply(
x = 1:10,
fun = function(x) {
# Sleep a bit.
Sys.sleep(0.25)
# Compute something.
output <- x + 1
# Return the result.
return(output)
}
)
# Get the task output.
backend$get_output()
# Clear the backend.
backend$clear()
# Check that there is nothing on the cluster.
backend$peek()
# Stop the backend.
backend$stop()
# Check that the backend is not active.
backend$active
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.