Difference between revisions of "R MPI Example"
Jump to navigation
Jump to search
Line 35: | Line 35: | ||
mpi.quit() | mpi.quit() | ||
</source> | </source> | ||
+ | |||
Example job script using rmpi_test.R script. | 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. | ||
<source lang=bash> | <source lang=bash> | ||
#!/bin/sh | #!/bin/sh |
Revision as of 18:51, 25 October 2016
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 --ntasks=24 # Number of MPI ranks
#SBATCH --cpus-per-task=1 # Number of cores per MPI rank
#SBATCH --nodes=2 #Number of nodes
#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
mpiexec Rscript /ufrc/data/training/SLURM/prime/rmpi_test.R
date