Rstan_model failing on CRAN Windows Test Systems

I am the maintainer of a package relying on rstan (CRAN - Package gppm). Since a few months, my package does not pass the CRAN tests on Windows anymore, while I did not change it (CRAN Package Check Results for Package gppm).

After looking into this, it seems that rstan::stan_model is the culprit (I am aware that the recommendation is to not compile models at runtime, but for my package, there seems to be no other way). To demonstrate this, I created an empty package, which contains the following as the only test code.

  stancode <- 'data {real y_mean;} parameters {real y;} model {y ~ normal(y_mean,1);}'
  mod <- rstan::stan_model(model_code = stancode)

Testing this package on the CRAN windows server via

devtools::install_github("karchjd/test_rstan")
devtools::check_win_release() 
#faster alternative: rhub::check(platform="windows-x86_64-release")

leads to

 == Failed tests ================================================================
  -- Error (test-all.R:3:3): stan_code -------------------------------------------
  Error: D:/Compiler/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: file230582d7c1e6b.o:file230582d7c1e6b.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[__ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1b): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'D:/Compiler/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: file230582d7c1e6b.o:file230582d7c1e6b.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD0Ev[__ZN3tbb10interface623task_scheduler_observerD0Ev]+0x1b): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'D:/Compiler/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: file230582d7c1e6b.o:file230582d7c1e6b.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD0Ev[__ZN3tbb10interface623task_scheduler_observerD0Ev]+0x39): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'D:/Compiler/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: file230582d7c1e6b.o:file230582d7c1e6b.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[__ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1b): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'D:/Compiler/rtools40/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.3.0/../../../../i686-w64-mingw32/bin/ld.exe: file230582d7c1e6b.o:file230582d7c1e6b.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[__ZN3tbb10interface623task_scheduler_observerD1Ev]+0x39): more undefined references to `tbb::internal::task_scheduler_observer_v3::observe(bool)' followcollect2.exe: error: ld returned 1 exit status
  Backtrace:
      x
   1. \-rstan::stan_model(model_code = stancode) test-all.R:3:2
   2.   \-rstan:::cxxfunctionplus(...)
   3.     +-pkgbuild::with_build_tools(...)
   4.     | \-withr::with_path(rtools_path(), code)
   5.     |   \-base::force(code)
   6.     \-inline::cxxfunction(...)
   7.       \-inline:::compileCode(f, code, language = language, verbose = verbose)
  -- Error (test-all.R:3:3): stan_code -------------------------------------------
  Error: invalid connection
  Backtrace:
      x
   1. \-rstan::stan_model(model_code = stancode) test-all.R:3:2
   2.   \-rstan:::cxxfunctionplus(...)
   3.     \-base::sink(type = "output")
  
  [ FAIL 2 | WARN 0 | SKIP 0 | PASS 0 ]
  Error: Test failures

The issue seems to be related to Rstan on Windows - #177 by rok_cesnovar, but this has been resolved as far as I understand.

What I don’t understand is why rstan itself is not failing on CRAN. One explanation could be that rstan_model is not included in any test or example, which would surprise me. What confuses me further is that rstan_model works perfectly fine on my Windows 10 machine and a Windows Server 2008 virtual machine I created.

Any suggestions on how to proceed would be highly appreciated.

1 Like

Pinging @hsbadr, @bgoodri, @jeffreypullin

Same here - I am also a maintainer of an R package relying on RStan. Recently my package has been failing CRAN checks, with no precise messages indicating what has been going on. Running the stan_model functions directly on my machine (MacOS Big Sur) has also become increasingly unstable, with occasional RStudio shutdowns. Any help will be appreciated!

The CRAN errors reported for gppm fail with a “undefined reference to tbb” error. I don’t know what the current best practice to solve that is, but probably @rok_cesnovar or @wds15 can point you in the right direction.

Hey, sorry, not really an expert on rstan Windows issues with TBB :/ Maybe @hsbadr would know?

@karchjd In general, all package that depend on rstan should be linking to RcppParallel (for Intel TBB) and use rstantools, if possible. Any package that doesn’t will fail with the next release of StanHeaders or rstan.

So, can you try linking to RcppParallel and respecting its flags (RcppParallel::CxxFlags() and RcppParallel::LdFlags())? Here’s an example: Link to RcppParallel for stan math · OpenMx/OpenMx@8e0a967 · GitHub.

2 Likes

@hsbadr thanks for your help! I copied everything that seemed relevant from the rstantools::rstan_create_package() skeleton package. Specifically, the LinkingTo section of the DESCRIPTION file, as well as importFrom(RcppParallel,RcppParallelLibs) to the NAMESPACE file. So, I linked to RcppParalell

I failed to respect its flags and would like to request more help regarding this. As far as I understand, the flags are set via a Makevars file in the src/ folder. However, I don’t have an src/ folder, since my package does not contain C/C++ code. Adding an empty src/ folder with only a Makevars file leads to an error at installation.

The current state of the package is available at GitHub - karchjd/gppm at bugfix