Summary
I am unable to use the expose_functions
method of a CmdStanModel
, using the CmdStanR
interface on a Linux HPC cluster. This issue came up in a number of recent topics:
https://discourse.mc-stan.org/t/tbb-interface-new-flag-is-not-working/35664
https://discourse.mc-stan.org/t/segfault-when-using-brms-cmdstanr-compile-model-methods-true/33771
https://discourse.mc-stan.org/t/brms-on-linux-cluster-file-permissions-for-expose-functions/34242
The solution given in those topics was to add the flag USE_STAN_THREADS = TRUE
to the cmdstan make/local
file (and then rebuild cmdstan). I’ve done that, but the issue is still there.
To be clear, apart from this issue involving the expose_functions
method, everything else involving cmdstan seems to run perfectly fine. I can compile models, sample from them, inspect the results, etc.
Details
- Operating system: RHEL 8.8
- cmdstan version: 2.35.0
- R version: 4.1.2
- cmdstanr version: 0.8.1.9000
Example script and output:
$ salloc -t 60 -n 1 --mem=10gb -p dev_single
[redacted]
$ module purge
$ module load math/R/4.1.2
Loading module dependency 'compiler/intel/2021.4.0'.
Loading module dependency 'numlib/mkl/2021.4.0'.
$ module load lib/icu4c/74.1_intel2021.4.0
$ module load devel/tbb/2021.4.0
$ export TBB_LIB=$TBB_LIB_DIR
$ export TBB_INC=$TBB_INC_DIR
$ export TBB_INTERFACE_NEW='true'
$ R -q
library(cmdstanr)
This is cmdstanr version 0.8.1.9000
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- Use set_cmdstan_path() to set the path to CmdStan
- Use install_cmdstan() to install CmdStan
set_cmdstan_path("~/opt/cmdstan")
CmdStan path set to: /home/[redacted]/opt/cmdstan
check_cmdstan_toolchain()
The C++ toolchain required for CmdStan is setup properly!
mod <- cmdstan_model(stan_file = "test_expose.stan")
mod$check_syntax(pedantic = TRUE)
Stan program is syntactically correct
mod$print()
functions {
real a_plus_b(real a, real b) {
return a + b;
}
}
mod$expose_functions(global = TRUE)
This returns a huge amount of messages to the R console, ending with:
compilation aborted for fileb247e961c461.cpp (code 4)
make: *** [/opt/bwhpc/common/math/R/4.1.2-mkl-2021.4.0-intel-2021.4.0_O3_pragma_noopt_el8_javafix_c++11/lib64/R/etc/Makeconf:177: fileb247e961c461.o] Error 4
Error in Rcpp::sourceCpp(code = code, env = env, verbose = verbose, ...) :
Error 1 occurred building shared library.
More details
My Makevars
file looks like this (following instructions from the wiki of my HPC cluster):
$ cat ~/.R/Makevars
CXX14=icpc
CXX17=icpc
CXX14FLAGS=-03 -fPIC -std=c++14 -wd308 -axCORE-AVX512,CORE-AVX2,AVX -xSSE4.2 -fp-model strict -qopenmp
CXX17FLAGS=-03 -fPIC -std=c++17 -wd308 -axCORE-AVX512,CORE-AVX2,AVX -xSSE4.2 -fp-model strict -qopenmp
CXXFLAGS += -wd308
PKG_CXXFLAGS += -std=c++14 -wd308
My cmdstan make/local
file looks like this:
$ cat ~/opt/cmdstan/make/local
STAN_THREADS=true
STAN_HAS_CXX17=true
CPPFLAGS_SUNDIALS += -fPIC
Lastly, not sure if this is relevant, but I was told by the administrators of my HPC cluster to change one line in one of the make files for cmdstan, due to an “unsupported compiler option”. Specifically, in cmdstan/stan/lib/stan_math/make/compiler_flags
, I changed this line:
CXX_VERSION := $(shell $(CXX) -dumpfullversion -dumpversion 2>&1)
into the following (i.e., removing -dumpfullversion
):
CXX_VERSION := $(shell $(CXX) -dumpversion 2>&1)
My R session info looks like this:
Click to show my R session info
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux 8.8 (Ootpa)
Matrix products: default
BLAS/LAPACK: /pfs/data5/software_uc2/all/toolkit/Intel_OneAPI/mkl/2021.4.0/lib/intel64/libmkl_intel_lp64.so.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] cmdstanr_0.8.1.9000
loaded via a namespace (and not attached):
[1] ps_1.7.7 fansi_1.0.6 utf8_1.2.4
[4] distributional_0.4.0 R6_2.5.1 jsonlite_1.8.8
[7] lifecycle_1.0.4 backports_1.5.0 magrittr_2.0.3
[10] posterior_1.6.0 pillar_1.9.0 rlang_1.1.4
[13] cli_3.6.3 checkmate_2.3.2 vctrs_0.6.5
[16] generics_0.1.3 tools_4.1.2 glue_1.7.0
[19] processx_3.8.4 xfun_0.41 abind_1.4-5
[22] compiler_4.1.2 pkgconfig_2.0.3 knitr_1.45
[25] tibble_3.2.1 tensorA_0.36.2.1
Many thanks in advance for your help!