我撰寫了一段代碼,嘗試模擬引數服務器,指定4個行程,0號和1號行程作為引數服務器,2號和3號行程作為作業行程。當我使用if指定0,1,2,3的時候,程式可以正常退出,但是當我使用if指定0和1號行程,else指定3號和4號行程的時候,程式會卡死。不知道什么原因,求大神指教。
以下是我的問題代碼:
import random
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if rank == 0:
print("Hello World000000000000000000000")
for dest in range(2,size,2):
data = comm.recv(source = dest, tag = dest)
if rank == 1:
print("Hello World111111111111111111111")
for dest in range(3,size,2):
data = comm.recv(source = dest, tag = dest)
else:
data = random.randint(1,10)
comm.send(data, dest = rank%2, tag = rank)
print("process {0} sends data {1} to {2}".format(rank, data,rank%2))

當我使用兩個if代替else的時候,程式可以正常退出:
import random
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if rank == 0:
print("Hello World000000000000000000000")
for dest in range(2,size,2):
data = comm.recv(source = dest, tag = dest)
if rank == 1:
print("Hello World111111111111111111111")
for dest in range(3,size,2):
data = comm.recv(source = dest, tag = dest)
if rank == 2:
data = random.randint(1,10)
comm.send(data, dest = rank%2, tag = rank)
print("process {0} sends data {1} to {2}".format(rank, data,rank%2))
if rank == 3:
data = random.randint(1,10)
comm.send(data, dest = rank%2, tag = rank)
print("process {0} sends data {1} to {2}".format(rank, data,rank%2))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/130366.html
標籤:其他技術專區
上一篇:求問nlp相關的網路資源
下一篇:求問
