florianhartig / BayesianTools

General-Purpose MCMC and SMC Samplers and Tools for Bayesian Statistics
https://cran.r-project.org/web/packages/BayesianTools/index.html
115 stars 29 forks source link

Resuming parallel sampling from a new R session fails #199

Open ashiklom opened 4 years ago

ashiklom commented 4 years ago

Trying to restart a sampler with parallel likelihood evaluation in a new session fails with invalid connection error. I think this is because the cluster object (cl) stored with the setup$likelihood function is no longer valid in a new R session.

Pseudocode to reproduce:

setup <- createBayesianSetup(likelihood, prior, parallel = 2)
samples <- runMCMC(setup)
saveRDS(samples, "testfile.rds")
quit()  # leave R
# Restart R
samples <- readRDS(samples)
samples <- runMCMC(samples)
# Should fail with an error

Currently, I'm working around this by re-creating the BayesianSetup object in the fresh R session and modifying the samples object's likelihood and posterior:

newsetup <- createBayesianSetup(likelihood, prior, parallel = 2)
samples <- readRDS(samples)
samples$setup$likelihood <- newsetup$likelihood
samples$setup$posterior <- newsetup$posterior

This seems to work fine, but is a bit clunky. One possible fix is to pass around the cl variable as an argument in those functions (instead of storing it with the function definition as a closure, which is my understanding of how the code works now).

Related to #186.

florianhartig commented 4 years ago

Hi Alexey,

thanks for reporting that. I urgently need to do a major cleanup for BT, just difficult to find the time at the moment. Will try to fix this very soon!

Best, Florian

woodwards commented 4 years ago

I had restart working in parallel I think. My code is here.

https://github.com/woodwards/basgra_nz/blob/master/scripts/BC_BASGRA_BT.R