Search in sources :

Example 56 with IJV

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

the class DataConverter method convertToDenseBlock.

public static DenseBlock convertToDenseBlock(MatrixBlock mb, boolean deep) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    DenseBlock ret = (!mb.isInSparseFormat() && mb.isAllocated() && !deep) ? mb.getDenseBlock() : // 0-initialized
    DenseBlockFactory.createDenseBlock(rows, cols);
    if (!mb.isEmptyBlock(false)) {
        if (mb.isInSparseFormat()) {
            Iterator<IJV> iter = mb.getSparseBlockIterator();
            while (iter.hasNext()) {
                IJV cell = iter.next();
                ret.set(cell.getI(), cell.getJ(), cell.getV());
            }
        } else if (deep) {
            ret.set(mb.getDenseBlock());
        }
    }
    return ret;
}
Also used : DenseBlock(org.apache.sysml.runtime.matrix.data.DenseBlock) IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 57 with IJV

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

the class DataConverter method convertToIntVector.

public static int[] convertToIntVector(MatrixBlock mb) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    // 0-initialized
    int[] ret = new int[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)

Example 58 with IJV

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

the class DataConverter method convertToBooleanVector.

public static boolean[] convertToBooleanVector(MatrixBlock mb) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    // false-initialized
    boolean[] ret = new boolean[rows * cols];
    if (mb.getNonZeros() > 0) {
        if (mb.isInSparseFormat()) {
            Iterator<IJV> iter = mb.getSparseBlockIterator();
            while (iter.hasNext()) {
                IJV cell = iter.next();
                ret[cell.getI() * cols + cell.getJ()] = (cell.getV() != 0.0);
            }
        } else {
            for (int i = 0, cix = 0; i < rows; i++) for (int j = 0; j < cols; j++, cix++) ret[cix] = (mb.getValueDenseUnsafe(i, j) != 0.0);
        }
    }
    return ret;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 59 with IJV

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

the class DataConverter method convertToMatrixBlockPartitions.

public static MatrixBlock[] convertToMatrixBlockPartitions(MatrixBlock mb, boolean colwise) {
    MatrixBlock[] ret = null;
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    long nnz = mb.getNonZeros();
    boolean sparse = mb.isInSparseFormat();
    double sparsity = ((double) nnz) / (rows * cols);
    if (// COL PARTITIONS
    colwise) {
        // allocate output partitions
        ret = new MatrixBlock[cols];
        for (int j = 0; j < cols; j++) ret[j] = new MatrixBlock(rows, 1, false);
        // cache-friendly sequential read/append
        if (!mb.isEmptyBlock(false)) {
            if (sparse) {
                // SPARSE
                Iterator<IJV> iter = mb.getSparseBlockIterator();
                while (iter.hasNext()) {
                    IJV cell = iter.next();
                    ret[cell.getJ()].appendValue(cell.getI(), 0, cell.getV());
                }
            } else {
                // DENSE
                for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ret[j].appendValue(i, 0, mb.getValueDenseUnsafe(i, j));
            }
        }
    } else // ROW PARTITIONS
    {
        // allocate output partitions
        ret = new MatrixBlock[rows];
        for (int i = 0; i < rows; i++) ret[i] = new MatrixBlock(1, cols, sparse, (long) (cols * sparsity));
        // cache-friendly sparse/dense row slicing
        if (!mb.isEmptyBlock(false)) {
            for (int i = 0; i < rows; i++) mb.slice(i, i, 0, cols - 1, ret[i]);
        }
    }
    return ret;
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 60 with IJV

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

the class DataConverter method toString.

/**
 * Returns a string representation of a matrix
 * @param mb matrix block
 * @param sparse if true, string will contain a table with row index, col index, value (where value != 0.0)
 * 				 otherwise it will be a rectangular string with all values of the matrix block
 * @param separator Separator string between each element in a row, or between the columns in sparse format
 * @param lineseparator Separator string between each row
 * @param rowsToPrint maximum number of rows to print, -1 for all
 * @param colsToPrint maximum number of columns to print, -1 for all
 * @param decimal number of decimal places to print, -1 for default
 * @return matrix as a string
 */
public static String toString(MatrixBlock mb, boolean sparse, String separator, String lineseparator, int rowsToPrint, int colsToPrint, int decimal) {
    StringBuffer sb = new StringBuffer();
    // Setup number of rows and columns to print
    int rlen = mb.getNumRows();
    int clen = mb.getNumColumns();
    int rowLength = rlen;
    int colLength = clen;
    if (rowsToPrint >= 0)
        rowLength = rowsToPrint < rlen ? rowsToPrint : rlen;
    if (colsToPrint >= 0)
        colLength = colsToPrint < clen ? colsToPrint : clen;
    DecimalFormat df = new DecimalFormat();
    df.setGroupingUsed(false);
    if (decimal >= 0) {
        df.setMinimumFractionDigits(decimal);
    }
    if (sparse) {
        // Sparse Print Format
        if (mb.isInSparseFormat()) {
            // Block is in sparse format
            Iterator<IJV> sbi = mb.getSparseBlockIterator();
            while (sbi.hasNext()) {
                IJV ijv = sbi.next();
                int row = ijv.getI();
                int col = ijv.getJ();
                double value = ijv.getV();
                if (row < rowLength && col < colLength) {
                    // Print (row+1) and (col+1) since for a DML user, everything is 1-indexed
                    sb.append(row + 1).append(separator).append(col + 1).append(separator);
                    sb.append(dfFormat(df, value)).append(lineseparator);
                }
            }
        } else {
            // Block is in dense format
            for (int i = 0; i < rowLength; i++) {
                for (int j = 0; j < colLength; j++) {
                    double value = mb.getValue(i, j);
                    if (value != 0.0) {
                        sb.append(i + 1).append(separator).append(j + 1).append(separator);
                        sb.append(dfFormat(df, value)).append(lineseparator);
                    }
                }
            }
        }
    } else {
        // Dense Print Format
        for (int i = 0; i < rowLength; i++) {
            for (int j = 0; j < colLength - 1; j++) {
                Double value = mb.quickGetValue(i, j);
                if (value.equals(-0.0d))
                    value = 0.0;
                sb.append(dfFormat(df, value));
                sb.append(separator);
            }
            Double value = mb.quickGetValue(i, colLength - 1);
            if (value.equals(-0.0d))
                value = 0.0;
            // Do not put separator after last element
            sb.append(dfFormat(df, value));
            sb.append(lineseparator);
        }
    }
    return sb.toString();
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) DecimalFormat(java.text.DecimalFormat)

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