.so file size when building an R package that uses compiled Stan code

,

@bgoodri has prompted me to build a separate R package for my R functions that use rstan. I followed the instructions here: https://mc-stan.org/rstantools/articles/minimal-rstan-package.html except that I did not run compile_dll. I’m running Ubuntu 20.04 with rstantools 2.1.1. Everthing has worked well and I can run the new package. But upon installation of the package, the libs/packagename.so file is 52MB in size, even with only one Stan module being compiled. The entirety of rstanarm has an .so file that is only about 9MB with many Stan programs included.

I am using only defaults when I run rstan_create_package().

How do I get a much smaller .so file?

That usually means that it was compiled with debug symbols. If you add -g0 to your ~/.R/Makevars file, it should be fine. The rstanarm.so is only 18MB and it has several more compiled Stan programs.

Thanks Ben. So this is what I’m trying for !/.R/Makevars

CXX14FLAGS=-O3 -march=native -mtune=native -fPIC -g0
CXX14=g++

That is fine. R CMD check will emit a warning message on your machine about the -march=native -mtune=native flags, but the .tar.gz file that it creates will be fine on winbuilder / CRAN.

1 Like

Thanks much. Down to 2MB now. And thanks for putting me onto doing compilation of Stan code during package buiild. This will make it much easier on users while avoiding the serialization problem.

Yeah, everyone should do that, although not everyone does. It makes things much easier for Mac / Windows users.

1 Like

I hope that CRAN uses the appropriate compiler flags when they produce binary packages.

They do when they create binaries, although CRAN’s testing framework utilizes flags that include debug info so you may see the huge sizes under

https://cran.r-project.org/web/checks/check_results_rms.html

but that is not problematic for CRAN.

Sorry for reviving this post but I was wondering if you could tell me where “-g0” should go in my Makevars file (I’m assuming it is the makevar file in R/rstantools/include/sys)?

Below is a copy of the contents of my Markevars file.

STANHEADERS_SRC = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "message()" -e "cat(system.file('include', 'src', package = 'StanHeaders', mustWork = TRUE))" -e "message()" | grep "StanHeaders")

STANC_FLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "cat(ifelse(utils::packageVersion('rstan') >= 2.26, '-DUSE_STANC3',''))")
PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error $(STANC_FLAGS)
PKG_CXXFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::CxxFlags()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::CxxFlags()")
PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()") $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()")

CXX_STD = CXX14

Many Thanks,
Philip