From 8b4bbbcd950a89ba3fdbaa1a99fbd17ee9914232 Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Tue, 8 Sep 2026 13:16:31 -0400 Subject: [PATCH 1/2] Dist_graph_create*: Finalize the created communicators --- src/topology.jl | 7 ++++++- test/test_neighbor_comm.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/topology.jl b/src/topology.jl index 5ace5bb19..b24d54f76 100644 --- a/src/topology.jl +++ b/src/topology.jl @@ -198,6 +198,8 @@ const WEIGHTS_EMPTY = WeightsEmpty() Create a new communicator from a given directed graph topology, described by local incoming and outgoing edges on an existing communicator. +[`MPI.free`](@ref) may be called on the returned communicator once it is no longer needed; otherwise it is freed at finalization. + # Arguments - `comm::Comm`: The communicator on which the distributed graph topology should be induced. - `sources::Vector{Cint}`: The local, incoming edges on the rank of the calling process. @@ -229,6 +231,7 @@ function Dist_graph_create_adjacent(comm::Comm, sources::Vector{Cint}, destinati length(sources), sources, source_weights, length(destinations), destinations, destination_weights, Info(infokws...), reorder, graph_comm) + graph_comm != COMM_NULL && finalizer(free, graph_comm) return graph_comm end @@ -237,6 +240,8 @@ end Create a new communicator from a given directed graph topology, described by incoming and outgoing edges on an existing communicator. +[`MPI.free`](@ref) may be called on the returned communicator once it is no longer needed; otherwise it is freed at finalization. + # Arguments - `comm::Comm`: The communicator on which the distributed graph topology should be induced. - `sources::Vector{Cint}`: An array with the ranks for which this call will specify outgoing edges. @@ -266,7 +271,7 @@ function Dist_graph_create(comm::Comm, sources::Vector{Cint}, degrees::Vector{Ci # MPI_Info info, int reorder, MPI_Comm *comm_dist_graph) API.MPI_Dist_graph_create(comm, length(sources), sources, degrees, destinations, weights, Info(infokws...), reorder, graph_comm) - + graph_comm != COMM_NULL && finalizer(free, graph_comm) return graph_comm end diff --git a/test/test_neighbor_comm.jl b/test/test_neighbor_comm.jl index 4dcb25a5a..fdeaf92bf 100644 --- a/test/test_neighbor_comm.jl +++ b/test/test_neighbor_comm.jl @@ -53,5 +53,32 @@ let @test srcw2 == srcw3 == Cint[prev_rank + comm_size] end +# Both `Dist_graph_create*` must attach a finalizer, like every other +# communicator constructor, so that the communicator is not leaked. +let + graph_comm = ring_graph(; weighted=false) + @test graph_comm != MPI.COMM_NULL + finalize(graph_comm) + @test graph_comm == MPI.COMM_NULL +end + +let + sources = Cint[prev_rank] + destinations = Cint[next_rank] + graph_comm = MPI.Dist_graph_create_adjacent(comm, sources, destinations) + @test graph_comm != MPI.COMM_NULL + finalize(graph_comm) + @test graph_comm == MPI.COMM_NULL +end + +# An explicit `free` must compose with the finalizer rather than double-free. +let + graph_comm = ring_graph(; weighted=false) + MPI.free(graph_comm) + @test graph_comm == MPI.COMM_NULL + finalize(graph_comm) + @test graph_comm == MPI.COMM_NULL +end + MPI.Finalize() @test MPI.Finalized() From 6c0bc1fb2e580bc0f71c72107a4a94284f64b04e Mon Sep 17 00:00:00 2001 From: Erik Schnetter Date: Tue, 22 Sep 2026 10:24:15 -0400 Subject: [PATCH 2/2] Remove test for `COMM_NULL` --- src/topology.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/topology.jl b/src/topology.jl index b24d54f76..ea0a850b0 100644 --- a/src/topology.jl +++ b/src/topology.jl @@ -231,7 +231,7 @@ function Dist_graph_create_adjacent(comm::Comm, sources::Vector{Cint}, destinati length(sources), sources, source_weights, length(destinations), destinations, destination_weights, Info(infokws...), reorder, graph_comm) - graph_comm != COMM_NULL && finalizer(free, graph_comm) + finalizer(free, graph_comm) return graph_comm end @@ -271,7 +271,7 @@ function Dist_graph_create(comm::Comm, sources::Vector{Cint}, degrees::Vector{Ci # MPI_Info info, int reorder, MPI_Comm *comm_dist_graph) API.MPI_Dist_graph_create(comm, length(sources), sources, degrees, destinations, weights, Info(infokws...), reorder, graph_comm) - graph_comm != COMM_NULL && finalizer(free, graph_comm) + finalizer(free, graph_comm) return graph_comm end