python - mpi4py: Communicating between spawned processes -
i have 1 process running program called t1.py spawns 3 other processes, of run t2.py. want broadcast value spawned process rank of 0 2 other spawned processes. however, when bcast called, program blocks. idea why happens? , how fix it?
t1.py
from mpi4py import mpi import sys sub_comm = mpi.comm_self.spawn(sys.executable, args=['t2.py'], maxprocs=3) print 'hi'
t2.py
from mpi4py import mpi comm = mpi.comm.get_parent() print 'ho ', comm.get_rank() = comm.bcast(comm.get_rank(), root=0) print
output
hi ho 2 ho 0 ho 1
if want childs talk each other, can use mpi.comm_world
:
a = mpi.comm_world.bcast(mpi.comm_world.get_rank(), root=0)
by printing mpi.comm_world.get_rank(), ' of ',mpi.comm_world.get_size()
, can check childs'mpi.comm_world
limited childs.
now, let's investigate reason why comm.bcast(...)
failed if comm
obtained comm=mpi.comm.get_parent()
. indeed, looking @ size , ranks of communicator, seems similar mpi.comm_world
. but, on contrary, comm
different mpi.comm_world
: intercommunicator. more precisely, way parent can talk childs. collective communications can used, processes, both parent , childs, must call function. please carrefully read mpi standards, in particular sections 5.2.2 , 5.2.3 intercommunicator collective operations. regarding bcast()
, mpi.root
, mpi.proc_null
used instead of rank of broadcaster root
specify direction (parent child of child parent) , sending process. lastly, intracommunicator can defined on base of intercommunicator using merge()
(corresponding mpi_intercomm_merge()
). in intracommunicator, parents , childs not belong 2 different groups: processes characterized unique rank, usual.
here modified versions of t1.py , t2.py, bcast()
intercommunicator performed. intercommunicator merge()
, bcast()
on resulting intracommunicator called usual.
t1.py
from mpi4py import mpi import sys sub_comm = mpi.comm_self.spawn(sys.executable, args=['t2.py'], maxprocs=3) val=42 sub_comm.bcast(val, mpi.root) common_comm=sub_comm.merge(false) print 'parent in common_comm ', common_comm.get_rank(), ' of ',common_comm.get_size() #mpi_intercomm_merge(parentcomm,1,&intracomm); val=13 c=common_comm.bcast(val, root=0) print "value rank 0 in common_comm", c
t2.py
from mpi4py import mpi comm = mpi.comm.get_parent() print 'ho ', comm.get_rank(), ' of ',comm.get_size(),' ', mpi.comm_world.get_rank(), ' of ',mpi.comm_world.get_size() = mpi.comm_world.bcast(mpi.comm_world.get_rank(), root=0) print "value other child", print "comm.is_inter", comm.is_inter() b = comm.bcast(comm.get_rank(), root=0) print "value parent", b common_comm=comm.merge(true) print "common_comm.is_inter", common_comm.is_inter() print 'common_comm ', common_comm.get_rank(), ' of ',common_comm.get_size() c=common_comm.bcast(0, root=0) print "value rank 0 in common_comm", c
Comments
Post a Comment