R
Category: Analysis-Mathematics
Description
R is a software environment for statistical computing and graphics with parallel capability. R and its several thousand add-on packages (CRAN package repository) is supported by a world-wide community.
For more information about R, see the R website.
Information about CRAN Task View: High-Performance and Parallel Computing with R can be found here: CRAN HPC with R
Use
There are several versions of R (2.12, 2.13, 2.14.2 and 2.15) installed on
Nautilus. The default version is 2.12.0. Load the R package with the
module command:
> module load r > module display r # to display R environment variables
Additionally, we have installed the following packages for parallel computing with R: Rmpi, snow, multicore, foreach, doMC, pnmath, gputools and many others. Please let us know, if you want to have some other packages to be installed on Nautilus(R Package request).
The following sections give detailed usage examples for these packages.
Example Scripts
Rmpi
An example R script, Rmpi_example.R:
# 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(nslaves=8) # 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()
An example PBS script to run Rmpi_example.R:
#PBS -N Rmpi_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j oe #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r module swap mpt mpt/2.04 export TMPDIR=$HOME export LC_ALL=C cd $PBS_O_WORKDIR mpirun -np 8 Rscript Rmpi_example.R
For more information about Rmpi, see the Rmpi Tutorial.
Snow
An example Snow script, Snow_example.R:
library(snow) cl <- makeCluster(8, type = "SOCK") ## Serial execution system.time(runif(1500000)) ## Paralel execution system.time(clusterApply(cl,c(600000,900000),runif)) stopCluster(cl)
An example PBS script to run Snow_example.R:
#PBS -N Snow_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j oe #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r module swap mpt mpt/2.04 export TMPDIR=$HOME export LC_ALL=C cd $PBS_O_WORKDIR mpirun -np 8 Rscript Snow_example.R
For more information about Snow, see the Snow documentation.
Multicore
An example multicore script, Multicore_example.R:
library(multicore)
multicore:::detectCores()
options(cores = 8)
getOption('cores')
test <- lapply(1:10,function(x) rnorm(10000))
system.time(x <- lapply(test,function(x) loess.smooth(x,x)))
system.time(x <- mclapply(test,function(x) loess.smooth(x,x)))
An example PBS script to run Multicore_example.R:
#PBS -N Multicore_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j or #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r export TMPDIR=$HOME export LC_ALL=C cd $PBS_O_WORKDIR R CMD BATCH Multicore_example.R
For more information about Multicore, see the Multicore documentation.
Foreach and doMC
An example foreach with doMC script, Foreach_example.R:
library(foreach) library(doMC) multicore:::detectCores() registerDoMC(cores=8) system.time(foreach(i=1:10) %do% sum(runif(10000000))) system.time(foreach(i=1:10) %dopar% sum(runif (10000000)))
An example PBS script to run Foreach_example.R:
#PBS -N Foreach_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j or #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r export TMPDIR=$HOME export LC_ALL=C cd $PBS_O_WORKDIR R CMD BATCH Foreach_example.R
For more information about Foreach and doMC, see the Foreach and doMC documentation.
R with Intel MKL
An example Intel MKL with R script, R_mkl.R:
its <- 2500
dim <- 1750
X <- matrix(rnorm(its*dim),its, dim)
system.time({C=matrix(0, dim, dim)for(i in 1:its)C = C + (X[i,] %o% X[i,])}) # single thread breakup calculation
system.time({C1 = t(X) %*% X}) # BLAS matrix mult (Try with single vs multithreads)
system.time({C2 = crossprod(X)}) # BLAS matrix mult (Try with single vs multithreads)
print(all.equal(C,C1,C2))
An example PBS script to run R_mkl.R:
#PBS -N R_MKL_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j or #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r export TMPDIR=$HOME export LC_ALL=C export MKL_NUM_THREADS=8 export MKL_DYNAMIC=FALSE cd $PBS_O_WORKDIR R CMD BATCH R_mkl.R
For more information about Intel MKL, see the MKL documentation.
R MKL Benchmark results on Nautilus
Pnmath
Pnmath uses the OpenMP parallel processing directives for implicit parallelism. Loading the pnmath package replaces the built-in math functions by the parallel versions. At load time a calibration is carried out to determine the parallel overhead. It implements parallelized versions of most of the non-RNG routines in the math library. It requires NO(very trivial) changes to serial code. It uses OMP_NUM_THREADS environment variable to set number of threads.
An example pnmath R script, pnmath_example.R:
library(pnmath) proc_num = getNumPnmathThreads() print(proc_num) v1 <- runif(1000) system.time(sqrt(v1)) system.time(exp(v1)) system.time(qtukey(v1,2,3))
An example PBS script to run pnmath_example.R:
#PBS -N pnmath_example #PBS -S /bin/bash #PBS -A your-account-number #PBS -j or #PBS -m abe #PBS -M your-emailid #PBS -l ncpus=8,mem=32GB,walltime=1:00:00 #PBS -q analysis module load r export TMPDIR=$HOME export LC_ALL=C export OMP_NUM_THREADS=8 export KMP_AFFINITY=disabled cd $PBS_O_WORKDIR R CMD BATCH pnmath_example.R
Pnmath Benchmark results on Nautilus
Qtukey(The Studentized Range Distribution) Benchmark results on Nautilus
rbenchmark results on Nautilus
Matrix mul, PCA, CF, LDA, SVD results on Nautilus
R Programming Language Workshop 2012
Support
This package has the following support level : Supported
Available Versions
| Version | Available Builds | |||||||
|---|---|---|---|---|---|---|---|---|
| intel | pgi | gnu | Other | |||||
| 2.15.1 |
|
|||||||
| 2.15.0 |
|
|||||||
| 2.14.2 |
|
|||||||
| 2.13.0 |
|
|||||||
| 2.12.0 |
|
|||||||