BwUniCluster2.0/Software/R/Rstan: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
<span style="color:red"><b>Note that the instructions provided below refer to R 4.2.1 (but not R 4.4.1)! We are currently updating our guides for R 4.4.1.</b></span>

= General information =
= General information =


rstan provides the R interface for [https://mc-stan.org/ Stan], a platform for statistical modeling and high-performance statistical computation.
<code>rstan</code> provides the R interface for [https://mc-stan.org/ Stan], a platform for statistical modeling and high-performance statistical computation.


To be compatible with our R installation, we recommend to install the development version of rstan.
To be compatible with our R installation, we recommend to install the development version of rstan.


= Installation =
= Installation instructions =


Please enter the following code, presented in the boxes below, directly in your shell/command line on '''bwUniCluster2.0'''.
Please enter the following code, presented in the boxes below, directly in your shell/command line on '''bwUniCluster2.0'''.


== Preparations ==
== Preparations ==
Installing <code>glmnet</code> involves compiling source code. Therefore, ensure that the following flags are set in <code>$HOME/.R/Makevars</code>:
Prepare .R directory (if it does not already exists). This is then filled with information how R should compile the packages ('compilation flags'). These are written (and can be reviewed) into the (text) file Makevars.

If you already have a .R/Makevars file, check whether these flags are already set.


<pre>
<pre>
cat ~/.R/Makevars
cat ~/.R/Makevars
</pre>


CXX14=g++
In this case, skip the following block of five commands. If this is not the case, enter the following commands in your shell (command line).
CXX17=g++
CXX14FLAGS += -std=c++14
CXX17FLAGS += -std=c++17
CXXFLAGS = -O3 -fPIC -march=cascadelake -ffp-contract=off -fno-fast-math -fno-signed-zeros -fopenmp -Wno-unknown-warning-option
</pre>


If necessary, the appropriate compiler flags can be set by running the following lines of code:
<pre>
<pre>
mkdir -p ~/.R
mkdir -p ~/.R
echo "CXX14=icpc" >> ~/.R/Makevars
echo "CXX14=g++" > ~/.R/Makevars
echo "CXX17=icpc" >> ~/.R/Makevars
echo "CXX17=g++" >> ~/.R/Makevars
echo "CXX14FLAGS=-O3 -fPIC -std=c++14 -wd308 -axCORE-AVX512,CORE-AVX2,AVX -xSSE4.2 -fp-model strict -qopenmp" >> ~/.R/Makevars
echo "CXXFLAGS = -O3 -fPIC -march=cascadelake -ffp-contract=off -fno-fast-math -fno-signed-zeros -fopenmp -Wno-unknown-warning-option" >> ~/.R/Makevars
echo "CXX17FLAGS=-O3 -fPIC -std=c++17 -wd308 -axCORE-AVX512,CORE-AVX2,AVX -xSSE4.2 -fp-model strict -qopenmp" >> ~/.R/Makevars
echo "CXX14FLAGS += -std=c++14" >> ~/.R/Makevars
echo "CXXFLAGS += -wd308" >> ~/.R/Makevars
echo "CXX17FLAGS += -std=c++17" >> ~/.R/Makevars
echo "PKG_CXXFLAGS += -std=c++14 -wd308" >> ~/.R/Makevars
</pre>
</pre>


Since installing <code>glmnet</code> involves compiling, we start an <b>interactive session</b> on one of the compute nodes:
The installation of rstan and its dependencies makes use other libraries (TBB, Threading Building Blocks) which need to be loaded before installation (and R, of course).

<pre>
<pre>
salloc -n 1 -t 30 -p dev_single
module purge
module load math/R/4.1.2
module load devel/tbb/2021.4.0
</pre>
</pre>



The installation process of rstan and its dependencies (Rcpp) needs input of how to use the TBB module. This is provided via specific environment variables. We also need to provide a further configuration information to install the R package v8 rstan depends on.
Within the interactive session, load the R 4.4.1 module and provide configuration information to install the R package <code>v8</code>, rstan depends on.


<pre>
<pre>
# Load the R software module:
export TBB_LIB=$TBB_LIB_DIR
module load math/R/4.4.1-mkl-2022.2.1-gnu-13.3
export TBB_INC=$TBB_INC_DIR

export TBB_INTERFACE_NEW='true'
# Provide configuration information for the v8 R package
export DOWNLOAD_STATIC_LIBV8=1
export DOWNLOAD_STATIC_LIBV8=1
</pre>
</pre>


== Installing the R package(s) [takes roughly 20 min] ==
== Installing the R package(s) [takes roughly 20 min] ==
Now, start <code>R</code> and install <code>RStan</code> using the lines of code below:
We can now install rstan (in its development version) and its dependencies within a R session

Consider starting an interactive job for compiling. You need at most 5000MB total memory, e.g. start

<pre>
salloc -t 30 -n 1 --mem=5000 -p dev_single
</pre>

Now, start R and install


<pre>
<pre>
Line 69: Line 60:
</pre>
</pre>


== Testing the installation ==
== Testing the installation ==


To check whether the installation worked, make a test run in R
To check whether the installation worked, make a test run in R
Line 76: Line 67:
> example(stan_model, package = "rstan", run.dontrun = TRUE)
> example(stan_model, package = "rstan", run.dontrun = TRUE)
</pre>
</pre>

= Preparations to use the rstan package =
To run its applications, rstan compiles new scripts. For this, it again uses TBB, so you need to set the corresponding environment variables whenever you use rstan - as well as to load the R and TBB modules.


For this, we recommend to add the modules

<pre>
module load math/R/4.1.2
module load devel/tbb/2021.4.0
</pre>


as well as the three export commands

<pre>
export TBB_LIB=$TBB_LIB_DIR
export TBB_INC=$TBB_INC_DIR
export TBB_INTERFACE_NEW='true'
</pre>

to your [[BwUniCluster_2.0_Slurm_common_Features#sbatch_Examples | batch job scripts]] that use rstan or to run them directly in the command line if you use an [[BwUniCluster_2.0_Batch_Queues | interactive session]].

Latest revision as of 18:08, 30 October 2024

General information

rstan provides the R interface for Stan, a platform for statistical modeling and high-performance statistical computation.

To be compatible with our R installation, we recommend to install the development version of rstan.

Installation instructions

Please enter the following code, presented in the boxes below, directly in your shell/command line on bwUniCluster2.0.

Preparations

Installing glmnet involves compiling source code. Therefore, ensure that the following flags are set in $HOME/.R/Makevars:

cat ~/.R/Makevars

CXX14=g++
CXX17=g++
CXX14FLAGS += -std=c++14
CXX17FLAGS += -std=c++17
CXXFLAGS = -O3 -fPIC -march=cascadelake -ffp-contract=off -fno-fast-math -fno-signed-zeros -fopenmp -Wno-unknown-warning-option

If necessary, the appropriate compiler flags can be set by running the following lines of code:

mkdir -p ~/.R
echo "CXX14=g++" > ~/.R/Makevars
echo "CXX17=g++" >> ~/.R/Makevars
echo "CXXFLAGS = -O3 -fPIC -march=cascadelake -ffp-contract=off -fno-fast-math -fno-signed-zeros -fopenmp -Wno-unknown-warning-option" >> ~/.R/Makevars
echo "CXX14FLAGS += -std=c++14" >> ~/.R/Makevars
echo "CXX17FLAGS += -std=c++17" >> ~/.R/Makevars

Since installing glmnet involves compiling, we start an interactive session on one of the compute nodes:

salloc -n 1 -t 30  -p dev_single


Within the interactive session, load the R 4.4.1 module and provide configuration information to install the R package v8, rstan depends on.

# Load the R software module:
module load math/R/4.4.1-mkl-2022.2.1-gnu-13.3

# Provide configuration information for the v8 R package
export DOWNLOAD_STATIC_LIBV8=1

Installing the R package(s) [takes roughly 20 min]

Now, start R and install RStan using the lines of code below:

R -q
> install.packages("remotes")
> install.packages("RcppParallel")
> install.packages("V8")
> remotes::install_github("hsbadr/rstan/StanHeaders@develop", force = TRUE)
> remotes::install_github("hsbadr/rstan/rstan/rstan@develop", force = TRUE)

Testing the installation

To check whether the installation worked, make a test run in R

> library(rstan)
> example(stan_model, package = "rstan", run.dontrun = TRUE)