mpi multiple init finalize
mpi
Solution
You've basically got the right solution, so I'll just confirm. It is in fact erroneous to call `MPI_Init` and `MPI_Finalize` multiple times, and if you have an entity that calls these internally on creation/destruction, then you can only instantiate that entity once. If you want to create multiple instances, you'll need to change the entity to do one of the following:
- Offer an option to not call Init and Finalize that the user can set externally
- Use `MPI_Initialized` and `MPI_Finalized` to decide whether it needs to call either of the above
Problem
Assuming I have good reason to do the following (I think I have), how to make it works? ``` #include "mpi.h" int main( int argc, char *argv[] ) { int myid, numprocs; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); // ... MPI_Finalize(); MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); // ... MPI_Finalize(); return 0; } ``` I got the error: ``` -------------------------------------------------------------------------- Calling any MPI-function after calling MPI_Finalize is erroneous. The only exceptions are MPI_Initialized, MPI_Finalized and MPI_Get_version. -------------------------------------------------------------------------- *** An error occurred in MPI_Init *** after MPI was finalized *** MPI_ERRORS_ARE_FATAL (your MPI job will now abort) [ange:13049] Abort after MPI_FINALIZE completed successfully; not able to guarantee that all other processes were killed! ``` The reason to do that: I've Python wrapping around C++ code. Some wrapped class have constructor that call MPI_Init, and destructor that call MPI_Finalize. I would like to be able in Python to freely create, delete re-create the Python object that wrap this C++ class. The ultimate goal is to create a webservice entirely in Python, that import the Python C++ exstension once, and execute some Python code given the user request. EDIT: I think I'll refactor the C++ code to give possibility to not MPI_Init and MPI_Finalize in constructor and destructor, so it's possible to do it exactly one time in the Python script (using mpi4py).