[[PageOutline]] = Running R on Cypress = == R Modules == As of June 7, 2019 the following versions of R are available on Cypress as modules. * R/3.1.2(default) * R/3.2.4 * R/3.2.5-intel * R/3.3.1-intel * R/3.4.1-intel * R/3.5.2-intel == Running R Interactively == ==== For Workshop ==== If you use a temporary workshop account, do this. {{{ export MY_PARTITION=workshop export MY_QUEUE=workshop }}} Start an interactive session using idev {{{ [tulaneID@cypress1 ~]$ idev Requesting 1 node(s) task(s) to workshop queue of workshop partition 1 task(s)/node, 20 cpu(s)/task, 2 MIC device(s)/node Time: 0 (hr) 60 (min). Submitted batch job 1164332 JOBID=1164332 begin on cypress01-121 --> Creating interactive terminal session (login) on node cypress01-121. --> You have 0 (hr) 60 (min). --> Assigned Host List : /tmp/idev_nodes_file_tuhpc002 Last login: Wed Aug 21 15:56:37 2019 from cypress1.cm.cluster [tulaneID@cypress01-121 ~]$ }}} ==== For Workshop ==== If you use a temporary workshop account, in order to use only 2 cpu's per node and thus allow for sharing the few available nodes in the interactive partition among many users, do this. {{{ [tulaneID@cypress1 ~]$ idev -c 2 }}} Load the R module {{{ [tulaneID@cypress01-121 ~]$ module load R/3.2.4 [tulaneID@cypress01-121 ~]$ module list Currently Loaded Modulefiles: 1) slurm/14.03.0 3) bbcp/amd64_rhel60 2) idev 4) R/3.2.4 }}} Run R in the command line window {{{ [tulaneID@cypress01-121 ~]$R R version 3.2.4 (2016-03-10) -- "Very Secure Dishes" Copyright (C) 2016 The R Foundation for Statistical Computing Platform: x86_64-pc-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. Natural language support but running in an English locale 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. > }}} == R Package Dependency == In order to run any of the SLURM scripts below, we would submit the SLURM script via '''sbatch''' command, but in general we would not expect the code to run successfully the very first time with out additional setup. We can try running the script without any additional setup, but we can expect to get the error such as in the following R session. {{{#!r > library(doParallel) Error in library(doParallel) : there is no package called ‘doParallel’ }}} Thus we should first ensure that the required R package, in this case the R package '''doParallel''', is available and installed in your environment. For a range of options for installing R packages - depending on the desired level of reproducibility, see the section [#InstallingRPackages Installing R Packages on Cypress]. '''For Workshop''' : If you use a temporary workshop account, use [#RPackageAlternative1 Alternative 1] - responding to the R prompts as needed - for installing R packages such as in the following. Thus we need to install the R package '''doParallel'''. {{{#!r > install.packages("doParallel") ... (respond to prompts as needed) ... > library(doParallel) Loading required package: foreach Loading required package: iterators Loading required package: parallel > q() Save workspace image? [y/n/c]: n [tuhpc002@cypress01-121 R]$ exit }}} Thus, now that we have resolved our package dependency, we can expect future jobs requiring '''doParallel''' to run without errors. == Running a R script in Batch mode == Besides running R interactively in an idev session, you can also submit your R job to the batch nodes (compute nodes) on Cypress. Inside your SLURM script, include a command to load the desired R module. Then invoke the '''Rscript''' command on your R script. {{{#!bash #!/bin/bash #SBATCH --qos=normal # Quality of Service #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of tasks (MPI processes) #SBATCH --cpus-per-task=1 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript myRscript.R }}} '''For Workshop''' : If you use a temporary workshop account, modify the SLURM script like: {{{#!bash #!/bin/bash #SBATCH --partition=workshop # Partition #SBATCH --qos=workshop # Quality of Service ##SBATCH --qos=normal ### Quality of Service (like a queue in PBS) #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of tasks (MPI processes) #SBATCH --cpus-per-task=1 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript myRscript.R }}} == Running a Parallel R Job == Starting with version 2.14.0, R has offered direct support for parallel computation through the "parallel" package. We will present two examples of running a parallel job of BATCH mode. They differ in the ways in which they communicate the number of cores reserved by SLURM to R. Both are based on code found in [https://cran.r-project.org/web/packages/doParallel/vignettes/gettingstartedParallel.pdf "Getting Started with doParallel and foreach" by Steve Weston and Rich Calaway] and modified by [https://rcc.uchicago.edu/docs/software/environments/R/index.html The University of Chicago Resource Computing Center]. === Passing (SLURM) Environment Variables === In the first example, we will use the built in R function '''Sys.getenv( )''' to get the SLURM environment variable from the operating system. Edit the new file '''bootstrap.R''' to contain the following code. {{{#!r #Based on code from the UCRCC website library(doParallel) # use the environment variable SLURM_CPUS_PER_TASK to set the number of cores registerDoParallel(cores=(Sys.getenv("SLURM_CPUS_PER_TASK"))) # Bootstrapping iteration example x <- iris[which(iris[,5] != "setosa"), c(1,5)] iterations <- 10000# Number of iterations to run # Parallel version of code # Note the '%dopar%' instruction part <- system.time({ r <- foreach(icount(iterations), .combine=cbind) %dopar% { ind <- sample(100, 100, replace=TRUE) result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit)) coefficients(result1) } })[3] # Shows the number of Parallel Workers to be used getDoParWorkers() # Executes the functions part }}} The above script will obtain the number of tasks per node via the SLURM environment variable '''SLURM_CPUS_PER_TASK''' set in our SLURM script and will pass that value to the '''registerDoParallel( )''' function. To implement this we need only set the correct parameters in our SLURM script. Suppose we wanted to use 16 cores. Then the correct SLURM script would be as follows. {{{#!bash #!/bin/bash #SBATCH --qos=normal # Quality of Service #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of Tasks per Node #SBATCH --cpus-per-task=16 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript bootstrap.R }}} '''For Workshop''' : If you use a temporary workshop account, modify the SLURM script like: {{{#!bash #!/bin/bash #SBATCH --partition=workshop # Partition #SBATCH --qos=workshop # Quality of Service ##SBATCH --qos=normal ### Quality of Service (like a queue in PBS) #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of Tasks per Node #SBATCH --cpus-per-task=16 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript bootstrap.R }}} Edit the new file '''bootstrap.sh''' to contain the above SLURM script code and submit as shown below. Also, note that since we did not specify an output file in the SLURM script, the output will be written to slurm-.out. For example: {{{ [tulaneID@cypress2 R]$ sbatch bootstrap.sh Submitted batch job 774081 [tulaneID@cypress2 R]$ cat slurm-774081.out Loading required package: foreach Loading required package: iterators Loading required package: parallel [1] "16" elapsed 2.954 [tulaneID@cypress2 R]$ }}} === Passing Parameters === The disadvantage of the above approach is that it is system specific. If we move our code to a machine that uses PBS-Torque as it's manager (sphynx for example) we have to change our source code. An alternative method that results in a more portable code base uses command line arguments to pass the value of our environment variables into the script. Edit the new file '''bootstrapWargs.R''' to contain the following code. {{{#!r #Based on code from the UCRCC website library(doParallel) # Enable command line arguments args<-commandArgs(TRUE) # use the first command line argument to set the number of cores registerDoParallel(cores=(as.integer(args[1]))) # Bootstrapping iteration example x <- iris[which(iris[,5] != "setosa"), c(1,5)] iterations <- 10000# Number of iterations to run # Parallel version of code # Note the '%dopar%' instruction part <- system.time({ r <- foreach(icount(iterations), .combine=cbind) %dopar% { ind <- sample(100, 100, replace=TRUE) result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit)) coefficients(result1) } })[3] # Shows the number of Parallel Workers to be used getDoParWorkers() # Executes the functions part }}} Note the use of '''args<-commandArgs(TRUE)''' and of '''as.integer(args[1])'''. This allows us to pass in a value from the command line when we call the script and the number of cores will be set to that value. Using the same basic submission script as last time, we need only pass the value of the correct SLRUM environment variable to the script at runtime. {{{#!bash #!/bin/bash #SBATCH --qos=normal # Quality of Service #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of Tasks per Node #SBATCH --cpus-per-task=16 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript bootstrapWargs.R $SLURM_CPUS_PER_TASK }}} '''For Workshop''' : If you use a temporary workshop account, modify the SLURM script like: {{{#!bash #!/bin/bash #SBATCH --partition=workshop # Partition #SBATCH --qos=workshop # Quality of Service ##SBATCH --qos=normal ### Quality of Service (like a queue in PBS) #SBATCH --job-name=R # Job Name #SBATCH --time=00:01:00 # WallTime #SBATCH --nodes=1 # Number of Nodes #SBATCH --ntasks-per-node=1 # Number of Tasks per Node #SBATCH --cpus-per-task=16 # Number of threads per task (OMP threads) module load R/3.2.4 Rscript bootstrapWargs.R $SLURM_CPUS_PER_TASK }}} Edit the new file '''bootstrapWargs.sh''' to contain the above SLURM script code. Now submit as in the following. {{{ [tulaneID@cypress1 ~]$ sbatch bootstrapWargs.sh Submitted batch job 52481 [tulaneID@cypress1 ~]$ cat slurm-52481.out Loading required package: foreach Loading required package: iterators Loading required package: parallel [1] "16" elapsed 3.282 [tulaneID@cypress1 ~]$ }}} == [=#InstallingRPackages Installing R Packages on Cypress] == See [wiki:InstallingRPackages here.] [[cypress/Python|Next Section: Running Python on Cypress]]