Reputation: 805
I have a piece of code that uses mpi4py to spawn several instances of an mpi exectuable. I want the code to halt while these processes complete, and then call a second group of the same executable.
The problem is that all calls to the mpi executable get spawned immediately.
It seems like there is no way to use a barrier to prevent this from happening. Does anyone know if this is correct, and if so does anyone have a bright idea to get the result I need.
#!/usr/bin/env python
from mpi4py import MPI
import numpy
import sys
import os
rank = MPI.COMM_WORLD.Get_rank()
new_comm = MPI.COMM_WORLD.Split(color=rank, key=rank)
print(new_comm.Get_rank())
cwd=os.getcwd()
directory=os.path.join(cwd,str(rank))
print(rank,directory)
os.chdir(directory)
new_comm.Spawn("SOME_F90_MPI_EXECUTABLE",
args=None,
maxprocs=4)
'''I want to pause here until the spawned processes finish running...'''
new_comm.Barrier()
MPI.COMM_WORLD.Barrier()
print(new_comm.Get_rank())
cwd=os.getcwd()
directory=os.path.join(cwd,str(rank))
print(rank,directory)
os.chdir(directory+"_2")
new_comm.Spawn("SOME_F90_MPI_EXECUTABLE",
args=["> output"],
maxprocs=4)
new_comm.Disconnect()
print("All finished here.....")
Thanks!
Upvotes: 0
Views: 1869
Reputation: 22650
You must use the intercommunicator returned by Spawn
:
child_comm = MPI.COMM_WORLD.Spawn("./spawnchild.py", args=None, maxprocs=2)
child_comm.Barrier()
For the child, get the parent intercommunicator (similarly in Fortran):
parent = MPI.COMM_WORLD.Get_parent()
assert(parent != MPI.COMM_NULL)
parent.Barrier();
Note that an intercommunicator, which consists of processes from two groups, behaves different than a traditional intercommunicator. E.g. for the Barrier, the following semantics apply:
If comm is an intercommunicator,
MPI_BARRIER
involves two groups. The call returns at processes in one group (group A) of the intercommunicator only after all members of the other group (group B) have entered the call (and vice versa). A process may return from the call before all processes in its own group have entered the call.
(MPI 3.1 Standard, 5.3)
Upvotes: 1