Fix csr2csc for zero-size matrix
@@ -879,9 +879,13 @@ def coosort(x, sort_by='r'):
def coo2csr(x):
handle = device.get_cusparse_handle()
m = x.shape[0]
+ nnz = x.nnz
+ if nnz == 0:
+ indptr = cupy.zeros(m + 1, 'i')
+ else:
indptr = cupy.empty(m + 1, 'i')
cusparse.xcoo2csr(
- handle, x.row.data.ptr, x.nnz, m,
+ handle, x.row.data.ptr, nnz, m,
indptr.data.ptr, cusparse.CUSPARSE_INDEX_BASE_ZERO)
return cupyx.scipy.sparse.csr.csr_matrix(
(x.data, x.col, indptr), shape=x.shape)
@@ -890,9 +894,13 @@ def coo2csr(x):
def coo2csc(x):
handle = device.get_cusparse_handle()
n = x.shape[1]
+ nnz = x.nnz
+ if nnz == 0:
+ indptr = cupy.zeros(n + 1, 'i')
+ else:
indptr = cupy.empty(n + 1, 'i')
cusparse.xcoo2csr(
- handle, x.col.data.ptr, x.nnz, n,
+ handle, x.col.data.ptr, nnz, n,
indptr.data.ptr, cusparse.CUSPARSE_INDEX_BASE_ZERO)
return cupyx.scipy.sparse.csc.csc_matrix(
(x.data, x.row, indptr), shape=x.shape)
@@ -915,7 +923,7 @@ def csr2coo(x, data, indices):
handle = device.get_cusparse_handle()
m = x.shape[0]
- nnz = len(x.data)
+ nnz = x.nnz
row = cupy.empty(nnz, 'i')
cusparse.xcsr2coo(
handle, x.indptr.data.ptr, nnz, m, row.data.ptr,
@@ -933,9 +941,11 @@ def csr2csc(x):
m, n = x.shape
nnz = x.nnz
data = cupy.empty(nnz, x.dtype)
- indptr = cupy.empty(n + 1, 'i')
indices = cupy.empty(nnz, 'i')
-
+ if nnz == 0:
+ indptr = cupy.zeros(n + 1, 'i')
+ else:
+ indptr = cupy.empty(n + 1, 'i')
_call_cusparse(
'csr2csc', x.dtype,
handle, m, n, nnz, x.data.data.ptr,
@@ -991,7 +1001,7 @@ def csc2coo(x, data, indices):
"""
handle = device.get_cusparse_handle()
n = x.shape[1]
- nnz = len(x.data)
+ nnz = x.nnz
col = cupy.empty(nnz, 'i')
cusparse.xcsr2coo(
handle, x.indptr.data.ptr, nnz, n, col.data.ptr,
@@ -1009,9 +1019,11 @@ def csc2csr(x):
m, n = x.shape
nnz = x.nnz
data = cupy.empty(nnz, x.dtype)
- indptr = cupy.empty(m + 1, 'i')
indices = cupy.empty(nnz, 'i')
-
+ if nnz == 0:
+ indptr = cupy.zeros(m + 1, 'i')
+ else:
+ indptr = cupy.empty(m + 1, 'i')
_call_cusparse(
'csr2csc', x.dtype,
handle, n, m, nnz, x.data.data.ptr,
