TuringLang / AbstractMCMC.jl

Abstract types and interfaces for Markov chain Monte Carlo methods
https://turinglang.org/AbstractMCMC.jl
MIT License
78 stars 18 forks source link

Use of thread-local variables in AbstractMCMC #128

Open astrozot opened 11 months ago

astrozot commented 11 months ago

I need to sample a distribution for which the calculation of the log-likelihood is particularly time consuming. To speed-up calculations, it is beneficial to pass to the likelihood mutable data that (1) partially contain pre-computed values and (2) can be modified by the log-likelihood depending on the specific set of parameters passed to each call.

It would be relatively easy to implement this on a sequential (non-parallel) execution of a sampling, since I could attach the mutable data to the object representing the distribution. Problems arise however for parallel processing: in this case I would need to have n copies of the mutable data, one for each task.

One way could be to store n-copies of the data and access them in each log-likelihood call using the threadid(), but as clarified here this is no longer correct.

Another possibility would be to use a lock, but this would cancel much of the benefits of going parallel, as the mutable data are used during all the loglikelihood computation.