2013-03-11 111 views
1

我需要執行只能由主進程執行的操作。奴隸們什麼都不能做,只能等着主人完成。所以我做了以下(僞代碼,我將多數程序,所以我想有一個很難拿出實際MPI代碼。我希望評論是在解釋我在做什麼很清楚)我可以使用MPI_Bcast進行同步嗎?

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

只要確保'MPI_Bcast'中的根級別匹配'isMaster()'謂詞中使用的級別。 – 2013-03-12 18:52:59

回答

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 
+0

好的。重點是,如果主控和從屬控制器不會將_the_same_call_命中到MPI_Bcast,則無關緊要。他們可以是兩個不同的電話,但最好是相同的。我理解正確嗎? – 2013-03-11 11:36:51

+0

是的,這正是我的觀點。 – Zulan 2013-03-11 14:18:48

+0

由於在OP中的非主代碼末尾有一個'return'語句,所以在添加'else:'時看不到這一點。重構版本確實更好。 – 2013-03-12 18:51:55

相關問題