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