From 5c37af63652b40e19b0dd740629b141461f6262d Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 28 Nov 2026 16:30:46 -0700 Subject: [PATCH 1/4] MPIExt: make transferred Datadeps slots Libc-backed on the receiving rank Datadeps frees the slot copies it makes at the end of a region, but `unsafe_free!` can only release a CPU `Array` eagerly when it is Libc-backed; anything else waits for GC. The generic slot transfer makes its copies freeable with `tochunk(libc_backed(move(...)))`. MPI's `move_rewrap` instead applied `libc_backed` to the `Chunk` returned by `mpi_endpoint_transfer`, and `libc_backed` passes a `Chunk` through unchanged. So no slot transferred over MPI was ever Libc-backed, the region's free released nothing, and every such copy stayed alive until a GC happened to collect it. Apply `libc_backed` inside the transfer instead, on the rank that holds the payload: after the receive and ingress move on the destination rank, and after a cross-space move on the owner when both spaces are on one rank. Within a single space `move` is the identity and the "copy" is the value itself, so it is left alone. Only rank-local storage changes: the Chunk's type and the messages exchanged are the same on every rank, so planning stays uniform. `libc_backed` is a no-op for anything but a CPU isbits `Array`, so device payloads are unaffected. Today this bites only slots that are transferred rather than allocated uninitialized (`slot_may_be_uninit`): ordinary dense isbits CPU slots already come from `alloc_libc_array`. Arguments that must be passed by value (NoAliasing, PR #834) or types that decline uninitialized slots take the transfer path. Co-Authored-By: Claude Opus 5.5 --- ext/MPIExt.jl | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 0d2d640ab..6bdb9c475 100644 --- a/ext/MPIExt.jl +++ b/ext/MPIExt.jl @@ -2031,6 +2031,15 @@ end # uniform Chunk (real payload on the destination, placeholder elsewhere). # Both ranks run move: owner does egress before send, dest does ingress after # recv. Chunk type is computed uniformly on every rank via move_type. +# +# The result is a Datadeps slot copy, which Datadeps frees at region end. Like +# the generic leaf (`tochunk(libc_backed(move(...)))`), the payload is made +# Libc-backed so that free actually releases it; this has to happen here, on +# the rank holding the payload, because the Chunk returned to every rank is +# only a handle (`libc_backed` passes a Chunk through unchanged). Only the +# destination rank's local storage is affected, never the Chunk's type or the +# messages exchanged, so the ranks stay uniform. `libc_backed` leaves anything +# but a CPU isbits `Array` alone, so device payloads are unaffected. function mpi_endpoint_transfer(accel::MPIAcceleration, from_proc, to_proc, from_space, to_space, w::MPIWireValue{T}) where T local_rank = MPI.Comm_rank(accel.comm) from_mpi = as_mpi_proc(from_proc, from_space) @@ -2043,6 +2052,11 @@ function mpi_endpoint_transfer(accel::MPIAcceleration, from_proc, to_proc, from_ # converting between inner spaces (e.g. CPU -> GPU) when they differ if local_rank == from_space.rank value = move(from_mpi, to_mpi, wire_value(w)) + # Within one space `move` is the identity: the "copy" is the value + # itself, which must be neither duplicated nor ever freed. + if from_space != to_space + value = Dagger.libc_backed(value) + end return tochunk(value, to_proc, to_space; type=T_new) else return tochunk(nothing, to_proc, to_space; type=T_new) @@ -2072,6 +2086,7 @@ function mpi_endpoint_transfer(accel::MPIAcceleration, from_proc, to_proc, from_ value = recv_yield(accel.comm, from_space.rank, tag) # Dest: ingress move into this space's native storage value = move(from_mpi, to_mpi, value) + value = Dagger.libc_backed(value) return tochunk(value, to_proc, to_space; type=T_new) else return tochunk(nothing, to_proc, to_space; type=T_new) @@ -2146,8 +2161,9 @@ function move_rewrap(accel::MPIAcceleration, cache::AliasedObjectCache, from_pro return mpi_endpoint_alloc(accel, to_proc, to_space, T_dest, w) end end + # The transfer makes its payload freeable on the destination rank return aliased_object!(cache, w) do w - return Dagger.libc_backed(mpi_endpoint_transfer(accel, from_proc, to_proc, from_space, to_space, w)) + return mpi_endpoint_transfer(accel, from_proc, to_proc, from_space, to_space, w) end end From 58f998a56b76c15747f52173d3c803bc33c1c36e Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 28 Nov 2026 16:30:51 -0700 Subject: [PATCH 2/4] test: MPI slots transferred across ranks are Libc-backed and freed A slot whose type declines uninitialized allocation is transferred from its owner. Check that, on the receiving rank, the payload is Libc-backed (the wrapper Chunk being "Libc-backed" is not enough), that readers see the whole payload while it is still allocated, and that the region frees it once they are done, under both the flat and hierarchical planners. Fails on master: every transferred slot is an ordinary GC-managed array. Co-Authored-By: Claude Opus 5.5 --- test/mpi/cpu.jl | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test/mpi/cpu.jl b/test/mpi/cpu.jl index ba97dfd08..d32207f04 100644 --- a/test/mpi/cpu.jl +++ b/test/mpi/cpu.jl @@ -128,6 +128,31 @@ function make_slot(cache, src_rank, dst_rank, obj) src_space, dst_space, obj) end +# An isbits element type that declines uninitialized slots, so that its +# cross-rank slots are transferred copies rather than fresh allocations. +struct XferElt + x::Float64 +end +Dagger.can_alloc_uninit(::Dagger.CPURAMMemorySpace, ::Type{<:Array{XferElt}}) = false +xfer_tile(n) = [XferElt(i) for i in 1:n] +# Readers keep the slot they were given (only to ask about it later, never to +# read it again) and report whether it was still allocated when they finished. +const XFER_SEEN = Any[] +const XFER_SEEN_LOCK = ReentrantLock() +function xfer_keep!(x) + sleep(0.25) + total = sum(e -> e.x, x) + @lock XFER_SEEN_LOCK push!(XFER_SEEN, x) + return (total, Dagger.is_libc_allocated(x)) +end +function xfer_seen_freed() + @lock XFER_SEEN_LOCK begin + result = (length(XFER_SEEN), !any(Dagger.is_libc_allocated, XFER_SEEN)) + empty!(XFER_SEEN) + return result + end +end + @testset "MPI" begin @testset "accelerate! idempotency and switch" begin @@ -520,6 +545,44 @@ end end end +@testset "Transferred cross-rank slots" begin + # A slot that cannot be left uninitialized is a transferred copy. As on the + # generic path, its payload must be Libc-backed on the receiving rank, or + # Datadeps' free at region end releases nothing and the copy lives until + # GC. Making the returned Chunk "Libc-backed" is not enough: `libc_backed` + # passes a Chunk through unchanged. + A = xfer_tile(16) + for (src, dst) in rank_pairs + src == dst && continue + @test !Dagger.slot_may_be_uninit(space_for_rank(src), space_for_rank(dst), typeof(A)) + obj = Dagger.tochunk(A, proc_for_rank(src), space_for_rank(src)) + slot = make_slot(fresh_cache(space_for_rank(dst)), src, dst, obj) + if rank == dst + val = Dagger.unwrap(slot) + @test val == A + @test Dagger.is_libc_allocated(val) + end + end + + if nranks > 1 + @testset "freed at region end, after its readers (hierarchical=$hierarchical)" for hierarchical in (false, true) + n, nreaders = 1000, 2 + v = Dagger.@spawn scope=rank_scope(0) xfer_tile(n) + kept = Vector{Dagger.DTask}(undef, nreaders) + Dagger.spawn_datadeps(; hierarchical) do + for i in 1:nreaders + kept[i] = Dagger.@spawn scope=rank_scope(1) xfer_keep!(In(v)) + end + end + # Every reader saw the whole payload, in a copy that was still + # allocated when it finished... + @test all(i -> fetch(kept[i]) == (n * (n + 1) / 2, true), 1:nreaders) + # ...and which the region freed once they were done. + @test fetch(Dagger.@spawn scope=rank_scope(1) xfer_seen_freed()) == (nreaders, true) + end + end +end + @testset "Partial slot currency" begin # The load-bearing invariant behind uninitialized slots: a task may read # spans that were never written in its own space, and those must be copied From ca11b25e7047c307b214dddfbc5e4382641f014e Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 28 Nov 2026 17:30:20 -0700 Subject: [PATCH 3/4] MPIExt: receive a CPU slot's payload straight into Libc memory Making a received slot freeable with `libc_backed` costs a second buffer: `recv_yield` first receives into an ordinary GC-managed `Array`, and `libc_backed` then copies it into Libc memory, leaving the first one as garbage. For a region whose slots are transferred, that is a GC allocation of every slot's full size, and the GC work to collect it. Let `recv_yield` take the allocator for its raw-bytes payload (default unchanged, `inplace_mpi_alloc`) and have the slot transfer pass `mpi_slot_alloc`, which allocates a dense isbits payload with `alloc_libc_array` when the destination is CPU memory. `libc_backed` then finds it already Libc-backed and returns it. A device destination keeps the ordinary host staging buffer: it is uploaded and dropped, and a Libc-backed one would only hide that memory from the GC. Other receives (task results, fetches) are never freed eagerly and keep the GC-managed default for the same reason. 2 ranks, rank 1 reading 16 transferred 8 MB tiles from rank 0 per region, 8 regions (after a warmup): GC-allocated bytes on rank 1 fell from 988.7 MiB to 12.2 MiB and GC runs from 35 to 0, with the same 122 MiB Libc high-water mark, freed at the end of every region. Co-Authored-By: Claude Opus 5.5 --- ext/MPIExt.jl | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 6bdb9c475..aa068553a 100644 --- a/ext/MPIExt.jl +++ b/ext/MPIExt.jl @@ -987,6 +987,17 @@ function Dagger.inplace_mpi_alloc(::Type{T}, shape::Tuple) where {T<:DenseArray} end Dagger.inplace_mpi_build(::Type{<:DenseArray}, (A,), ::Tuple) = A +# Host buffers for a Datadeps slot received into CPU memory. Datadeps frees its +# slot copies eagerly at region end, which only releases memory that is +# Libc-backed (see `libc_backed`), so a dense isbits payload is received +# straight into Libc memory: making it freeable afterwards would cost a second, +# GC-managed buffer of the same size plus a copy. Anything else is allocated as +# usual. Not the default allocator: an ordinary received value (a task result, +# a fetch) is never freed eagerly, and malloc'd memory exerts no GC pressure. +mpi_slot_alloc(::Type{T}, shape::Dims) where {T<:DenseArray} = + isbitstype(eltype(T)) ? (Dagger.alloc_libc_array(eltype(T), shape),) : inplace_mpi_alloc(T, shape) +mpi_slot_alloc(::Type{T}, header) where {T} = inplace_mpi_alloc(T, header) + function supports_inplace_mpi(value) if value isa DenseArray && isbitstype(eltype(value)) return true @@ -1069,7 +1080,9 @@ function recv_yield!(buffer, comm, src, tag) end end -function recv_yield(comm, src, tag) +# `alloc(T, header)` supplies the buffers a raw-bytes (`InplaceInfo`) payload is +# received into; see `mpi_slot_alloc` for why a caller would pick another one. +function recv_yield(comm, src, tag, alloc=inplace_mpi_alloc) rank = MPI.Comm_rank(comm) #Core.println("[rank $(MPI.Comm_rank(comm))][tag $tag] Starting recv from [$src]") @@ -1078,7 +1091,7 @@ function recv_yield(comm, src, tag) try value = recv_yield_serialized(comm, rank, src, tag) if value isa InplaceInfo - return recv_yield_inplace(value, comm, rank, src, tag) + return recv_yield_inplace(value, comm, rank, src, tag, alloc) end return value finally @@ -1130,9 +1143,9 @@ function _recv_yield_inplace_raw!(array, comm, my_rank, their_rank, tag) end end -function recv_yield_inplace(_value::InplaceInfo, comm, my_rank, their_rank, tag) +function recv_yield_inplace(_value::InplaceInfo, comm, my_rank, their_rank, tag, alloc=inplace_mpi_alloc) T = _value.type - parts = inplace_mpi_alloc(T, _value.header) + parts = alloc(T, _value.header) parts = map(p -> recv_yield_inplace!(p, comm, my_rank, their_rank, tag), parts) return inplace_mpi_build(T, parts, _value.header) end @@ -2083,9 +2096,13 @@ function mpi_endpoint_transfer(accel::MPIAcceleration, from_proc, to_proc, from_ send_yield(value, accel.comm, to_space.rank, tag) return tochunk(nothing, to_proc, to_space; type=T_new) elseif local_rank == to_space.rank - value = recv_yield(accel.comm, from_space.rank, tag) - # Dest: ingress move into this space's native storage + # Dest: receive a CPU slot's dense payload straight into Libc memory + # (see `mpi_slot_alloc`); a device slot's host staging stays ordinary + alloc = to_space.innerSpace isa CPURAMMemorySpace ? mpi_slot_alloc : inplace_mpi_alloc + value = recv_yield(accel.comm, from_space.rank, tag, alloc) + # Ingress move into this space's native storage value = move(from_mpi, to_mpi, value) + # A no-op for a payload received into Libc memory; covers any other value = Dagger.libc_backed(value) return tochunk(value, to_proc, to_space; type=T_new) else From cc6878c44aba0068940bfc4847acfc03da173708 Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 28 Nov 2026 17:31:06 -0700 Subject: [PATCH 4/4] AGENTS.md: make a value freeable where its payload lives, not on its handle Record why MPI's transferred slots were never freed eagerly: `libc_backed` was applied to the returned Chunk, which it passes through, and the uninitialized-slot path hid the problem for ordinary dense slots. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 61b1c43b8..92202acb4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1023,3 +1023,19 @@ lesson. and MPI paths. When diagnosing MPI uniformity errors, check for an earlier task failure that left ranks on different operations. Compare the actual Julia versions behind rolling CI labels such as `pre`. + +101. **Make a value freeable where its payload lives, not on its handle.** + `libc_backed` passes anything but a CPU isbits `Array` through unchanged, + a `Chunk` included, so MPIExt's `libc_backed(mpi_endpoint_transfer(...))` + looked like the generic slot path and did nothing: no slot transferred + over MPI was Libc-backed, and Datadeps' region-end `unsafe_free!` released + none of them. Nothing failed, because the uninitialized-slot path + (`slot_may_be_uninit`) already allocates ordinary dense isbits slots with + `alloc_libc_array`, so only transferred slots were affected (types that + decline uninit allocation, and by-value `NoAliasing` arguments). Check a + slot with `is_libc_allocated` on the rank that holds it, and force the + transfer path in tests with a test-local type for which `can_alloc_uninit` + is `false`. Receive such a payload straight into Libc memory + (`mpi_slot_alloc`) rather than copying it afterwards; the copy leaves a + GC-managed buffer of the same size behind, which for 16 8 MB slots per + region was 989 MiB of GC allocation and 35 collections over 8 regions.