Skip to content

MPI: free transferred Datadeps slots at region end - #838

Merged
jpsamaroo merged 5 commits into
masterfrom
jps/mpi-libc-backed-slots
Nov 29, 2026
Merged

jpsamaroo merged 5 commits into
masterfrom
jps/mpi-libc-backed-slots

Conversation

@jpsamaroo

Copy link
Copy Markdown
Member

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:

  • Ordinary dense isbits CPU slots are not transferred at all. The uninitialized-slot path (slot_may_be_uninit) allocates them on the destination with alloc_libc_array, so they were already freed correctly. A DArray matmul and Cholesky behave the same before and after this PR (numbers below).
  • Slots that must be transferred: types that decline uninitialized allocation (can_alloc_uninit is false), and, with Datadeps: follow-ups to #792 (NoAliasing slots) #834, NoAliasing arguments, which that PR passes by value.

Cause

Datadeps frees its slot copies with unsafe_free!. That only releases a CPU Array early if its memory came from Libc.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's move_rewrap instead wrapped the result of mpi_endpoint_transfer, which is a Chunk, a handle that every rank gets. libc_backed passes a Chunk through unchanged (libc_backed(@nospecialize(x)) = x). So the call looked right and did nothing.

Fix

  1. Make the payload freeable on the rank that holds it (5c37af63). mpi_endpoint_transfer applies libc_backed to 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, move returns 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_backed does nothing to device arrays, so GPU slots are unaffected.
  2. Receive straight into Libc memory (ca11b25e). Step 1 alone costs an extra buffer: recv_yield receives into an ordinary GC-managed array, and libc_backed then copies it, leaving the first buffer as garbage. recv_yield now takes the allocator for its raw-bytes payload. The default is unchanged. The slot transfer passes mpi_slot_alloc, which allocates a dense isbits payload with alloc_libc_array when 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 whose can_alloc_uninit is false. That forces the transfer path. It checks that:

  • the received slot is Libc-backed on the destination rank, for every rank pair;
  • inside a region, readers see the whole payload, and it is still allocated when they finish;
  • the region frees it once they are done, with both the flat and the hierarchical planner.

On master it fails 3 of 8 checks. With the fix it passes 8 of 8.

Run Result
julia --project=<env> test/mpi/run.jl 2 2 test/mpi/cpu.jl pass: 1151 and 1154 assertions on the two ranks, exit 0
julia --project=<env> test/mpi/run.jl 4 2 test/mpi/cpu.jl pass: 1503 to 1518 assertions per rank, "MPI suite OK" on all 4, exit 0
--test datadeps pass: 1533 passed, 2 broken (already marked), exit 0
--test allocations pass: 24 of 24, exit 0 (an InterruptException from the eager scheduler is logged at process exit, after the summary)

ext/MPIExt.jl is the only source file changed, and datadeps and allocations do 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, and libc_backed does 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.

master step 1 only this PR
Libc memory still live after each region 0 (never Libc-backed) 0 0
Libc high-water mark 0 122 MiB (one region) 122 MiB
Live GC heap (gc_live_bytes) 1022 MiB, growing ~122 MiB per region 68 MiB 53 MiB
GC-allocated during the 8 regions 989 MiB 989 MiB 12 MiB
GC runs during the 8 regions 4 35 0
Peak RSS of rank 1 1884 MiB 1163 MiB 1051 MiB

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*A plus 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) passes NoAliasing arguments by value, which sends isbits NoAliasing arrays down this transfer path under MPI. Its MPI free test (test/mpi/noaliasing_args.jl, "unkeyed copies are freed") currently expects freeable=false under MPI because of this bug. The two PRs are independent. Whichever merges second should flip that expectation to the default freeable=true. Both PRs also append a lesson to AGENTS.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

jpsamaroo and others added 4 commits November 28, 2026 16:30
…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>
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Dagger benchmarks: dirty vs master

Counts are benchmark metrics (time, allocations, bytes). Timing changes require non-overlapping median ± IQR bands; both revisions need at least five timed samples, and timing regressions need an independent confirmation run.

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).

@jpsamaroo
jpsamaroo merged commit e642b52 into master Nov 29, 2026
50 of 56 checks passed
@jpsamaroo
jpsamaroo deleted the jps/mpi-libc-backed-slots branch November 29, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant