From 1831d199f1ce27cc1f35a4f778a977a4e5489104 Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Mon, 30 Nov 2026 18:56:35 -0700 Subject: [PATCH 1/2] Keep MPI alias batch indices in first argument encounter order Argument hashes can contain process-local type hashes, so Dict values can put different owners at the same collective index on different ranks. Reconstruct first occurrence order from task metadata with reusable UInt membership scratch, leaving Distributed batching unchanged. Exact master and AOT-parent two-rank copy/collect repros fail before. The repair passes84 mixed-type and identity/upper/lower dependency assertions with uniformity checks enabled, plus3 deterministic ordering/inference assertions. Five warmed1000-call profiles at4/64/256 unique arguments retain exactly the baseline allocation counts and bytes; scanning1024 encounters adds about4.6microseconds versus collecting the256 unique entries on this host. Written by an AI agent on jpsamaroo's review feedback. --- AGENTS.md | 3 +++ src/datadeps/hierarchical.jl | 30 ++++++++++++++++++++---- test/datadeps.jl | 27 ++++++++++++++++++++++ test/mpi/alias_ordering.jl | 45 ++++++++++++++++++++++++++++++++++++ test/mpi/cpu.jl | 1 + 5 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 test/mpi/alias_ordering.jl diff --git a/AGENTS.md b/AGENTS.md index ad8c5740e..028a756ca 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1217,3 +1217,6 @@ this section verbatim). Each entry is one bullet starting with `- `. the protocol alone is not enough for them. Test with a remote reader *and* a remote writer, in both orders: in one process every slot is the argument itself, so none of this is visible. + + +109. **MPI batch indices must follow the task walk, not a Dict's iteration.** `batch_ainfos` broadcasts answers by vector index. Chunk and argument hashes may include process-local type hashes, so collecting `values(unique_arg_ws)` can put different owners at the same index on different ranks. A two-rank copy followed by `collect` reproduced reciprocal missing-alias answers on master. Reconstruct first-seen argument order from the already ordered task metadata, using reusable membership scratch only for deduplication. Mixed element types and several dependency modifiers for the same chunk must share that ordering too. diff --git a/src/datadeps/hierarchical.jl b/src/datadeps/hierarchical.jl index d7b41b6ec..df4bc5a15 100644 --- a/src/datadeps/hierarchical.jl +++ b/src/datadeps/hierarchical.jl @@ -834,16 +834,36 @@ function _collect_arg_value!(all_deps::Vector{HierarchicalTaskInfo}, return arg_chunk end +# Dict iteration is rank-local: argument hashes can contain process-local type +# hashes. Reconstruct first occurrence order from the uniform task/argument walk +# before using vector indices as collective object identities. +function ordered_aliasing_args(unique_arg_ws, task_metas) + ordered = Vector{ArgumentWrapper}(undef, length(unique_arg_ws)) + seen = @reusable_dict(HierarchicalAliasOrderSeen, UInt, Nothing, zero(UInt), nothing, 128) + count = 0 + for meta in task_metas, dep in meta.deps + arg_w = dep.arg_w + haskey(seen, arg_w.hash) && continue + seen[arg_w.hash] = nothing + count += 1 + ordered[count] = unique_arg_ws[arg_w] + end + @assert count == length(ordered) + return ordered +end + """ - build_aliasing_parallel(unique_arg_ws) -> (lookup, ainfos_overlaps, arg_to_ainfo) + build_aliasing_parallel(unique_arg_ws, task_metas=nothing) -> (lookup, ainfos_overlaps, arg_to_ainfo) Phase 1: Computes `AliasingWrapper` for every unique `ArgumentWrapper` in parallel. On each worker, threads are used to compute aliasing info for local data. Results are gathered and reduced into a single `AliasingLookup` with -overlap information. +overlap information. Under uniform execution, `task_metas` supplies the original +argument encounter order used to index the collective batch. """ -function build_aliasing_parallel(unique_arg_ws::Dict{ArgumentWrapper, ArgumentWrapper}) - arg_ws_vec = collect(values(unique_arg_ws)) +function build_aliasing_parallel(unique_arg_ws::Dict{ArgumentWrapper, ArgumentWrapper}, task_metas=nothing) + arg_ws_vec = uniform_execution() && task_metas !== nothing ? + ordered_aliasing_args(unique_arg_ws, task_metas) : collect(values(unique_arg_ws)) by_worker = Dict{Int, Vector{ArgumentWrapper}}() for arg_w in arg_ws_vec @@ -1707,7 +1727,7 @@ function _distribute_tasks_hierarchical!(queue::DataDepsTaskQueue, # Phase 1: Collect arguments and compute aliasing in parallel task_metas, unique_arg_ws = @hier_phase prescan collect_aliased_args(seen_tasks) _lookup, ainfos_overlaps, arg_to_ainfo = - @hier_phase aliasing build_aliasing_parallel(unique_arg_ws) + @hier_phase aliasing build_aliasing_parallel(unique_arg_ws, task_metas) # Phase 2: Build dependency DAG dag = @hier_phase dag build_dependency_dag(task_metas, arg_to_ainfo, ainfos_overlaps) diff --git a/test/datadeps.jl b/test/datadeps.jl index a97720a5e..5f37a4208 100644 --- a/test/datadeps.jl +++ b/test/datadeps.jl @@ -1685,3 +1685,30 @@ end end end end + + +@testset "Collective alias arguments follow first encounter order" begin + A=ones(2,2) + B=Float32[1,2] + C=ComplexF64[1+im 2;3 4-im] + a,b,c = Dagger.tochunk(A),Dagger.tochunk(B),Dagger.tochunk(C) + wrappers = Dagger.ArgumentWrapper[ + Dagger.ArgumentWrapper(a,identity), + Dagger.ArgumentWrapper(a,UpperTriangular), + Dagger.ArgumentWrapper(a,LowerTriangular), + Dagger.ArgumentWrapper(b,identity), + Dagger.ArgumentWrapper(c,identity)] + unique_args=Dict(w=>w for w in wrappers) + # Deliberately choose a task walk different from this Dict's iteration. + expected=reverse(collect(values(unique_args))) + task=Dagger.DTask(rand(UInt),Dagger.ThunkFuture(),Dagger.DTaskMetadata(Nothing)) + pair=Dagger.DTaskPair(Dagger.DTaskSpec((Dagger.TypedArgument(1,identity),),Dagger.Options()),task) + dependencies(ws)=[Dagger.HierarchicalTaskInfo(w,true,false) for w in ws] + metas=Dagger.HierarchicalTaskMeta[ + Dagger.HierarchicalTaskMeta(pair,nothing,true,true,dependencies(expected[1:3]),Int[]), + Dagger.HierarchicalTaskMeta(pair,nothing,true,true,dependencies(expected[[1,4,2,5]]),Int[])] + ordered=@inferred Dagger.ordered_aliasing_args(unique_args,metas) + @test ordered==expected + @test length(ordered)==length(unique_args) + @test ordered!=collect(values(unique_args)) +end diff --git a/test/mpi/alias_ordering.jl b/test/mpi/alias_ordering.jl new file mode 100644 index 000000000..927398493 --- /dev/null +++ b/test/mpi/alias_ordering.jl @@ -0,0 +1,45 @@ +# Batch indices follow the task walk, rather than dictionary iteration. +# cpu.jl runs this with its ordinary uniformity checks enabled. +using Dagger, MPI, LinearAlgebra, Test + +alias_order_upper_sum(A) = sum(UpperTriangular(A)) +alias_order_lower_sum(A) = sum(LowerTriangular(A)) + +@testset "MPI mixed alias batch order" begin + hosts = Any[] + inputs = Any[] + outputs = Any[] + for T in (Float32,Float64,ComplexF64) + host = reshape(T.(1:16),4,4) + T <: Complex && (host .*= T(1+0.25im)) + push!(hosts,host) + push!(inputs,Dagger.DArray(host,Dagger.Blocks(2,2))) + push!(outputs,Dagger.zeros(Dagger.Blocks(2,2),T,4,4)) + end + foreach(wait,inputs) + foreach(wait,outputs) + for hierarchical in (false,true) + Dagger.spawn_datadeps(;hierarchical) do + for (A,B) in zip(inputs,outputs), i in eachindex(A.chunks) + Dagger.@spawn copyto!(Dagger.Out(B.chunks[i]),Dagger.In(A.chunks[i])) + end + end + for (B,host) in zip(outputs,hosts) + @test collect(B) ≈ host + end + end + tasks = Dagger.DTask[] + expected = Any[] + Dagger.spawn_datadeps(;hierarchical=true) do + for (A,host) in zip(inputs,hosts), I in CartesianIndices(A.chunks) + tile=view(host,(2I[1]-1):(2I[1]),(2I[2]-1):(2I[2])) + for (f,modifier) in ((sum,identity),(alias_order_upper_sum,UpperTriangular),(alias_order_lower_sum,LowerTriangular)) + push!(tasks,Dagger.@spawn f(Dagger.Deps(A.chunks[I],Dagger.In(modifier)))) + push!(expected,sum(modifier(tile))) + end + end + end + for (task,value) in zip(tasks,expected) + @test fetch(task) ≈ value + end +end diff --git a/test/mpi/cpu.jl b/test/mpi/cpu.jl index e950e75de..4d2c0e754 100644 --- a/test/mpi/cpu.jl +++ b/test/mpi/cpu.jl @@ -47,6 +47,7 @@ include(joinpath(@__DIR__, "..", "array", "sparse_defs.jl")) Dagger.accelerate!(:mpi) Dagger.check_uniformity!(true) +include("alias_ordering.jl") const comm = MPI.COMM_WORLD const rank = MPI.Comm_rank(comm) const nranks = MPI.Comm_size(comm) From 8460e6b3bc393d52485e8541c385fd63227f1b2d Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Tue, 1 Dec 2026 20:36:07 -0700 Subject: [PATCH 2/2] Format alias-ordering tests and wrap the MPI ordering lesson Use consistent operator and argument spacing, readable tuple/vector layouts, and the surrounding Markdown indentation so the regression tests and lesson are easy to review. Both test files retain identical parsed Julia expressions after removing line-location metadata; AGENTS.md retains the same words. No numerical or production code changes. Written by an AI agent on jpsamaroo's review feedback. --- AGENTS.md | 11 +++++++-- test/datadeps.jl | 46 ++++++++++++++++++++------------------ test/mpi/alias_ordering.jl | 42 +++++++++++++++++----------------- 3 files changed, 55 insertions(+), 44 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 028a756ca..db72c1e22 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1218,5 +1218,12 @@ this section verbatim). Each entry is one bullet starting with `- `. *and* a remote writer, in both orders: in one process every slot is the argument itself, so none of this is visible. - -109. **MPI batch indices must follow the task walk, not a Dict's iteration.** `batch_ainfos` broadcasts answers by vector index. Chunk and argument hashes may include process-local type hashes, so collecting `values(unique_arg_ws)` can put different owners at the same index on different ranks. A two-rank copy followed by `collect` reproduced reciprocal missing-alias answers on master. Reconstruct first-seen argument order from the already ordered task metadata, using reusable membership scratch only for deduplication. Mixed element types and several dependency modifiers for the same chunk must share that ordering too. +109. **MPI batch indices must follow the task walk, not a Dict's iteration.** + `batch_ainfos` broadcasts answers by vector index. Chunk and argument hashes + may include process-local type hashes, so collecting `values(unique_arg_ws)` + can put different owners at the same index on different ranks. A two-rank + copy followed by `collect` reproduced reciprocal missing-alias answers on + master. Reconstruct first-seen argument order from the already ordered task + metadata, using reusable membership scratch only for deduplication. Mixed + element types and several dependency modifiers for the same chunk must share + that ordering too. diff --git a/test/datadeps.jl b/test/datadeps.jl index 5f37a4208..5b959be6b 100644 --- a/test/datadeps.jl +++ b/test/datadeps.jl @@ -1686,29 +1686,31 @@ end end end - @testset "Collective alias arguments follow first encounter order" begin - A=ones(2,2) - B=Float32[1,2] - C=ComplexF64[1+im 2;3 4-im] - a,b,c = Dagger.tochunk(A),Dagger.tochunk(B),Dagger.tochunk(C) + A = ones(2, 2) + B = Float32[1, 2] + C = ComplexF64[1 + im 2; 3 4 - im] + a, b, c = Dagger.tochunk(A), Dagger.tochunk(B), Dagger.tochunk(C) wrappers = Dagger.ArgumentWrapper[ - Dagger.ArgumentWrapper(a,identity), - Dagger.ArgumentWrapper(a,UpperTriangular), - Dagger.ArgumentWrapper(a,LowerTriangular), - Dagger.ArgumentWrapper(b,identity), - Dagger.ArgumentWrapper(c,identity)] - unique_args=Dict(w=>w for w in wrappers) + Dagger.ArgumentWrapper(a, identity), + Dagger.ArgumentWrapper(a, UpperTriangular), + Dagger.ArgumentWrapper(a, LowerTriangular), + Dagger.ArgumentWrapper(b, identity), + Dagger.ArgumentWrapper(c, identity), + ] + unique_args = Dict(w => w for w in wrappers) # Deliberately choose a task walk different from this Dict's iteration. - expected=reverse(collect(values(unique_args))) - task=Dagger.DTask(rand(UInt),Dagger.ThunkFuture(),Dagger.DTaskMetadata(Nothing)) - pair=Dagger.DTaskPair(Dagger.DTaskSpec((Dagger.TypedArgument(1,identity),),Dagger.Options()),task) - dependencies(ws)=[Dagger.HierarchicalTaskInfo(w,true,false) for w in ws] - metas=Dagger.HierarchicalTaskMeta[ - Dagger.HierarchicalTaskMeta(pair,nothing,true,true,dependencies(expected[1:3]),Int[]), - Dagger.HierarchicalTaskMeta(pair,nothing,true,true,dependencies(expected[[1,4,2,5]]),Int[])] - ordered=@inferred Dagger.ordered_aliasing_args(unique_args,metas) - @test ordered==expected - @test length(ordered)==length(unique_args) - @test ordered!=collect(values(unique_args)) + expected = reverse(collect(values(unique_args))) + task = Dagger.DTask(rand(UInt), Dagger.ThunkFuture(), Dagger.DTaskMetadata(Nothing)) + pair = Dagger.DTaskPair( + Dagger.DTaskSpec((Dagger.TypedArgument(1, identity),), Dagger.Options()), task) + dependencies(ws) = [Dagger.HierarchicalTaskInfo(w, true, false) for w in ws] + metas = Dagger.HierarchicalTaskMeta[ + Dagger.HierarchicalTaskMeta(pair, nothing, true, true, dependencies(expected[1:3]), Int[]), + Dagger.HierarchicalTaskMeta(pair, nothing, true, true, dependencies(expected[[1, 4, 2, 5]]), Int[]), + ] + ordered = @inferred Dagger.ordered_aliasing_args(unique_args, metas) + @test ordered == expected + @test length(ordered) == length(unique_args) + @test ordered != collect(values(unique_args)) end diff --git a/test/mpi/alias_ordering.jl b/test/mpi/alias_ordering.jl index 927398493..00d9bb960 100644 --- a/test/mpi/alias_ordering.jl +++ b/test/mpi/alias_ordering.jl @@ -9,37 +9,39 @@ alias_order_lower_sum(A) = sum(LowerTriangular(A)) hosts = Any[] inputs = Any[] outputs = Any[] - for T in (Float32,Float64,ComplexF64) - host = reshape(T.(1:16),4,4) - T <: Complex && (host .*= T(1+0.25im)) - push!(hosts,host) - push!(inputs,Dagger.DArray(host,Dagger.Blocks(2,2))) - push!(outputs,Dagger.zeros(Dagger.Blocks(2,2),T,4,4)) + for T in (Float32, Float64, ComplexF64) + host = reshape(T.(1:16), 4, 4) + T <: Complex && (host .*= T(1 + 0.25im)) + push!(hosts, host) + push!(inputs, Dagger.DArray(host, Dagger.Blocks(2, 2))) + push!(outputs, Dagger.zeros(Dagger.Blocks(2, 2), T, 4, 4)) end - foreach(wait,inputs) - foreach(wait,outputs) - for hierarchical in (false,true) - Dagger.spawn_datadeps(;hierarchical) do - for (A,B) in zip(inputs,outputs), i in eachindex(A.chunks) - Dagger.@spawn copyto!(Dagger.Out(B.chunks[i]),Dagger.In(A.chunks[i])) + foreach(wait, inputs) + foreach(wait, outputs) + for hierarchical in (false, true) + Dagger.spawn_datadeps(; hierarchical) do + for (A, B) in zip(inputs, outputs), i in eachindex(A.chunks) + Dagger.@spawn copyto!(Dagger.Out(B.chunks[i]), Dagger.In(A.chunks[i])) end end - for (B,host) in zip(outputs,hosts) + for (B, host) in zip(outputs, hosts) @test collect(B) ≈ host end end tasks = Dagger.DTask[] expected = Any[] - Dagger.spawn_datadeps(;hierarchical=true) do - for (A,host) in zip(inputs,hosts), I in CartesianIndices(A.chunks) - tile=view(host,(2I[1]-1):(2I[1]),(2I[2]-1):(2I[2])) - for (f,modifier) in ((sum,identity),(alias_order_upper_sum,UpperTriangular),(alias_order_lower_sum,LowerTriangular)) - push!(tasks,Dagger.@spawn f(Dagger.Deps(A.chunks[I],Dagger.In(modifier)))) - push!(expected,sum(modifier(tile))) + Dagger.spawn_datadeps(; hierarchical=true) do + for (A, host) in zip(inputs, hosts), I in CartesianIndices(A.chunks) + tile = view(host, (2I[1] - 1):(2I[1]), (2I[2] - 1):(2I[2])) + for (f, modifier) in ((sum, identity), + (alias_order_upper_sum, UpperTriangular), + (alias_order_lower_sum, LowerTriangular)) + push!(tasks, Dagger.@spawn f(Dagger.Deps(A.chunks[I], Dagger.In(modifier)))) + push!(expected, sum(modifier(tile))) end end end - for (task,value) in zip(tasks,expected) + for (task, value) in zip(tasks, expected) @test fetch(task) ≈ value end end