From 845ac20ef14aad1507757bc3bcda169b48933334 Mon Sep 17 00:00:00 2001 From: Phillip Weinberg Date: Thu, 5 Jan 2023 12:18:18 -0500 Subject: [PATCH 1/2] exporting mul! --- src/ParallelMergeCSR.jl | 2 ++ src/parallel_csc_mv.jl | 2 +- src/parallel_csr_mv.jl | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ParallelMergeCSR.jl b/src/ParallelMergeCSR.jl index 590916d..2dd5af1 100644 --- a/src/ParallelMergeCSR.jl +++ b/src/ParallelMergeCSR.jl @@ -9,6 +9,8 @@ using SparseArrays: AbstractSparseMatrixCSC, getcolptr +export mul! + include("parallel_csr_mv.jl") include("parallel_csc_mv.jl") diff --git a/src/parallel_csc_mv.jl b/src/parallel_csc_mv.jl index 92ae8f5..061139a 100644 --- a/src/parallel_csc_mv.jl +++ b/src/parallel_csc_mv.jl @@ -25,7 +25,7 @@ function atomic_add!(a::AbstractVector{T},b::T) where T <: Complex end end -function SparseArrays.mul!(C::StridedVecOrMat, A::AbstractSparseMatrixCSC, B::DenseInputVecOrMat, α::Number, β::Number) +function mul!(C::StridedVecOrMat, A::AbstractSparseMatrixCSC, B::DenseInputVecOrMat, α::Number, β::Number) size(A, 2) == size(B, 1) || throw(DimensionMismatch()) size(A, 1) == size(C, 1) || throw(DimensionMismatch()) size(B, 2) == size(C, 2) || throw(DimensionMismatch()) diff --git a/src/parallel_csr_mv.jl b/src/parallel_csr_mv.jl index e7122c4..c2662ea 100644 --- a/src/parallel_csr_mv.jl +++ b/src/parallel_csr_mv.jl @@ -121,7 +121,7 @@ end # C = transpose(A)B + Cβ # C = xABα + Cβ for (T, t) in ((Adjoint, adjoint), (Transpose, transpose)) - @eval function SparseArrays.mul!(C::StridedVecOrMat, xA::$T{<:Any,<:AbstractSparseMatrixCSC}, B::DenseInputVecOrMat, α::Number, β::Number) + @eval function mul!(C::StridedVecOrMat, xA::$T{<:Any,<:AbstractSparseMatrixCSC}, B::DenseInputVecOrMat, α::Number, β::Number) # obtains the original matrix underneath the "lazy wrapper" A = xA.parent size(A, 2) == size(C, 1) || throw(DimensionMismatch()) From 591c6ffe556f4b52b51b8ae7dd06bee98cbb19ec Mon Sep 17 00:00:00 2001 From: Phillip Weinberg Date: Thu, 5 Jan 2023 12:29:16 -0500 Subject: [PATCH 2/2] updating tests --- test/merge_csr_mv!.jl | 36 ++++++++++++++++++------------------ test/mul!.jl | 41 +++++++++++------------------------------ 2 files changed, 29 insertions(+), 48 deletions(-) diff --git a/test/merge_csr_mv!.jl b/test/merge_csr_mv!.jl index 5375e85..d7beca7 100644 --- a/test/merge_csr_mv!.jl +++ b/test/merge_csr_mv!.jl @@ -1,6 +1,6 @@ using Test using ParallelMergeCSR: merge_csr_mv!, Range -using SparseArrays +using SparseArrays: sprand, sparse @testset "Range" begin @@ -32,7 +32,7 @@ end merge_csr_mv!(0.3, A, x, y, transpose) - @test Matrix(A) * x * 0.3 == y + @test A * x * 0.3 == y end @testset "Singleton (Complex)" begin @@ -50,7 +50,7 @@ end @testset "Single row (Real)" begin # 10 x 1 converted to 1 x 10 - A = 10.0 * SparseArrays.sprand(10, 1, 0.3) + A = 10.0 * sprand(10, 1, 0.3) # x needs to be 10 x 1 x = rand(size(A, 1)) @@ -59,13 +59,13 @@ end merge_csr_mv!(1.1, A, x, y, transpose) - @test (transpose(Matrix(A)) * x) * 1.1 ≈ y + @test (transpose(A) * x) * 1.1 ≈ y end @testset "Single row (Complex)" begin # 10 x 1 is treated as 1 x 10 inside merge_csr_mv! - A = SparseArrays.sprand(Complex{Float64}, 10, 1, 0.3) + A = sprand(Complex{Float64}, 10, 1, 0.3) x = rand(eltype(A), size(A, 1)) @@ -73,7 +73,7 @@ end merge_csr_mv!(1.1, A, x, y, transpose) - @test (transpose(Matrix(A)) * x) * 1.1 ≈ y + @test (transpose(A) * x) * 1.1 ≈ y end @@ -92,7 +92,7 @@ end # test fails, y seems to have lots of zero-entries @testset "Square (Real)" begin - A = SparseArrays.sprand(10,10,0.3) + A = sprand(10,10,0.3) # 10 x 1 x = rand(10) @@ -102,12 +102,12 @@ end merge_csr_mv!(1.1, A, x, y, adjoint) - @test (adjoint(Matrix(A)) * x) * 1.1 ≈ y + @test (adjoint(A) * x) * 1.1 ≈ y end @testset "Square (Complex)" begin - A = SparseArrays.sprand(Complex{Float64}, 10, 10, 0.3) + A = sprand(Complex{Float64}, 10, 10, 0.3) x = 10 * rand(Complex{Float64}, 10) @@ -115,7 +115,7 @@ end merge_csr_mv!(1.1, A, x, y, adjoint) - @test (adjoint(Matrix(A)) * x) * 1.1 ≈ y + @test (adjoint(A) * x) * 1.1 ≈ y end @testset "4x6 (Real)" begin @@ -138,7 +138,7 @@ end merge_csr_mv!(2.0, A, x, y, adjoint) - @test Matrix(adjoint(m)) * x * 2.0 == y + @test adjoint(m) * x * 2.0 == y end @testset "4 x 6 (Complex)" begin @@ -160,13 +160,13 @@ end # multiply merge_csr_mv!(2.0, A, x, y, adjoint) - @test Matrix(adjoint(m)) * x * 2.0 == y + @test adjoint(m) * x * 2.0 == y end @testset "100x100 (Real)" begin # create matrix - A = SparseArrays.sprand(100, 100, 0.3) + A = sprand(100, 100, 0.3) # create vector x = rand(100) @@ -176,12 +176,12 @@ end merge_csr_mv!(3.0, A, x, y, transpose) - @test transpose(Matrix(A)) * x * 3 ≈ y + @test transpose(A) * x * 3 ≈ y end @testset "100x100 (Complex)" begin # create matrix - A = SparseArrays.sprand(Complex{Float64}, 100, 100, 0.3) + A = sprand(Complex{Float64}, 100, 100, 0.3) # create vector x = rand(Complex{Float64}, 100) @@ -191,7 +191,7 @@ end merge_csr_mv!(3.0, A, x, y, transpose) - @test transpose(Matrix(A)) * x * 3 ≈ y + @test transpose(A) * x * 3 ≈ y end #= @@ -215,7 +215,7 @@ end merge_csr_mv!(α, A, col, Y_view, transpose) end - @test transpose(Matrix(A)) * X * 9.2 ≈ Y + @test transpose(A) * X * 9.2 ≈ Y end @@ -236,6 +236,6 @@ end merge_csr_mv!(α, A, col, Y_view, adjoint) end - @test adjoint(Matrix(A)) * X * 9.2 ≈ Y + @test adjoint(A) * X * 9.2 ≈ Y end \ No newline at end of file diff --git a/test/mul!.jl b/test/mul!.jl index 5b22b6a..fc07c3d 100644 --- a/test/mul!.jl +++ b/test/mul!.jl @@ -14,9 +14,7 @@ using SparseArrays C_copy = deepcopy(C) β = 3.9 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -31,9 +29,7 @@ end C_copy = deepcopy(C) β = 1.2 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end # trigger merge_csr_mv! @@ -49,10 +45,8 @@ end C_copy = deepcopy(C) β = 1.0 - SparseArrays.mul!(C, A, B, α, β) - # right hand side is correct, left hand side is problematic - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -67,9 +61,8 @@ end C_copy = deepcopy(C) β = 6.11 + 9.2im - SparseArrays.mul!(C, A, B, α, β) - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @testset "Transpose Rectangular Real" begin @@ -83,9 +76,7 @@ end C_copy = deepcopy(C) β = 1.0 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -100,9 +91,7 @@ end C_copy = deepcopy(C) β = 1.0 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -117,8 +106,7 @@ end C_copy = deepcopy(C) β = 0.3 - SparseArrays.mul!(C, A, B, α, β) - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -133,8 +121,7 @@ end C_copy = deepcopy(C) β = 0.3 - SparseArrays.mul!(C, A, B, α, β) - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -148,9 +135,7 @@ end C_copy = deepcopy(C) β = 5.2 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @testset "Matrix x Vector (Real)" begin @@ -163,9 +148,7 @@ end C_copy = deepcopy(C) β = 5.2 - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end @@ -179,8 +162,6 @@ end C_copy = deepcopy(C) β = 2.1+0.1im - SparseArrays.mul!(C, A, B, α, β) - - @test C ≈ Matrix(A) * B * α + C_copy * β + @test ParallelMergeCSR.mul!(C, A, B, α, β) ≈ SparseArrays.mul!(C_copy, A, B, α, β) end \ No newline at end of file