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

PBS job script example

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

#PBS -S /bin/bash
#PBS -q beta
#PBS -l select=1:ncpus=24:mpiprocs=24
#PBS -j oe

# move back to the directory where the job was submitted
cd $PBS_O_WORKDIR

# load R module
module load R/3.6.3

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

exit 0

This job script :

  • requests PBS for a single node in the beta partition, where 24 CPUs will be used by means of 24 MPI processes.
  • assumes the R script named ‘test-dompi.R’ is in the directory where the jobitself was submitted (information held in variable PBS_O_WORKDIR).
  • uses R version 3.6.3, but other versions are available.
  • starts 24 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:144, .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 144.
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.