Skip to content

Commit

Permalink
Match MPI_Finalize behavior to MPI_Init (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilMiller authored Mar 27, 2024
1 parent 84866bf commit 17123e9
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Core/schism_msgp.F90
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ module schism_msgp
! Private data
!-----------------------------------------------------------------------------

logical,save :: schism_initialized_mpi ! Whether SCHISM initialized MPI, and thus is responsible for finalizing it

integer,save :: mnesend ! Max number of elements to send to a nbr
integer,save,allocatable :: nesend(:) ! Number of elements to send to each nbr
integer,save,allocatable :: iesend(:,:) ! Local index of send elements
Expand Down Expand Up @@ -382,10 +384,12 @@ subroutine parallel_init(communicator)
if (present(communicator)) then
! Expect external call to mpi_init,
! use communicator as provided by the interface
schism_initialized_mpi = .FALSE.
call mpi_comm_dup(communicator,comm_schism,ierr)
else
! Initialize MPI
call mpi_init(ierr)
schism_initialized_mpi = .TRUE.
if(ierr/=MPI_SUCCESS) call parallel_abort(error=ierr)
! Duplicate communication space to isolate ELCIRC communication
call mpi_comm_dup(MPI_COMM_WORLD,comm_schism,ierr)
Expand Down Expand Up @@ -431,8 +435,10 @@ end subroutine parallel_init
subroutine parallel_finalize
implicit none

call mpi_finalize(ierr)
if(ierr/=MPI_SUCCESS) call parallel_abort(error=ierr)
if(schism_initialized_mpi) then
call mpi_finalize(ierr)
if(ierr/=MPI_SUCCESS) call parallel_abort(error=ierr)
endif

end subroutine parallel_finalize

Expand Down

0 comments on commit 17123e9

Please sign in to comment.