diff --git a/ext/MPIExt.jl b/ext/MPIExt.jl index 05d4f5eac..4ed3b13e4 100644 --- a/ext/MPIExt.jl +++ b/ext/MPIExt.jl @@ -2425,7 +2425,26 @@ 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...) + 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...) +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..5a87f385f 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 + @test reftid_size() == before + # 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