Manual finalizing and initializing MPI environment does not work!

Issue #85 closed
Ellert van der Velden
created an issue

So, I have been learning how to use MPI in C in the last week and wanted to have a go with it in Python (since that is my main language). Now, I found out that mpi4py automatically initializes and finalizes the MPI environment for convenience. However, I want to be capable of doing this myself as well (whether or not it will be useful or relevant does not really matter). So, since the package actually has a method that allows you to do this, I tried it out by using the following code that I modified from a simple "hello world"-example.

from mpi4py import MPI

rank = MPI.COMM_WORLD.Get_rank()
size = MPI.COMM_WORLD.Get_size()

print("Hello from rank %s of %s." % (rank, size))
print(MPI.Is_initialized())
print(MPI.Is_finalized())

MPI.COMM_WORLD.Barrier()
MPI.Finalize()

print(MPI.Is_initialized())
print(MPI.Is_finalized())

MPI.Init()

The output of this will be that all cores print the hello-statement and print that MPI is initialized and not finalized. However, after the MPI.Finalize()-call, the prints will state that MPI is both initialized and finalized at the same time (by all cores). Because of this, I cannot reinitialize the MPI environment since it is already initialized, but I cannot use any MPI tasks either, since the environment has already been finalized. All prints are still printed by all cores that were being use in the MPI environment, but no specific MPI tasks can be used. This basically means that it is impossible to use MPI for as long as this Python process exists.

I have a feeling that this is not really the way it should work.

Dependencies:

  • I am using the latest version of mpi4py (as far as I know): 3.0.0
  • I tried this on Linux Ubuntu 16.04, Windows 10 64-bit and Windows 8.1 32-bit.
  • Python versions used were Python 3.3, 3.4, 3.5 and 3.6.
  • MPI distributions used were MS-MPI for Windows and OpenMPI for Linux.

Comments (5)

  1. Lisandro Dalcin

    To disable automatic init and finalize you have to do this:

    import mpi4py
    mpi4py.rc.initialize = False
    mpi4py.rc.finalize = False
    from mpi4py import MPI
    ...
    MPI.Init()
    ...
    MPI.Finalize()
    

    This way also works:

    import  mpi4py
    mpi4py.rc(initialize=False, finalize=False)
    from mpi4py import MPI
    ...
    

    Of course, you really need to use mpi4py.rc BEFORE the first from mpi4py import MPI import ever, usually at the beginning of your main script or entry point.

  2. Log in to comment