Skip to main content

R and dompi Package

This page explains how to configure your job in order to run a R script that relies on the doMPI package.

Job script example​

Below is an example of a Slurm job script which will execute a doMPI-R script in parallel :

#!/bin/bash
#SBATCH -n 32
#SBATCH -p std

# load R module
module load R/4.5.0

# launch parallel execution instances of R running script file test-dompi.R
mpirun /bin/bash $(which R) CMD BATCH --no-save test-dompi.R

exit 0

This job script :

  • requests Slurm for 32 task slots (i.e. 32 CPUs) in the std partition
  • assumes the R script named ‘test-dompi.R’ is in the directory where the job itself was submitted.
  • uses R version 4.3.3, but other versions might be available.
  • starts 32 R processes by using mpirun command. All R processes will process the script file ‘test-dompi.R’, but they will synchronize in order to execute concurrently the dopar loops.

R script example​

Below is an example of an R script where the parallel computation relies on doMPI :

# doMPI initialization
library(doMPI)
cl <- startMPIcluster()
registerDoMPI(cl)
# parallel computation: x[i]=sqrt(i)
x <- foreach(i=1:512, .combine="c") %dopar% {
sqrt(i)
}
# display result
x
# script termination
closeCluster(cl)
mpi.quit()

This R script contains a single parallel computation which computes the square-root of all integers between 1 and 512. This computation being parallel, the foreach loop receives the dopar operator.

doMPI initialization and script termination lines are mandatory for having a working parallel execution of R.