Skip to content

Commit 8d2d4fa

Browse files
committed
copy data to allow in-place edits
1 parent 0052756 commit 8d2d4fa

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

cupy/cusparse.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ def cscsort(x):
564564

565565

566566
def coosort(x):
567+
"""Sorts indices of COO-matrix in place
568+
569+
Args:
570+
x (cupyx.scipy.sparse.coo_matrix): A sparse matrix to sort.
571+
572+
"""
567573
nnz = x.nnz
568574
if nnz == 0:
569575
return
@@ -574,16 +580,16 @@ def coosort(x):
574580
handle, m, n, nnz, x.row.data.ptr, x.col.data.ptr)
575581
buf = cupy.empty(buffer_size, 'b')
576582
P = cupy.empty(nnz, 'i')
577-
data_sorted = cupy.empty_like(x.data)
583+
# copy data to do safe in-place edits by gthr
584+
data_orig = x.data.copy()
578585
cusparse.createIdentityPermutation(handle, nnz, P.data.ptr)
579586
cusparse.xcoosortByRow(
580587
handle, m, n, nnz, x.row.data.ptr, x.col.data.ptr,
581588
P.data.ptr, buf.data.ptr)
582589
_call_cusparse(
583590
'gthr', x.dtype,
584-
handle, nnz, x.data.data.ptr, data_sorted.data.ptr,
591+
handle, nnz, data_orig.data.ptr, x.data.data.ptr,
585592
P.data.ptr, cusparse.CUSPARSE_INDEX_BASE_ZERO)
586-
x.data = data_sorted
587593

588594

589595
def coo2csr(x):

0 commit comments

Comments
 (0)