Skip to content

Build model methods and exposed functions against RcppParallel's TBB when it is installed - #1286

Open
jgabry wants to merge 6 commits into
v1.0from
rcppparallel-tbb
Open

jgabry wants to merge 6 commits into
v1.0from
rcppparallel-tbb

Conversation

@jgabry

@jgabry jgabry commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Submission Checklist

  • Run unit tests
  • Declare copyright holder and agree to license (see below)

Summary

I had Claude help implement these changes. I have reviewed all code and doc.


CmdStan bundles TBB 2020.3 and RcppParallel ships oneTBB under the same library file name on every platform (libtbb.dylib, tbb.dll, libtbb.so.2). Once one copy is in the R process, code linked against the other fails to load, which prevents using e.g. rstan, rstanarm, brms in the same session as exposing functions or model methods via cmdstanr.

Both $init_model_methods() and $expose_functions() build through rcpp_source_stan(). When RcppParallel is installed with its oneTBB, that function now passes TBB_INC, TBB_LIB and TBB_INTERFACE_NEW=1 to CmdStan's make, so the flags it gets back point at RcppParallel's TBB instead of the bundled one. It also puts RcppParallel's lib directory on the PATH so the built object finds tbb.dll on Windows.

I verified it locally on macOS with identical log_prob and gradient values. CI will test on Linux and Windows too.

Closes #1270.

Copyright and Licensing

Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Jonah Gabry

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses:

CmdStan bundles TBB 2020.3 and RcppParallel ships oneTBB under the
same library file name on every platform. Once one copy is in the R
process, code linked against the other fails to load, so the model
methods failed after rstan or brms had loaded RcppParallel's copy, and
RcppParallel failed to load after the methods had brought in CmdStan's.

rcpp_source_stan() now asks make for its flags with TBB_INC, TBB_LIB
and TBB_INTERFACE_NEW pointing at RcppParallel's install whenever
RcppParallel is there with its oneTBB. Stan Math supports oneTBB
through TBB_INTERFACE_NEW and CmdStan's makefiles already handle the
substitution. RcppParallel goes in Suggests for the new test, which
also gets CI to install it on every platform.

Closes #1270.
@jgabry

jgabry commented Sep 23, 2026 •

Copy link
Copy Markdown
Member Author

@andrjohns Could you take a quick look at this since it touches your model methods and expose functions code? Previously we couldn't use rstan, rstanarm, etc. in the same session as compiling cmdstanr model methods or exposing functions. I ran the idea by @WardBrian and @SteveBronder but hopefully you can take a peek at the code changes (it's a small diff)

Make's print rule echoes through the shell, which drops quotes, so an
R library path with a space came back as two arguments and the build
failed. Quote the two paths after they come back, the way
get_cmdstan_flags() already quotes CmdStan's own. The roxygen for
rcpp_source_stan() now says the flags come from the selected
installation rather than the one that built the executable.

Part of #1270.
rcppparallel_tbb() returns NULL for such a build, and lapply() turned
that into an empty list that sent the mocked helper down the
RcppParallel branch without paths.

Part of #1270.
@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 1 line in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (v1.0@90db219). Learn more about missing BASE report.

Files with missing lines Patch % Lines
R/expose.R 96.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             v1.0    #1286   +/-   ##
=======================================
  Coverage        ?   93.11%           
=======================================
  Files           ?       19           
  Lines           ?     7337           
  Branches        ?        0           
=======================================
  Hits            ?     6832           
  Misses          ?      505           
  Partials        ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

In R CMD check's temporary library system.file() returns the RcppParallel
paths with backslashes. The flags make prints go through sh in the
Makevars, which strips the backslashes, so the compiler saw
-isystem C:UsersRUNNER~1... and every model-method build failed on the
Windows CI jobs. repair_path() replaces them with forward slashes.

Part of #1270.
stanc3 changed its error reports on 2026-09-23 (stanc3 PR #1724): the
"Semantic error in '<file>', line N" heading is gone and the location
is now a "<file>:N:M" frame under the message. The CmdStan 2.40.0
tarball ships a placeholder for the macOS stanc binary, so its makefile
downloads the nightly and every macOS CI job started seeing the new
format. The two tests that pin the location now match either format.

Part of #1270.
When TBB_LIB is set CmdStan's make adds -Wl,--disable-new-dtags on
every OS except macOS. The flag only exists for ELF binaries, so
Rtools' ld and lld both refuse it and every model-methods build on
Windows failed at link time. Passing LDFLAGS_TBB_DTAGS= on the make
command line leaves the flag out; CmdStan's own Windows builds never
set TBB_LIB, which is why nobody hit this before.

The test that builds against a library path with a space handed the
mocked rcppparallel_tbb() a backslash path from tempdir(), which make's
print rule mangled so the quoting in rcpp_source_stan() no longer
matched. The real function repairs its paths, so the mock now does too.

Part of #1270.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiled model methods and expose_functions fail to load when RcppParallel's TBB is already in the session

2 participants