1

我需要执行一个只能由主进程执行的操作。奴隶们真的什么都做不了,只能等主人说完。因此,我做了以下事情(伪代码,我包装了大多数例程,所以我很难想出实际的 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

这行得通,还是我滥用广播?

4

1 回答 1

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
于 2013-03-11T11:15:30.063 回答