stan-dev / stan-dev/rstantools
rstan_package_skeleton: multiple translation units
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 48
- Forks
- 24
- Avg merge
- 1h 17m
- Merged PRs (30d)
- 1
Description
I am using rstantools::rstan_package_skeleton to implement a dozen stan models in an R package. While tools/make_cpp.R creates multiple .cc files in src, the cleanup file has the lines
cat src/*.cc > src/Modules.cpp
rm src/*.cc
effectively leading to one translation unit. This means when I modify any of the stan models they all need to be recompiled, which makes development burdensome.
Unfortunately, removing the lines above (i.e., multiple translation units) leads to "duplicate symbols" linker errors. To my understanding, this is because stan declares + defines functions inside namespaces inside header files only, for which header guarding + inline keyword does not seem to prevent symbol duplication.
I'd like to fix this issue and can think of two ways of going about it:
- Modify the
stanlibrary so as to somehow nest thestannamespace within each model. I'm reluctant to do this, and have tried many many ways around this, including:- creating separate
src/include/models.hppfiles for each model. Didn't work, asstan/model/model.hppgoes into each.owhich all get linked into one.so. - something like
I.e., I'm trying to nest thenamespace myModel { #include "src/include/models.hpp" // ... }stannamespace into the namespace for each model. Unfortunately this doesn't work either because within thestannamespace there areusingdeclarations which no longer refer to the right things when you nest them.
- creating separate
- A different approach is to build the
stanmodels when the package is installed, and then save them as.RDatafiles in thedatafolder. Then later when the installed package gets loaded, the models aren't rebuilt.
Perhaps I should point out that I implemented approach 2 long before rstantools was released. It worked perfectly until I had to install on a multi-architecture Windows platform. There the models only got built on one platform, and of course R crashed when I tried to load them from the other.
I thought about this some more and I believe the problem can be resolved by building architecture-dependent .RData files at install time, and sealing the objects into the package namespace at load time. However, I don't want to veer off from the stan-supported way of doing things, so I'm asking for advice/feedback on how to proceed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tools/make_cpp.R and the cleanup file, focusing on how src/*.cc is combined into src/Modules.cpp and removed. Reproduce the duplicate-symbol linker errors with separate translation units, then verify that the eventual approach supports architecture-dependent package installation without rebuilding every model for unrelated changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, r
- Domain
- build-system, developer-experience
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100