Difference between revisions of "R MPI Example"

From UFRC
Jump to navigation Jump to search
Line 47: Line 47:
 
#SBATCH --mail-type=ALL # Mail events (NONE, BEGIN, END, FAIL, ALL)
 
#SBATCH --mail-type=ALL # Mail events (NONE, BEGIN, END, FAIL, ALL)
 
#SBATCH --mail-user=ENTER_YOUR_EMAIL_HERE # Where to send mail
 
#SBATCH --mail-user=ENTER_YOUR_EMAIL_HERE # Where to send mail
#SBATCH --ntasks=24 # Number of MPI ranks
 
 
#SBATCH --cpus-per-task=1 # Number of cores per MPI rank  
 
#SBATCH --cpus-per-task=1 # Number of cores per MPI rank  
 
#SBATCH --nodes=2 #Number of nodes
 
#SBATCH --nodes=2 #Number of nodes
 +
#SBATCH --ntasks=24 # Number of MPI ranks
 
#SBATCH --ntasks-per-node=12 #How many tasks on each node
 
#SBATCH --ntasks-per-node=12 #How many tasks on each node
 
#SBATCH --ntasks-per-socket=6 #How many tasks on each CPU or socket
 
#SBATCH --ntasks-per-socket=6 #How many tasks on each CPU or socket
Line 63: Line 63:
 
module load intel/2016.0.109 openmpi/1.10.2 Rmpi/3.3.1
 
module load intel/2016.0.109 openmpi/1.10.2 Rmpi/3.3.1
  
mpiexec Rscript /ufrc/data/training/SLURM/prime/rmpi_test.R
+
# Make sure the mpi.spawn.Rslaves(nslaves=X) code spawns X slaves where X is one less than the number of MPI ranks
 +
mpiexec -np 1 Rscript /ufrc/data/training/SLURM/prime/rmpi_test.R
  
 
date
 
date
 
</source>
 
</source>

Revision as of 19:57, 2 November 2016

back to the main R page

Example, of using the parallel module to run MPI jobs under Rmpi.

{{#fileAnchor: rmpi_test.R}} Download raw source of the [{{#fileLink: rmpi_test.R}} rmpi_test.R] file.

# Load the R MPI package if it is not already loaded.
if (!is.loaded("mpi_initialize")) {
    library("Rmpi")
    }
                                                                                
# Spawn as many slaves as possible
mpi.spawn.Rslaves()
                                                                                
# 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()


Example job script using rmpi_test.R script.


{{#fileAnchor: mpi_job.sh}} Download raw source of the [{{#fileLink: mpi_job.sh}} mpi_job.sh] file.

#!/bin/sh
#SBATCH --job-name=mpi_job_test # Job name
#SBATCH --mail-type=ALL # Mail events (NONE, BEGIN, END, FAIL, ALL)
#SBATCH --mail-user=ENTER_YOUR_EMAIL_HERE # Where to send mail	
#SBATCH --cpus-per-task=1 # Number of cores per MPI rank 
#SBATCH --nodes=2 #Number of nodes
#SBATCH --ntasks=24 # Number of MPI ranks
#SBATCH --ntasks-per-node=12 #How many tasks on each node
#SBATCH --ntasks-per-socket=6 #How many tasks on each CPU or socket
#SBATCH --distribution=cyclic:cyclic #Distribute tasks cyclically on nodes and sockets
#SBATCH --mem-per-cpu=1gb # Memory per processor
#SBATCH --time=00:05:00 # Time limit hrs:min:sec
#SBATCH --output=mpi_test_%j.out # Standard output and error log
pwd; hostname; date

echo "Running example Rmpi script. Using $SLURM_JOB_NUM_NODES nodes with $SLURM_NTASKS 
tasks, each with $SLURM_CPUS_PER_TASK cores."

module load intel/2016.0.109 openmpi/1.10.2 Rmpi/3.3.1

# Make sure the mpi.spawn.Rslaves(nslaves=X) code spawns X slaves where X is one less than the number of MPI ranks
mpiexec -np 1 Rscript /ufrc/data/training/SLURM/prime/rmpi_test.R

date