Newer
Older
# R
## Introduction
R is a language and environment for statistical computing and graphics. R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, etc.) and graphical techniques, and is highly extensible.
One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.
Another convenience is the ease with which the C code or third party libraries may be integrated within R.
Extensive support for parallel computing is available within R.
Read more on [http://www.r-project.org/][a] and [http://cran.r-project.org/doc/manuals/r-release/R-lang.html][b].
## Modules
R version 3.1.1 is available on the cluster, along with GUI interface RStudio
| Application | Version | module |
| ----------- | ----------------- | ------------------- |
| **R** | R 3.1.1 | R/3.1.1-intel-2015b |
```console
$ ml R
```
## Execution
R on cluster is linked to a highly optimized MKL mathematical library. This provides threaded parallelization to many R kernels, notably the linear algebra subroutines. R runs these heavy calculation kernels without any penalty. You may control the threads by setting the `OMP_NUM_THREADS` environment variable.
### Interactive Execution
To run R interactively, using RStudio GUI, log in with the `ssh -X` parameter for X11 forwarding. Run RStudio:
```console
$ ml RStudio
$ rstudio
```
### Batch Execution
To run R in batch mode, write an R script, then write a bash jobscript and execute via the `sbatch` command. By default, R will use 24 threads on Salomon when running MKL kernels.
Example jobscript:
```bash
#!/bin/bash
# change to local scratch directory
mkdir -p "$DIR"
cd "$DIR" || exit
# load R module
ml R
# execute the calculation
R CMD BATCH rscript.R routput.out
# copy output file to home
The inputs are in the `rscript.R` file, the outputs in the `routput.out` file.
See the single node jobscript example in the [Job execution section][1].
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
## Parallel R
Parallel execution of R may be achieved in many ways. One approach is the implied parallelization due to linked libraries or specially enabled functions, as [described above][2]. In the following sections, we focus on explicit parallelization, where parallel constructs are directly stated within the R script.
## Package Parallel
The package parallel provides support for parallel computation, including by forking (taken from package multicore), by sockets (taken from package snow) and random-number generation.
The package is activated this way:
```console
$ R
> library(parallel)
```
More information and examples may be obtained directly by reading the documentation available in R:
```r
> ?parallel
> library(help = "parallel")
> vignette("parallel")
```
Forking is the most simple to use. Forking family of functions provide parallelized, drop-in replacement for the serial `apply()` family of functions.
!!! warning
Forking via package parallel provides functionality similar to OpenMP construct omp parallel for
Only cores of single node can be utilized this way!
Forking example:
```r
library(parallel)
#integrand function
f <- function(i,h) {
x <- h*(i-0.5)
return (4/(1 + x*x))
}
#initialize
size <- detectCores()
while (TRUE)
{
#read number of intervals
cat("Enter the number of intervals: (0 quits) ")
fp<-file("stdin"); n<-scan(fp,nmax=1); close(fp)
if(n<=0) break
#run the calculation
n <- max(n,size)
h <- 1.0/n
i <- seq(1,n);
pi3 <- h*sum(simplify2array(mclapply(i,f,h,mc.cores=size)));
#print results
cat(sprintf("Value of PI %16.14f, diff= %16.14fn",pi3,pi3-pi))
}
```
The above example is the classic parallel example for calculating the number π. Note the `detectCores()` and `mclapply()` functions. Execute the example as:
```console
$ R --slave --no-save --no-restore -f pi3p.R
```
Every evaluation of the integrad function runs in parallel on different process.
## Package Rmpi
The Rmpi package provides an interface (wrapper) to MPI APIs.
It also provides interactive R slave environment. On the cluster, Rmpi provides interface to the [OpenMPI][3].
Read more on Rmpi [here][c], reference manual is available [here][d].
When using the Rmpi package, both the `openmpi` and `R` modules must be loaded:
```console
$ ml OpenMPI
$ ml R
```
Rmpi may be used in three basic ways. The static approach is identical to executing any other MPI program. In addition, there is the Rslaves dynamic MPI approach and the mpi.apply approach. In the following section, we will use the number π integration example, to illustrate all these concepts.
### Static Rmpi
Static Rmpi programs are executed via `mpiexec`, as any other MPI programs. The number of processes is static - given at the launch time.
Static Rmpi example:
```r
library(Rmpi)
#integrand function
f <- function(i,h) {
x <- h*(i-0.5)
return (4/(1 + x*x))
}
#initialize
invisible(mpi.comm.dup(0,1))
rank <- mpi.comm.rank()
size <- mpi.comm.size()
n<-0
while (TRUE)
{
#read number of intervals
if (rank==0) {
cat("Enter the number of intervals: (0 quits) ")
fp<-file("stdin"); n<-scan(fp,nmax=1); close(fp)
}
#broadcat the intervals
n <- mpi.bcast(as.integer(n),type=1)
if(n<=0) break
#run the calculation
n <- max(n,size)
h <- 1.0/n
i <- seq(rank+1,n,size);
mypi <- h*sum(sapply(i,f,h));
pi3 <- mpi.reduce(mypi)
#print results
if (rank==0) cat(sprintf("Value of PI %16.14f, diff= %16.14fn",pi3,pi3-pi))
}
mpi.quit()
```
The above is the static MPI example for calculating the number π. Note the `library(Rmpi)` and `mpi.comm.dup()` function calls. Execute the example as:
```console
$ mpirun R --slave --no-save --no-restore -f pi3.R
```
### Dynamic Rmpi
Dynamic Rmpi programs are executed by calling the R directly. The `OpenMPI` module must still be loaded. The R slave processes will be spawned by a function call within the Rmpi program.
Dynamic Rmpi example:
```r
#integrand function
f <- function(i,h) {
x <- h*(i-0.5)
return (4/(1 + x*x))
}
#the worker function
workerpi <- function()
{
#initialize
rank <- mpi.comm.rank()
size <- mpi.comm.size()
n<-0
while (TRUE)
{
#read number of intervals
if (rank==0) {
cat("Enter the number of intervals: (0 quits) ")
fp<-file("stdin"); n<-scan(fp,nmax=1); close(fp)
}
#broadcat the intervals
n <- mpi.bcast(as.integer(n),type=1)
if(n<=0) break
#run the calculation
n <- max(n,size)
h <- 1.0/n
i <- seq(rank+1,n,size);
mypi <- h*sum(sapply(i,f,h));
pi3 <- mpi.reduce(mypi)
#print results
if (rank==0) cat(sprintf("Value of PI %16.14f, diff= %16.14fn",pi3,pi3-pi))
}
}
#main
library(Rmpi)
cat("Enter the number of slaves: ")
fp<-file("stdin"); ns<-scan(fp,nmax=1); close(fp)
mpi.spawn.Rslaves(nslaves=ns)
mpi.bcast.Robj2slave(f)
mpi.bcast.Robj2slave(workerpi)
mpi.bcast.cmd(workerpi())
workerpi()
mpi.quit()
```
The above example is the dynamic MPI example for calculating the number π. Both master and slave processes carry out the calculation. Note the `mpi.spawn.Rslaves()`, `mpi.bcast.Robj2slave()`, **and the `mpi.bcast.cmd()`** function calls.
Execute the example as:
```console
$ mpirun -np 1 R --slave --no-save --no-restore -f pi3Rslaves.R
```
Note that this method uses `MPI_Comm_spawn` (Dynamic process feature of MPI-2) to start the slave processes - the master process needs to be launched with MPI. In general, Dynamic processes are not well supported among MPI implementations, some issues might arise. In addition, environment variables are not propagated to spawned processes, so they will not see paths from modules.
### mpi.apply Rmpi
`mpi.apply` is a specific way of executing Dynamic Rmpi programs.
`mpi.apply()` family of functions provide MPI parallelized, drop in replacement for the serial `apply()` family of functions.
Execution is identical to other dynamic Rmpi programs.
mpi.apply Rmpi example:
```r
#integrand function
f <- function(i,h) {
x <- h*(i-0.5)
return (4/(1 + x*x))
}
#the worker function
workerpi <- function(rank,size,n)
{
#run the calculation
n <- max(n,size)
h <- 1.0/n
i <- seq(rank,n,size);
mypi <- h*sum(sapply(i,f,h));
return(mypi)
}
#main
library(Rmpi)
cat("Enter the number of slaves: ")
fp<-file("stdin"); ns<-scan(fp,nmax=1); close(fp)
mpi.spawn.Rslaves(nslaves=ns)
mpi.bcast.Robj2slave(f)
mpi.bcast.Robj2slave(workerpi)
while (TRUE)
{
#read number of intervals
cat("Enter the number of intervals: (0 quits) ")
fp<-file("stdin"); n<-scan(fp,nmax=1); close(fp)
if(n<=0) break
#run workerpi
i=seq(1,2*ns)
pi3=sum(mpi.parSapply(i,workerpi,2*ns,n))
#print results
cat(sprintf("Value of PI %16.14f, diff= %16.14fn",pi3,pi3-pi))
}
mpi.quit()
```
The above is the mpi.apply MPI example for calculating the number π. Only the slave processes carry out the calculation. Note the `mpi.parSapply()`, function call. The package parallel example above may be trivially adapted (for much better performance) to this structure using the `mclapply()` in place of `mpi.parSapply()`.
Execute the example as:
```console
$ mpirun -np 1 R --slave --no-save --no-restore -f pi3parSapply.R
```
## Combining Parallel and Rmpi
Currently, the two packages cannot be combined for hybrid calculations.
## Parallel Execution
R parallel jobs are executed via the SLURM partition system exactly as any other parallel jobs. The user must create an appropriate jobscript and submit it via `sbatch`
An example jobscript for [static Rmpi][4] parallel R execution, running 1 process per core:
```bash
#!/bin/bash
#SBATCH -q qprod
#SBATCH -N Rjob
#SBATCH --nodes=100 --ntasks-per-node=24 --cpus-per-task=1
mkdir -p "$DIR"
cd "$DIR" || exit
ml R OpenMPI
mpirun -bycore -bind-to-core R --slave --no-save --no-restore -f rscript.R
#exit
exit
```
For more information about jobscripts and MPI execution, refer to the [Job submission][1] and general [MPI][5] sections.
[1]: ../../general/job-submission-and-execution.md
[2]: #interactive-execution
[4]: #static-rmpi
[5]: ../mpi/mpi.md
[a]: http://www.r-project.org/
[b]: http://cran.r-project.org/doc/manuals/r-release/R-lang.html
[c]: http://cran.r-project.org/web/packages/Rmpi/
[d]: http://cran.r-project.org/web/packages/Rmpi/Rmpi.pdf