Skip to content

Commit fddedcc

Browse files
committed
[MINOR] bug fix: infinity loop in rollSparse
Closes #2124
1 parent 0cbfdb8 commit fddedcc

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/main/java/org/apache/sysds/runtime/matrix/data/LibMatrixReorg.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,20 +2369,18 @@ private static void copySparseMtx(MatrixBlock in, MatrixBlock out, int inIdx, in
23692369
SparseBlock a = in.getSparseBlock();
23702370
SparseBlock c = out.getSparseBlock();
23712371

2372-
while (copyLen > 0) {
2373-
if (a.isEmpty(inIdx)) continue; // skip empty rows
2374-
2375-
final int apos = a.pos(inIdx);
2376-
final int alen = a.size(inIdx) + apos;
2377-
final int[] aix = a.indexes(inIdx);
2378-
final double[] avals = a.values(inIdx);
2379-
2380-
// copy only non-zero elements
2381-
for (int k = apos; k < alen; k++) {
2382-
c.set(outIdx, aix[k], avals[k]);
2383-
}
2384-
2385-
inIdx++; outIdx++; copyLen--;
2372+
for (int i = 0; i < copyLen; i++) {
2373+
if (!a.isEmpty(inIdx)){
2374+
final int apos = a.pos(inIdx);
2375+
final int alen = a.size(inIdx) + apos;
2376+
final int[] aix = a.indexes(inIdx);
2377+
final double[] avals = a.values(inIdx);
2378+
2379+
// copy only non-zero elements
2380+
for (int k = apos; k < alen; k++)
2381+
c.set(outIdx, aix[k], avals[k]);
2382+
}
2383+
inIdx++; outIdx++;
23862384
}
23872385
}
23882386

src/main/python/tests/matrix/test_roll.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
from systemds.context import SystemDSContext
2727

2828
np.random.seed(7)
29+
random.seed(7)
2930
shape = (random.randrange(1, 25), random.randrange(1, 25))
3031

3132
m = np.random.rand(shape[0], shape[1])
3233
my = np.random.rand(shape[0], 1)
3334
m_empty = np.asarray([[]])
34-
m_sparse = sparse.random(shape[0], shape[1], density=0.1, format="csr").toarray()
35+
m_sparse = sparse.random(shape[0], shape[1], density=0.1, format="csr", random_state=5).toarray()
3536
m_sparse = np.around(m_sparse, decimals=22)
3637

38+
3739
class TestRoll(unittest.TestCase):
3840
sds: SystemDSContext = None
3941

@@ -61,5 +63,6 @@ def test_sparse_matrix(self):
6163
r = self.sds.from_numpy(m_sparse).roll(1).compute()
6264
self.assertTrue(np.allclose(r, np.roll(m_sparse, axis=0, shift=1)))
6365

66+
6467
if __name__ == "__main__":
6568
unittest.main(exit=False)

0 commit comments

Comments
 (0)