Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add finalize hooks to cleanup datatypes #696

Merged
merged 1 commit into from
Dec 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/datatypes.jl
Original file line number Diff line number Diff line change
@@ -66,7 +66,9 @@ end

# datatype attribute to store Julia type
const JULIA_TYPE_PTR_ATTR = Ref(Cint(0))
add_init_hook!(() -> JULIA_TYPE_PTR_ATTR[] = create_keyval(Datatype))
add_init_hook!() do
JULIA_TYPE_PTR_ATTR[] = create_keyval(Datatype)
end

"""
to_type(datatype::Datatype)
@@ -134,6 +136,11 @@ _defined_datatype_methods = nothing
# constructed in MPI.Get. Without the cache, each Get would commit the
# same datatype over and over again.
const created_datatypes = IdDict{Type, Datatype}()
add_finalize_hook!() do
for datatype in values(created_datatypes)
free(datatype)
end
end

function Datatype(::Type{T}) where {T}
global created_datatypes
18 changes: 15 additions & 3 deletions src/environment.jl
Original file line number Diff line number Diff line change
@@ -44,13 +44,22 @@ end
const mpi_init_hooks = Any[]
add_init_hook!(f) = push!(mpi_init_hooks, f)
function run_init_hooks()
for f in mpi_init_hooks
while !isempty(mpi_init_hooks)
f = popfirst!(mpi_init_hooks) # FIFO
f()
end
empty!(mpi_init_hooks)
return nothing
end

const mpi_finalize_hooks = Any[]
add_finalize_hook!(f) = push!(mpi_finalize_hooks, f)
function run_finalize_hooks()
while !isempty(mpi_finalize_hooks)
f = pop!(mpi_finalize_hooks) # LIFO
f()
end
return nothing
end

"""
Init(;threadlevel=:serialized, finalize_atexit=true, errors_return=true)
@@ -225,7 +234,10 @@ Julia exits, if it hasn't already been called.
$(_doc_external("MPI_Finalize"))
"""
function Finalize()
MPI.Finalized() || API.MPI_Finalize()
if !MPI.Finalized()
run_finalize_hooks()
API.MPI_Finalize()
end
return nothing
end