diff --git a/AGENTS.md b/AGENTS.md index 3977148ca..199750168 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1071,7 +1071,7 @@ this section verbatim). Each entry is one bullet starting with `- `. reasoning about which arguments Datadeps touches, check `type_may_alias`, not `aliasing`. A single-process `spawn_datadeps` with two such values reproduces it; it is not MPI-specific. - + 100. **An argument's write history must stay sorted by write number.** `compute_remainder_for_arg!` walks `arg_history` backwards and treats each entry as newer than the ones before it. It uses each entry both to choose @@ -1253,7 +1253,7 @@ this section verbatim). Each entry is one bullet starting with `- `. A launcher can therefore be instrumented while the extension exercised by every rank reports zero hits. Forward the enabled mode/path and merge the PID-qualified rank reports. - + 113. **Threaded test RPCs need ready peer connections.** Julia 1.11's stdlib `Distributed` creates its lazy peer condition on the worker's initial thread; the first peer RPC from another thread can throw `lock must be @@ -1263,7 +1263,7 @@ this section verbatim). Each entry is one bullet starting with `- `. production transport defaults must stay unchanged. Every test worker addition must use the same setting: both transports reject mixing lazy and eager workers in one cluster. - + 114. **LU singularity is an exact zero-pivot condition, not an epsilon test.** The fallback pivot search marked a nonzero column maximum at or below `eps(T)` as singular, rejecting an invertible matrix merely because it @@ -1279,7 +1279,7 @@ this section verbatim). Each entry is one bullet starting with `- `. Honor that flag before raising a positive pivot error, and retain the global singular-column metadata. Check the public distributed LU call on the older runtime: modern-runtime tests only exercise the other branch. - + 116. **A backend's generic scan and native product scan may initialize differently.** AMDGPU and oneAPI default generic `accumulate` to a zero initializer, even for multiplication; their native `cumprod` supplies the @@ -1304,7 +1304,7 @@ this section verbatim). Each entry is one bullet starting with `- `. Never repair this by modifying installed package caches or disabling GPU validation, and keep backend-only preflight and Dagger integration in separate processes to isolate process-level state during diagnosis. - + 118. **Span offsets must use the backend's address domain.** An array's `pointer` need not convert to an integer: Metal before 1.10 returns an opaque `MtlPtr`, while its `data_address` method supplies the numeric @@ -1314,7 +1314,7 @@ this section verbatim). Each entry is one bullet starting with `- `. serialization, hiding the original conversion error. An opaque-pointer CPU fixture can protect the address-hook contract; it does not replace native backend validation. - + 119. **Choose GPU test scalar types from the selected device's capability.** A backend can expose both Float64-capable and incapable devices. A Float32 array with a `0.0` scan initializer widens to Float64, so a test @@ -1325,7 +1325,7 @@ this section verbatim). Each entry is one bullet starting with `- `. coincidence. Test those fields with different values. Keep the Float64 case on capable hardware and run the same behavioral assertion with a supported initializer elsewhere; a vendor-wide skip loses coverage on devices that support it. - + 120. **`type_may_alias(T) == false` is a promise, and a non-concrete field cannot make it.** Datadeps passes such values through untracked, with no reader/writer ordering. `type_may_alias` used to answer `false` for any @@ -1380,3 +1380,13 @@ this section verbatim). Each entry is one bullet starting with `- `. retaining `memory_spans` as the extension fallback. Preserve its exact emptiness semantics: a zero-length contiguous span is still one span, but a strided view with an empty outer dimension has none. + +125. **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 8325da65e..5dea7f921 100644 --- a/src/datadeps/hierarchical.jl +++ b/src/datadeps/hierarchical.jl @@ -838,16 +838,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 @@ -1711,7 +1731,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 fb2fb1e22..10aca588e 100644 --- a/test/datadeps.jl +++ b/test/datadeps.jl @@ -1728,3 +1728,32 @@ end end include("datadeps/slot_metadata.jl") + +@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..00d9bb960 --- /dev/null +++ b/test/mpi/alias_ordering.jl @@ -0,0 +1,47 @@ +# 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 5ab3d8390..b14febf2f 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)