Search code examples
pythonmpimpi4py

Create group communicator in mpi (mpi4py)


I am trying to create an MPI group using mpi4py. My current attempt looks like this:

from mpi4py import MPI

comm = MPI.COMM_WORLD
newGroup = comm.group.Excl([0, 1, 2])
print newGroup.size

newComm = comm.Create_group(newGroup)
print newComm
print newComm.Get_size()

The newGroup.size call does return 2 (I started the application with 5 processes) and the newComm variable indicates to be a communicator. But as soon as I try to call Get_size an exception is thrown:

mpi4py.MPI.Exception: MPI_ERR_COMM: invalid communicator

How can I create a new communicator based on a predefined sequence of process IDs?


Solution

  • newComm is a legit communicator on tasks [3-4], but is MPI_COMM_NULL on tasks [0-2]. The standard does not allow you to invoke MPI_Comm_size(MPI_COMM_NULL, ...), and hence your error.

    The solution is to call newComm.Get_size() explicitly on tasks [3-4], or on any tasks where newComm is not MPI_COMM_NULL.