Skip to content

RFC: Handle Hermitian(::Sparse) \ SparseArray - #322

Merged
dkarrasch merged 6 commits into
mainfrom
dk/sparseherm_solve_sparse
Feb 15, 2023
Merged

dkarrasch merged 6 commits into
mainfrom
dk/sparseherm_solve_sparse

Conversation

@dkarrasch

Copy link
Copy Markdown
Member

While playing with JuliaLang/LinearAlgebra.jl#977 a little more I noticed one peculiarity.

On v1.8

Hermitian(hess_mat) \ hcat(grad_vec, grad_vec)
ERROR: MethodError: no method matching lu!(::SparseMatrixCSC{Float64, Int64}, ::RowMaximum; check=true)
Closest candidates are:
  lu!(::StridedMatrix{T}, ::RowMaximum; check) where T<:Union{Float32, Float64, ComplexF32, ComplexF64} at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:80
  lu!(::Union{Hermitian{T, S}, Symmetric{T, S}} where {T, S}, ::Union{NoPivot, RowMaximum}; check) at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:89
  lu!(::StridedMatrix{T} where T, ::Union{NoPivot, RowMaximum}; check) at /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:135
  ...
Stacktrace:
 [1] lu!(A::Hermitian{Float64, SparseMatrixCSC{Float64, Int64}}, pivot::RowMaximum; check::Bool)
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:91
 [2] lu(A::Hermitian{Float64, SparseMatrixCSC{Float64, Int64}}, pivot::RowMaximum; check::Bool)
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:279
 [3] lu(A::Hermitian{Float64, SparseMatrixCSC{Float64, Int64}}, pivot::RowMaximum) (repeats 2 times)
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/lu.jl:278
 [4] \(A::Hermitian{Float64, SparseMatrixCSC{Float64, Int64}}, B::SparseMatrixCSC{Float64, Int64})
   @ LinearAlgebra /Applications/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/generic.jl:1110

On v1.9:

julia> Hermitian(hess_mat) \ hcat(grad_vec, grad_vec)
6×2 Matrix{Float64}:
 0.508367  0.508367
 0.508367  0.508367
 2.0       2.0
 0.5       0.5
 0.307692  0.307692
 0.5       0.5

While this works, the problem is that this runs by _generic_lufact! due to the relaxations in JuliaLang/julia#47107. We have two options: (i) throw and suggest to densify the rhs, or (ii) densify silently. I think (ii) is not too bad, because if a user expects the solve to be successful, then a dense version of the result will be typically more memory efficient than a sparse version thereof due to intrinsic density.

@dkarrasch

Copy link
Copy Markdown
Member Author

Whatever we decide on, this should be backported to v1.9 I guess.

@codecov-commenter

codecov-commenter commented Jan 5, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #322 (f38bf97) into main (fee88bb) will decrease coverage by 0.02%.
The diff coverage is 50.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main     JuliaLang/julia#322      +/-   ##
==========================================
- Coverage   93.68%   93.66%   -0.02%     
==========================================
  Files          12       12              
  Lines        7454     7455       +1     
==========================================
  Hits         6983     6983              
- Misses        471      472       +1     
Impacted Files Coverage Δ
src/abstractsparse.jl 60.41% <ø> (ø)
src/solvers/cholmod.jl 91.01% <0.00%> (-0.12%) ⬇️
src/solvers/umfpack.jl 88.42% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@dkarrasch

Copy link
Copy Markdown
Member Author

Since this was not working on earlier versions, I think it's more natural to make this throw for now, as a quickfix. The general case about what to do with sparse rhs should be discussed first, and then probably requires modifications in more places than this.

@dkarrasch
dkarrasch requested review from SobhanMP and rayegun January 26, 2023 11:06
@ViralBShah

Copy link
Copy Markdown
Member

Sparse RHS can be optimized, but usually I believe you need to be able to leverage that in your solver as discussed in the Sparse Matrices in Matlab paper.

@DrTimothyAldenDavis @Wimmerer IIRC SuiteSparse doesn't support sparse RHS in solvers. I just wanted to confirm or be corrected.

@SobhanMP SobhanMP left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, oh missed Viral's comment, is there actually any operation that SS supports with sparse rhs?

Comment thread src/solvers/umfpack.jl
# We could do this as lu(A') = lu(A)' with UMFPACK, but the user could want to do one over the other
lu(A::Union{Adjoint{T, S}, Transpose{T, S}}; check::Bool = true) where {T<:UMFVTypes, S<:AbstractSparseMatrixCSC{T}} =
lu(copy(A); check)
lu(A::AdjOrTrans{T,S}; check::Bool = true) where {T<:UMFVTypes, S<:AbstractSparseMatrixCSC{T}} =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see there has been some nice clean ups!

@DrTimothyAldenDavis

Copy link
Copy Markdown

I do exploit the sparse right-hand-side internally in several places (it forms the key kernel for cs_lu and KLU for example, and is called n times to do the entire LU factorization, solving x = L\b where x, L, and b are all saprse). I also exploit it inside CHOLMOD in the update/downdate. However, it's hard to exploit in general for solving x=A\b because if b is sparse and A forms a single strongly-connected component (the typical case) then x is dense. So it's hard to exploit. x can be sparse for x=A\b if A is permutable into block triangular form (via cs_dmperm for example). I use the BTF form in KLU+BTF but I can't recall if I also allow b to be sparse when doing x=A\b.

@rayegun

rayegun commented Feb 14, 2023

Copy link
Copy Markdown
Member

@DrTimothyAldenDavis I don't see any support for it in the KLU interface, but I may experiment with doing it with the Julia port.

@rayegun rayegun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@DrTimothyAldenDavis

Copy link
Copy Markdown

OK .. then I guess I don't have one. I think I may have one for UMFPACK but I convert it to full first ... I can't recall exactly ... too much code over too many years.

@dkarrasch

Copy link
Copy Markdown
Member Author

Making this throw (now) is only a continuation of the same earlier behavior. We can still always make it work if we want by either calling specific code paths, or densifying the rhs.

@dkarrasch
dkarrasch merged commit 0d2d30a into main Feb 15, 2023
@dkarrasch
dkarrasch deleted the dk/sparseherm_solve_sparse branch February 15, 2023 12:35
@github-actions

Copy link
Copy Markdown

The backport to 1.9 failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-1.9 1.9
# Navigate to the new working tree
cd .worktrees/backport-1.9
# Create a new branch
git switch --create backport-322-to-1.9
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick --mainline 1 0d2d30afb156b48d329ed212309b56a31c4a84a4
# Push it to GitHub
git push --set-upstream origin backport-322-to-1.9
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-1.9

Then, create a pull request where the base branch is 1.9 and the compare/head branch is backport-322-to-1.9.

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.

6 participants