| Title: | Apply-Style Dispatch for High-Performance Computing |
|---|---|
| Description: | Provides apply-style functions using the Message Passing Interface ('MPI') to improve the High-Performance Computing ('HPC') environment in R. The package supports long vectors and efficient handling of large datasets for MPI-based parallel computations. |
| Authors: | Ei-ji Nakama [aut, cre], Junji NAKANO [aut] |
| Maintainer: | Ei-ji Nakama <[email protected]> |
| License: | AGPL-3 |
| Version: | 0.26.4 |
| Built: | 2026-07-22 10:16:31 UTC |
| Source: | https://github.com/e-nakama/rhpc |
Provides MPI-aware apply-style functions and worker utilities for high-performance computing in R.
The Rhpc package offers MPI-aware versions of common apply-style functions and helper utilities for distributed computation. It supports both static worker configuration and dynamic process generation.
Main functions:
Rhpc_initializeInitialize MPI environment
Rhpc_getHandleGet communicator handle to workers
Rhpc_lapply, Rhpc_lapplyLB
Parallel lapply variants
Rhpc_sapply, Rhpc_sapplyLB
Parallel sapply variants
Rhpc_applyParallel apply for arrays/matrices
Rhpc_worker_callCall functions on workers
Rhpc_worker_nobackCall functions without waiting for results on workers
Rhpc_ExportExport variables to workers
Rhpc_setupRNGSetup random number generators
Rhpc_finalizeClean up MPI environment
MPI-related options are exposed through getOption(): Rhpc.mpi.c.comm, Rhpc.mpi.f.comm, Rhpc.mpi.rank, and Rhpc.mpi.procs.
See https://github.com/e-nakama/Rhpc for more information and examples.
MPI-based version of apply for arrays and matrices.
Rhpc_apply(cl = NULL, X, MARGIN, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))Rhpc_apply(cl = NULL, X, MARGIN, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))
cl |
external pointer to MPI communicator |
X |
array or matrix |
MARGIN |
vector giving the subscripts which the function will be applied over.
|
FUN |
function to apply |
... |
additional arguments passed to |
usequote |
logical; if |
This function applies FUN to array margins specified by MARGIN,
distributing the work across MPI workers.
If X has dimension subscripts not in MARGIN, those are used as the margin dimensions
for applying the function.
Array or matrix of results from applying FUN to the specified margins.
Quotes arguments for use in remote function calls on worker processes.
Rhpc_enquote(...)Rhpc_enquote(...)
... |
arguments to be quoted |
This function quotes its arguments to prevent premature evaluation in the master process. The quoted expressions are then evaluated on the worker processes where they have access to the worker's data and environment.
This is primarily an internal utility; users typically do not need to call it directly.
A quoted representation of the arguments suitable for remote evaluation.
Evaluates an R expression on all worker processes.
Rhpc_EvalQ(cl, expr, usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), envir = .GlobalEnv)Rhpc_EvalQ(cl, expr, usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), envir = .GlobalEnv)
cl |
external pointer to MPI communicator |
expr |
expression to evaluate on workers |
usequote |
logical; if |
envir |
environment in which to evaluate the expression on workers (default: global environment) |
This function evaluates the given expression on all worker processes and returns the results. It is useful for initialization tasks or querying worker state.
Results of evaluating the expression on all workers.
Sends R objects from the master process to all worker processes.
Rhpc_Export(cl, variableNames, usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), pos = 1, envir = as.environment(pos))Rhpc_Export(cl, variableNames, usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), pos = 1, envir = as.environment(pos))
cl |
external pointer to MPI communicator |
variableNames |
character vector of object names to export |
usequote |
logical; if |
pos |
environment position from which to retrieve objects (default: global environment) |
envir |
environment from which to retrieve objects (default: environment at position |
This function retrieves objects from the master's environment and sends them to all worker processes, making them available in the workers' global environment.
Objects are sent via collective communication.
Returns invisibly. Objects are assigned in worker environments.
Finalizes the MPI environment and terminates all worker processes.
Rhpc_finalize()Rhpc_finalize()
This function should be called at the end of an Rhpc session to properly shut down the MPI environment and clean up all resources.
Returns invisibly. Resources are cleaned up.
Rhpc_initialize, Rhpc_getHandle
Obtains a handle to the MPI communicator for worker processes.
Rhpc_getHandle(procs = NA)Rhpc_getHandle(procs = NA)
procs |
number of processes to create via dynamic process generation. If |
This function returns an external pointer to the MPI communicator, which is used in all subsequent Rhpc function calls. It also initializes the random number generation stream for workers.
If procs is specified, dynamic process generation is used (MPI_Comm_spawn).
If procs is NA, static worker configuration is assumed.
An external pointer to the MPI communicator handle, which can be passed to other Rhpc functions.
Rhpc_initialize, Rhpc_numberOfWorker, Rhpc_finalize
Initializes the MPI environment for Rhpc.
Rhpc_initialize()Rhpc_initialize()
This function must be called before any other Rhpc functions to initialize the MPI communicator. It sets up the MPI environment and prepares the system for distributed computation.
Returns invisibly. MPI initialization results are stored internally.
MPI-based version of lapply that distributes work across worker processes sequentially.
Rhpc_lapply(cl, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))Rhpc_lapply(cl, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))
cl |
external pointer to MPI communicator |
X |
vector or list to be processed |
FUN |
function to apply to each element of |
... |
additional arguments passed to |
usequote |
logical; if |
This function distributes elements of X sequentially to worker processes.
The input X is divided among workers and each worker processes its portion in order.
Results are collected and returned in a list maintaining the original order.
A list with results from applying FUN to each element of X.
Rhpc_lapplyLB, Rhpc_sapply, Rhpc_apply
MPI-based version of lapply with load balancing for better performance on heterogeneous systems.
Rhpc_lapplyLB(cl, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))Rhpc_lapplyLB(cl, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))
cl |
external pointer to MPI communicator |
X |
vector or list to be processed |
FUN |
function to apply to each element of |
... |
additional arguments passed to |
usequote |
logical; if |
This function distributes work to workers using a load-balancing approach. Workers receive tasks dynamically as they become available, which can provide better performance when task execution times vary significantly.
Results are collected and returned in a list maintaining the original order.
A list with results from applying FUN to each element of X.
Rhpc_lapply, Rhpc_sapplyLB, Rhpc_apply
Returns the number of active worker processes.
Rhpc_numberOfWorker(cl)Rhpc_numberOfWorker(cl)
cl |
external pointer to MPI communicator (obtained from |
An integer giving the number of worker processes.
MPI-based version of sapply that distributes work across worker processes.
Rhpc_sapply(cl = NULL, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), simplify = TRUE, USE.NAMES = TRUE)Rhpc_sapply(cl = NULL, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), simplify = TRUE, USE.NAMES = TRUE)
cl |
external pointer to MPI communicator |
X |
vector or list to be processed |
FUN |
function to apply to each element of |
... |
additional arguments passed to |
usequote |
logical; if |
simplify |
logical or character string; if possible, simplify the result to a vector or matrix |
USE.NAMES |
logical; if |
This function combines the functionality of Rhpc_lapply with automatic simplification of results.
When possible, results are simplified to a vector or matrix form rather than remaining as a list.
A simplified version of the result (vector, matrix, or array) or list depending on simplify parameter.
Rhpc_sapplyLB, Rhpc_lapply, Rhpc_apply
MPI-based version of sapply with load balancing for better performance.
Rhpc_sapplyLB(cl = NULL, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), simplify = TRUE, USE.NAMES = TRUE)Rhpc_sapplyLB(cl = NULL, X, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE), simplify = TRUE, USE.NAMES = TRUE)
cl |
external pointer to MPI communicator |
X |
vector or list to be processed |
FUN |
function to apply to each element of |
... |
additional arguments passed to |
usequote |
logical; if |
simplify |
logical or character string; if possible, simplify the result |
USE.NAMES |
logical; if |
This function combines load-balanced distribution with result simplification. Workers receive tasks dynamically as they become available, and results are simplified when possible.
A simplified version of the result (vector, matrix, array, or list) depending on simplify.
Rhpc_sapply, Rhpc_lapplyLB, Rhpc_apply
Serialize R objects for transmission or storage, and unserialize them back.
Rhpc_serialize(obj) Rhpc_serialize_onlysize(obj) Rhpc_serialize_norealloc(obj) Rhpc_unserialize(obj)Rhpc_serialize(obj) Rhpc_serialize_onlysize(obj) Rhpc_serialize_norealloc(obj) Rhpc_unserialize(obj)
obj |
R object to serialize, or serialized object to unserialize |
These functions provide low-level serialization utilities for handling R objects:
Rhpc_serializeFully serializes an object to raw bytes, allocating necessary memory
Rhpc_serialize_onlysizeReturns only the size needed for serialization without actual serialization
Rhpc_serialize_noreallocSerializes without reallocating memory (for efficiency)
Rhpc_unserializeReconstructs an R object from its serialized form
These functions are primarily intended for internal Rhpc use but may be useful for debugging or advanced users.
For Rhpc_serialize: raw vector containing serialized data
For Rhpc_serialize_onlysize: integer giving the size in bytes
For Rhpc_serialize_norealloc: raw vector containing serialized data
For Rhpc_unserialize: original R object
## Serialization example (executable without MPI) data_to_serialize <- list(a = 1:5, b = c("x", "y", "z")) serialized <- Rhpc_serialize(data_to_serialize) deserialized <- Rhpc_unserialize(serialized) stopifnot(identical(data_to_serialize, deserialized))## Serialization example (executable without MPI) data_to_serialize <- list(a = 1:5, b = c("x", "y", "z")) serialized <- Rhpc_serialize(data_to_serialize) deserialized <- Rhpc_unserialize(serialized) stopifnot(identical(data_to_serialize, deserialized))
Initializes independent random number streams for each worker process using L'Ecuyer-CMRG algorithm.
Rhpc_setupRNG(cl, iseed = NULL)Rhpc_setupRNG(cl, iseed = NULL)
cl |
external pointer to MPI communicator |
iseed |
optional integer seed for reproducibility. If |
This function sets up independent random number streams for each worker to ensure they produce different but reproducible random sequences. It uses the L'Ecuyer-CMRG (Combined Multiple Recursive Generator) algorithm.
The master process's original .Random.seed is saved to options("Rhpc.oldseed") but is NOT
automatically restored due to CRAN policy. Users should manually restore if needed:
assign(".Random.seed", getOption("Rhpc.oldseed"), envir = globalenv())
Returns invisibly the original .Random.seed that was saved.
Divides a list or vector into approximately equal-sized chunks for distribution to workers.
Rhpc_splitList(var, num)Rhpc_splitList(var, num)
var |
vector or list to be split |
num |
number of chunks to create (typically equal to the number of workers) |
This function divides var into num roughly equal-sized chunks.
It is useful for manually distributing work to workers when fine-grained control is needed.
This is primarily an internal utility; the apply-style functions handle splitting automatically.
A list of length num, each element containing a portion of var.
Rhpc_lapply, Rhpc_numberOfWorker
Executes a function on all worker processes and collects the results.
Rhpc_worker_call(cl, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))Rhpc_worker_call(cl, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))
cl |
external pointer to MPI communicator |
FUN |
function or string naming the function to be called on workers |
... |
arguments to pass to |
usequote |
logical; if |
This function broadcasts a function call to all worker processes and waits for results. Results from all workers are collected and returned.
The function or its name is sent to workers via collective communication only on the first call; subsequent calls with the same function reuse the cached version.
Results collected from all worker processes.
Rhpc_worker_noback, Rhpc_lapply, Rhpc_EvalQ
Executes a function on all worker processes without waiting for or collecting results.
Rhpc_worker_noback(cl, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))Rhpc_worker_noback(cl, FUN, ..., usequote = ifelse(is.logical(getOption("Rhpc.usequote")), getOption("Rhpc.usequote"), TRUE))
cl |
external pointer to MPI communicator |
FUN |
function or string naming the function to be called on workers |
... |
arguments to pass to |
usequote |
logical; if |
This function sends a function call to all worker processes but does not wait for results. It is useful for operations like setting options or initializing worker state where return values are not needed.
Returns invisibly. No results are collected from workers.