Skip to content

Python garbage collector and mpi interface interaction issues. #77

@roigcarlo

Description

@roigcarlo

It appears that for some unknown reason python does not call the destructor of the mpi module when executed with mpirun. The problem is mainly that the job never ends so it keeps consuming CPU time and also it interferes with any 3rd party tool that tries to monitor for the end of the process as for example profilers

Example case:

mpirun -np 16 python MainKratos.py

In this case we should call 16 instances of:

/// Destructor, finalizes MPI if necessary.
~PythonMPI()
{
    int MpiIsFinalized = 0;
    MPI_Finalized(&MpiIsFinalized);
	
    if (MpiIsFinalized == 0)
        MPI_Finalize();
}

But only 11 or 12 are called. This drives me to the conclusion that the garbage collector is (a garbage) not waiting until the function is called at the end of script. Or the problem lies in how boost-python handles the destruction of the class. I am not sure.

A proposed solution for this, which I am using right now would be to explicitly define a finalize function:

/// Destructor, finalizes MPI if necessary.
void finalize()
{
    int MpiIsFinalized = 0;
    MPI_Finalized(&MpiIsFinalized);
	
    if (MpiIsFinalized == 0)
        MPI_Finalize();
}

Which basically does the same as the destructor but is explicitly called at the end of the script ( ideally leaving some room for the python script to do more things )

from KratosMultiphysics import mpi    # mpi_Initialize() is done here

# Fancy code here

mpi.finalize()

# Some more fancy code here if possible

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions