Skip to content

SparseArrays: Resolve ArrayDomain indexing for sparse vectors - #865

Open
SwayamBorateIIT wants to merge 1 commit into
JuliaParallel:masterfrom
SwayamBorateIIT:sparse-vector-arraydomain
Open

SwayamBorateIIT wants to merge 1 commit into
JuliaParallel:masterfrom
SwayamBorateIIT:sparse-vector-arraydomain

Conversation

@SwayamBorateIIT

@SwayamBorateIIT SwayamBorateIIT commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

distribute of a sparse vector fails on Julia nightly, because the getindex call it makes on each block is ambiguous:

julia> using Dagger, SparseArrays

julia> distribute(sprand(100, 0.1), Blocks(10))
ERROR: MethodError: getindex(::SparseVector{Float64, Int64}, ::ArrayDomain{1, Tuple{UnitRange{Int64}}}) is ambiguous.
Candidates:
  getindex(x::AbstractSparseVector{Tv, Ti}, I...) where {Tv, Ti}
    @ SparseArrays [...]/stdlib/v1.15/SparseArrays/src/indexing.jl:42
  getindex(arr::AbstractArray, d::ArrayDomain)
    @ Dagger src/array/darray.jl:35

Reproduced on current master (c3352d72) with Julia 1.15.0-DEV.1070. Julia 1.13.0 is not affected.

Why

SparseArrays' method is more specific than Dagger's in the first argument (AbstractSparseVector against AbstractArray), and Dagger's is more specific in the second (ArrayDomain against the untyped I...), so neither wins. Forcing SparseArrays' method to take an ArrayDomain fails with ArgumentError: invalid index, because an ArrayDomain is not an index, so the fix has to be on Dagger's side.

#780 fixed the matrix half of this (428a54f). The vector method was not added and no test distributed a sparse vector, so CI did not show it. Refs #786.

The change

One method in ext/SparseArraysExt.jl, next to the matrix one:

Base.getindex(x::SparseArrays.AbstractSparseVector, d::Dagger.ArrayDomain) =
    x[Dagger.indexes(d)...]

It is more specific than each of the two candidates, which settles the call. A test in the distribute testset of test/array/allocation.jl distributes a sparse vector and checks the round trip.

Tests

Each run was julia --project=test test/runtests.jl --test array/allocation, after deleting test/Manifest.toml and instantiating, on one Linux machine. I ran these three:

Julia vector method result
1.15.0-DEV.1070 in 983 / 983 pass
1.13.0 in 983 / 983 pass
1.15.0-DEV.1070 removed, test kept 980 pass, 1 error: getindex(::SparseVector, ::ArrayDomain) is ambiguous at allocation.jl:150

The last row is the negative control: with the method removed, the new test fails on nightly, and only the distribute testset is affected.

Claude Code also ran the same file on a separate copy of the branch, on both versions, with and without the method. Those four runs gave the same numbers as mine. The one I did not repeat is 1.13.0 with the method removed (983 / 983 pass), which is where "Julia 1.13.0 is not affected" comes from.

Not covered

I did not run Julia 1.10 or 1.14 (pre), or the MPI and GPU jobs; I am leaving those to CI. The method is defined only when SparseArrays is loaded and does not touch scheduling, but I have not measured speed or allocations.

AI disclosure

I used Claude Code (Opus 5, later Sonnet 5.5) for this change.

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 JuliaParallel#786

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@SwayamBorateIIT

Copy link
Copy Markdown
Contributor Author

Hi, when you have a moment, could you approve the CI workflows for this PR? Happy to adjust anything. Thanks!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant