def add_hook():
    """
    Add a global hook function that captures all unhandled exceptions.
    The function calls MPI_Abort() to force all processes abort.

    An MPI runtime is expected to kill all of its child processes
    if one of them exits abnormally or without calling `MPI_Finalize()`.
    However, when a Python program run on `mpi4py`, the MPI runtime
    often fails to detect a process failure, and the rest of the processes
    hang infinitely.

    See https://github.com/chainer/chainermn/issues/236 and
    https://mpi4py.readthedocs.io/en/stable/mpi4py.run.html for more
    information.
    """
    global _orig_except_hook

    if _orig_except_hook is not None:
        logger.warning("GlobalExceptHook.add_hook() seems to be called multiple times. Ignoring.")
        return

    logger.info("Adding global except hook for the distributed job to shutdown MPI if unhandled exception is raised on some of the ranks.")
    _orig_except_hook = sys.excepthook
    sys.excepthook = _global_except_hook

    Add a global hook function that captures all unhandled exceptions.
    The function calls MPI_Abort() to force all processes abort.

    An MPI runtime is expected to kill all of its child processes
    if one of them exits abnormally or without calling `MPI_Finalize()`.
    However, when a Python program run on `mpi4py`, the MPI runtime
    often fails to detect a process failure, and the rest of the processes
    hang infinitely.

    See https://github.com/chainer/chainermn/issues/236 and
    https://mpi4py.readthedocs.io/en/stable/mpi4py.run.html for more
    information.
    
trainer/utils/hook.py