Skip to content

Commit 5062fc9

Browse files
committed
implement workaround for coo->csc and coo->csr
1 parent 43032f6 commit 5062fc9

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/cusparse/conversions.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function sort_coo(A::CuSparseMatrixCOO{Tv,Ti}, type::SparseChar='R') where {Tv,T
199199
# and we have the following error in the tests:
200200
# "Out of GPU memory trying to allocate 127.781 TiB".
201201
# We set 0 as default value to avoid it.
202-
out = Ref{Csize_t}(0)
202+
out = Ref{Csize_t}(1)
203203
cusparseXcoosort_bufferSizeExt(handle(), m, n, nnz(A), A.rowInd, A.colInd, out)
204204
return out[]
205205
end
@@ -626,8 +626,10 @@ end
626626
# need both typevars for compatibility with GPUArrays
627627
function CuSparseMatrixCSR{Tv, Ti}(coo::CuSparseMatrixCOO{Tv, Ti}; index::SparseChar='O') where {Tv, Ti}
628628
m, n = size(coo)
629-
csrRowPtr = (index == 'O') ? CUDA.ones(Cint, m + 1) : CUDA.zeros(Cint, m + 1)
630-
nnz(coo) == 0 && return CuSparseMatrixCSR{Tv}(csrRowPtr, coo.colInd, nonzeros(coo), size(coo))
629+
csrRowPtr = CuArray{Ti}(undef, m + 1)
630+
if iszero(m)
631+
csrRowPtr .= (index == 'O') ? 1 : 0 # cusparseXcoo2csr does not initialize csrRowPtr correctly if m == 0
632+
end
631633
coo = sort_coo(coo, 'R')
632634
cusparseXcoo2csr(handle(), coo.rowInd, nnz(coo), m, csrRowPtr, index)
633635
CuSparseMatrixCSR{Tv,Cint}(csrRowPtr, coo.colInd, nonzeros(coo), size(coo))
@@ -637,7 +639,6 @@ CuSparseMatrixCSR(coo::CuSparseMatrixCOO{Tv, Ti}; index::SparseChar='O') where {
637639

638640
function CuSparseMatrixCOO{Tv, Ti}(csr::CuSparseMatrixCSR{Tv}; index::SparseChar='O') where {Tv, Ti}
639641
m,n = size(csr)
640-
nnz(csr) == 0 && return CuSparseMatrixCOO{Tv,Cint}(CUDA.zeros(Cint, 0), CUDA.zeros(Cint, 0), nonzeros(csr), size(csr))
641642
cooRowInd = CuVector{Cint}(undef, nnz(csr))
642643
cusparseXcsr2coo(handle(), csr.rowPtr, nnz(csr), m, cooRowInd, index)
643644
CuSparseMatrixCOO{Tv,Cint}(cooRowInd, csr.colVal, nonzeros(csr), size(csr))
@@ -649,8 +650,10 @@ CuSparseMatrixCOO(csr::CuSparseMatrixCSR{Tv, Ti}; index::SparseChar='O') where {
649650

650651
function CuSparseMatrixCSC{Tv, Ti}(coo::CuSparseMatrixCOO{Tv, Ti}; index::SparseChar='O') where {Tv, Ti}
651652
m, n = size(coo)
652-
cscColPtr = (index == 'O') ? CUDA.ones(Cint, n + 1) : CUDA.zeros(Cint, n + 1)
653-
nnz(coo) == 0 && return CuSparseMatrixCSC{Tv}(cscColPtr, coo.rowInd, nonzeros(coo), size(coo))
653+
cscColPtr = CuArray{Ti}(undef, n + 1)
654+
if iszero(n)
655+
cscColPtr .= (index == 'O') ? 1 : 0 # cusparseXcoo2csr does not initialize cscColPtr correctly if n == 0
656+
end
654657
coo = sort_coo(coo, 'C')
655658
cusparseXcoo2csr(handle(), coo.colInd, nnz(coo), n, cscColPtr, index)
656659
CuSparseMatrixCSC{Tv,Cint}(cscColPtr, coo.rowInd, nonzeros(coo), size(coo))
@@ -660,7 +663,6 @@ CuSparseMatrixCSC(coo::CuSparseMatrixCOO{Tv, Ti}; index::SparseChar='O') where {
660663

661664
function CuSparseMatrixCOO{Tv, Ti}(csc::CuSparseMatrixCSC{Tv, Ti}; index::SparseChar='O') where {Tv, Ti}
662665
m,n = size(csc)
663-
nnz(csc) == 0 && return CuSparseMatrixCOO{Tv,Cint}(CUDA.zeros(Cint, 0), CUDA.zeros(Cint, 0), nonzeros(csc), size(csc))
664666
cooColInd = CuVector{Cint}(undef, nnz(csc))
665667
cusparseXcsr2coo(handle(), csc.colPtr, nnz(csc), n, cooColInd, index)
666668
coo = CuSparseMatrixCOO{Tv,Cint}(csc.rowVal, cooColInd, nonzeros(csc), size(csc))

test/libraries/cusparse/conversions.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ if !(v"12.0" <= CUSPARSE.version() < v"12.1")
113113
end
114114
end
115115

116+
function __test_valid_ptr(A::CuSparseMatrixCSC)
117+
colPtr = collect(A.colPtr)
118+
@test length(A.colPtr) >= 1
119+
@test colPtr[1] == 1 # assuming one based indexing (index='O')
120+
@test issorted(colPtr)
121+
end
122+
123+
function __test_valid_ptr(A::CuSparseMatrixCSR)
124+
rowPtr = collect(A.rowPtr)
125+
@test length(A.rowPtr) >= 1
126+
@test rowPtr[1] == 1 # assuming one based indexing (index='O')
127+
@test issorted(rowPtr)
128+
end
129+
130+
__test_valid_ptr(::CuSparseMatrixCOO) = nothing
131+
__test_valid_ptr(::CuSparseMatrixBSR) = nothing
132+
116133
for (n, bd, p) in [(100, 5, 0.02), (5, 1, 0.8), (4, 2, 0.5), (0, 1, 0.0)]
117134
v"12.0" <= CUSPARSE.version() < v"12.1" && n == 4 && continue
118135
@testset "conversions between CuSparseMatrices (n, bd, p) = ($n, $bd, $p)" begin
@@ -122,13 +139,15 @@ for (n, bd, p) in [(100, 5, 0.02), (5, 1, 0.8), (4, 2, 0.5), (0, 1, 0.0)]
122139
if CuSparseMatrixType1 == CuSparseMatrixBSR && n == 0 continue end # TODO: conversion to CuSparseMatrixBSR breaks with (0x0) matrices
123140
dA1 = CuSparseMatrixType1 == CuSparseMatrixBSR ? CuSparseMatrixType1(A, blockdim) : CuSparseMatrixType1(A)
124141
@testset "conversion $CuSparseMatrixType1 --> SparseMatrixCSC" begin
142+
__test_valid_ptr(dA1)
125143
@test SparseMatrixCSC(dA1) A
126144
end
127145
for CuSparseMatrixType2 in (CuSparseMatrixCSC, CuSparseMatrixCSR, CuSparseMatrixCOO, CuSparseMatrixBSR)
128146
if CuSparseMatrixType2 == CuSparseMatrixBSR && n == 0 continue end # TODO: conversion to CuSparseMatrixBSR breaks with (0x0) matrices
129147
CuSparseMatrixType1 == CuSparseMatrixType2 && continue
130148
dA2 = CuSparseMatrixType2 == CuSparseMatrixBSR ? CuSparseMatrixType2(dA1, blockdim) : CuSparseMatrixType2(dA1)
131149
@testset "conversion $CuSparseMatrixType1 --> $CuSparseMatrixType2" begin
150+
__test_valid_ptr(dA2)
132151
@test collect(dA1) collect(dA2)
133152
end
134153
end

0 commit comments

Comments
 (0)