Since the HPCC OS transition to Ubuntu in summer 2024, R modules have changed. This tutorial applies to R in general; however, HPCC-specific module commands need to be updated by the users at the moment. We will revamp R-related tutorials in the near future.
rstan
The example here follows that
in RStan Getting Started. To test rstan on the HPCC,
first load R 3.6.2:
library("rstan")options(mc.cores=parallel::detectCores())rstan_options(auto_write=TRUE)schools_dat<-list(J=8,y=c(28,8,-3,7,-1,1,18,12),sigma=c(15,10,16,11,9,11,10,18))fit<-stan(file='8schools.stan',data=schools_dat)print(fit)pairs(fit,pars=c("mu","tau","lp__"))la<-extract(fit,permuted=TRUE)# return a list of arraysmu<-la$mu
To run the model from the command line:
Rscript run.R
In addition to the results printed to the stdout, there is an R object
file named 8schools.rds generated. This is due to that we've set
auto_write to TRUE in run.R. More about the
auto_write option:
Logical, defaulting to the value of rstan_options("auto_write"),
indicating whether to write the object to the hard disk using saveRDS.
Although this argument is FALSE by default, we recommend calling
rstan_options("auto_write" = TRUE) in order to avoid unnecessary
recompilations. If file is supplied and its dirname is writable, then
the object will be written to that same directory, substituting a .rds
extension for the .stan extension. Otherwise, the object will be
written to the tempdir.
rjags
To use {rjags}, first load R and JAGS from a dev-node (e.g. dev-amd20) as follows:
Next, we will run a short example of data analysis using rjags. This
example comes from this tutorial which presents many Bayesian models
using this package.
To invoke R from the command line: R --vanilla
Then, in the R console, you can run the following codes (for detailed
explanation refer to the tutorial mentioned above):
model {
for ( i in 1:N ) {
P[i] <- 1/V[i]
y[i] ~ dnorm(d, P[i])
}
### Define the priors
d ~ dnorm(0, 0.00001)
### Transform the ln(OR) to OR
OR <- exp(d)
}
A screen shot of the entire run including the output figures is attached
here.
R2OpenBUGS
R package R2OpenBUGS is available on the HPCC.
OpenBUGS is a software package that performs Bayesian inference Using
Gibbs Sampling. The latest version of OpenBUGS on the HPCC is
3.2.3. R2OpenBUGS is an R package that allows users to run OpenBUGS from
R.
library(tensorflow)library(keras)library(tfdatasets)use_python("/mnt/home/user123/anaconda3/envs/tf_gpu_Feb2023/bin")use_condaenv("/mnt/home/user123/anaconda3/envs/tf_gpu_Feb2023")# simple testtf$config$list_physical_devices("GPU")# model training# loading the keras inbuilt mnist datasetdata<-dataset_mnist()#separating train and test filetrain_x<-data$train$xtrain_y<-data$train$ytest_x<-data$test$xtest_y<-data$test$yrm(data)# converting a 2D array into a 1D array for feeding into the MLP and normalising the matrixtrain_x<-array(train_x,dim=c(dim(train_x)[1],prod(dim(train_x)[-1])))/255test_x<-array(test_x,dim=c(dim(test_x)[1],prod(dim(test_x)[-1])))/255# converting the target variable to once hot encoded vectors using keras inbuilt functiontrain_y<-to_categorical(train_y,10)test_y<-to_categorical(test_y,10)# defining a keras sequential modelmodel<-keras_model_sequential()# defining the model with 1 input layer[784 neurons], 1 hidden layer[784 neurons] with dropout rate 0.4 and 1 output layer[10 neurons]# i.e number of digits from 0 to 9model%>%layer_dense(units=784,input_shape=784)%>%layer_dropout(rate=0.4)%>%layer_activation(activation='relu')%>%layer_dense(units=10)%>%layer_activation(activation='softmax')# compiling the defined model with metric = accuracy and optimiser as adam.model%>%compile(loss='categorical_crossentropy',optimizer='adam',metrics=c('accuracy'))# fitting the model on the training datasetmodel%>%fit(train_x,train_y,epochs=20,batch_size=128)# evaluating model on the cross validation datasetloss_and_metrics<-model%>%evaluate(test_x,test_y,batch_size=128)
R 3.5.1 with Intel MKL
Intel MKL can accelerate R's speed in linear algebra calculations (such
as cross-product, matrix decomposition, inverse computation, linear
regression and etc.) by providing BLAS with higher performance. On the HPCC, only 3.5.1 has been built by linking to Intel MKL.
You could double check the BLAS/LAPACK libraries linked by running
sessionInfo() in R.
Benchmarking
We have a simple R code, crossprod.R, for testing the computation
time. The code is below, where the function crossprod can run in a
multi-threaded mode implemented by OpenMP.
Now, open an interactive SLURM job session by requesting 4 cores:
Benchmarking OpenBLAS vs. MKL (multi-threads)
1 2 3 4 5 6 7 8 91011121314151617181920
salloc--time=2:00:00--cpus-per-task=4--mem=50G--constraint=intel18
# After you are allocated a compute node, do the following.# 1) Run R/OpenBLAS
modulepurge
moduleloadGCC/7.3.0-2.30OpenMPI/3.1.1R/3.5.1-X11-20180604
Rscript--vanillacrossprod.R&# Time output:# user system elapsed # 50.036 1.574 14.156# 2) Run R/MKL
modulepurge
moduleloadR-Core/3.5.1-intel-mkl
Rscript--vanillacrossprod.R&# Time output:# user system elapsed # 28.484 1.664 8.737
Above, the boost in speed is clear from using MKL as compared with
OpenBLAS.
Even if we use a single thread (by requesting one core), MKL still shows
some advantage.
Benchmarking OpenBLAS vs. MKL (single-thread)
1 2 3 4 5 6 7 8 91011121314151617181920
salloc--time=2:00:00--cpus-per-task=1--mem=50G--constraint=intel18
# After you are allocated a compute node, do the following.# 1) Run R/OpenBLAS
modulepurge
moduleloadGCC/7.3.0-2.30OpenMPI/3.1.1R/3.5.1-X11-20180604
Rscript--vanillacrossprod.R&# Time output:# user system elapsed # 47.763 0.598 49.287# 2) Run R/MKL
modulepurge
moduleloadR-Core/3.5.1-intel-mkl
Rscript--vanillacrossprod.R&# Time output:# user system elapsed # 25.846 0.641 27.006
Notes
When loading R, the OpenMP environment variable OMP_NUM_THREADS is left
unset. This means that when running R code directly on a dev-node, all
CPUs on that node will be used by the internal multithreading library
compiled into R. This is discouraged since the node will be
overloaded and your job may even fail. Therefore, please set
OMP_NUM_THREADS to a proper value before running the R code. For
example,
$ OMP_NUM_THREADS=4
$ Rscript --vanilla crossprod.R
On the other hand, when the code is run on a compute node allocated by
SLURM, you don’t need to set OMP_NUM_THREADS as R would automatically
detect CPUs available for use (which should have been requested in your
salloc command or sbatch script).