-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use _setindex! to fix DimensionMismatch in sparse assignment, Fixes #28963 #30507
Conversation
Please review this PR. |
A lot of people are on vacation this week. Hence reviews are likely to be slow. Also may I request you to make the PR title and commit have a bit more detail about the issue in addition to the number? |
@ViralBShah |
There can be many places where dimension mismatch happens. It is important to mention that it is fixing the mismatch in assignment. |
I've reviewed this and am happy to merge once the commit message and PR titles are updated with a little more info as above. |
Thanks @ViralBShah for helping out. |
@fredrikekre are we going to backport to 1.0 as well? I have a bunch of other PRs merged that should also be backported. |
Yes. |
Use _setindex! to fix DimensionMismatch in sparse assignment Fixes #28963
Use _setindex! to fix DimensionMismatch in sparse assignment Fixes #28963
@@ -2575,6 +2575,7 @@ function setindex!(A::SparseMatrixCSC{Tv,Ti}, V::AbstractVecOrMat, Ix::Union{Int | |||
@assert !has_offset_axes(A, V, Ix, Jx) | |||
(I, J) = Base.ensure_indexable(to_indices(A, (Ix, Jx))) | |||
checkbounds(A, I, J) | |||
Base._setindex!(IndexStyle(A), A, V, to_indices(A, (Ix, Jx))...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this just assign into the array twice? Once with the generic (slow) dense version, and then once with the optimized sparse version? We just want to be checking the shape of V
, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed: before:
julia> A = spzeros(1000,1000)
1000×1000 SparseMatrixCSC{Float64,Int64} with 0 stored entries
julia> @btime $A[:,:] = $(spzeros(1000, 1000))
5.557 μs (5 allocations: 8.39 KiB)
After:
julia> @btime $A[:,:] = $(spzeros(1000, 1000))
9.545 ms (5 allocations: 8.39 KiB)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
without assigning into the array twice.
Fixes #28963.