我需要执行只能由主进程执行的操作。奴隶们无法做任何事情,只能等待主人完成。因此,我做了以下(伪代码,我包装了大多数例程,所以我很难想出实际的MPI代码。我希望这些评论在解释我在做什么时已经足够清楚了)
def routine():
if not isMaster():
# I am a slave. I just sit here, waiting for the master to finish.
# wait for a string from the master explaining the state
string = MPI_Bcast("whatever", 0)
return (string == "SUCCESS")
<master does its long running business>
string = MPI_Bcast("SUCCESS", 0) # tell the slaves it worked fine.
return True
这是否有效,还是我滥用广播?
答案 0 :(得分:2)
如果你制作那个伪代码:
def routine():
if not isMaster():
# I am a slave. I just sit here, waiting for the master to finish.
# wait for a string from the master explaining the state
string = MPI_Bcast("whatever", 0)
return (string == "SUCCESS")
else:
<master does its long running business>
string = MPI_Bcast("SUCCESS", 0) # tell the slaves it worked fine.
return True
然后你应该没事。 (当然,您需要确保为您的语言提供特定的MPI API。)通常,对于广播或任何集体操作,通信器中的每个等级都需要执行相同数量的呼叫。为了理解,以一种无条件地在同一源代码行上为每个等级执行Bcast的方式重构程序可能是有益的,例如。
def routine():
status_code = UNKNOWN
if isMaster():
#do stuff
status_code = OK
MPI_Bcast(&status_code, 0)
#check status_code on each rank