我有一些启用 MPI 的 python MCMC 采样代码,可以触发对单独内核的并行可能性调用。因为它(必然 - 不要问)拒绝采样,所以我只需要一个 np 样本就可以成功开始下一次迭代,并且过去通过这种方法非常高兴地实现了 ~ np x 加速。
我已将此应用于一个新问题,其中可能性调用 f2py 包装的 fortran 子例程。在这种情况下,在每次迭代中,其他 np-1 进程等待最慢(有时非常慢)的结果返回,即使其中一个 np-1 已经可以接受。
所以我怀疑我需要将一条消息传递给所有非获胜(在速度方面)的进程以终止,以便下一次迭代可以开始,并且我需要明确执行此操作的最佳方法的一些细节,如下所示。
python代码是这样的。采样器是 PyMultiNEST。
from mpi4py import MPI
world=MPI.COMM_WORLD
def myloglike(parameters,data,noise):
modelDataRealisation,status=call_fortran_sub(parameters)
if status == 0: # Model generated OK
winner=world.rank # This is the rank of the current winner
# I want to pass a message to the other still-running processes
# identifying that a successful sample has come back
won=world.bcast(winner,root=winner)
# I tried receiving the message here but the fortran_sub doesn't know
# anything about this - need to go deeper - see below
# Calculate chisq value etc.
loglike = f(data,modelDataRealisation,noise)
return loglike
广播应该通过主进程吗?
现在,棘手的部分是如何在 F90 代码中接收终止信号。大概如果代码总是在监听(while循环?)它会减慢很多 - 但我无论如何应该使用类似的东西:
call MPI_RECV(winner,1,MPI_DOUBLE_PRECISION,MPI_ANY_SOURCE,MPI_ANY_TAG&
&,MPI_COMM_WORLD,0,0)
然后如何在收到消息后最好地终止该进程?
最后,我是否需要在 F 代码中做任何事情以使下一次迭代重新启动 OK/产生新进程?
谢谢!