pbs-assess / sdmTMB

:earth_americas: An R package for spatial and spatiotemporal GLMMs with TMB
https://pbs-assess.github.io/sdmTMB/
187 stars 27 forks source link

Error in getUserDLL() : Multiple TMB models loaded. Failed to guess DLL name. #98

Closed gfmg closed 2 years ago

gfmg commented 2 years ago

Hi,

After updating R to R 4.2, I am getting the following error when trying to run two different sdmTMB movels in the same session:

Error in getUserDLL() : Multiple TMB models loaded. Failed to guess DLL name.

This same error also appear when trying to simulate residuals as: s_gamma <- simulate(m, nsim = 500) pred_fixed <- m$family$linkinv(predict(m)$est_non_rf)

I have tried to look for the potential explanation but with no luck!

Any help will be greatly appreciated

G

seananderson commented 2 years ago

Thanks for posting the issue. I just confirmed the same thing. It was working, so I wonder if this is from TMB 1.9.0, which was just released.

seananderson commented 2 years ago

Indeed, that is the issue. TMB::openmp() now requires a DLL argument as of TMB 1.9.0. I'll push a fix shortly.

seananderson commented 2 years ago

OK, it should be fixed in the main and dev branches. For now I just removed the openmp() calls, so parallel processing won't work, but I was finding minimal improvement anyways with the new TMBad auto-diff library.

I think you can set it anyways with:

TMB::openmp(n = 3, DLL = "sdmTMB")

but be careful because it's not always faster with more cores.

If you're working with simulated residuals, note there's also a type of residual now where the fixed effects are at their MLEs and the random effects are sampled with Stan/MCMC.

r <- residuals(fit, type = "mle-mcmc")

Setting mcmc_iter and mcmc_warmup as desired. These avoid the Laplace approximation error, which can dominate residuals sometimes. See ?residuals.sdmTMB.

gfmg commented 2 years ago

Many thanks Sean to fix the bug, and also for the extra information on residuals.

G

sschooler commented 1 year ago

Hello, I'm having trouble implementing parallel processing in sdmTMB. The documentation isn't clear as to how to implement the TMB::openmp function. E.g. should this function be run before the sdmTMB or be put inside the "parallel" argument in the sdmTMB run, or is there currently no way to run sdmTMB in parallel? Thanks!

seananderson commented 1 year ago

It should be run sometime before the sdmTMB() call. I've been seeing minimal benefit recently and I'm not sure if that's because of how I've set up the .cpp file or if it's a function of the model configuration and the newer TMBad library.

library(sdmTMB)

mesh <- make_mesh(pcod, c("X", "Y"), cutoff = 10)

tictoc::tic()
fit <- sdmTMB(
  density ~ s(depth),
  data = pcod, mesh = mesh,
  time = "year",
  family = tweedie(link = "log")
)
tictoc::toc()
#> 7.333 sec elapsed

TMB::openmp(n = 2, DLL = "sdmTMB")
#> sdmTMB 
#>      2 
#> attr(,"autopar")
#> [1] FALSE
tictoc::tic()
fit <- sdmTMB(
  density ~ s(depth),
  data = pcod, mesh = mesh,
  time = "year",
  family = tweedie(link = "log")
)
tictoc::toc()
#> 6.796 sec elapsed

Created on 2023-05-09 with reprex v2.0.2