Repository navigation
MPI: free transferred Datadeps slots at region end - #838
Merged
Merged
Conversation
…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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
Contributor
Dagger benchmarks:
|
| Backend | Regressions | Improvements | Within noise | Inconclusive time | Suites |
|---|---|---|---|---|---|
| Threads | 0 | 1 | 10 | 3 | 4/4 |
| Distributed | 0 | 2 | 0 | 0 | 4/4 |
| Distributed+Threads | 0 | 1 | 0 | 2 | 4/4 |
| MPI | 0 | 1 | 1 | 1 | 4/4 |
| MPI+Threads | 2 | 1 | 18 | 1 | 4/4 |
| Total | 2 | 6 | 29 | 7 | 20/20 |
Regressions across all backends
| Backend | Benchmark | Metric | Change |
|---|---|---|---|
| MPI+Threads | linalg/dagger/N=1024 (block 512)/solve (A\b via lu) |
time | +88.5% |
| MPI+Threads | linalg/dagger/N=1024 (block 512)/cholesky |
time | +83.5% |
Improvements across all backends
| Backend | Benchmark | Metric | Change |
|---|---|---|---|
| Distributed+Threads | array/dagger/N=1024 (block 512)/alloc (rand) |
time | -63.9% |
| MPI+Threads | stencil/dagger/N=256 (block 256)/alloc (neighbors Wrap) |
time | -63.0% |
| Distributed | sparse/dagger/N=256 (block 16)/cg solve (laplacian) |
memory | -46.2% |
| Distributed | sparse/dagger/N=256 (block 16)/cg solve (laplacian) |
allocs | -43.2% |
| Threads | linalg/dagger/N=256 (block 128)/solve (A\b via lu) |
time | -40.1% |
| MPI | linalg/dagger/N=1024 (block 512)/syrk (A'*A) |
time | -36.6% |
Threads (1 process × 4 threads) — 0 regressions, 1 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 4 | 3 |
| linalg | 0 | 1 | 2 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 4 | 0 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=256 (block 128)/solve (A\b via lu) |
time | -40.1% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=256 (block 128)/assign (const) |
time | +96.5% |
stencil/dagger/N=256 (block 128)/alloc (neighbors Wrap) |
time | +57.0% |
array/dagger/N=256 (block 256)/reduce (sum) |
time | +53.2% |
linalg/dagger/N=1024 (block 512)/matvec (A*x) |
time | +49.2% |
linalg/dagger/N=256 (block 128)/matvec (A*x) |
time | +41.6% |
array/dagger/N=256 (block 128)/reduce (sum) |
time | +30.0% |
stencil/dagger/N=256 (block 128)/update (+) |
time | -26.3% |
array/dagger/N=1024 (block 512)/transpose (permutedims) |
time | -29.3% |
array/dagger/N=1024 (block 512)/alloc (rand) |
time | -32.6% |
stencil/dagger/N=256 (block 128)/multi-expr |
time | -34.1% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=1024 (block 512)/reduce (sum) |
time | +105.9% |
array/dagger/N=1024 (block 512)/norm |
time | +41.8% |
array/dagger/N=256 (block 128)/norm |
time | +30.5% |
Distributed (4 processes × 1 thread) — 0 regressions, 2 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 0 | 0 |
| linalg | 0 | 0 | 0 | 0 |
| sparse | 0 | 2 | 0 | 0 |
| stencil | 0 | 0 | 0 | 0 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
sparse/dagger/N=256 (block 16)/cg solve (laplacian) |
memory | -46.2% |
sparse/dagger/N=256 (block 16)/cg solve (laplacian) |
allocs | -43.2% |
Distributed+Threads (2 processes × 2 threads) — 0 regressions, 1 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 1 | 0 | 0 |
| linalg | 0 | 0 | 0 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 0 | 2 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=1024 (block 512)/alloc (rand) |
time | -63.9% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=1024 (block 512)/neighbors (Wrap) |
time | +71.5% |
stencil/dagger/N=1024 (block 512)/neighbors (Pad) |
time | +63.5% |
MPI (4 ranks × 1 thread) — 0 regressions, 1 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 0 | 0 |
| linalg | 0 | 1 | 1 | 0 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 0 | 0 | 1 |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=1024 (block 512)/syrk (A'*A) |
time | -36.6% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=256 (block 128)/lu |
time | -43.0% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=1024 (block 128)/neighbors (Reflect) |
time | +59.9% |
MPI+Threads (2 ranks × 2 threads) — 2 regressions, 1 improvements
| Suite | Regressions | Improvements | Within noise | Inconclusive time |
|---|---|---|---|---|
| array | 0 | 0 | 7 | 0 |
| linalg | 2 | 0 | 3 | 1 |
| sparse | 0 | 0 | 0 | 0 |
| stencil | 0 | 1 | 8 | 0 |
Regressions
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=1024 (block 512)/solve (A\b via lu) |
time | +88.5% |
linalg/dagger/N=1024 (block 512)/cholesky |
time | +83.5% |
Improvements
| Benchmark | Metric | Change |
|---|---|---|
stencil/dagger/N=256 (block 256)/alloc (neighbors Wrap) |
time | -63.0% |
Within noise
| Benchmark | Metric | Change |
|---|---|---|
array/dagger/N=1024 (block 512)/reduce (sum) |
time | +187.7% |
stencil/dagger/N=1024 (block 512)/update (+) |
time | +140.1% |
stencil/dagger/N=256 (block 256)/update (+) |
time | +97.1% |
stencil/dagger/N=256 (block 128)/alloc (neighbors Wrap) |
time | +78.0% |
linalg/dagger/N=256 (block 256)/matmul (A*A) |
time | +70.0% |
stencil/dagger/N=1024 (block 512)/assign (const) |
time | +68.9% |
array/dagger/N=256 (block 128)/add (X + X) |
time | +56.1% |
stencil/dagger/N=256 (block 128)/neighbors (Pad) |
time | +55.0% |
linalg/dagger/N=256 (block 128)/matvec (A*x) |
time | +50.4% |
array/dagger/N=1024 (block 512)/norm |
time | +46.2% |
array/dagger/N=256 (block 128)/alloc (rand) |
time | +41.9% |
array/dagger/N=1024 (block 512)/broadcast (X .+ 1) |
time | +41.6% |
linalg/dagger/N=1024 (block 512)/matvec (A*x) |
time | +38.4% |
stencil/dagger/N=1024 (block 512)/neighbors (Clamp) |
time | -36.1% |
array/dagger/N=256 (block 128)/reduce (sum) |
time | -48.6% |
array/dagger/N=256 (block 128)/broadcast (X .+ 1) |
time | -51.4% |
stencil/dagger/N=256 (block 256)/assign (const) |
time | -58.6% |
stencil/dagger/N=256 (block 128)/multi-expr |
time | -60.2% |
Inconclusive timing changes
| Benchmark | Metric | Change |
|---|---|---|
linalg/dagger/N=1024 (block 512)/lu |
time | +47.0% |
Full results and plots (download the benchmark-results-* artifacts).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
Under MPI, a Datadeps slot that is transferred from another rank is never freed when the region ends. It stays alive until a garbage collection happens to reclaim it. A long run with many such slots keeps a region's worth of copies per region, until the GC catches up.
On master this is latent for ordinary dense arrays and live for everything else that takes the transfer path:
slot_may_be_uninit) allocates them on the destination withalloc_libc_array, so they were already freed correctly. A DArray matmul and Cholesky behave the same before and after this PR (numbers below).can_alloc_uninitisfalse), and, with Datadeps: follow-ups to #792 (NoAliasing slots) #834,NoAliasingarguments, which that PR passes by value.Cause
Datadeps frees its slot copies with
unsafe_free!. That only releases a CPUArrayearly if its memory came fromLibc.malloc(libc_backed,src/utils/libc-array.jl); anything else waits for GC.The generic slot transfer gets this right:
tochunk(libc_backed(move(...)))wraps the moved array. MPI'smove_rewrapinstead wrapped the result ofmpi_endpoint_transfer, which is aChunk, a handle that every rank gets.libc_backedpasses aChunkthrough unchanged (libc_backed(@nospecialize(x)) = x). So the call looked right and did nothing.Fix
5c37af63).mpi_endpoint_transferapplieslibc_backedto the received value on the destination rank, and to a cross-space move on the owner when both spaces are on one rank. When the two spaces are the same,movereturns the value itself, so the value is not copied (it is the user's data). Only local storage on that rank changes. The Chunk's type and the messages sent are the same on every rank, so the ranks still make the same decisions.libc_backeddoes nothing to device arrays, so GPU slots are unaffected.ca11b25e). Step 1 alone costs an extra buffer:recv_yieldreceives into an ordinary GC-managed array, andlibc_backedthen copies it, leaving the first buffer as garbage.recv_yieldnow takes the allocator for its raw-bytes payload. The default is unchanged. The slot transfer passesmpi_slot_alloc, which allocates a dense isbits payload withalloc_libc_arraywhen the destination is CPU memory. A GPU destination keeps the ordinary host staging buffer: it is uploaded and then dropped, and making it Libc-backed would only hide it from the GC. Other receives (task results, fetches) keep the default for the same reason: nothing frees them early.Tests
A new MPI CPU testset, "Transferred cross-rank slots" (
58f998a5), uses a test-local isbits element type whosecan_alloc_uninitisfalse. That forces the transfer path. It checks that:On master it fails 3 of 8 checks. With the fix it passes 8 of 8.
julia --project=<env> test/mpi/run.jl 2 2 test/mpi/cpu.jljulia --project=<env> test/mpi/run.jl 4 2 test/mpi/cpu.jl--test datadeps--test allocationsInterruptExceptionfrom the eager scheduler is logged at process exit, after the summary)ext/MPIExt.jlis the only source file changed, anddatadepsandallocationsdo not load it. They are there to show nothing else moved. The GPU MPI suites were not run here. GPU destinations still use the old receive allocator, andlibc_backeddoes nothing to device arrays.Measurements
2 ranks × 2 threads, Julia 1.12.6. Rank 1 reads 16 tiles of 8 MB each, owned by rank 0, in each of 8 regions (after a warmup). The tiles' element type declines uninitialized slots, so every slot is transferred. Figures are from rank 1, after the 8th region.
gc_live_bytes)On master the transferred copies pile up in the GC heap: these are 8 MB objects, and GC does not collect them between regions. With the fix, each region's 122 MiB is released when the region ends. With step 1 alone, the copies are freed, but every slot also leaves a garbage buffer the size of its payload, which is what drives the 35 collections.
An ordinary workload is unchanged, as expected, because its slots already come from the uninitialized-slot path. DArray
A*Aplus Cholesky, 2048², 512 tiles: Libc high-water mark 16 / 32 MiB (rank 0 / 1), 20 / 56 MiB allocated in total, all freed, on both master and this PR. GC-allocated bytes are the same within noise (268 / 217 MiB on master, 268 / 211 MiB here).Relation to #834
#834 (
jps/review/pr792) passesNoAliasingarguments by value, which sends isbitsNoAliasingarrays down this transfer path under MPI. Its MPI free test (test/mpi/noaliasing_args.jl, "unkeyed copies are freed") currently expectsfreeable=falseunder MPI because of this bug. The two PRs are independent. Whichever merges second should flip that expectation to the defaultfreeable=true. Both PRs also append a lesson toAGENTS.md; this one is numbered 101 to avoid a clash, but the two appends touch the same end of the file.🤖 Generated with Claude Code