Learn R Programming

pbdMPI

  • License:
  • Download:
  • Status:
  • Author: See section below.

This package provides a simplified, efficient, interface to MPI for HPC clusters. This derivation and rethinking of the Rmpi package embraces the prevalent parallel programming style on HPC clusters. It is based on S4 classes and methods.

If you don't have access to an HPC cluster, consider applying for an allocation at an HPC facility in your country. For example, US ACCESS, US INCITE, EU PRACE, Australia NCI, Canada RAC, Czechia IT4I, India NSM (National Super Computing Mission), Japan HPCI. (Please notify us if you have more examples or updates from your country.). Applying for a startup allocation can be easier than most would expect, sometimes as little as a paragraph describing your application and software. Large allocations require a full proposal.

With few exceptions, R does computations in memory. When data becomes too large to handle in the memory of a single node, or when more processors than those offered in commodity hardware are needed for a job, a typical strategy is to add more nodes. MPI, or the "Message Passing Interface", is the standard for managing multi-node computing. pbdMPI is a package that greatly simplifies the use of MPI from R.

In pbdMPI, we make extensive use of R's S4 system to simplify the interface. Instead of needing to specify the type (e.g., integer or double) of the data via function name (as in C implementations) or in an argument (as in Rmpi), you need only call the generic function on your data and we will always "do the right thing".

In pbdMPI, we write programs in the "Single Program/Multiple Data" or SPMD style, which is the prevalent style on HPC clusters. Contrary to the way much of the R world is aquainted with parallelism, there is no "manager". Each process (MPI rank) runs the same program as every other process, but operates on its own data or its own section of a global parameter space. This is arguably one of the simplest extensions of serial to massively parallel programming, and has been the standard way of doing things in the large-scale HPC community for decades. The "single program" can be viewed as a generalization of the serial program.

Installation

Installation with install.packages("pbdMPI") from CRAN or with remotes::install_github("RBigData/pbdMPI") from GitHub works on systems with MPI installed in a standard location. This is usually true on HPC Cluster Systems and also if you follow the Linux, MacOS, or Windows Notes below for MPI installation.

Usage

If you are comfortable with MPI concepts, you should find pbdMPI very agreeable and simple to use. Below is a basic "hello world" program:

# load the package and initialize MPI
suppressMessages(library(pbdMPI, quietly = TRUE))

# Hello world
message <- paste("Hello from rank", comm.rank(), "of", comm.size())
comm.print(message, all.rank = TRUE, quiet = TRUE)

# shut down the communicators and exit
finalize()

Save this as, say, mpi_hello_world.r and run it via:

mpirun -np 4 Rscript mpi_hello_world.r

The function comm.print() is a "sugar" function custom to pbdMPI that makes it simple to print in a distributed environment. The argument all.rank = TRUE specifies that all MPI ranks should print, and the quiet = TRUE argument tells each rank not to "announce" itself when it does its printing. This function and its companion comm.cat() automatically cooperate across the parallel instances of the single program to control printing.

Numerous other examples can be found in both the pbdMPI vignette as well as the pbdDEMO package and its corresponding vignette. While these were written for version 0.3-0 of pbdMPI, they are still highly relevant.

HPC Cluster Systems Notes

HPC clusters are Linux systems and use Environment Modules to manage software. Consult your local cluster documentation as specifics with respect to R and MPI can differ. Usually, an MPI version is installed and should work with pbdMPI standard install, although sometimes a module load openmpi might be needed to get OpenMPI.

Some common module commands are:

module list  # lists currently loaded software modules
module avail # lists available software modules
module load <module_name> # loads module <module_name>

Available R modules are typically loaded via module load r or module load R, possibly with directory and version information. On some systems, this needs to be preceded by selecting a programming environment, which may be gnu, pgi, etc., while on others loading R automatically selects the correct programming environment. Please consult your HPC cluster documentation. Typically, software installations are done on login nodes and parallel debugging and production runs on compute nodes.

A resource manager, usually Slurm, PBS, LSF, or SGE is used to allocate compute nodes for a job. Consult your cluster documentation, as defaults tend to be site-specific.

Scripts are usually submitted as batch jobs but interactive allocations are possible too. For batch submission, we recommend writing a shell script. Here we give a shell script example for Slurm and note that a translation table is available to other resource managers.

#!/bin/bash
#SBATCH -J <my_job>
#SBATCH -A <my_account>
#SBATCH --nodes=4
#SBATCH --exclusive
#SBATCH -t 00:20:00
#SBATCH --mem=0

module load gcc
module load openmpi
module load r

mpirun --map-by ppr:4:node Rscript <your_r_script> 

This example runs asynchronously 16 copies (4 per node) of <your_r_script> in separate R sessions, communicating with each other via OpenMPI. If 128 cores are available on a node, further parallelism (32 per R session) is available for shared-memory parallel approaches (such as mclapply() or multithreaded libraries, like OpenBLAS, possibly via FlexiBLAS). The parameter --exclusive requests exclusive access to all cores on the nodes, --mem=0 requests all memory, and -t 00:20:00 asks for 20 minutes of time. Save this Slurm script in a file <your_script.sh> and submit with sbatch <your_script.sh>. To quickly troubleshoot a Slurm script at your location, replace Rscript <your_r_script> with hostname.

Linux Notes

See INSTALL file for details.

Mac OS Notes

MacOS does not provide MPI, so first install a recent version of OpenMPI. This is best done via Homebrew. Homebrew will automatically ask to install Xcode Command Line Tools (CLT) if you have not yet done so (You don't need all of Xcode, just the CLT), see Homebrew installation. After installing Homebrew,

brew install openmpi

will install OpenMPI in a location that pbdMPI can find. Then, follow standard R package installation for pbdMPI.

Parallelizing with distributed-memory concepts (like MPI) on shared-memory platforms (like a single node or a laptop) does produce excellent speedups but does not extend available memory for larger data objects. Chunking of larger objects does not extend available memory but does prevent duplication of the objects in memory when running several R sessions in shared memory of a laptop.

Windows Notes

Windows does not provide MPI, so first an MPI installation (binary, header, and libraries) is needed. We recommend installing Microsoft MPI which is based on MPICH.

Download MS-MPI v10.1.3 (msmpisetup.exe) and SDK (msmpisdk.msi) from the Microsoft Download Center.

See INSTALL file for the installation and for the usage of mpiexec.exe.

Authors

pbdMPI is authored and maintained by the pbdR core team:

  • Wei-Chen Chen
  • George Ostrouchov
  • Drew Schmidt

With additional contributions from:

  • Pragneshkumar Patel
  • Hao Yu
  • Christian Heckendorf
  • Brian Ripley (Windows HPC Pack 2012)
  • The R Core team (some functions are modified from the base packages)

Copy Link

Version

Install

install.packages('pbdMPI')

Monthly Downloads

858

Version

0.5-2

License

Mozilla Public License 2.0

Maintainer

Last Published

September 18th, 2024

Functions in pbdMPI (0.5-2)

gather-methods

A Rank Gathers Objects from Every Rank
reduce-method

A Rank Receive a Reduction of Objects from Every Rank
isend-method

A Rank Send (Nonblocking) an Object to the Other Rank
is.comm.null

Check if a MPI_COMM_NULL
sendrecv-method

Send and Receive an Object to and from Other Ranks
Utility execmpi

Execute MPI code in system
get job id

Divide Job ID by Ranks
global Rprof

A Rprof Function for SPMD Routines
global all pairs

Global All Pairs
probe

Probe Functions
allreduce-method

All Ranks Receive a Reduction of Objects from Every Rank
global sort

Global Quick Sort for Distributed Vectors or Matrices
global stop and warning

Global Stop and Warning Functions
Task Pull

Functions for Task Pull Parallelism
alltoall

All to All
info

Info Functions
bcast-method

A Rank Broadcast an Object to Every Rank
sendrecv.replace-method

Send and Receive an Object to and from Other Ranks
comm.chunk

comm.chunk
Comm Internal Functions

All Comm Internal Functions
wait

Wait Functions
sourcetag

Functions to Obtain source and tag
SPMD Internal Functions

All SPMD Internal Functions
global reading

Global Reading Functions
global writing

Global Writing Functions
SPMD Control Functions

Sets of controls in pbdMPI.
pbdMPI-package

R Interface to MPI (Programming with Big Data in R Project)
scatter-method

A Rank Scatter Objects to Every Rank
Package Tools

Functions for Get/Print MPI_COMM Pointer (Address)
SPMD Control

Default control in pbdMPI.
global any and all

Global Any and All Functions
global base

Global Base Functions
apply and lapply

Parallel Apply and Lapply Functions
recv-method

A Rank Receives (Blocking) an Object from the Other Rank
global print and cat

Global Print and Cat Functions
send-method

A Rank Send (blocking) an Object to the Other Rank
irecv-method

A Rank Receives (Nonblocking) an Object from the Other Rank
seed for RNG

Parallel random number generation with reproducible results
global pairwise

Global Pairwise Evaluations
global range, max, and min

Global Range, Max, and Min Functions
Set global pbd options

Set Global pbdR Options
Get Configures Used at Compiling Time

Functions to Get MPI and/or pbdMPI Configures Used at Compiling Time
allgather-methods

All Ranks Gather Objects from Every Rank
MPI array pointers

Set or Get MPI Array Pointers in R
communicator

Communicator Functions
global as.gbd

Global As GBD Function
global balanc

Global Balance Functions
global distance function

Global Distance for Distributed Matrices
global match.arg

Global Argument Matching
global timer

A Timing Function for SPMD Routines
global which, which.max, and which.min

Global Which Functions