diff --git a/AGENTS.md b/AGENTS.md index 661556d0f..03120cce2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1092,3 +1092,19 @@ this section verbatim). Each entry is one bullet starting with `- `. and the copy tasks' start and end times found it in one run. Test planner-ordering invariants directly on `DataDepsState`; end-to-end tests catch them only at whatever rate the timing allows. + +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. diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 0d2d640ab..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 @@ -2031,6 +2044,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 +2065,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) @@ -2069,9 +2096,14 @@ 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 return tochunk(nothing, to_proc, to_space; type=T_new) @@ -2146,8 +2178,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 diff --git a/test/mpi/cpu.jl b/test/mpi/cpu.jl index 6ba4aded8..78ae0d054 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