use of org.apache.sysml.runtime.matrix.data.SparseBlockMCSR in project incubator-systemml by apache.
the class SparseBlockDelete method runSparseBlockDeleteTest.
/**
*
* @param btype
* @param sparsity
*/
private void runSparseBlockDeleteTest(SparseBlock.Type btype, double sparsity) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 456);
//init sparse block
SparseBlock sblock = null;
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
//delete range per row via set
for (int i = 0; i < rows; i++) for (int j = cl; j < cu; j++) {
A[i][j] = 0;
sblock.set(i, j, 0);
}
//check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
//check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
//check correct values
Iterator<IJV> iter = sblock.getIterator();
int count = 0;
while (iter.hasNext()) {
IJV cell = iter.next();
if (cell.getV() != A[cell.getI()][cell.getJ()])
Assert.fail("Wrong value returned by iterator: " + cell.getV() + ", expected: " + A[cell.getI()][cell.getJ()]);
count++;
}
if (count != nnz)
Assert.fail("Wrong number of values returned by iterator: " + count + ", expected: " + nnz);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockMCSR in project incubator-systemml by apache.
the class SparseBlockGetFirstIndex method runSparseBlockGetFirstIndexTest.
/**
*
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockGetFirstIndexTest(SparseBlock.Type btype, double sparsity, IndexType itype) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 3456);
//init sparse block
SparseBlock sblock = null;
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
//check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
//check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
//check correct index values
for (int i = 0; i < rows; i++) {
int ix = getFirstIx(A, i, i, itype);
int sixpos = -1;
switch(itype) {
case GT:
sixpos = sblock.posFIndexGT(i, i);
break;
case GTE:
sixpos = sblock.posFIndexGTE(i, i);
break;
case LTE:
sixpos = sblock.posFIndexLTE(i, i);
break;
}
int six = (sixpos >= 0) ? sblock.indexes(i)[sixpos] : -1;
if (six != ix) {
Assert.fail("Wrong index returned by index probe (" + itype.toString() + "," + i + "): " + six + ", expected: " + ix);
}
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockMCSR in project incubator-systemml by apache.
the class SparseBlockGetSet method runSparseBlockGetSetTest.
/**
*
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockGetSetTest(SparseBlock.Type btype, double sparsity, InitType itype) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 7654321);
//init sparse block
SparseBlock sblock = null;
if (itype == InitType.BULK) {
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
} else if (itype == InitType.SEQ_SET || itype == InitType.RAND_SET) {
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(rows, cols);
break;
case CSR:
sblock = new SparseBlockCSR(rows, cols);
break;
case COO:
sblock = new SparseBlockCOO(rows, cols);
break;
}
if (itype == InitType.SEQ_SET) {
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) sblock.append(i, j, A[i][j]);
} else if (itype == InitType.RAND_SET) {
LongLongDoubleHashMap map = new LongLongDoubleHashMap();
for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) map.addValue(i, j, A[i][j]);
for (//random hash order
LLDoubleEntry e : //random hash order
map.extractValues()) sblock.set((int) e.key1, (int) e.key2, e.value);
}
}
//check basic meta data
if (sblock.numRows() != rows)
Assert.fail("Wrong number of rows: " + sblock.numRows() + ", expected: " + rows);
//check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
//check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
//check correct values
for (int i = 0; i < rows; i++) if (!sblock.isEmpty(i))
for (int j = 0; j < cols; j++) {
double tmp = sblock.get(i, j);
if (tmp != A[i][j])
Assert.fail("Wrong get value for cell (" + i + "," + j + "): " + tmp + ", expected: " + A[i][j]);
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockMCSR in project incubator-systemml by apache.
the class SparseBlockIndexRange method runSparseBlockIndexRangeTest.
/**
*
* @param btype
* @param sparsity
*/
private void runSparseBlockIndexRangeTest(SparseBlock.Type btype, double sparsity, UpdateType utype) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 456);
//init sparse block
SparseBlock sblock = null;
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
//delete range per row via set
if (utype == UpdateType.DELETE) {
for (int i = 0; i < rows; i++) {
sblock.deleteIndexRange(i, cl, cu);
Arrays.fill(A[i], cl, cu, 0);
}
} else if (utype == UpdateType.INSERT) {
double[] vals = new double[cu - cl];
for (int j = cl; j < cu; j++) vals[j - cl] = j;
for (int i = 0; i < rows; i++) {
sblock.setIndexRange(i, cl, cu, vals, 0, cu - cl);
System.arraycopy(vals, 0, A[i], cl, cu - cl);
}
}
//check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
//check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
//check correct values
Iterator<IJV> iter = sblock.getIterator();
int count = 0;
while (iter.hasNext()) {
IJV cell = iter.next();
if (cell.getV() != A[cell.getI()][cell.getJ()])
Assert.fail("Wrong value returned by iterator: " + cell.getV() + ", expected: " + A[cell.getI()][cell.getJ()]);
count++;
}
if (count != nnz)
Assert.fail("Wrong number of values returned by iterator: " + count + ", expected: " + nnz);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.sysml.runtime.matrix.data.SparseBlockMCSR in project incubator-systemml by apache.
the class SparseBlockScan method runSparseBlockScanTest.
/**
*
* @param sparseM1
* @param sparseM2
* @param instType
*/
private void runSparseBlockScanTest(SparseBlock.Type btype, double sparsity) {
try {
//data generation
double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 1234);
//init sparse block
SparseBlock sblock = null;
MatrixBlock mbtmp = DataConverter.convertToMatrixBlock(A);
SparseBlock srtmp = mbtmp.getSparseBlock();
switch(btype) {
case MCSR:
sblock = new SparseBlockMCSR(srtmp);
break;
case CSR:
sblock = new SparseBlockCSR(srtmp);
break;
case COO:
sblock = new SparseBlockCOO(srtmp);
break;
}
//check for correct number of non-zeros
int[] rnnz = new int[rows];
int nnz = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
nnz += rnnz[i];
}
if (nnz != sblock.size())
Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
//check correct isEmpty return
for (int i = 0; i < rows; i++) if (sblock.isEmpty(i) != (rnnz[i] == 0))
Assert.fail("Wrong isEmpty(row) result for row nnz: " + rnnz[i]);
//check correct values
int count = 0;
for (int i = 0; i < rows; i++) {
int alen = sblock.size(i);
int apos = sblock.pos(i);
int[] aix = sblock.indexes(i);
double[] avals = sblock.values(i);
for (int j = 0; j < alen; j++) {
if (avals[apos + j] != A[i][aix[apos + j]])
Assert.fail("Wrong value returned by scan: " + avals[apos + j] + ", expected: " + A[i][apos + aix[j]]);
count++;
}
}
if (count != nnz)
Assert.fail("Wrong number of values returned by scan: " + count + ", expected: " + nnz);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
Aggregations