Support CSC#527
Closed
michel2323 wants to merge 5 commits into
Closed
Conversation
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/lib/mkl/array.jl b/lib/mkl/array.jl
index d712081..2f46847 100644
--- a/lib/mkl/array.jl
+++ b/lib/mkl/array.jl
@@ -18,7 +18,7 @@ mutable struct oneSparseMatrixCSC{Tv, Ti} <: oneAbstractSparseMatrix{Tv, Ti}
colPtr::oneVector{Ti}
rowVal::oneVector{Ti}
nzVal::oneVector{Tv}
- dims::NTuple{2,Int}
+ dims::NTuple{2, Int}
nnz::Ti
end
@@ -46,7 +46,7 @@ SparseArrays.nnz(A::oneAbstractSparseMatrix) = A.nnz
SparseArrays.nonzeros(A::oneAbstractSparseMatrix) = A.nzVal
for (gpu, cpu) in [:oneSparseMatrixCSR => :SparseMatrixCSC,
- :oneSparseMatrixCSC => :SparseMatrixCSC,
+ :oneSparseMatrixCSC => :SparseMatrixCSC,
:oneSparseMatrixCOO => :SparseMatrixCSC]
@eval Base.show(io::IOContext, x::$gpu) =
show(io, $cpu(x))
diff --git a/lib/mkl/interfaces.jl b/lib/mkl/interfaces.jl
index 9e9100c..a9fe668 100644
--- a/lib/mkl/interfaces.jl
+++ b/lib/mkl/interfaces.jl
@@ -7,9 +7,9 @@ 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
+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' : (tA == 'N' ? 'T' : 'N')
- sparse_gemv!(tA, _add.alpha, A, B, _add.beta, C)
+ return sparse_gemv!(tA, _add.alpha, A, B, _add.beta, C)
end
function LinearAlgebra.generic_matmatmul!(C::oneMatrix{T}, tA, tB, A::oneSparseMatrixCSR{T}, B::oneMatrix{T}, _add::MulAddMul) where T <: BlasFloat
@@ -18,10 +18,10 @@ 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
+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' : (tA == 'N' ? 'T' : 'N')
tB = tB in ('S', 's', 'H', 'h') ? 'N' : tB
- sparse_gemm!(tA, tB, _add.alpha, A, B, _add.beta, C)
+ return sparse_gemm!(tA, tB, _add.alpha, A, B, _add.beta, C)
end
for SparseMatrixType in (:oneSparseMatrixCSR,)
diff --git a/lib/mkl/wrappers_sparse.jl b/lib/mkl/wrappers_sparse.jl
index e4c0fa7..9dbce54 100644
--- a/lib/mkl/wrappers_sparse.jl
+++ b/lib/mkl/wrappers_sparse.jl
@@ -46,7 +46,7 @@ for (fname, elty, intty) in ((:onemklSsparse_set_csr_data , :Float32 , :Int3
nnzA = length(A.nzval)
queue = global_queue(context(nzVal), device())
$fname(sycl_queue(queue), handle_ptr[], n, m, 'O', colPtr, rowVal, nzVal) # CSC of A is CSR of Aᵀ
- dA = oneSparseMatrixCSC{$elty, $intty}(handle_ptr[], colPtr, rowVal, nzVal, (m,n), nnzA)
+ dA = oneSparseMatrixCSC{$elty, $intty}(handle_ptr[], colPtr, rowVal, nzVal, (m, n), nnzA)
finalizer(sparse_release_matrix_handle, dA)
return dA
end
@@ -122,17 +122,21 @@ for SparseMatrix in (:oneSparseMatrixCSR, :oneSparseMatrixCOO)
end
# Special handling for CSC matrices since they are stored as transposed CSR
-for (fname, elty) in ((:onemklSsparse_gemv, :Float32),
- (:onemklDsparse_gemv, :Float64),
- (:onemklCsparse_gemv, :ComplexF32),
- (:onemklZsparse_gemv, :ComplexF64))
+for (fname, elty) in (
+ (:onemklSsparse_gemv, :Float32),
+ (:onemklDsparse_gemv, :Float64),
+ (:onemklCsparse_gemv, :ComplexF32),
+ (:onemklZsparse_gemv, :ComplexF64),
+ )
@eval begin
- function sparse_gemv!(trans::Char,
- alpha::Number,
- A::oneSparseMatrixCSC{$elty},
- x::oneStridedVector{$elty},
- beta::Number,
- y::oneStridedVector{$elty})
+ function sparse_gemv!(
+ trans::Char,
+ alpha::Number,
+ A::oneSparseMatrixCSC{$elty},
+ x::oneStridedVector{$elty},
+ beta::Number,
+ y::oneStridedVector{$elty}
+ )
# CSC(A) is represented by storing CSR(A^T). Map operations accordingly:
# - trans = 'N': want A*x -> use op(S)='T' with S=A^T.
@@ -233,18 +237,22 @@ function sparse_optimize_gemm!(trans::Char, transB::Char, nrhs::Int, A::oneSpars
end
# Special handling for CSC matrices since they are stored as transposed CSR (S = A^T)
-for (fname, elty) in ((:onemklSsparse_gemm, :Float32),
- (:onemklDsparse_gemm, :Float64),
- (:onemklCsparse_gemm, :ComplexF32),
- (:onemklZsparse_gemm, :ComplexF64))
+for (fname, elty) in (
+ (:onemklSsparse_gemm, :Float32),
+ (:onemklDsparse_gemm, :Float64),
+ (:onemklCsparse_gemm, :ComplexF32),
+ (:onemklZsparse_gemm, :ComplexF64),
+ )
@eval begin
- function sparse_gemm!(transa::Char,
- transb::Char,
- alpha::Number,
- A::oneSparseMatrixCSC{$elty},
- B::oneStridedMatrix{$elty},
- beta::Number,
- C::oneStridedMatrix{$elty})
+ function sparse_gemm!(
+ transa::Char,
+ transb::Char,
+ alpha::Number,
+ A::oneSparseMatrixCSC{$elty},
+ B::oneStridedMatrix{$elty},
+ beta::Number,
+ C::oneStridedMatrix{$elty}
+ )
# Map op(A) to op(S) where S = A^T stored as CSR in the handle
# transa: 'N' -> op(S)='T'; 'T' -> op(S)='N'; 'C' ->
@@ -256,8 +264,8 @@ for (fname, elty) in ((:onemklSsparse_gemm, :Float32),
(nB != nC) && (transb == 'N') && throw(ArgumentError("B and C must have the same number of columns."))
(mB != nC) && (transb != 'N') && throw(ArgumentError("Bᵀ and C must have the same number of columns."))
nrhs = size(B, 2)
- ldb = max(1,stride(B,2))
- ldc = max(1,stride(C,2))
+ ldb = max(1, stride(B, 2))
+ ldc = max(1, stride(C, 2))
queue = global_queue(context(C), device())
diff --git a/test/onemkl.jl b/test/onemkl.jl
index ab0a620..d4bfa0b 100644
--- a/test/onemkl.jl
+++ b/test/onemkl.jl
@@ -3,7 +3,7 @@ if Sys.iswindows()
else
using oneAPI
-using oneAPI.oneMKL: band, bandex, oneSparseMatrixCSR, oneSparseMatrixCOO, oneSparseMatrixCSC
+ using oneAPI.oneMKL: band, bandex, oneSparseMatrixCSR, oneSparseMatrixCOO, oneSparseMatrixCSC
using SparseArrays
using LinearAlgebra
@@ -1088,16 +1088,16 @@ end
end
end
- @testset "oneSparseMatrixCSC" begin
- (T isa Complex) && continue
- for S in (Int32, Int64)
- A = sprand(T, 20, 10, 0.5)
- A = SparseMatrixCSC{T, S}(A)
- B = oneSparseMatrixCSC(A)
- A2 = SparseMatrixCSC(B)
- @test A == A2
+ @testset "oneSparseMatrixCSC" begin
+ (T isa Complex) && continue
+ for S in (Int32, Int64)
+ A = sprand(T, 20, 10, 0.5)
+ A = SparseMatrixCSC{T, S}(A)
+ B = oneSparseMatrixCSC(A)
+ A2 = SparseMatrixCSC(B)
+ @test A == A2
+ end
end
- end
@testset "oneSparseMatrixCOO" begin
for S in (Int32, Int64)
@@ -1110,7 +1110,7 @@ end
end
@testset "sparse gemv" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCOO, oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCOO, oneSparseMatrixCSR, oneSparseMatrixCSC)
@testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
A = sprand(T, 20, 10, 0.5)
x = transa == 'N' ? rand(T, 10) : rand(T, 20)
@@ -1130,129 +1130,134 @@ end
end
@testset "sparse gemm" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
- @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
- @testset "transb = $transb" for (transb, opb) in [('N', identity), ('T', transpose), ('C', adjoint)]
- (transb == 'N') || continue
- A = sprand(T, 10, 10, 0.5)
- B = transb == 'N' ? rand(T, 10, 2) : rand(T, 2, 10)
- C = rand(T, 10, 2)
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ @testset "transb = $transb" for (transb, opb) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ (transb == 'N') || continue
+ A = sprand(T, 10, 10, 0.5)
+ B = transb == 'N' ? rand(T, 10, 2) : rand(T, 2, 10)
+ C = rand(T, 10, 2)
+
+ dA = SparseMatrix(A)
+ dB = oneMatrix{T}(B)
+ dC = oneMatrix{T}(C)
- dA = SparseMatrix(A)
- dB = oneMatrix{T}(B)
- dC = oneMatrix{T}(C)
-
- alpha = rand(T)
- beta = rand(T)
- oneMKL.sparse_optimize_gemm!(transa, dA)
- oneMKL.sparse_gemm!(transa, transb, alpha, dA, dB, beta, dC)
- @test alpha * opa(A) * opb(B) + beta * C ≈ collect(dC)
- end
+ alpha = rand(T)
+ beta = rand(T)
+ oneMKL.sparse_optimize_gemm!(transa, dA)
+ oneMKL.sparse_gemm!(transa, transb, alpha, dA, dB, beta, dC)
+ @test alpha * opa(A) * opb(B) + beta * C ≈ collect(dC)
+ end
end
end
end
@testset "sparse symv" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
- @testset "uplo = $uplo" for uplo in ('L', 'U')
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "uplo = $uplo" for uplo in ('L', 'U')
A = sprand(T, 10, 10, 0.5)
- A = A + A'
+ A = A + A'
x = rand(T, 10)
y = rand(T, 10)
- dA = uplo == 'L' ? SparseMatrix(A |> tril) : SparseMatrix(A |> triu)
+ dA = uplo == 'L' ? SparseMatrix(A |> tril) : SparseMatrix(A |> triu)
dx = oneVector{T}(x)
dy = oneVector{T}(y)
alpha = rand(T)
beta = rand(T)
- oneMKL.sparse_symv!(uplo, alpha, dA, dx, beta, dy)
- # @test alpha * A * x + beta * y ≈ collect(dy)
+ oneMKL.sparse_symv!(uplo, alpha, dA, dx, beta, dy)
+ # @test alpha * A * x + beta * y ≈ collect(dy)
end
end
end
- @testset "sparse trmv" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
- @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
- for (uplo, diag, wrapper) in [('L', 'N', LowerTriangular), ('L', 'U', UnitLowerTriangular),
- ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular)]
- (transa == 'N') || continue
- A = sprand(T, 10, 10, 0.5)
- x = rand(T, 10)
- y = rand(T, 10)
+ @testset "sparse trmv" begin
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ for (uplo, diag, wrapper) in [
+ ('L', 'N', LowerTriangular), ('L', 'U', UnitLowerTriangular),
+ ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular),
+ ]
+ (transa == 'N') || continue
+ A = sprand(T, 10, 10, 0.5)
+ x = rand(T, 10)
+ y = rand(T, 10)
- B = uplo == 'L' ? tril(A) : triu(A)
- B = diag == 'U' ? B - Diagonal(B) + I : B
- dA = SparseMatrix(B)
- dx = oneVector{T}(x)
- dy = oneVector{T}(y)
+ B = uplo == 'L' ? tril(A) : triu(A)
+ B = diag == 'U' ? B - Diagonal(B) + I : B
+ dA = SparseMatrix(B)
+ dx = oneVector{T}(x)
+ dy = oneVector{T}(y)
- alpha = rand(T)
- beta = rand(T)
+ alpha = rand(T)
+ beta = rand(T)
- oneMKL.sparse_optimize_trmv!(uplo, transa, diag, dA)
- oneMKL.sparse_trmv!(uplo, transa, diag, alpha, dA, dx, beta, dy)
- @test alpha * wrapper(opa(A)) * x + beta * y ≈ collect(dy)
- end
+ oneMKL.sparse_optimize_trmv!(uplo, transa, diag, dA)
+ oneMKL.sparse_trmv!(uplo, transa, diag, alpha, dA, dx, beta, dy)
+ @test alpha * wrapper(opa(A)) * x + beta * y ≈ collect(dy)
+ end
end
end
end
- @testset "sparse trsv" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
- @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ @testset "sparse trsv" begin
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
for (uplo, diag, wrapper) in [('L', 'N', LowerTriangular), ('L', 'U', UnitLowerTriangular),
- ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular)]
+ ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular),
+ ]
(transa == 'N') || continue
alpha = rand(T)
A = rand(T, 10, 10) + I
A = sparse(A)
- x = rand(T, 10)
- y = rand(T, 10)
+ x = rand(T, 10)
+ y = rand(T, 10)
B = uplo == 'L' ? tril(A) : triu(A)
B = diag == 'U' ? B - Diagonal(B) + I : B
- dA = SparseMatrix(B)
- dx = oneVector{T}(x)
- dy = oneVector{T}(y)
-
- oneMKL.sparse_optimize_trsv!(uplo, transa, diag, dA)
- oneMKL.sparse_trsv!(uplo, transa, diag, alpha, dA, dx, dy)
- y = wrapper(opa(A)) \ (alpha * x)
- @test y ≈ collect(dy)
+ dA = SparseMatrix(B)
+ dx = oneVector{T}(x)
+ dy = oneVector{T}(y)
+
+ oneMKL.sparse_optimize_trsv!(uplo, transa, diag, dA)
+ oneMKL.sparse_trsv!(uplo, transa, diag, alpha, dA, dx, dy)
+ y = wrapper(opa(A)) \ (alpha * x)
+ @test y ≈ collect(dy)
+ end
end
end
end
- end
- @testset "sparse trsm" begin
- @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
- @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
- @testset "transx = $transx" for (transx, opx) in [('N', identity), ('T', transpose), ('C', adjoint)]
- (transx != 'N') && continue
- for (uplo, diag, wrapper) in [('L', 'N', LowerTriangular), ('L', 'U', UnitLowerTriangular),
- ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular)]
- (transa == 'N') || continue
- alpha = rand(T)
- A = rand(T, 10, 10) + I
- A = sparse(A)
- X = transx == 'N' ? rand(T, 10, 4) : rand(T, 4, 10)
- Y = rand(T, 10, 4)
-
- B = uplo == 'L' ? tril(A) : triu(A)
- B = diag == 'U' ? B - Diagonal(B) + I : B
- dA = SparseMatrix(B)
- dX = oneMatrix{T}(X)
- dY = oneMatrix{T}(Y)
-
- oneMKL.sparse_optimize_trsm!(uplo, transa, diag, dA)
- oneMKL.sparse_trsm!(uplo, transa, transx, diag, alpha, dA, dX, dY)
- Y = wrapper(opa(A)) \ (alpha * opx(X))
- @test Y ≈ collect(dY)
-
- oneMKL.sparse_optimize_trsm!(uplo, transa, diag, 4, dA)
- end
+ @testset "sparse trsm" begin
+ @testset "$SparseMatrix" for SparseMatrix in (oneSparseMatrixCSR, oneSparseMatrixCSC)
+ @testset "transa = $transa" for (transa, opa) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ @testset "transx = $transx" for (transx, opx) in [('N', identity), ('T', transpose), ('C', adjoint)]
+ (transx != 'N') && continue
+ for (uplo, diag, wrapper) in [
+ ('L', 'N', LowerTriangular), ('L', 'U', UnitLowerTriangular),
+ ('U', 'N', UpperTriangular), ('U', 'U', UnitUpperTriangular),
+ ]
+ (transa == 'N') || continue
+ alpha = rand(T)
+ A = rand(T, 10, 10) + I
+ A = sparse(A)
+ X = transx == 'N' ? rand(T, 10, 4) : rand(T, 4, 10)
+ Y = rand(T, 10, 4)
+
+ B = uplo == 'L' ? tril(A) : triu(A)
+ B = diag == 'U' ? B - Diagonal(B) + I : B
+ dA = SparseMatrix(B)
+ dX = oneMatrix{T}(X)
+ dY = oneMatrix{T}(Y)
+
+ oneMKL.sparse_optimize_trsm!(uplo, transa, diag, dA)
+ oneMKL.sparse_trsm!(uplo, transa, transx, diag, alpha, dA, dX, dY)
+ Y = wrapper(opa(A)) \ (alpha * opx(X))
+ @test Y ≈ collect(dY)
+
+ oneMKL.sparse_optimize_trsm!(uplo, transa, diag, 4, dA)
+ end
end
end
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
No description provided.