diff --git a/CMakeLists.txt b/CMakeLists.txt index 3cfea2227f..7ecd6e3211 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -433,6 +433,7 @@ set(LIBOPENMC_FORTRAN_SRC ) set(LIBOPENMC_CXX_SRC src/initialize.cpp + src/finalize.cpp src/hdf5_interface.cpp src/message_passing.cpp src/plot.cpp diff --git a/src/api.F90 b/src/api.F90 index 49d86edb67..bc0757be57 100644 --- a/src/api.F90 +++ b/src/api.F90 @@ -105,6 +105,11 @@ contains function openmc_finalize() result(err) bind(C) integer(C_INT) :: err + interface + subroutine openmc_free_bank() bind(C) + end subroutine openmc_free_bank + end interface + ! Clear results err = openmc_reset() @@ -170,6 +175,7 @@ contains #ifdef OPENMC_MPI ! Free all MPI types call MPI_TYPE_FREE(MPI_BANK, err) + call openmc_free_bank() #endif end function openmc_finalize diff --git a/src/finalize.cpp b/src/finalize.cpp new file mode 100644 index 0000000000..696a1e80e5 --- /dev/null +++ b/src/finalize.cpp @@ -0,0 +1,10 @@ +#include "finalize.h" + +#include "message_passing.h" + +void openmc_free_bank() +{ +#ifdef OPENMC_MPI + MPI_Type_free(&openmc::mpi::bank); +#endif +} diff --git a/src/finalize.h b/src/finalize.h new file mode 100644 index 0000000000..e606493ca1 --- /dev/null +++ b/src/finalize.h @@ -0,0 +1,6 @@ +#ifndef FINALIZE_H +#define FINALIZE_H + +extern "C" void openmc_free_bank(); + +#endif // FINALIZE_H diff --git a/src/hdf5_interface.F90 b/src/hdf5_interface.F90 index 3fd5e860fd..40f56d88ef 100644 --- a/src/hdf5_interface.F90 +++ b/src/hdf5_interface.F90 @@ -1103,7 +1103,7 @@ contains character(*), intent(in), target :: buffer(:) ! read data to here logical, intent(in), optional :: indep ! independent I/O - integer :: i + integer(HSIZE_T) :: i integer(HSIZE_T) :: dims(1) integer(C_SIZE_T) :: m, n logical(C_BOOL) :: indep_ diff --git a/src/initialize.cpp b/src/initialize.cpp index d8045e4337..c7c37e31db 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -71,7 +71,8 @@ void initialize_mpi(MPI_Comm intracomm) // Initialize MPI int flag; - if (!MPI_Initialized(&flag)) MPI_Init(nullptr, nullptr); + MPI_Initialized(&flag); + if (!flag) MPI_Init(nullptr, nullptr); // Determine number of processes and rank for each MPI_Comm_size(intracomm, &openmc::mpi::n_procs); diff --git a/src/main.cpp b/src/main.cpp index 42593dc658..6d0cf1f268 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,12 +38,14 @@ int main(int argc, char* argv[]) { err = openmc_calculate_volumes(); break; } + if (err) openmc::fatal_error(openmc_err_msg); // Finalize and free up memory - openmc_finalize(); + err = openmc_finalize(); + if (err) openmc::fatal_error(openmc_err_msg); // If MPI is in use and enabled, terminate it -#ifdef MPI +#ifdef OPENMC_MPI MPI_Finalize(); #endif }