from mpi4py import MPIfrom mpi_config import check_mpi,mpi_setup,my_rank,my_commimport sys
mpi_setup()mpi_info = MPI.Info.Create()mpi_info.Set("host", MPI.Get_processor_name())
my_comm.Set_errhandler(MPI.ERRORS_ARE_FATAL)
if my_rank > 0: try: commspawn = MPI.COMM_SELF.Spawn(sys.executable, args=['child_exception.py'], maxprocs=4,info = mpi_info) commspawn.Barrier() commspawn.Disconnect() except: print("exception raised")
my_comm.Barrier()
from mpi4py import MPImy_rank = MPI.COMM_WORLD.Get_rank()my_comm = MPI.COMM_WORLD
parent = MPI.Comm.Get_parent()if my_rank == 2: 1/0
parent.Barrier()parent.Disconnect()
Traceback (most recent call last): File "child_exception.py", line 8, in <module> 1/0ZeroDivisionError: division by zeroTraceback (most recent call last): File "child_exception.py", line 8, in <module> 1/0ZeroDivisionError: division by zeroTraceback (most recent call last): File "child_exception.py", line 8, in <module> 1/0ZeroDivisionError: division by zero
import sys, mpi4pyfrom mpi4py import MPIimport osimport distutils.spawn
my_rank = MPI.COMM_WORLD.Get_rank()my_comm = MPI.COMM_WORLD
def check_mpi(): mpiexec_path, _ = os.path.split(distutils.spawn.find_executable("mpiexec")) for executable, path in mpi4py.get_config().items(): if executable not in ['mpicc', 'mpicxx', 'mpif77', 'mpif90', 'mpifort']: continue if mpiexec_path not in path: raise ImportError("mpi4py may not be configured against the same version of 'mpiexec' that you are using. The 'mpiexec' path is {mpiexec_path} and mpi4py.get_config() returns:\n{mpi4py_config}\n".format(mpiexec_path=mpiexec_path, mpi4py_config=mpi4py.get_config()))
def mpi_setup(): sys_excepthook = sys.excepthook def mpi_excepthook(type, value, traceback): sys_excepthook(type,value,traceback) mpi4py.MPI.COMM_WORLD.Abort(1)
sys.excepthook = mpi_excepthook check_mpi()
--
You received this message because you are subscribed to the Google Groups "mpi4py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mpi4py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpi4py/c1d482a7-e1c7-43a8-8a78-20c846396de0o%40googlegroups.com.
Look at the implementation of the mpi4py.run module.I you want to get really serious about the best way to do things, I would recommend the following practice as an start:from mpi4py.run import set_abort_status
try:
run_your_mpi_code()
except SystemExit as exc:
set_abort_status(exc.code)
raise
except:
set_abort_status(1)
raise`SystemExit` is handled specially because `raise SystemExit` (or equivalently, `sys.exit()`) should cleanly exit the process with success.`set_abort_status(1/nonzero)` will trigger the MPI_Abort() call you want, but that will happen after all the usual Python finalization occurs (which is a good thing, IMHO).If you add dynamic process management to the mix, maybe you need a `finally` clause to handle the comm.Disconnect() calls.
To unsubscribe from this group and stop receiving emails from it, send an email to mpi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpi4py/c1d482a7-e1c7-43a8-8a78-20c846396de0o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to mpi4py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpi4py/abcf12e3-4ee4-4515-beeb-3673a46d208eo%40googlegroups.com.
Oh, I see... well, it is quite doable, though maybe slightly involved for your taste.Look at the attached example$ mpiexec -n 1 python parent.py<no ouput, all good>$ mpiexec -n 1 python parent.py failTraceback (most recent call last):
File "parent.py", line 11, in <module>
assert ok, "failure in child process"
AssertionError: failure in child processIs that enough for you?PS: Exception handling is not only about errors, but also about control flow. Abuse of it and profit, `python -m this | grep practicality`.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpi4py/abcf12e3-4ee4-4515-beeb-3673a46d208eo%40googlegroups.com.