Skip to content
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

[CUSPARSE] Fix constructor of sparse empty matrices #2575

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/cusparse/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ for elty in (:Float32, :Float64, :ComplexF32, :ComplexF64)
@eval begin
function CuSparseMatrixCSC{$elty}(csr::CuSparseMatrixCSR{$elty}; index::SparseChar='O', action::cusparseAction_t=CUSPARSE_ACTION_NUMERIC, algo::cusparseCsr2CscAlg_t=CUSPARSE_CSR2CSC_ALG1)
m,n = size(csr)
colPtr = CUDA.zeros(Cint, n+1)
colPtr = CUDA.ones(Cint, n+1)
rowVal = CUDA.zeros(Cint, nnz(csr))
nzVal = CUDA.zeros($elty, nnz(csr))
function bufferSize()
Expand All @@ -348,7 +348,7 @@ for elty in (:Float32, :Float64, :ComplexF32, :ComplexF64)

function CuSparseMatrixCSR{$elty}(csc::CuSparseMatrixCSC{$elty}; index::SparseChar='O', action::cusparseAction_t=CUSPARSE_ACTION_NUMERIC, algo::cusparseCsr2CscAlg_t=CUSPARSE_CSR2CSC_ALG1)
m,n = size(csc)
rowPtr = CUDA.zeros(Cint,m+1)
rowPtr = CUDA.ones(Cint,m+1)
colVal = CUDA.zeros(Cint,nnz(csc))
nzVal = CUDA.zeros($elty,nnz(csc))
function bufferSize()
Expand All @@ -375,7 +375,7 @@ for (elty, welty) in ((:Float16, :Float32),
@eval begin
function CuSparseMatrixCSC{$elty}(csr::CuSparseMatrixCSR{$elty}; index::SparseChar='O', action::cusparseAction_t=CUSPARSE_ACTION_NUMERIC, algo::cusparseCsr2CscAlg_t=CUSPARSE_CSR2CSC_ALG1)
m,n = size(csr)
colPtr = CUDA.zeros(Cint, n+1)
colPtr = CUDA.ones(Cint, n+1)
rowVal = CUDA.zeros(Cint, nnz(csr))
nzVal = CUDA.zeros($elty, nnz(csr))
if $elty == Float16 #broken for ComplexF16?
Expand All @@ -401,7 +401,7 @@ for (elty, welty) in ((:Float16, :Float32),

function CuSparseMatrixCSR{$elty}(csc::CuSparseMatrixCSC{$elty}; index::SparseChar='O', action::cusparseAction_t=CUSPARSE_ACTION_NUMERIC, algo::cusparseCsr2CscAlg_t=CUSPARSE_CSR2CSC_ALG1)
m,n = size(csc)
rowPtr = CUDA.zeros(Cint,m+1)
rowPtr = CUDA.ones(Cint,m+1)
colVal = CUDA.zeros(Cint,nnz(csc))
nzVal = CUDA.zeros($elty,nnz(csc))
if $elty == Float16 #broken for ComplexF16?
Expand Down Expand Up @@ -490,7 +490,7 @@ for (fname,elty) in ((:cusparseScsr2bsr, :Float32),
nnz_ref = Ref{Cint}(1)
mb = cld(m, blockDim)
nb = cld(n, blockDim)
bsrRowPtr = CUDA.zeros(Cint,mb + 1)
bsrRowPtr = CUDA.ones(Cint,mb + 1)
cudesca = CuMatrixDescriptor('G', 'L', 'N', index)
cudescc = CuMatrixDescriptor('G', 'L', 'N', indc)
cusparseXcsr2bsrNnz(handle(), dir, m, n, cudesca, csr.rowPtr,
Expand Down Expand Up @@ -519,7 +519,7 @@ for (fname,elty) in ((:cusparseSbsr2csr, :Float32),
nb = cld(n, bsr.blockDim)
cudesca = CuMatrixDescriptor('G', 'L', 'N', index)
cudescc = CuMatrixDescriptor('G', 'L', 'N', indc)
csrRowPtr = CUDA.zeros(Cint, m + 1)
csrRowPtr = CUDA.ones(Cint, m + 1)
csrColInd = CUDA.zeros(Cint, nnz(bsr))
csrNzVal = CUDA.zeros($elty, nnz(bsr))
$fname(handle(), bsr.dir, mb, nb,
Expand Down
7 changes: 7 additions & 0 deletions lib/cusparse/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ function densetosparse(A::CuMatrix{T}, fmt::Symbol, index::SparseChar, algo::cus
end
cusparseDenseToSparse_convert(handle(), desc_dense, desc_sparse, algo, buffer)
end
# Issue with the CUDA routine `cusparseDenseToSparse_convert`
if (index == 'O') && (fmt == :csc) && (n == 0)
B.colPtr = CuVector{Cint}([1])
end
if (index == 'O') && (fmt == :csr) && (m == 0)
B.rowPtr = CuVector{Cint}([1])
end
return B
end

Expand Down
50 changes: 49 additions & 1 deletion test/libraries/cusparse/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using CUDA
J = [2,3,4] |> cu
V = Float32[1,2,3] |> cu

dense = rand(3,3) |> cu
dense = rand(Float32, 3,3) |> cu

# check defaults
@test sparse(I, J, V) isa CuSparseMatrixCSC
Expand Down Expand Up @@ -39,6 +39,54 @@ using CUDA
end
end

@testset "sparse -- empty matrix" begin
n, m = 0, 0
I = [] |> cu
J = [] |> cu
V = Float64[] |> cu

dense = rand(m,n) |> cu

# check defaults
@test sparse(I, J, V) isa CuSparseMatrixCSC
@test sparse(dense) isa CuSparseMatrixCSC

for (fmt, T) in [(:coo, CuSparseMatrixCOO),
(:csc, CuSparseMatrixCSC),
(:csr, CuSparseMatrixCSR),
(:bsr, CuSparseMatrixBSR)
]
@testset "sparse $T" begin
if fmt != :bsr # bsr not supported
x = sparse(I, J, V; fmt=fmt)
@test x isa T{Float64}
@test size(x) == (0, 0)

x = sparse(I, J, V, m, n; fmt=fmt)
@test x isa T{Float64}
@test size(x) == (0, 0)

if fmt == :csc
@test collect(x.colPtr) == [1]
end
if fmt == :csr
@test collect(x.rowPtr) == [1]
end
end

x = sparse(dense; fmt=fmt)
@test x isa T{Float64}
@test collect(x) == collect(dense)
if fmt == :csc
@test collect(x.colPtr) == [1]
end
if fmt == :csr
@test collect(x.rowPtr) == [1]
end
end
end
end

@testset "unsorted sparse (CUDA.jl#1407)" begin
I = [1, 1, 2, 3, 3, 4, 5, 4, 6, 4, 5, 6, 6, 6]
J = [4, 6, 4, 5, 6, 6, 6, 1, 1, 2, 3, 3, 4, 5]
Expand Down