Skip to content

CmdSTAN

A powerful source-to-source compiler which compiles STAN code to C++


CmdSTAN requires an environment module

In order to use CmdSTAN, you must first load the appropriate environment module:

module load gnu cmdstan

STAN is a very powerful statistical modeling package. CmdSTAN is one of several implementations available. This implementation contains the STAN source-to-source compiler stanc which compiles STAN code to C++. Also included are the necessary options, include paths and library paths to compile the resulting C++ code to an executable file.

Using CmdSTAN on the HPC#

After downloading the appropriate models, the stanc and all the required options are available in the pkgconf file. As an example, we will use the Bernoulli example which can be found at the CmdSTAN GitHub Site. To begin, we will need to download the STAN file and the data files. We can download these with wget as follows:

1
2
3
wget https://raw.githubusercontent.com/stan-dev/cmdstan/develop/examples/bernoulli/bernoulli.stan
wget https://raw.githubusercontent.com/stan-dev/cmdstan/develop/examples/bernoulli/bernoulli.data.json
wget https://raw.githubusercontent.com/stan-dev/cmdstan/develop/examples/bernoulli/bernoulli.data.R

Now we need to compile the bernoulli.stan STAN model code to C++ using the stanc source-to-source compiler. To do this, we just need to run the following code:

module load gnu cmdstan
stanc bernoulli.stan

Now we should have a new file called bernoulli.hpp in our working directory. You can check this by typing ls and looking through the output for the bernoulli.hpp file.

Now that we have the C++ code, we need to compile it with GCC. In order to do this, it is a two-step process. First we compile the output bernoulli.hpp C++ code to an object .o file. This can be done with g++ as follows:

g++ $(pkgconf --cflags cmdstan) $(pkgconf --libs cmdstan) $EXTRAS bernoulli.o bernoulli.hpp

Note that the $EXTRAS is an environment variable provided by the cmdstan module file. It contains the extra compiler options needed by g++ to correctly compile the C++ code to an object file.

Now that we have the object file, the last thing to do is link that object file together with the necessary static libraries and the standard STAN main.o object file (this is provided by STAN itself). This can be done with g++ as follows:

g++ $(pkgconf --cflags cmdstan) $(pkgconf --libs cmdstan) $ABSLIBS $CMDSTAN_MAIN bernoulli.o -o bernoulli

Here, the $ABSLIBS is an environment variable provided by the cmdstan module file, which provides all the paths to the static libraries needed by STAN to build a working executable file. Also, the $CMDSTAN_MAIN is another environment variable which provides the full path to the standard STAN main function's object file (main.o) which is needed by C++ to produce a working program.

Now, if you type ls, you should see an executable file called bernoulli. From here, you can run the code with the necessary options by typing:

./bernoulli [OPTIONS] [FILES]

Where [OPTIONS] are the options the program needs to run and [FILES] are the needed data files.