Skip to content

Commit

Permalink
Made sc_finalize optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Markert committed Jul 26, 2023
1 parent 9435b34 commit 12a20e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/auxiliary/t8code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function init_t8code()
end

# Initialize the sc library, has to happen before we initialize t8code.
let catch_signals = 0, print_backtrace = 0
T8code.Libt8.sc_init(mpi_comm(), catch_signals, print_backtrace, C_NULL,
let catch_signals = 0, print_backtrace = 0, log_handler = C_NULL
T8code.Libt8.sc_init(mpi_comm(), catch_signals, print_backtrace, log_handler,
T8code.Libt8.SC_LP_ERROR)
end

Expand All @@ -26,10 +26,16 @@ function init_t8code()
# Initialize t8code with log level ERROR to prevent a lot of output in AMR simulations.
t8_init(T8code.Libt8.SC_LP_ERROR)

# `sc_finalize` should always be called during shutdown of an application.
# It checks whether there is still un-freed memory by t8code and/or
# T8code.jl and throws an exception if this is the case.
MPI.add_finalize_hook!(T8code.Libt8.sc_finalize)
if haskey(ENV, "TRIXI_T8CODE_SC_FINALIZE")
# Normally, `sc_finalize` should always be called during shutdown of an
# application. It checks whether there is still un-freed memory by t8code
# and/or T8code.jl and throws an exception if this is the case. For
# production runs this is not mandatory, but is helpful during
# development. Hence, this option is only activated when environment
# variable TRIXI_T8CODE_SC_FINALIZE exists.
@warn "T8code.jl: sc_finalize will be called during shutdown of Trixi.jl."
MPI.add_finalize_hook!(T8code.Libt8.sc_finalize)
end

return nothing
end
Expand Down
19 changes: 12 additions & 7 deletions src/meshes/t8code_mesh.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,24 @@ mutable struct T8codeMesh{NDIMS, RealT <: Real, IsParallel, NDIMSP2, NNODES} <:
# `cmesh` does some MPI calls for deallocating shared memory
# arrays. Due to garbage collection in Julia the order of shutdown
# is not deterministic. The following code might happen after MPI
# is already in finalized state. In this case the `finalize_hook`
# of the MPI module already took care of the cleanup. See further
# down.
# is already in finalized state.
# If the environment variable `TRIXI_T8CODE_SC_FINALIZE` is set the
# `finalize_hook` of the MPI module takes care of the cleanup. See
# further down. However, this might cause a pile-up of `mesh`
# objects during long-running sessions.
if !MPI.Finalized()
trixi_t8_unref_forest(mesh.forest)
end
end

MPI.add_finalize_hook!() do
try
# This finalizer call is only recommended during development and not for
# production runs, especially long-running sesions since a reference to

Check warning on line 60 in src/meshes/t8code_mesh.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"sesions" should be "sessions".
# the `mesh` object will be kept throughout the lifetime of the session.
# See comments in `init_t8code()` in file `src/auxiliary/t8code.jl` for
# more information.
if haskey(ENV, "TRIXI_T8CODE_SC_FINALIZE")
MPI.add_finalize_hook!() do
trixi_t8_unref_forest(mesh.forest)
catch
# The `mesh` object was already finalized. Do nothing.
end
end

Expand Down

0 comments on commit 12a20e5

Please sign in to comment.