# R R je programski jezik za statistično računanje in grafiko. [![](../files/u3/R.png)](../files/u3/R.png) Vzporedno računanje z R omogoča paket [Rmpi](http://math.acadiau.ca/ACMMaC/Rmpi/), ki je nameščen s podporo OpenMPI. Delo pričnemo z namestivijo modula r in kopiranjem inicializacijskega profila (samo ob prvi uporabi) ``` text $ module load r Loading intel/12.1.3 Loading openmpi/1.4.4 for compiler intel-12.1 Loading r/2.15.0 $ cp /usr/local/r/2.15.0/lib64/R/library/Rmpi/Rprofile $HOME/.Rprofile ``` Za primer bomo pokazali osnovno komunikacijo s programom mpi.R ``` text # 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() ``` Pripravimo še datoteko rmpi-demo.sh za poganjanje v čakalni vrsti (particiji) na šestih procesorjih ``` text #!/bin/bash #SBATCH --ntasks=6 #SBATCH --job-name=R-demo #SBATCH --partition=rome #SBATCH --ntasks-per-core=1 module load r mpirun R --slave CMD BATCH mpi.R ``` ki ob zagonu z ukazom ``` text sbatch rmpi-demo.sh ``` izpiše v datoteko mpi.Rout naslednji izpis ``` text R version 2.15.0 (2012-03-30) Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: x86_64-unknown-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. master (rank 0, comm 1) of size 6 is running on: cn52 slave1 (rank 1, comm 1) of size 6 is running on: cn52 slave2 (rank 2, comm 1) of size 6 is running on: cn52 slave3 (rank 3, comm 1) of size 6 is running on: cn52 slave4 (rank 4, comm 1) of size 6 is running on: cn52 slave5 (rank 5, comm 1) of size 6 is running on: cn52 > # 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() Error in mpi.spawn.Rslaves() : It seems there are some slaves running on comm 1 > # 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 6" $slave2 [1] "I am 2 of 6" $slave3 [1] "I am 3 of 6" $slave4 [1] "I am 4 of 6" $slave5 [1] "I am 5 of 6" > # Tell all slaves to close down, and exit the program > mpi.close.Rslaves() [1] 1 > mpi.quit() ``` Na večjem številu procesorjev in brez SLURM skriptne datoteke lahko poženemo R z ukazom ``` text sbatch --partition=rome --ntasks=48 --ntasks-per-core=1 --mail-type=END --wrap="mpirun R --slave CMD BATCH mpi.R" ``` S tem da dobimo obvestilo o poteku v poštni predal. Za enostavnejše programiranje lahko uporabimo še knjižnico snowfall.