Search in sources :

Example 31 with IJV

use of org.apache.sysml.runtime.matrix.data.IJV 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)

Example 32 with IJV

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

the class SpoofOuterProduct method executeCompressed.

private void executeCompressed(CompressedMatrixBlock a, DenseBlock u, DenseBlock v, SideInput[] b, double[] scalars, DenseBlock c, 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
    boolean left = (_outerProductType == OutProdType.LEFT_OUTER_PRODUCT);
    Iterator<IJV> iter = !left ? a.getIterator(rl, ru, false) : // cl/cu -> colgroups
    a.getIterator(rl, ru, cl, cu, false);
    while (iter.hasNext()) {
        IJV cell = iter.next();
        double[] uvals = u.values(cell.getI());
        double[] vvals = v.values(cell.getJ());
        double[] cvals = c.values(left ? cell.getJ() : cell.getI());
        int uix = u.pos(cell.getI());
        int vix = v.pos(cell.getJ());
        genexecDense(cell.getV(), uvals, uix, vvals, vix, b, scalars, cvals, left ? vix : uix, m, n, k, cell.getI(), cell.getJ());
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 33 with IJV

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

the class WriterBinaryCell method writeBinaryCellMatrixToHDFS.

@SuppressWarnings("deprecation")
protected void writeBinaryCellMatrixToHDFS(Path path, JobConf job, MatrixBlock src, long rlen, long clen, int brlen, int bclen) throws IOException {
    boolean sparse = src.isInSparseFormat();
    boolean entriesWritten = false;
    FileSystem fs = IOUtilFunctions.getFileSystem(path, job);
    SequenceFile.Writer writer = new SequenceFile.Writer(fs, job, path, MatrixIndexes.class, MatrixCell.class);
    MatrixIndexes indexes = new MatrixIndexes();
    MatrixCell cell = new MatrixCell();
    int rows = src.getNumRows();
    int cols = src.getNumColumns();
    try {
        // bound check per block
        if (rows > rlen || cols > clen) {
            throw new IOException("Matrix block [1:" + rows + ",1:" + cols + "] " + "out of overall matrix range [1:" + rlen + ",1:" + clen + "].");
        }
        if (// SPARSE
        sparse) {
            Iterator<IJV> iter = src.getSparseBlockIterator();
            while (iter.hasNext()) {
                IJV lcell = iter.next();
                indexes.setIndexes(lcell.getI() + 1, lcell.getJ() + 1);
                cell.setValue(lcell.getV());
                writer.append(indexes, cell);
                entriesWritten = true;
            }
        } else // DENSE
        {
            for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) {
                double lvalue = src.getValueDenseUnsafe(i, j);
                if (// for nnz
                lvalue != 0) {
                    indexes.setIndexes(i + 1, j + 1);
                    cell.setValue(lvalue);
                    writer.append(indexes, cell);
                    entriesWritten = true;
                }
            }
        }
        // handle empty result
        if (!entriesWritten) {
            writer.append(new MatrixIndexes(1, 1), new MatrixCell(0));
        }
    } finally {
        IOUtilFunctions.closeSilently(writer);
    }
}
Also used : SequenceFile(org.apache.hadoop.io.SequenceFile) IJV(org.apache.sysml.runtime.matrix.data.IJV) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) FileSystem(org.apache.hadoop.fs.FileSystem) MatrixCell(org.apache.sysml.runtime.matrix.data.MatrixCell) IOException(java.io.IOException)

Example 34 with IJV

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

the class WriterTextCell method writeTextCellMatrixToFile.

protected static void writeTextCellMatrixToFile(Path path, JobConf job, FileSystem fs, MatrixBlock src, int rl, int ru) throws IOException {
    boolean sparse = src.isInSparseFormat();
    int clen = src.getNumColumns();
    try (BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(path, true)))) {
        // for obj reuse and preventing repeated buffer re-allocations
        StringBuilder sb = new StringBuilder();
        if (// SPARSE
        sparse) {
            Iterator<IJV> iter = src.getSparseBlockIterator(rl, ru);
            while (iter.hasNext()) {
                IJV cell = iter.next();
                sb.append(cell.getI() + 1);
                sb.append(' ');
                sb.append(cell.getJ() + 1);
                sb.append(' ');
                sb.append(cell.getV());
                sb.append('\n');
                // same as append
                br.write(sb.toString());
                sb.setLength(0);
            }
        } else // DENSE
        {
            for (int i = rl; i < ru; i++) {
                String rowIndex = Integer.toString(i + 1);
                for (int j = 0; j < clen; j++) {
                    double lvalue = src.getValueDenseUnsafe(i, j);
                    if (// for nnz
                    lvalue != 0) {
                        sb.append(rowIndex);
                        sb.append(' ');
                        sb.append(j + 1);
                        sb.append(' ');
                        sb.append(lvalue);
                        sb.append('\n');
                        // same as append
                        br.write(sb.toString());
                        sb.setLength(0);
                    }
                }
            }
        }
        // handle empty result
        if (src.isEmptyBlock(false) && rl == 0)
            br.write(IOUtilFunctions.EMPTY_TEXT_LINE);
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 35 with IJV

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

the class SGDNesterovUpdate method multiplyByConstant.

private static void multiplyByConstant(MatrixBlock in, double constant, double[] out) {
    if (in.isInSparseFormat()) {
        Iterator<IJV> iter = in.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV ijv = iter.next();
            out[ijv.getI() * ijv.getJ()] += ijv.getV() * constant;
        }
    } else {
        double[] denseBlock = in.getDenseBlockValues();
        if (denseBlock != null) {
            // If not empty block
            for (int i = 0; i < out.length; i++) {
                out[i] += denseBlock[i] * constant;
            }
        }
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Aggregations

IJV (org.apache.sysml.runtime.matrix.data.IJV)43 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)12 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)7 BufferedWriter (java.io.BufferedWriter)6 OutputStreamWriter (java.io.OutputStreamWriter)6 FileSystem (org.apache.hadoop.fs.FileSystem)6 SequenceFile (org.apache.hadoop.io.SequenceFile)6 SparseBlock (org.apache.sysml.runtime.matrix.data.SparseBlock)6 File (java.io.File)5 Path (org.apache.hadoop.fs.Path)5 JobConf (org.apache.hadoop.mapred.JobConf)5 IOException (java.io.IOException)4 Iterator (java.util.Iterator)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)4 MatrixCell (org.apache.sysml.runtime.matrix.data.MatrixCell)4 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)4 ArrayList (java.util.ArrayList)3 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)3 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)3 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)3