ALIs

kommt noch

R: MPI Extension

This R package allow you to create R programs which run cooperatively in parallel across multiple machines, or multiple CPUs on one machine, to accomplish a goal more quickly than running a single program on one machine.

How to use Rmpi

Rmpi is an implementation of R which runs across multiple processors, possibly on multiple machines, using the MPI programming model. Please note that this page is not a tutuorial on how to write Rmpi scripts, just a short example of how to run an Rmpi job at OSC. Like any R job, you must load the R module before using Rmpi.

module load R/2.8.1mpicc

To run an Rmpi job you will need to create both a PBS script (to submit the job) and an Rmpi script. In the example below, we start an Rmpi process on four processors on each of two machines (8 processors and processes in total) and simply ask them to say "Hello". The PBS script looks like this:

#!/bin/bash

#PBS -V
#PBS -N Rtest
#PBS -l walltime=01:00:00
#PBS -l select=2:procs=4

cd $PBS_O_WORKDIR

cp $R_HOME/lib64/R/library/Rmpi/Rprofile ./.Rprofile

mpiexec -n 8 R CMD BATCH myjob_mpi.R

Note that for Rmpi the use of mpiexec and not mpirun is mandatory. The reasons are too lengthy to go into here but it's to do with how R picks up on the right processors/nodes to use. The script runs an Rmpi job saved in a file called "myjob_mpi.R". The R file looks like this:

# Load the R MPI package if it is not already loaded.
if  (!is.loaded("mpi_initialize")) {
   library("Rmpi")
}
# In case R exits unexpectedly, have it automatically clean up
# resources taken up by Rmpi (slaves, memory, etc...)

.Last <- function(){
if (is.loaded("mpi_initialize")){
      if (mpi.comm.size(1) > 0){
          print("Please use mpi.close.Rslaves() to close slaves.")
          mpi.close.Rslaves()
      }
      print("Please use mpi.quit() to quit R")
      .Call("mpi_finalize")
  }
}

# Tell all slaves to return a message identifying themselves
mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))

# Tell all slaves to close down, and exit the program
mpi.close.Rslaves()
mpi.quit()

The output of the job looks something like this:

R version 2.8.1 (2008-02-08)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0 
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to...
.
.
. 
master (rank 0, comm 1) of size 8 is running on: compB051
slave1 (rank 1, comm 1) of size 8 is running on: compB051
slave2 (rank 2, comm 1) of size 8 is running on: compB051
slave3 (rank 3, comm 1) of size 8 is running on: compB051
slave4 (rank 4, comm 1) of size 8 is running on: compB053
slave5 (rank 5, comm 1) of size 8 is running on: compB053
slave6 (rank 6, comm 1) of size 8 is running on: compB053
slave7 (rank 7, comm 1) of size 8 is running on: compB053

> # Load the R MPI package if it is not already loaded.
>
> if  (!is.loaded("mpi_initialize")) {
+    library("Rmpi")
+    }
>
> # In case R exits unexpectedly, have it automatically clean up
> # resources taken up by Rmpi (slaves, memory, etc...)
>
> .Last <- function(){
+   if (is.loaded("mpi_initialize")){
+       if (mpi.comm.size(1) > 0){
+           print("Please use mpi.close.Rslaves() to close slaves.")
+           mpi.close.Rslaves()
+       }
+       print("Please use mpi.quit() to quit R")
+       .Call("mpi_finalize")
+   }
+ }
>
> # Tell all slaves to return a message identifying themselves
> mpi.remote.exec(paste("I am",mpi.comm.rank(),"of",mpi.comm.size()))
$slave1
[1] "I am 1 of 8"
$slave2
[1] "I am 2 of 8"
$slave3
[1] "I am 3 of 8"
$slave4
[1] "I am 4 of 8"
$slave5
[1] "I am 5 of 8"
$slave6
[1] "I am 6 of 8"
$slave7
[1] "I am 7 of 8"
>
> # Tell all slaves to close down, and exit the program
> mpi.close.Rslaves()
[1] 1
> mpi.quit()