'foreach' Parallel Adapter for 'parabar' Backends.
Parallel Adaptor
foreach 🔌 parabar
The doParabar package acts as a foreach parallel adaptor for parabar backends. It provides a minimal implementation for the foreach::%dopar% operator, enabling seamless integration of the parabar package with the foreach package.
Installation
You can install doParabar directly from CRAN using the following command:
# Install the package from `CRAN`.
install.packages("doParabar")
Alternatively, you can also install the latest development version from GitHub via:
# Install the package from `GitHub`.
remotes::install_github("mihaiconstantin/doParabar")
Then, load the package as usual using the library function:
# Load the package.
library(doParabar)
Note. By default, and for various reasons, the doParabar package does not automatically load other packages. Instead, it is recommended to load the foreach and parabar packages explicitly in your scripts (i.e., or add them to your Imports in the DESCRIPTION file when developing an R package).
# Load the `foreach` package.
library(foreach)
# Load the `parabar` package.
library(parabar)
Note. Should you need to suppress the package startup messages (e.g., from the parabar package) you can use the suppressPackageStartupMessages function (e.g., suppressPackageStartupMessages(parabar)).
Usage
Below you can find a minimal example of how to use doParabar and parabar packages in your R scripts. All examples below assume that you have already installed and loaded the packages.
[!TIP] For a more detailed discussion see the vignette "Using
parabarwithforeach".
# Create an asynchronous `parabar` backend.
backend <- start_backend(cores = 2, cluster_type = "psock", backend_type = "async")
# Register the backend with the `foreach` package for the `%dopar%` operator.
registerDoParabar(backend)
# Get the parallel backend name.
getDoParName()
# Check that the parallel backend has been registered.
getDoParRegistered()
# Get the current version of backend registration.
getDoParVersion()
# Get the number of cores used by the backend.
getDoParWorkers()
# Define some variables strangers to the backend.
x <- 10
y <- 100
z <- "Not to be exported."
# Used the registered backend to run a task in parallel via `foreach`.
results <- foreach(i = 1:300, .export = c("x", "y"), .combine = c) %dopar% {
# Sleep a bit.
Sys.sleep(0.01)
# Compute and return.
i + x + y
}
# Show a few results.
head(results, n = 10)
tail(results, n = 10)
# Verify that the variable `z` was not exported.
try(evaluate(backend, z))
# To make packages available on the backend, see the `.packages` argument.
# Stop the backend.
stop_backend(backend)
[!NOTE] The
doParabarpackage provides only a minimal implementation for theforeach::%dopar%operator. If you need additional functionality, please consider contributing to the package, or opening an issue onGitHub.
Contributing
- Any contributions are welcome and greatly appreciated. Please open a pull request on
GitHub. - To report bugs, or request new features, please open an issue on
GitHub.
License
- The package source code in this repository is licensed under the MIT license.
The
parabaranddoParabardocumentation, vignettes, and other website materials by Mihai Constantin are licensed under CC BY 4.0.
