From 1add48611585d1a2fd9d58e74492925973e5dc59 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 10 Sep 2022 23:37:39 +0200 Subject: [PATCH 1/3] don't rely on PETSc for MPI init --- src/parallelization.f90 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/parallelization.f90 b/src/parallelization.f90 index 5c7e9bff3..fc328001a 100644 --- a/src/parallelization.f90 +++ b/src/parallelization.f90 @@ -67,10 +67,12 @@ subroutine parallelization_init PetscErrorCode :: err_PETSc #ifdef _OPENMP ! If openMP is enabled, check if the MPI libary supports it and initialize accordingly. - ! Otherwise, the first call to PETSc will do the initialization. call MPI_Init_Thread(MPI_THREAD_FUNNELED,threadLevel,err_MPI) if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI init failed' if (threadLevel Date: Sat, 10 Sep 2022 23:53:59 +0200 Subject: [PATCH 2/3] clean termination in case of MPI runs (no deadlock) --- src/quit.f90 | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/quit.f90 b/src/quit.f90 index 4fc6bf11b..d9d9fbd21 100644 --- a/src/quit.f90 +++ b/src/quit.f90 @@ -22,23 +22,16 @@ subroutine quit(stop_id) integer, dimension(8) :: dateAndTime integer :: err_HDF5 - integer(MPI_INTEGER_KIND) :: err_MPI + integer(MPI_INTEGER_KIND) :: err_MPI, worldsize PetscErrorCode :: err_PETSc - call h5open_f(err_HDF5) - if (err_HDF5 /= 0_MPI_INTEGER_KIND) write(6,'(a,i5)') ' Error in h5open_f ',err_HDF5 ! prevents error if not opened yet + + call h5open_f(err_HDF5) ! prevents error if not opened yet + if (err_HDF5 /= 0) write(6,'(a,i5)') ' Error in h5open_f ',err_HDF5 call h5close_f(err_HDF5) - if (err_HDF5 /= 0_MPI_INTEGER_KIND) write(6,'(a,i5)') ' Error in h5close_f ',err_HDF5 + if (err_HDF5 /= 0) write(6,'(a,i5)') ' Error in h5close_f ',err_HDF5 call PetscFinalize(err_PETSc) - CHKERRQ(err_PETSc) - -#ifdef _OPENMP - call MPI_finalize(err_MPI) - if (err_MPI /= 0_MPI_INTEGER_KIND) write(6,'(a,i5)') ' Error in MPI_finalize',err_MPI -#else - err_MPI = 0_MPI_INTEGER_KIND -#endif call date_and_time(values = dateAndTime) write(6,'(/,a)') ' DAMASK terminated on:' @@ -49,10 +42,15 @@ subroutine quit(stop_id) dateAndTime(6),':',& dateAndTime(7) - if (stop_id == 0 .and. & - err_HDF5 == 0 .and. & - err_MPI == 0_MPI_INTEGER_KIND .and. & - err_PETSC == 0) stop 0 ! normal termination - stop 1 ! error (message from IO_error) + if (stop_id == 0 .and. err_HDF5 == 0 .and. err_PETSC == 0) then + call MPI_Finalize(err_MPI) + if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI_Finalize error' + stop 0 ! normal termination + else + call MPI_Comm_size(MPI_COMM_WORLD,worldsize,err_MPI) + if (err_MPI /= 0_MPI_INTEGER_KIND) error stop 'MPI_Comm error' + if (stop_id /= 0 .and. worldsize > 1) call MPI_Abort(MPI_COMM_WORLD,1,err_MPI) + stop 1 ! error (message from IO_error) + endif end subroutine quit From 210dd8b4977d32905e6b86ea57aef54262d50cf4 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 23 Jan 2023 11:45:03 +0100 Subject: [PATCH 3/3] report errors to STDERR --- src/quit.f90 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/quit.f90 b/src/quit.f90 index d9d9fbd21..1583a20ff 100644 --- a/src/quit.f90 +++ b/src/quit.f90 @@ -5,6 +5,7 @@ !> everything is fine. Exit code 1 signals an error, message according to IO_error. !-------------------------------------------------------------------------------------------------- subroutine quit(stop_id) + use, intrinsic :: ISO_fortran_env, only: ERROR_UNIT #include use PETScSys #if (PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR>14) && !defined(PETSC_HAVE_MPI_F90MODULE_VISIBILITY) @@ -27,9 +28,9 @@ subroutine quit(stop_id) call h5open_f(err_HDF5) ! prevents error if not opened yet - if (err_HDF5 /= 0) write(6,'(a,i5)') ' Error in h5open_f ',err_HDF5 + if (err_HDF5 /= 0) write(ERROR_UNIT,'(a,i5)') ' Error in h5open_f ',err_HDF5 call h5close_f(err_HDF5) - if (err_HDF5 /= 0) write(6,'(a,i5)') ' Error in h5close_f ',err_HDF5 + if (err_HDF5 /= 0) write(ERROR_UNIT,'(a,i5)') ' Error in h5close_f ',err_HDF5 call PetscFinalize(err_PETSc)