我可以使用MPI\U Bcast进行同步吗?

我可以使用MPI\U Bcast进行同步吗?,mpi,Mpi,我需要执行一个只能由主进程执行的操作。奴隶们除了等待主人完成外,什么也做不了。因此,我做了以下工作(伪代码,我包装了大多数例程,因此我很难想出实际的MPI代码。我希望注释在解释我所做的事情时足够清楚) def例程(): 如果不是isMaster(): #我是奴隶。我只是坐在这里,等着主人讲完。 #等待来自主机的解释状态的字符串 string=MPI_Bcast(“随便什么”,0) 返回(字符串==“成功”) string=MPI_Bcast(“SUCCESS”,0)#告诉奴隶它工作正常。 返回真

我需要执行一个只能由主进程执行的操作。奴隶们除了等待主人完成外,什么也做不了。因此,我做了以下工作(伪代码,我包装了大多数例程,因此我很难想出实际的MPI代码。我希望注释在解释我所做的事情时足够清楚)

def例程():
如果不是isMaster():
#我是奴隶。我只是坐在这里,等着主人讲完。
#等待来自主机的解释状态的字符串
string=MPI_Bcast(“随便什么”,0)
返回(字符串==“成功”)
string=MPI_Bcast(“SUCCESS”,0)#告诉奴隶它工作正常。
返回真值

这行得通吗,还是我误用了广播?

如果您使用了伪代码:

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\u Bcast执行相同的调用并不重要。他们可以是两个不同的电话,但这是一个很好的做法,这是相同的。我理解正确吗?因为OP中非主代码的末尾有一个
return
语句,所以我不认为添加
else:
有什么意义。重构版本确实更好。只需确保
MPI\u Bcast
中的根秩与
isMaster()
谓词中使用的根秩匹配即可。
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
def routine():
    status_code = UNKNOWN
    if isMaster(): 
        #do stuff
        status_code = OK

    MPI_Bcast(&status_code, 0)
    #check status_code on each rank