From fb8effcf4750c80f1bd5a95b896c29fa674902f7 Mon Sep 17 00:00:00 2001 From: Anna-Lena Date: Sat, 21 Nov 2026 12:24:19 +0100 Subject: [PATCH 1/2] MPI: drop a plain task's ref-id counter when the task is done _MPIREF_TID holds one sub-counter per task id for the ref ids a task takes while it is moved and executed. It was reclaimed only at the end of a Datadeps region, so a program that only uses @spawn grew it by one entry per task for the whole session. A task that is not part of a Datadeps region (no tag option) takes ids only during its own move and execute!, never during planning, so its counter is dead once execute! returns: delete it there. Tagged (Datadeps) tasks keep the later cleanup, since their planning shares the key with their execution and reclaiming in between would collide on sub-ids. Test (test/mpi/spawn.jl): 300 plain tasks (unscoped, pinned, and with a body that may throw) must leave the table within 20 entries, and task ids stay uniform afterwards. At 2 ranks x 2 threads (MPICH) the test fails on upstream master (the table grows) and passes with this change. --- ext/MPIExt.jl | 21 +++++++++++++++++++++ test/mpi/spawn.jl | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 05d4f5eac..110f6b4e5 100644 --- a/ext/MPIExt.jl +++ b/ext/MPIExt.jl @@ -2425,7 +2425,28 @@ function mpi_execute_bcast_plan(f, args, proc::MPIProcessor) return (; need_type_bcast=false, nothrow, inferred) end +# A task that is not part of a Datadeps region (no tag option) generates ref ids only while it +# is moved to and executed on its processor, never during planning, so once `execute!` returns +# nobody takes another id for its thunk id and its sub-counter is dead. Without this the table +# grew by one entry per task until the next Datadeps `wait_all`, which a program that only uses +# `@spawn` never reaches. Datadeps tasks (tagged) keep the later cleanup: their planning shares +# the key with their execution, and reclaiming between the two would collide on sub-ids. function execute!(proc::MPIProcessor, f, args...; kwargs...) + if Dagger.in_task() + opts = Dagger.get_tls().task_spec.options + if opts !== nothing && opts.tag === nothing + tid = sch_handle().thunk_id.id + try + return mpi_execute_body(proc, f, args...; kwargs...) + finally + mpi_cleanup_tid(tid) + end + end + end + return mpi_execute_body(proc, f, args...; kwargs...) +end + +function mpi_execute_body(proc::MPIProcessor, f, args...; kwargs...) local_rank = MPI.Comm_rank(proc.comm) islocal = local_rank == proc.rank inplace_move = f === move! diff --git a/test/mpi/spawn.jl b/test/mpi/spawn.jl index fc74028bc..585bfc5e6 100644 --- a/test/mpi/spawn.jl +++ b/test/mpi/spawn.jl @@ -2,6 +2,8 @@ # progress at different speeds. Check actual execution, scopes, and locality. mpi_spawn_location(x=nothing) = (MPI.Comm_rank(MPI.COMM_WORLD), Threads.threadid(), x) mpi_spawn_value() = (sleep(0.05); 42) +mpi_spawn_value_fast(i) = 42 + i - i +mpi_spawn_checked(i::Int) = i > 0 ? i : error("negative") function check_spawn_placements(tasks, allowed, value=nothing) placements = Dagger.Processor[] @@ -84,3 +86,24 @@ end end end end + +# The ref-id bookkeeping of a task that is not part of a Datadeps region is dropped when the +# task is done; it used to grow by one entry per task for the whole session +reftid_size() = lock(d -> length(d), MPIExt._MPIREF_TID) +@testset "Ref-id table does not grow with plain tasks" begin + cpus = filter(p -> p.innerProc isa Dagger.ThreadProc, mpi_procs()) + foreach(fetch, [Dagger.@spawn(mpi_spawn_value_fast(i)) for i in 1:8]) # warm up + MPI.Barrier(comm) + before = reftid_size() + # unscoped, pinned to every rank in turn, and a body that may throw + foreach(wait, [Dagger.@spawn(mpi_spawn_value_fast(i)) for i in 1:100]) + foreach(wait, [Dagger.@spawn(scope=Dagger.ExactScope(cpus[mod1(i, length(cpus))]), mpi_spawn_value_fast(i)) for i in 1:100]) + foreach(wait, [Dagger.@spawn(mpi_spawn_checked(i)) for i in 1:100]) + MPI.Barrier(comm) + # Nothing is left behind by 300 tasks (a few entries may belong to tasks still finishing) + @test reftid_size() - before < 20 + # and ids are still uniform afterwards + t = Dagger.@spawn mpi_spawn_value_fast(1) + @test fetch(t) == 42 + @test allequal(MPI.Allgather(Int(t.uid), comm)) +end From 6b68d4dc4421977981daf8441ee9c32f1a511b13 Mon Sep 17 00:00:00 2001 From: Anna-Lena Date: Tue, 24 Nov 2026 20:59:58 +0100 Subject: [PATCH 2/2] MPI: review fixes for the ref-id cleanup (no in_task check, exact table size in the test) execute! is only ever called from inside a Dagger task, so the in_task() guard around the cleanup was dead code; read the task options directly. The regression test compared the table size with a slack of 20 entries. The cleanup runs in the task's own finally block, before its result is published, so once the tasks have been waited for nothing of theirs is left: require the size to be exactly what it was before. --- ext/MPIExt.jl | 16 +++++++--------- test/mpi/spawn.jl | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 110f6b4e5..4ed3b13e4 100644 --- a/ext/MPIExt.jl +++ b/ext/MPIExt.jl @@ -2432,15 +2432,13 @@ end # `@spawn` never reaches. Datadeps tasks (tagged) keep the later cleanup: their planning shares # the key with their execution, and reclaiming between the two would collide on sub-ids. function execute!(proc::MPIProcessor, f, args...; kwargs...) - if Dagger.in_task() - opts = Dagger.get_tls().task_spec.options - if opts !== nothing && opts.tag === nothing - tid = sch_handle().thunk_id.id - try - return mpi_execute_body(proc, f, args...; kwargs...) - finally - mpi_cleanup_tid(tid) - end + opts = Dagger.get_tls().task_spec.options + if opts !== nothing && opts.tag === nothing + tid = sch_handle().thunk_id.id + try + return mpi_execute_body(proc, f, args...; kwargs...) + finally + mpi_cleanup_tid(tid) end end return mpi_execute_body(proc, f, args...; kwargs...) diff --git a/test/mpi/spawn.jl b/test/mpi/spawn.jl index 585bfc5e6..5a87f385f 100644 --- a/test/mpi/spawn.jl +++ b/test/mpi/spawn.jl @@ -100,8 +100,8 @@ reftid_size() = lock(d -> length(d), MPIExt._MPIREF_TID) foreach(wait, [Dagger.@spawn(scope=Dagger.ExactScope(cpus[mod1(i, length(cpus))]), mpi_spawn_value_fast(i)) for i in 1:100]) foreach(wait, [Dagger.@spawn(mpi_spawn_checked(i)) for i in 1:100]) MPI.Barrier(comm) - # Nothing is left behind by 300 tasks (a few entries may belong to tasks still finishing) - @test reftid_size() - before < 20 + # Nothing is left behind by 300 tasks + @test reftid_size() == before # and ids are still uniform afterwards t = Dagger.@spawn mpi_spawn_value_fast(1) @test fetch(t) == 42