-
Notifications
You must be signed in to change notification settings - Fork 29
Fix sparse gemm, gemv, and mul #536
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
Open
michel2323
wants to merge
1
commit into
master
Choose a base branch
from
ms/sparse_fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,8 +7,13 @@ function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A:: | |
| sparse_gemv!(tA, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::oneSparseMatrixCSC{T}, B::oneVector{T}, _add::MulAddMul) where T <: BlasReal | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'T' : flip_trans(tA) | ||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::oneSparseMatrixCSC{T}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| return sparse_gemv!(tA, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::oneSparseMatrixCOO{T}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| sparse_gemv!(tA, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
||
|
|
@@ -18,8 +23,14 @@ function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::oneSparseM | |
| sparse_gemm!(tA, tB, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::oneSparseMatrixCSC{T}, B::oneMatrix{T}, _add::MulAddMul) where T <: BlasReal | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'T' : flip_trans(tA) | ||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::oneSparseMatrixCSC{T}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| return sparse_gemm!(tA, tB, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::oneSparseMatrixCOO{T}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| sparse_gemm!(tA, tB, _add.alpha, A, B, _add.beta, C) | ||
| end | ||
|
|
@@ -31,3 +42,233 @@ end | |
| function LinearAlgebra.generic_trimatdiv!(C::oneMatrix{T}, uploc, isunitc, tfun::Function, A::oneSparseMatrixCSR{T}, B::oneMatrix{T}) where T <: BlasFloat | ||
| sparse_trsm!(uploc, tfun === identity ? 'N' : tfun === transpose ? 'T' : 'C', 'N', isunitc, one(T), A, B, C) | ||
| end | ||
|
|
||
| # Handle Transpose and Adjoint wrappers for sparse matrices | ||
| # Let the low-level wrappers handle the CSC->CSR conversion and flip_trans logic | ||
|
|
||
| # Matrix-vector multiplication with transpose/adjoint | ||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:oneSparseMatrixCSR{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
| return sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Adjoint{T, <:oneSparseMatrixCSR{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemv!('N', conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:oneSparseMatrixCSC{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar issue, you need a special handle of |
||
| return sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Adjoint{T, <:oneSparseMatrixCSC{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemv!('N', conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:oneSparseMatrixCOO{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again, special handle of |
||
| return sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Adjoint{T, <:oneSparseMatrixCOO{T}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemv!('N', conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemv!(tA_final, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| # Handle Transpose{T, Adjoint{T, ...}} for complex matrices | ||
| # transpose(adjoint(A)) for complex matrices needs special handling | ||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCSR{T}}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasComplex} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| # transpose(adjoint(A)) = conj(A), so we need to conjugate | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| if tA == 'N' | ||
| sparse_gemv!('N', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| elseif tA == 'T' | ||
| sparse_gemv!('T', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| else # tA == 'C' | ||
| sparse_gemv!('C', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| end | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCSC{T}}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasComplex} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| # transpose(adjoint(A)) = conj(A), so we need to conjugate | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| if tA == 'N' | ||
| sparse_gemv!('N', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| elseif tA == 'T' | ||
| sparse_gemv!('T', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| else # tA == 'C' | ||
| sparse_gemv!('C', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| end | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matvecmul!(C::oneVector{T}, tA::AbstractChar, A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCOO{T}}}, B::oneVector{T}, _add::MulAddMul) where {T <: BlasComplex} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| # transpose(adjoint(A)) = conj(A), so we need to conjugate | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| if tA == 'N' | ||
| sparse_gemv!('N', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| elseif tA == 'T' | ||
| sparse_gemv!('T', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| else # tA == 'C' | ||
| sparse_gemv!('C', conj(alpha), A.parent.parent, B, conj(beta), C) | ||
| end | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| return C | ||
| end | ||
|
|
||
| # Custom * operators for Transpose{T, Adjoint{T, ...}} to ensure correct output size allocation | ||
| function Base.:*(A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCSR{T}}}, x::oneVector{T}) where {T <: BlasComplex} | ||
| m, n = size(A) | ||
| y = similar(x, T, m) | ||
| LinearAlgebra.generic_matvecmul!(y, 'N', A, x, LinearAlgebra.MulAddMul(one(T), zero(T))) | ||
| return y | ||
| end | ||
|
|
||
| function Base.:*(A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCSC{T}}}, x::oneVector{T}) where {T <: BlasComplex} | ||
| m, n = size(A) | ||
| y = similar(x, T, m) | ||
| LinearAlgebra.generic_matvecmul!(y, 'N', A, x, LinearAlgebra.MulAddMul(one(T), zero(T))) | ||
| return y | ||
| end | ||
|
|
||
| function Base.:*(A::Transpose{T, <:Adjoint{T, <:oneSparseMatrixCOO{T}}}, x::oneVector{T}) where {T <: BlasComplex} | ||
| m, n = size(A) | ||
| y = similar(x, T, m) | ||
| LinearAlgebra.generic_matvecmul!(y, 'N', A, x, LinearAlgebra.MulAddMul(one(T), zero(T))) | ||
| return y | ||
| end | ||
|
|
||
| # Matrix-matrix multiplication with transpose/adjoint | ||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Transpose{T, <:oneSparseMatrixCSR{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
| return sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Adjoint{T, <:oneSparseMatrixCSR{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemm!('N', tB, conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Transpose{T, <:oneSparseMatrixCSC{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
| return sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Adjoint{T, <:oneSparseMatrixCSC{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemm!('N', tB, conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Transpose{T, <:oneSparseMatrixCOO{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| tA_final = tA == 'N' ? 'T' : (tA == 'T' ? 'N' : 'C') | ||
| return sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
|
|
||
| function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::Adjoint{T, <:oneSparseMatrixCOO{T}}, B::oneMatrix{T}, _add::MulAddMul) where {T <: BlasFloat} | ||
| tA = tA in ('S', 's', 'H', 'h') ? 'N' : tA | ||
| tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB | ||
| if tA == 'T' | ||
| alpha = _add.alpha | ||
| beta = _add.beta | ||
| B .= conj.(B) | ||
| C .= conj.(C) | ||
| sparse_gemm!('N', tB, conj(alpha), A.parent, B, conj(beta), C) | ||
| C .= conj.(C) | ||
| B .= conj.(B) | ||
| else | ||
| tA_final = tA == 'N' ? 'C' : 'N' | ||
| sparse_gemm!(tA_final, tB, _add.alpha, A.parent, B, _add.beta, C) | ||
| end | ||
| return C | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@michel2323 In the case
tA = 'C', you need to do a product with the conjugate.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.
I am wondering if you didn't mixed
BlasRealwithBlasFloat?