From b5a31b25d018dbc06da950139014cba26b94b563 Mon Sep 17 00:00:00 2001 From: SwayamBorateIIT <167902886+SwayamBorateIIT@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:09:06 +0000 Subject: [PATCH] DArray: Fix `\` returning a dense array on Julia 1.14+ `src/array/alloc.jl` overloads `LinearAlgebra._zeros` so that the generic `ldiv(F, B)` behind `\` stages its right-hand side into a `DArray` rather than the dense `zeros(...)` buffer it would otherwise allocate. JuliaLang/LinearAlgebra.jl#1746, shipped in Julia 1.14, changed that function's third argument from the factorization's width to the factorization itself, so both overloads stopped matching. Nothing errors and nothing warns: `\` falls through to the generic method and silently returns an `Array`, so a solve against a `DMatrix` loses its distribution and the result no longer answers to `wait`. Define the new signature alongside the old one, forwarding to it so the size arithmetic is not duplicated -- upstream's `_ret_size(F, B)` computes exactly what the existing methods compute from `size(F, 2)`. Whichever signature the running Julia does not use is simply never called, so one source serves the whole supported range from 1.10 up. The existing solve tests all call `ldiv!` directly and so never reach `_zeros`, which is why this went unnoticed; the failure surfaced two testsuites away, as `MethodError: no method matching wait(::Vector{Float64})` at `test/allocations.jl:214`. Assert on `\` itself instead, for `cholesky` and `lu` with both vector and matrix right-hand sides. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01XsjCQbwr3mFza8CTCW9f7Q --- src/array/alloc.jl | 14 +++++++++++++- test/array/linalg/solve.jl | 26 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/array/alloc.jl b/src/array/alloc.jl index 1497bec08..65e1b9fc4 100644 --- a/src/array/alloc.jl +++ b/src/array/alloc.jl @@ -132,7 +132,15 @@ function Base.zero(x::DArray{T,N}) where {T,N} return _to_darray(a) end -# Weird LinearAlgebra dispatch in `\` needs this +# Weird LinearAlgebra dispatch in `\` needs this. `LinearAlgebra.ldiv`, which +# `\` calls, stages the right-hand side into a buffer from `_zeros`; for a +# plain `AbstractVecOrMat` that buffer is a dense `zeros(...)`, so without +# these the result of `\` comes back as an `Array` with its distribution lost. +# +# Julia <= 1.13 passes the width of the factorization as an `Integer`; +# JuliaLang/LinearAlgebra.jl#1746, in Julia 1.14, changed that third argument +# to the factorization itself. Both signatures are defined so that one Dagger +# version spans the change; whichever does not match is simply never called. function LinearAlgebra._zeros(::Type{T}, B::DVector, n::Integer) where T m = max(size(B, 1), n) sz = (m,) @@ -143,6 +151,10 @@ function LinearAlgebra._zeros(::Type{T}, B::DMatrix, n::Integer) where T sz = (m, size(B, 2)) return zeros(auto_blocks(sz), T, sz) end +LinearAlgebra._zeros(::Type{T}, B::DVector, F::LinearAlgebra.Factorization) where T = + LinearAlgebra._zeros(T, B, size(F, 2)) +LinearAlgebra._zeros(::Type{T}, B::DMatrix, F::LinearAlgebra.Factorization) where T = + LinearAlgebra._zeros(T, B, size(F, 2)) function Base.view(A::AbstractArray{T,N}, p::Blocks{N}) where {T,N} d = ArrayDomain(Base.index_shape(A)) diff --git a/test/array/linalg/solve.jl b/test/array/linalg/solve.jl index c29471270..7128878fe 100644 --- a/test/array/linalg/solve.jl +++ b/test/array/linalg/solve.jl @@ -69,5 +69,31 @@ LinearAlgebra.ldiv!(chol_A, b_ref) LinearAlgebra.ldiv!(chol_DA, Db) @test collect(Db) ≈ b_ref rtol=tol + + # `\` must keep the result distributed. `\` routes through + # `LinearAlgebra.ldiv`, which stages the right-hand side into a buffer + # from `LinearAlgebra._zeros`; when Dagger's `_zeros` methods do not + # match, that buffer is a dense `Array`, so `\` silently returns one + # and the distribution is lost. Every other test in this file calls + # `ldiv!` directly and so never reaches that path -- which is why + # JuliaLang/LinearAlgebra.jl#1746, changing `_zeros`' third argument + # from the factorization's width to the factorization itself, went + # unnoticed here until the allocations testsuite tripped over it. + A = rand(T, N, N); A = A'A + I + B = rand(T, N, N) + b = rand(T, N) + + x = cholesky(DArray(A, Blocks(bs_i, bs_j))) \ DArray(b, Blocks(bs_i)) + @test x isa DVector{T} + @test (wait(x); true) + @test collect(x) ≈ A \ b rtol=tol + + X = cholesky(DArray(A, Blocks(bs_i, bs_j))) \ DArray(B, Blocks(bs_i, bs_j)) + @test X isa DMatrix{T} + @test collect(X) ≈ A \ B rtol=tol + + y = lu(DArray(A, Blocks(bs_i, bs_j)), RowMaximum()) \ DArray(b, Blocks(bs_i)) + @test y isa DVector{T} + @test collect(y) ≈ A \ b rtol=tol end end \ No newline at end of file