Search in sources :

Example 16 with SparseBlock

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

the class SparseBlockIterator method runSparseBlockIteratorTest.

/**
 * @param sparseM1
 * @param sparseM2
 * @param instType
 */
private void runSparseBlockIteratorTest(SparseBlock.Type btype, double sparsity, boolean partial) {
    try {
        // data generation
        double[][] A = getRandomMatrix(rows, cols, -10, 10, sparsity, 8765432);
        // 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;
        int rl = partial ? rlPartial : 0;
        for (int i = rl; i < rows; i++) {
            for (int j = 0; j < cols; j++) rnnz[i] += (A[i][j] != 0) ? 1 : 0;
            nnz += rnnz[i];
        }
        if (!partial && nnz != sblock.size())
            Assert.fail("Wrong number of non-zeros: " + sblock.size() + ", expected: " + nnz);
        // check correct isEmpty return
        for (int i = rl; 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 = !partial ? sblock.getIterator() : sblock.getIterator(rl, rows);
        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 17 with SparseBlock

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

the class CompressedMatrixBlock method decompress.

/**
 * Decompress block.
 *
 * @return a new uncompressed matrix block containing the contents of this
 *         block
 */
public MatrixBlock decompress() {
    // early abort for not yet compressed blocks
    if (!isCompressed())
        return new MatrixBlock(this);
    Timing time = new Timing(true);
    // preallocation sparse rows to avoid repeated reallocations
    MatrixBlock ret = new MatrixBlock(getNumRows(), getNumColumns(), isInSparseFormat(), getNonZeros());
    if (ret.isInSparseFormat()) {
        int[] rnnz = new int[rlen];
        for (ColGroup grp : _colGroups) grp.countNonZerosPerRow(rnnz, 0, rlen);
        ret.allocateSparseRowsBlock();
        SparseBlock rows = ret.getSparseBlock();
        for (int i = 0; i < rlen; i++) rows.allocate(i, rnnz[i]);
    }
    // core decompression (append if sparse)
    for (ColGroup grp : _colGroups) grp.decompressToBlock(ret, 0, rlen);
    // post-processing (for append in decompress)
    ret.setNonZeros(nonZeros);
    if (ret.isInSparseFormat())
        ret.sortSparseRows();
    if (LOG.isDebugEnabled())
        LOG.debug("decompressed block in " + time.stop() + "ms.");
    return ret;
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock)

Example 18 with SparseBlock

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

the class SpoofCellwise method executeCompressedNoAgg.

private long executeCompressedNoAgg(CompressedMatrixBlock a, SideInput[] b, double[] scalars, MatrixBlock out, int m, int n, boolean sparseSafe, int rl, int ru) {
    double[] c = (out.getDenseBlock() != null) ? out.getDenseBlockValues() : null;
    SparseBlock csblock = out.getSparseBlock();
    // whenever k/2 * BITMAP_BLOCK_SZ > m (i.e., it does not limit parallelism)
    if (out.isInSparseFormat() && rl % BitmapEncoder.BITMAP_BLOCK_SZ == 0 && ru % BitmapEncoder.BITMAP_BLOCK_SZ == 0) {
        int[] rnnz = a.countNonZerosPerRow(rl, ru);
        for (int i = rl; i < ru; i++) csblock.allocate(i, rnnz[i - rl]);
    }
    long lnnz = 0;
    Iterator<IJV> iter = a.getIterator(rl, ru, !sparseSafe);
    while (iter.hasNext()) {
        IJV cell = iter.next();
        double val = genexec(cell.getV(), b, scalars, m, n, cell.getI(), cell.getJ());
        if (out.isInSparseFormat()) {
            csblock.allocate(cell.getI());
            csblock.append(cell.getI(), cell.getJ(), val);
        } else
            c[cell.getI() * n + cell.getJ()] = val;
        lnnz += (val != 0) ? 1 : 0;
    }
    return lnnz;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock)

Example 19 with SparseBlock

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

the class SpoofOuterProduct method executeCellwiseSparse.

private void executeCellwiseSparse(SparseBlock sblock, DenseBlock u, DenseBlock v, SideInput[] b, double[] scalars, MatrixBlock out, int m, int n, int k, long nnz, OutProdType type, int rl, int ru, int cl, int cu) {
    // NOTE: we don't create sparse side inputs w/ row-major cursors because
    // cache blocking would lead to non-sequential access
    final int blocksizeIJ = (int) (8L * m * n / nnz);
    int[] curk = new int[Math.min(blocksizeIJ, ru - rl)];
    if (// DENSE
    !out.isInSparseFormat()) {
        DenseBlock c = out.getDenseBlock();
        double tmp = 0;
        for (int bi = rl; bi < ru; bi += blocksizeIJ) {
            int bimin = Math.min(ru, bi + blocksizeIJ);
            // prepare starting indexes for block row
            Arrays.fill(curk, 0);
            // blocked execution over column blocks
            for (int bj = 0; bj < n; bj += blocksizeIJ) {
                int bjmin = Math.min(n, bj + blocksizeIJ);
                for (int i = bi; i < bimin; i++) {
                    if (sblock.isEmpty(i))
                        continue;
                    int wpos = sblock.pos(i);
                    int wlen = sblock.size(i);
                    int[] wix = sblock.indexes(i);
                    double[] wvals = sblock.values(i);
                    double[] cvals = c.values(i);
                    double[] uvals = u.values(i);
                    int uix = u.pos(i);
                    int index = wpos + curk[i - bi];
                    if (type == OutProdType.CELLWISE_OUTER_PRODUCT)
                        for (; index < wpos + wlen && wix[index] < bjmin; index++) {
                            int jix = wix[index];
                            cvals[jix] = genexecCellwise(wvals[index], uvals, uix, v.values(jix), v.pos(jix), b, scalars, m, n, k, i, wix[index]);
                        }
                    else
                        for (; index < wpos + wlen && wix[index] < bjmin; index++) {
                            int jix = wix[index];
                            tmp += genexecCellwise(wvals[index], uvals, uix, v.values(jix), v.pos(jix), b, scalars, m, n, k, i, wix[index]);
                        }
                    curk[i - bi] = index - wpos;
                }
            }
        }
        if (type != OutProdType.CELLWISE_OUTER_PRODUCT)
            c.set(0, 0, tmp);
    } else // SPARSE
    {
        SparseBlock c = out.getSparseBlock();
        for (int bi = rl; bi < ru; bi += blocksizeIJ) {
            int bimin = Math.min(ru, bi + blocksizeIJ);
            // prepare starting indexes for block row
            Arrays.fill(curk, 0);
            // blocked execution over column blocks
            for (int bj = 0; bj < n; bj += blocksizeIJ) {
                int bjmin = Math.min(n, bj + blocksizeIJ);
                for (int i = bi; i < bimin; i++) {
                    if (sblock.isEmpty(i))
                        continue;
                    int wpos = sblock.pos(i);
                    int wlen = sblock.size(i);
                    int[] wix = sblock.indexes(i);
                    double[] wval = sblock.values(i);
                    double[] uvals = u.values(i);
                    int uix = u.pos(i);
                    int index = wpos + curk[i - bi];
                    for (; index < wpos + wlen && wix[index] < bjmin; index++) {
                        int jix = wix[index];
                        c.append(i, wix[index], genexecCellwise(wval[index], uvals, uix, v.values(jix), v.pos(jix), b, scalars, m, n, k, i, wix[index]));
                    }
                    curk[i - bi] = index - wpos;
                }
            }
        }
    }
}
Also used : DenseBlock(org.apache.sysml.runtime.matrix.data.DenseBlock) SparseBlock(org.apache.sysml.runtime.matrix.data.SparseBlock)

Example 20 with SparseBlock

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

the class SpoofOuterProduct method executeCellwiseCompressed.

private void executeCellwiseCompressed(CompressedMatrixBlock a, DenseBlock u, DenseBlock v, SideInput[] b, double[] scalars, MatrixBlock out, int m, int n, int k, OutProdType type, int rl, int ru, int cl, int cu) {
    // NOTE: we don't create sparse side inputs w/ row-major cursors because
    // compressed data is access in a column-major order
    double[] c = (out.getDenseBlock() != null) ? out.getDenseBlockValues() : null;
    SparseBlock csblock = out.getSparseBlock();
    Iterator<IJV> iter = a.getIterator(rl, ru, false);
    while (iter.hasNext()) {
        IJV cell = iter.next();
        double[] uvals = u.values(cell.getI());
        double[] vvals = v.values(cell.getJ());
        int uix = u.pos(cell.getI());
        int vix = v.pos(cell.getJ());
        if (type == OutProdType.CELLWISE_OUTER_PRODUCT) {
            if (out.isInSparseFormat()) {
                csblock.allocate(cell.getI());
                csblock.append(cell.getI(), cell.getJ(), genexecCellwise(cell.getV(), uvals, uix, vvals, vix, b, scalars, m, n, k, cell.getI(), cell.getJ()));
            } else {
                c[cell.getI() * n + cell.getJ()] = genexecCellwise(cell.getV(), uvals, uix, vvals, vix, b, scalars, m, n, k, cell.getI(), cell.getJ());
            }
        } else {
            c[0] += genexecCellwise(cell.getV(), uvals, uix, vvals, vix, b, scalars, m, n, k, cell.getI(), cell.getJ());
        }
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) 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