我是MPI的新手,如果满足条件,我试图从其中一个进程广播。问题是当我尝试将其打印出来时,我在其他进程上看不到广播的消息。当进程通过第二个if
条件时,它会广播numpy
数组。在我的代码中,进程0,1,3,5,7传递第二个if条件。这是代码:
import numpy as np
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
rand_num=np.empty(5,dtype='float')
bands = [(0, 65), (66, 132), (133, 198), (199, 264), (265, 330), (331, 396), (397, 462), (463, 529)]
volts = {'map1':[478], 'map2':[346], 'map3':[213], 'map4':[81], 'map5':[34]}
for items in volts.keys():
if(len(volts[items])!=0):
neur_id = volts[items][0]
if neur_id in range(bands[comm.rank][0],bands[comm.rank][1]):
rand_num = np.array([comm.rank for i in range(0,5)],dtype='float')
comm.Bcast(rand_num, root=comm.rank)
comm.Barrier() #hoping to stop other processes from proceeding.
print 'data and rank',rand_num,comm.rank
我期待输出看起来像:
data and rank [ 0. 0. 0. 0. 0.] 0 data and rank [ 1. 1. 1. 1. 1.] 0 data and rank [ 3. 3. 3. 3. 3.] 0 data and rank [ 5. 5. 5. 5. 5.] 0 data and rank [ 7. 7. 7. 7. 7.] 0
data and rank [ 0. 0. 0. 0. 0.] 1 data and rank [ 1. 1. 1. 1. 1.] 1 data and rank [ 3. 3. 3. 3. 3.] 1 data and rank [ 5. 5. 5. 5. 5.] 1 data and rank [ 7. 7. 7. 7. 7.] 1
data and rank [ 0. 0. 0. 0. 0.] 2 data and rank [ 1. 1. 1. 1. 1.] 2 data and rank [ 3. 3. 3. 3. 3.] 2 data and rank [ 5. 5. 5. 5. 5.] 2 data and rank [ 7. 7. 7. 7. 7.] 2
data and rank [ 0. 0. 0. 0. 0.] 3 data and rank [ 1. 1. 1. 1. 1.] 3 data and rank [ 3. 3. 3. 3. 3.] 3 data and rank [ 5. 5. 5. 5. 5.] 3 data and rank [ 7. 7. 7. 7. 7.] 3
data and rank [ 0. 0. 0. 0. 0.] 4 data and rank [ 1. 1. 1. 1. 1.] 4 data and rank [ 3. 3. 3. 3. 3.] 4 data and rank [ 5. 5. 5. 5. 5.] 4 data and rank [ 7. 7. 7. 7. 7.] 4
data and rank [ 0. 0. 0. 0. 0.] 5 data and rank [ 1. 1. 1. 1. 1.] 5 data and rank [ 3. 3. 3. 3. 3.] 5 data and rank [ 5. 5. 5. 5. 5.] 5 data and rank [ 7. 7. 7. 7. 7.] 5
data and rank [ 0. 0. 0. 0. 0.] 6 data and rank [ 1. 1. 1. 1. 1.] 6 data and rank [ 3. 3. 3. 3. 3.] 6 data and rank [ 5. 5. 5. 5. 5.] 6 data and rank [ 7. 7. 7. 7. 7.] 6
data and rank [ 0. 0. 0. 0. 0.] 7 data and rank [ 1. 1. 1. 1. 1.] 7 data and rank [ 3. 3. 3. 3. 3.] 7 data and rank [ 5. 5. 5. 5. 5.] 7 data and rank [ 7. 7. 7. 7. 7.] 7
然而,输出看起来完全不同。我的代码中没有任何逻辑错误。
PS:我注意到这个问题与this重复。我的进程都没有陷入僵局。我的问题是关于同步。我还阅读here如果所有流程都没有调用Bcast
,则MPI不会同步Bcast,这就是我Barrier
之后立即使用Bcast
的原因。由于未在所有进程(排名)上调用bcast
,因此将其标记为重复。如果在我的申请中,MPI bcast
无法按要求使用,那么我应该使用哪个MPI命令进行"广播"?
因为这个问题被标记为重复,我自己无法回答。我发布了我的解决方案,我在for循环中使用了Send
和Recv
来解决我的问题:
import numpy as np
from mpi4py import MPI
from mpi4py.MPI import ANY_SOURCE
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
rand_num=np.empty(5,dtype='float')
bands = [(0, 65), (66, 132), (133, 198), (199, 264), (265, 330), (331, 396), (397, 462), (463, 529)]
volts = {'map1':[478], 'map2':[346], 'map3':[213], 'map4':[81], 'map5':[34]}
for items in volts.keys():
if(len(volts[items])!=0):
neur_id = volts[items][0]
if neur_id in range(bands[comm.rank][0],bands[comm.rank][1]):
rand_num = np.array([comm.rank for i in range(0,5)],dtype='float')
all_ranks = [i for i in range(0,size)]
all_ranks.remove(rank)
rest_ranks = all_ranks
for ranks in rest_ranks:
comm.Send(rand_num, dest= ranks)
else:
comm.Recv(rand_num, source = ANY_SOURCE)
print 'data and rank',rand_num,comm.rank, items