Search in sources :

Example 11 with SparseBlock

use of org.apache.sysml.runtime.matrix.data.SparseBlock 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);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) IJV(org.apache.sysml.runtime.matrix.data.IJV) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 12 with SparseBlock

use of org.apache.sysml.runtime.matrix.data.SparseBlock in project incubator-systemml by apache.

the class SparseBlockMerge method runSparseBlockMergeTest.

private void runSparseBlockMergeTest(SparseBlock.Type btype1, SparseBlock.Type btype2, double sparsity) {
    try {
        // data generation
        double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 1234);
        double[][] B1 = new double[A.length][];
        double[][] B2 = new double[A.length][];
        for (int i = 0; i < A.length; i++) {
            B1[i] = new double[A[i].length];
            B2[i] = new double[A[2].length];
            for (int j = 0; j < A[i].length; j++) {
                if (j % 2 == 0)
                    B1[i][j] = A[i][j];
                else
                    B2[i][j] = A[i][j];
            }
        }
        // init sparse block
        MatrixBlock mb1 = DataConverter.convertToMatrixBlock(B1);
        MatrixBlock mb2 = DataConverter.convertToMatrixBlock(B2);
        long nnz = mb1.getNonZeros() + mb2.getNonZeros();
        mb1.setSparseBlock(SparseBlockFactory.copySparseBlock(btype1, mb1.getSparseBlock(), false));
        mb2.setSparseBlock(SparseBlockFactory.copySparseBlock(btype2, mb2.getSparseBlock(), false));
        // execute merge
        mb1.merge(mb2, false);
        // check for correct number of non-zeros
        if (nnz != mb1.getNonZeros())
            Assert.fail("Wrong number of non-zeros: " + mb1.getNonZeros() + ", expected: " + nnz);
        // check correct values
        long count = 0;
        SparseBlock sblock = mb1.getSparseBlock();
        if (sblock != null) {
            for (int i = 0; i < rows; i++) {
                if (sblock.isEmpty(i))
                    continue;
                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 merge: " + count + ", expected: " + nnz);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock)

Example 13 with SparseBlock

use of org.apache.sysml.runtime.matrix.data.SparseBlock 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);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 14 with SparseBlock

use of org.apache.sysml.runtime.matrix.data.SparseBlock in project incubator-systemml by apache.

the class SparseBlockSize method runSparseBlockSizeTest.

/**
 * @param btype
 * @param sparsity
 */
private void runSparseBlockSizeTest(SparseBlock.Type btype, double sparsity) {
    try {
        // data generation
        double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 123);
        // 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;
        }
        // prepare summary statistics nnz
        int[] rnnz = new int[rows];
        int nnz = 0;
        int nnz2 = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                rnnz[i] += (A[i][j] != 0) ? 1 : 0;
                nnz2 += (i >= rl && j >= cl && i < ru && j < cu && A[i][j] != 0) ? 1 : 0;
            }
            nnz += rnnz[i];
        }
        // check full block nnz
        if (nnz != sblock.size())
            Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
        // check row nnz
        for (int i = 0; i < rows; i++) if (sblock.size(i) != rnnz[i]) {
            Assert.fail("Wrong number of row non-zeros (" + i + "): " + sblock.size(i) + ", expected: " + rnnz[i]);
        }
        // check two row nnz
        for (int i = 1; i < rows; i++) if (sblock.size(i - 1, i + 1) != rnnz[i - 1] + rnnz[i]) {
            Assert.fail("Wrong number of row block non-zeros (" + (i - 1) + "," + (i + 1) + "): " + sblock.size(i - 1, i + 1) + ", expected: " + rnnz[i - 1] + rnnz[i]);
        }
        // check index range nnz
        if (sblock.size(rl, ru, cl, cu) != nnz2)
            Assert.fail("Wrong number of range non-zeros: " + sblock.size(rl, ru, cl, cu) + ", expected: " + nnz2);
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO)

Example 15 with SparseBlock

use of org.apache.sysml.runtime.matrix.data.SparseBlock in project incubator-systemml by apache.

the class SparseBlockAppendSort method runSparseBlockAppendSortTest.

/**
 * @param sparseM1
 * @param sparseM2
 * @param instType
 */
private void runSparseBlockAppendSortTest(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;
        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]);
            Iterator<ADoubleEntry> iter = map.getIterator();
            while (iter.hasNext()) {
                // random hash order
                ADoubleEntry e = iter.next();
                sblock.append((int) e.getKey1(), (int) e.getKey2(), e.value);
            }
        }
        // sort appended values
        sblock.sort();
        // 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);
    }
}
Also used : LongLongDoubleHashMap(org.apache.sysml.runtime.util.LongLongDoubleHashMap) ADoubleEntry(org.apache.sysml.runtime.util.LongLongDoubleHashMap.ADoubleEntry) SparseBlockCSR(org.apache.sysml.runtime.matrix.data.SparseBlockCSR) SparseBlockCOO(org.apache.sysml.runtime.matrix.data.SparseBlockCOO) SparseBlockMCSR(org.apache.sysml.runtime.matrix.data.SparseBlockMCSR) Iterator(java.util.Iterator) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock)

Aggregations

SparseBlock (org.apache.sysml.runtime.matrix.data.SparseBlock)24 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)14 SparseBlockMCSR (org.apache.sysml.runtime.matrix.data.SparseBlockMCSR)12 SparseBlockCOO (org.apache.sysml.runtime.matrix.data.SparseBlockCOO)11 SparseBlockCSR (org.apache.sysml.runtime.matrix.data.SparseBlockCSR)11 IJV (org.apache.sysml.runtime.matrix.data.IJV)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)4 LongLongDoubleHashMap (org.apache.sysml.runtime.util.LongLongDoubleHashMap)2 ADoubleEntry (org.apache.sysml.runtime.util.LongLongDoubleHashMap.ADoubleEntry)2 BufferedWriter (java.io.BufferedWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Iterator (java.util.Iterator)1 DoubleIntListHashMap (org.apache.sysml.runtime.compress.utils.DoubleIntListHashMap)1 IntArrayList (org.apache.sysml.runtime.compress.utils.IntArrayList)1 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)1 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)1 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)1 BooleanObject (org.apache.sysml.runtime.instructions.cp.BooleanObject)1 CSVFileFormatProperties (org.apache.sysml.runtime.matrix.data.CSVFileFormatProperties)1 DenseBlock (org.apache.sysml.runtime.matrix.data.DenseBlock)1