Search in sources :

Example 51 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 52 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 53 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)

Example 54 with IJV

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

the class SGDNesterovUpdate method copy.

// Assumption dest is zero-ed out.
private static void copy(MatrixBlock src, double[] dest) {
    if (src.isInSparseFormat()) {
        Iterator<IJV> iter = src.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV ijv = iter.next();
            dest[ijv.getI() * ijv.getJ()] = ijv.getV();
        }
    } else {
        double[] denseBlock = src.getDenseBlockValues();
        if (denseBlock != null) {
            // If not empty block
            System.arraycopy(denseBlock, 0, dest, 0, dest.length);
        }
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 55 with IJV

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

the class DataConverter method convertToLongVector.

public static long[] convertToLongVector(MatrixBlock mb) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    // 0-initialized
    long[] ret = new long[rows * cols];
    if (mb.isEmptyBlock(false))
        return ret;
    if (mb.isInSparseFormat()) {
        Iterator<IJV> iter = mb.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV cell = iter.next();
            ret[cell.getI() * cols + cell.getJ()] = (int) cell.getV();
        }
    } else {
        // memcopy row major representation if at least 1 non-zero
        for (int i = 0, cix = 0; i < rows; i++) for (int j = 0; j < cols; j++, cix++) ret[cix] = (int) (mb.getValueDenseUnsafe(i, j));
    }
    return ret;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Aggregations

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