stan-dev / rstan

RStan, the R interface to Stan
https://mc-stan.org
1.03k stars 264 forks source link

Building R package with integrate_ode_bdf() results in `Symbol not found: _CVode` #825

Open seananderson opened 4 years ago

seananderson commented 4 years ago

Summary:

If I try to build an R package that uses rstan and includes the BDF ODE solver (integrate_ode_bdf), I receive the error Symbol not found: _CVode.

Description:

Reproducible Steps:

Try to install this minimal package I put together using rstantools::rstan_create_package() and an example from the Stan manual:

remotes::install_github("seananderson/testBDF")

Current Output:

Error: package or namespace load failed for ‘testBDF’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so':
  dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so, 6): Symbol not found: _CVode
  Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so
Error: loading failed
Execution halted

I assume the important error is Symbol not found: _CVode

The above was on OS X as described below. On Windows R 3.6.2 I've been told the error is:

undefined reference to `CVodeFree' collect2.exe: error: ld returned 1 exit status

Expected Output:

The package to build and install. The package will build and install if integrate_ode_bdf is replaced with integrate_ode_rk45.

RStan Version:

rstan: 2.21.2 StanHeaders: 2.21.0.5 rstantools: 2.1.1 or 2.1.1.9000

R Version:

R version 4.0.2 (2020-06-22)

(Also tested on Windows R 3.6.2)

Operating System:

OS X 10.15.6

(Also tested on Windows 10 1909)

jgabry commented 4 years ago

I assume the important error is Symbol not found: _CVode

The above was on OS X as described below. On Windows R 3.6.2 I've been told the error is:

undefined reference to `CVodeFree' collect2.exe: error: ld returned 1 exit status

Sorry you're getting an error. Thanks for reporting this. @bgoodri Any idea what's going here?

bgoodri commented 4 years ago

I can replicate the installation error on Linux, but I don't know why it is occurring. Perhaps the shared library of StanHeaders is not built correctly, but after a quick look it seems to include the right headers for that CVodeUserData thing.

On Fri, Jul 31, 2020 at 1:24 PM Sean Anderson notifications@github.com wrote:

Summary:

If I try to build an R package that uses rstan and includes the BDF ODE solver (integrate_ode_bdf), I receive the error Symbol not found: _CVode. Description:

  • I am unable to build R packages that use the integrate_ode_bdf ODE solver using the set up as built by rstantools.
  • The integrate_ode_rk45 ODE solver works.
  • I can also build rstan models that use integrate_ode_bdf through rstan::stan_model or rstan::stan outside of an R package structure.
  • I have posted a minimal R package in a GitHub repo below as an example.
  • I'm not sure if this is an rstan or rstantools issue. If this should be an rstantools issue this could be moved there or this can be closed and I will repost.

Reproducible Steps:

Try to install this minimal package https://github.com/seananderson/testBDF I put together using rstantools::rstan_create_package() and an example from the Stan manual:

remotes::install_github("seananderson/testBDF")

Current Output:

Error: package or namespace load failed for ‘testBDF’ in dyn.load(file, DLLpath = DLLpath, ...):

unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so':

dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so, 6): Symbol not found: _CVode

Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so

Expected in: flat namespace

in /Library/Frameworks/R.framework/Versions/4.0/Resources/library/00LOCK-testBDF/00new/testBDF/libs/testBDF.so

Error: loading failed

Execution halted

I assume the important error is Symbol not found: _CVode

The above was on OS X as described below. On Windows R 3.6.2 I've been told the error is:

undefined reference to `CVodeFree' collect2.exe: error: ld returned 1 exit status

Expected Output:

The package to build and install. The package will build and install if integrate_ode_bdf is replaced with integrate_ode_rk45. RStan Version:

rstan: 2.21.2 StanHeaders: 2.21.0.5 rstantools: 2.1.1 or 2.1.1.9000 R Version:

R version 4.0.2 (2020-06-22)

(Also tested on Windows R 3.6.2) Operating System:

OS X 10.15.6

(Also tested on Windows 10 1909)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/825, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKTGK5DLO6DWMYWEHSLR6L46LANCNFSM4PQTGMZA .

bgoodri commented 4 years ago

OK, this is not as bad as I feared. Basically, rstantools does not do the right thing when the package wants to utilize CVODES. To fix it on Linux / Mac

  1. Open Makevars
  2. Delete the line that says "# Generated by rstantools. Do not edit by hand."
  3. At the end of the line that starts with PKG_LIBS, add the text -L"$(STANHEADERS_SRC)/../../lib/" -lStanHeaders

To fix it on Windows,

  1. Open Makevars.win
  2. Delete the line that says "# Generated by rstantools. Do not edit by hand."
  3. At the end of the line that starts with PKG_LIBS, add the text -L"$(STANHEADERS_SRC)/../../libs/" -lStanHeaders

Note that the added line refers to the lib subdirectory on Linux / Mac and the libs subdirectory on Windows.

jgabry commented 4 years ago

@bgoodri Thanks for figuring this out. Can this be fixed in rstantools to avoid people having to manually edit Makevars?

bgoodri commented 4 years ago

Yes

On Fri, Aug 7, 2020, 12:04 PM Jonah Gabry notifications@github.com wrote:

@bgoodri https://github.com/bgoodri Thanks for figuring this out. Can this be fixed in rstantools to avoid people having to manually edit Makevars?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/825#issuecomment-670589751, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKWFPX6JSEDUPMOFAOTR7QQXFANCNFSM4PQTGMZA .

jgabry commented 4 years ago

Ok, we might as well submit a new rstantools. If you fix it in rstantools let me know and I can submit a new version of rstantools to CRAN.

bgoodri commented 4 years ago

We need to somehow detect when the Stan programs are calling a function that utilizes CVODES. I guess grepl() on the Stan programs could work.

On Sat, Aug 8, 2020 at 3:57 PM Jonah Gabry notifications@github.com wrote:

Ok, we might as well submit a new rstantools. If you fix it in rstantools let me know and I can submit a new version of rstantools to CRAN.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/stan-dev/rstan/issues/825#issuecomment-670967865, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAZ2XKXHKM4KNTFRVU3MRO3R7WUZNANCNFSM4PQTGMZA .