Call MPI_ABORT from exception destructor?

I have a large (quite complex ...) C++/MPI codebase. Parts of this code has developed into a do-not-throw exceptions zone because the MPI application will deadlock when one process throws an uncatched exception - in the case of uncaught exception I just want entire application (including all MPI processes to terminate). As I understand it the MPI_ABORT(MPI_COMM_WORLD) call will terminate all MPI processes - I have been toying with a custom exception like this:

class my_exception {
public:
    explicit my_exception(const std::string& m) :
        msg(m)
    {}

    ~my_exception() {
        if (!this->handled)
           Mpi_abort();
    }
    
    void handle() {
        this->handled = true;
    }

private:
    std::string msg;
    bool handled{false};
}

The purpose of this is to ensure that MPI_ABORT() is called for uncaught exceptions. In the case exceptions are indeed caught the catch block must mutate the state of the exception object to ensure safe destruction - that is "OK". It is also "OK" that the application goes down in flames - the present deadlock is even worse.

I have not tried this out - but would be interested if someone up front could judge whether it is feasible at all. Alternative ways to avoid the deadlock is also more than welcome.

1 answer

  • answered 2021-09-28 07:06 user422005

    The comment from @Daniel Langr about wrapping main was a better alternative

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum