| Title: | 'foreach' Parallel Adapter for 'parabar' Backends |
|---|---|
| Description: | Provides a 'foreach' parallel adapter for 'parabar' backends. This package offers a minimal implementation of the '%dopar%' operator, enabling users to run 'foreach' loops in parallel, leveraging the parallel and progress-tracking capabilities of the 'parabar' package. Learn more about 'parabar' and 'doParabar' at <https://parabar.mihaiconstantin.com>. |
| Authors: | Mihai Constantin [aut, cre] (ORCID: <https://orcid.org/0000-0002-6460-0107>) |
| Maintainer: | Mihai Constantin <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.2 |
| Built: | 2026-05-27 07:07:11 UTC |
| Source: | https://github.com/mihaiconstantin/doparabar |
The registerDoParabar() function registers the provided
backend created by parabar::start_backend()
to be used as the parallel processing backend for the foreach::%dopar%
operator implementation.
registerDoParabar(backend)registerDoParabar(backend)
backend |
An object of class |
Additional information about the registered parallel backend can be extracted
using the foreach::getDoParName(), foreach::getDoParRegistered(),
foreach::getDoParVersion(), and foreach::getDoParWorkers() functions. See
the Examples section.
The registerDoParabar() function returns void.
The parallel backend implementation for the foreach::%dopar% operator is
provided by the doPar() function. Please check the Details
section of its documentation to understand the extent of completeness of the
implementation.
doParabar, doPar(), parabar::start_backend()
and parabar::stop_backend().
# Manually load the libraries. library(doParabar) library(parabar) library(foreach) # 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)# Manually load the libraries. library(doParabar) library(parabar) library(foreach) # 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)