From 8aa7772cc9c5691cf6a2cc3f8145c1fb8668246e Mon Sep 17 00:00:00 2001 From: SwayamBorateIIT <167902886+SwayamBorateIIT@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:55:57 +0000 Subject: [PATCH] SparseArrays: Resolve ArrayDomain indexing for sparse vectors On Julia nightly (1.15.0-DEV.1070), `distribute(sprand(100, 0.1), Blocks(10))` throws `MethodError: getindex(::SparseVector, ::ArrayDomain) is ambiguous`. SparseArrays now defines `getindex(x::AbstractSparseVector, I...)`. That is more specific than Dagger's `getindex(::AbstractArray, ::ArrayDomain)` in the first argument and less specific in the second, so neither method wins. `distribute` reaches it when it slices each block out of the source vector. Julia 1.13 is not affected. Add an `AbstractSparseVector` method for `ArrayDomain` that slices with the domain's ranges, next to the existing `AbstractSparseMatrixCSC` one (428a54f). It is more specific than both candidates, which settles the call. An `ArrayDomain` is not an index and SparseArrays cannot lower it, so the method has to live on Dagger's side. The `distribute` testset covered a sparse matrix but not a sparse vector, so CI did not show this. Add a sparse-vector case next to it. Refs #786 Co-Authored-By: Claude Sonnet 5.5 --- ext/SparseArraysExt.jl | 7 +++++-- test/array/allocation.jl | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/SparseArraysExt.jl b/ext/SparseArraysExt.jl index c3136653d..6f8a1c998 100644 --- a/ext/SparseArraysExt.jl +++ b/ext/SparseArraysExt.jl @@ -6,10 +6,13 @@ import LinearAlgebra import Dagger import Dagger: Blocks, AutoBlocks, BlocksOrAuto, AssignmentType, DSparseArray, DSparseMatrix -# SparseArrays' vararg CSC indexing and Dagger's AbstractArray domain indexing -# each constrain a different argument. Resolve their intersection explicitly. +# SparseArrays' vararg CSC and sparse-vector indexing and Dagger's AbstractArray +# domain indexing each constrain a different argument. Resolve their +# intersection explicitly. Base.getindex(A::SparseArrays.AbstractSparseMatrixCSC, d::Dagger.ArrayDomain) = A[Dagger.indexes(d)...] +Base.getindex(x::SparseArrays.AbstractSparseVector, d::Dagger.ArrayDomain) = + x[Dagger.indexes(d)...] # Keep tiles sparse through `collect`/`cat`; the outer `collect` densifies. Dagger._sparse_collect(M::SparseMatrixCSC) = copy(M) diff --git a/test/array/allocation.jl b/test/array/allocation.jl index 4548f3ed1..5bfec4f50 100644 --- a/test/array/allocation.jl +++ b/test/array/allocation.jl @@ -146,6 +146,11 @@ end test_dist(rand(100, 100)) test_dist(sprand(100, 100, 0.1)) + v = sprand(100, 0.1) + Dv = distribute(v, Blocks(10)) + @test Dv isa DArray{eltype(v),1} + @test collect(Dv) == v + x = distribute(rand(10), 2) @test collect(distribute(x, 3)) == collect(x) end