我目前正在使用 emcee 包通过教程https://emcee.readthedocs.io/en/stable/tutorials/parallel/获取 MCMC 示例。代码的串行版本可以工作,但速度很慢,所以我想使用并行化技术来加速这个过程。
我的代码目前如下所示(提供伪代码):
def loglike(theta):
cosmo = blackbox_function_from_another_package(theta)
model = calculate_model(cosmo)
diff = data - model
return -0.5 * (diff).T @ ivar @ diff
def mcmc_parallel(loglikelihood, init_pos, nsamples):
nwalkers = init_pos.shape[0]
ndim = init_pos.shape[1]
with Pool() as pool:
sampler = emcee.EnsembleSampler(
nwalkers, ndim, loglikelihood, pool=pool)
sampler.run_mcmc(p0, nsamples, progress=True);
#Main code
from multiprocessing import Pool
sampler = mcmc_parallel(loglikelihood=loglike, init_pos=p0, nsamples=5)
data
并且ivar
是用于酸洗目的的全局变量,如教程中所述。每当我尝试运行此代码时,它都会无限期地执行,当我中断执行时,我会收到以下回调:
294 try: # restore state no matter what (e.g., KeyboardInterrupt)
295 if timeout is None:
--> 296 waiter.acquire()
297 gotit = True
298 else:
我不完全确定发生了什么以及代码冻结的原因。如果有人可以帮助我,我将不胜感激。