Can the spawned process interact with the "main" MPI communicator

Is there a way to use MPI to allow spawned processes to interact with all other MPI_WORLD participants, and not just the parent who spawned this process?

Now I have two main agents, the so-called main and subordinate agents, which run the following code (spawn.py):

# Spawn test: master and first slave import sys from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() if rank == 0 : # master code print "i am the master on rank %i" % (rank) running = True while running : msg = comm.recv(source=MPI.ANY_SOURCE,tag=0) print "master received message: ", msg if msg == "Done" : running = False print "master is done" if rank == 1 : # slave code no_spawn = 1 print "I am a slave on rank %i, about the spawn lower slaves" % (rank) icomm = MPI.COMM_SELF.Spawn(sys.executable,args=["Cpi.py","ben"],maxprocs=no_spawn) comm.send("Test_comm",dest=0,tag=0) icomm.send("Test_icomm",dest=0,tag=0) isize = icomm.Get_size() print "on slave, isize= %i" % (isize) rec = 0 while rec <= (no_spawn-1) : msg = icomm.recv(source=MPI.ANY_SOURCE,tag=20) print "slave received message: %s (rec=%i)" % (msg, rec) rec = rec +1 import time print "slave going to sleep\n" time.sleep(1) for i in range(no_spawn) : message = ("To spawn from slave",) icomm.send(message,dest=i,tag=0) for i in range(no_spawn) : message = ("Done",) icomm.send(message,dest=i,tag=0) msg = comm.recv(source=MPI.ANY_SOURCE,tag=0) print "slave received message: ", msg comm.send("Done",dest=0,tag=0) MPI.Finalize() 

In turn, the slave device generates another 1 process that runs the following code (CPi.py, named after the mpi4py training file):

 #!/usr/bin/env python import sys from mpi4py import MPI comm = MPI.COMM_WORLD icomm = MPI.Comm.Get_parent() irank = icomm.Get_rank() print "Spawn irank=%i" % (irank) message = "From_Spawn_%i"%(irank) icomm.send(message,dest=0,tag=20) running = True while running : msg = icomm.recv(source=MPI.ANY_SOURCE,tag=0) print "Spawn on irank %i received message: %s " %(irank,msg) if msg[0] == "Done" : running = False print "spawn %i sending a last msg to the master and the slave" % (irank) comm.send(("To master from spawn",), dest=0,tag=0) comm.send(("To slave from spawn",), dest=0,tag=0) 

Between the master and the slave, I can send messages using the comm communicator. Between the slave and the spawned process, I can send messages through the icomm communicator. But I really want to create a process and that this process can communicate with both the master and the slave over the communicator comm ; see the last two lines of the generated process. Is it possible? And is it possible for the spawned process to also listen on the main comm used by the slave and the master? What rank will he send / listen to?

The code provided does not end because the last two messages sent by the spawned process are not accepted by the slave or the master. (I am running code with mpiexec -n 2 python spawn.py )

+2
python mpi spawn
source share
1 answer

For the process generated by the slave to talk to the master, he will need to create another new communicator using something like MPI_CONNECT and MPI_ACCEPT. This can be done, but you will need to use a slave to transfer the connection data between them.

Before you go through all this, make sure that you cannot just start your work with more processes and arbitrarily assign different roles for different ranks. It is a pain to use intercommunicators in the best circumstances, and it is probably easier to start with the right number of processes.

+1
source share

All Articles