Problems linking to tbb with latest StanHeaders

Hi all,

I am developing an R package using Stan called rater, which can be found here. While I was developing it today I started to get failures on my CI builds (I am currently using Travis). I tracked this back to recent update to StanHeaders on CRAN. (2.21.0-3).

Originally I got errors of the form:

tbb/task_scheduler_observer.h: No such file or directory
 #include <tbb/task_scheduler_observer.h>
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

but I was able to fix these by updating my MakeVars with rstantools 2.1.0 and adding RcppParallel to both Imports and the description.

However I now get the following error:

/usr/bin/ld: cannot find -ltbb
/usr/bin/ld: cannot find -ltbbmalloc
collect2: error: ld returned 1 exit status

(The full log including the linker invocation which causes the error can be found here

I had a read of what the different linker flags could mean, but honestly I’m well over my head at this point.

I also tried to build to the package locally on my Macbook Air running High Sierra 10.13.6.

Any help or suggestions would be greatly appreciated! Thanks for Stan - it’s an amazing tool.

What is

dir(system.file("lib", package = "RcppParallel"))

?

same problem here. The files are libtbb and libtbbmalloc and not ltbb and ltbbmalloc . edit: under ubuntu with rcppParallel 5.0.1

I also can’t install RcppParallel from source on windows, it gives similar errors… I filed an issue here: https://github.com/RcppCore/RcppParallel/issues/118

Did u follow this:

http://rcppcore.github.io/RcppParallel/#r_packages

?

seems to be outdated… RcppParallelLibs is not available for R4.0

edit: this was just me being confused!

What is outdated? And where do you got that RcppParallel is not available for R 4.0? The package is available in binary form from CRAN for r-release which is R 4.0?!

RcppParallelLibs

I am not sure what RcppParallelLibs is… but you need RcppParallel.

the guide you linked to also has a requirement for RcppParallelLibs though, hence my comment…

I see. The importFrom statement is maybe wrong, but the rest of the instructions were correct - and this is the page which is linked to from the correct RcppParallel README on github.

It‘s about the right compile flags and about loading the shared library at package load.

(This stuff should probably go into rstantools to make this easier for r package devs)

my bad I read it too quickly and got stuck on my first interpretation, sorry… but it doesn’t seem to help anyway, or at least, the calls to the libraries still fail.

As far as I can see updating the makevars like this ‘seems’ to work. No testing with parallel functionality, and I still get segfaults when trying to load package under ubuntu…

windows:

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")
PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DRCPP_PARALLEL_USE_TBB=1
PKG_LIBS = $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::LdFlags()")
PKG_CXXFLAGS  += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::CxxFlags()")
CXX_STD = CXX14

other:

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")
PKG_CPPFLAGS = -I"../inst/include" -I"$(STANHEADERS_SRC)" -DBOOST_DISABLE_ASSERTS -DEIGEN_NO_DEBUG -DBOOST_MATH_OVERFLOW_ERROR_POLICY=errno_on_error
PKG_LIBS = $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::RcppParallelLibs()")
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::LdFlags()")
PKG_CXXFLAGS  += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::CxxFlags()")
CXX_STD = CXX14

do u load the shared tbb lib when your package is loaded? This should happen when RcppParallel is loaded when you load yours.

On my mac:

> dir(system.file("lib", package = "RcppParallel"))
[1] "libtbb.dylib"             "libtbbmalloc_proxy.dylib" "libtbbmalloc.dylib"      

I’ll try to check on travis

Okay, I think I may have figured out the problem or at least part of the problem.

The issue is coming from the linker invocation, which uses the

PKG_LIBS = `"${R_HOME}/bin$(R_ARCH_BIN)/Rscript" -e "RcppParallel::RcppParallelLibs()"` `"${R_HOME}/bin$(R_ARCH_BIN)/Rscript" -e "StanHeaders:::LdFlags()"`

from the Makevars

I suspected the problem would be in the call to

StanHeaders:::LdFlags()

because that actually produces the -ltbb flags etc.

(The source for that function can be found here)

On my Macbook Air this produces:

-L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/' -Wl,-rpath /Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/ -ltbb -ltbbmalloc

Note in particular the section

-Wl,-rpath /a/long/path/etc

I was trying to work out what all these flags meant when I found this stack overflow question

which suggested that instead of the above snippet we should be using:

-Wl,rpath,/a/long/path/etc

or possibly some other alternatives. To see if this would make difference, I forked rstantools made the necessary changes (only for my laptop mind you) and it now builds successfully on my laptop!

Have I missed something or could this be a solution?

Very happy to provide more info.

Does it also work with the StanHeaders on CRAN if you (temporarily) put RcppParallel in the Depends field of your DESCRIPTION file?

No, I get the same error. Namely:

ld: warning: directory not found for option '-L'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/RcppParallel/lib/''
ld: library not found for -ltbb
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [rater.so] Error 1
ERROR: compilation failed for package ‘rater’

OK. Can you try with

source("https://raw.githubusercontent.com/stan-dev/rstan/develop/StanHeaders/install-github.R")
install_StanHeaders(rstan_branch = "develop", 
                    math_branch = "StanHeaders_2.21", 
                    library_branch = "StanHeaders_2.21")

Okay, I’m trying that now!