Skip to content

Commit

Permalink
Even more CUSPARSE tests (#2682)
Browse files Browse the repository at this point in the history
* Even more CUSPARSE tests

* More tests and a bugfix

* Move show test into allowscalar block
  • Loading branch information
kshyatt authored Mar 8, 2025
1 parent da014f0 commit 6bf72dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cusparse/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ CuSparseVector{T}(Mat::SparseMatrixCSC) where {T} =
throw(ArgumentError("The input argument must have a single column"))
CuSparseMatrixCSC{T}(Vec::SparseVector) where {T} =
CuSparseMatrixCSC{T}(CuVector{Cint}([1]), CuVector{Cint}(Vec.nzind),
CuVector{T}(Vec.nzval), size(Vec))
CuVector{T}(Vec.nzval), (length(Vec), 1))
CuSparseMatrixCSC{T}(Mat::SparseMatrixCSC) where {T} =
CuSparseMatrixCSC{T}(CuVector{Cint}(Mat.colptr), CuVector{Cint}(Mat.rowval),
CuVector{T}(Mat.nzval), size(Mat))
Expand Down
2 changes: 1 addition & 1 deletion lib/cusparse/batched.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end

# repeat non-matrix dimensions
function Base.repeat(A::Union{CuSparseArrayCSR, CuSparseMatrixCSR}, r1::Int64, r2::Int64, rs::Int64...)
@assert r1 == 1 && r2 == 1 "Cannot repeat matrix dimensions of CuSparseCSR"
(r1 == 1 && r2 == 1) || throw(ArgumentError("Cannot repeat matrix dimensions of CuSparseCSR"))
CuSparseArrayCSR(repeat(A.rowPtr, 1, rs...),
repeat(A.colVal, 1, rs...),
repeat(A.nzVal, 1, rs...),
Expand Down
5 changes: 4 additions & 1 deletion lib/cusparse/device.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# on-device sparse array functionality

# should be excluded from coverage counts
# COV_EXCL_START
using SparseArrays

# NOTE: this functionality is currently very bare-bones, only defining the array types
Expand Down Expand Up @@ -131,3 +132,5 @@ function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceArrayCSR)
println(io, " colVal: $(A.colVal)")
print(io, " nzVal: $(A.nzVal)")
end

# COV_EXCL_STOP
21 changes: 20 additions & 1 deletion test/libraries/cusparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using CUDA.CUSPARSE

using LinearAlgebra
using SparseArrays
using SparseArrays: nonzeroinds, getcolptr
using SparseArrays: rowvals, nonzeroinds, getcolptr

@test CUSPARSE.version() isa VersionNumber

Expand All @@ -22,6 +22,7 @@ blockdim = 5
@test ndims(d_x) == 1
dense_d_x = CuVector(x)
CUDA.@allowscalar begin
@test sprint(show, d_x) == replace(sprint(show, x), "SparseVector{Float64, Int64}"=>"CUDA.CUSPARSE.CuSparseVector{Float64, Int32}", "sparsevec(["=>"sparsevec(Int32[")
@test Array(d_x[:]) == x[:]
@test d_x[firstindex(d_x)] == x[firstindex(x)]
@test d_x[div(end, 2)] == x[div(end, 2)]
Expand All @@ -34,7 +35,13 @@ blockdim = 5
@test nnz(d_x) == nnz(x)
@test Array(nonzeros(d_x)) == nonzeros(x)
@test Array(nonzeroinds(d_x)) == nonzeroinds(x)
@test Array(rowvals(d_x)) == nonzeroinds(x)
@test nnz(d_x) == length(nonzeros(d_x))
d_y = copy(d_x)
CUDA.unsafe_free!(d_y)
x = sprand(m,0.2)
d_x = CuSparseMatrixCSC{Float64}(x)
@test size(d_x) == (m, 1)
x = sprand(m,n,0.2)
d_x = CuSparseMatrixCSC(x)
@test CuSparseMatrixCSC(d_x) === d_x
Expand Down Expand Up @@ -74,6 +81,8 @@ blockdim = 5
@test !ishermitian(d_x)
@test_throws ArgumentError size(d_x,0)
@test_throws ArgumentError CUSPARSE.CuSparseVector(x)
d_y = copy(d_x)
CUDA.unsafe_free!(d_y)
y = sprand(k,n,0.2)
d_y = CuSparseMatrixCSC(y)
@test_throws ArgumentError copyto!(d_y,d_x)
Expand Down Expand Up @@ -111,7 +120,15 @@ blockdim = 5
@test_throws ArgumentError copyto!(d_y,d_x)
d_y = CuSparseMatrixCSR(d_y)
d_x = CuSparseMatrixCSR(d_x)
d_z = copy(d_x)
CUDA.unsafe_free!(d_z)
@test CuSparseMatrixCSR(d_x) === d_x
@test reshape(d_x, :, :, 1, 1, 1) isa CuSparseArrayCSR
@test_throws ArgumentError("Cannot repeat matrix dimensions of CuSparseCSR") repeat(d_x, 2, 1, 3)
@test repeat(d_x, 1, 1, 3) isa CuSparseArrayCSR
@test reshape(repeat(d_x, 1, 1, 3), size(d_x, 1), size(d_x, 2), 3, 1, 1) isa CuSparseArrayCSR
# to hit the CuSparseArrayCSR path
CUDA.unsafe_free!(repeat(d_x, 1, 1, 3))
@test length(d_x) == m*n
@test_throws ArgumentError copyto!(d_y,d_x)
CUDA.@allowscalar begin
Expand All @@ -123,6 +140,8 @@ blockdim = 5
d_y = CuSparseMatrixBSR(d_y, blockdim)
d_x = CuSparseMatrixBSR(d_x, blockdim)
@test CuSparseMatrixBSR(d_x) === d_x
d_z = copy(d_x)
CUDA.unsafe_free!(d_z)
@test_throws ArgumentError copyto!(d_y,d_x)
CUDA.@allowscalar begin
@test d_y[1, 1] y[1, 1]
Expand Down

0 comments on commit 6bf72dd

Please sign in to comment.