python-3.xmpi4py

Determine if Python program is running in parallel


Is there a way to find out whether a Python program is running in parallel through mpirun or mphexec. I thought I could get this information by getting sys.argv but it's not there. Is there any way I could get this information, perhaps by looking at the mpi4py module?


Solution

  • If you use the mpi4py package, you can check whether the size of the MPI_COMM_WORLD communicator is greater than 1 or not. If yes, you are running in parallel. If not, you are only running on one process. Note that you can run on only one process even though if you run your script using the mpirun command (e.g. mpirun -n 1 python test.py)

    You can get the size of the MPI_COMM_WORLD communicator with the following lines of code:

    from mpi4py import MPI
    comm = MPI.COMM_WORLD
    size = comm.Get_size()