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.
- 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.