Search in sources :

Example 11 with IJV

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

the class MultiInputCbind method execute.

@Override
public void execute() {
    int numInputs = Integer.parseInt(((Scalar) getFunctionInput(0)).getValue());
    spagetize = Boolean.parseBoolean(((Scalar) getFunctionInput(1)).getValue());
    // Compute output dimensions
    numRetCols = 0;
    if (spagetize) {
        // Assumption the inputs are of same shape
        MatrixBlock in = ((Matrix) getFunctionInput(2)).getMatrixObject().acquireRead();
        numRetRows = in.getNumRows() * in.getNumColumns();
        numRetCols = numInputs;
        ((Matrix) getFunctionInput(2)).getMatrixObject().release();
    } else {
        for (int inputID = 2; inputID < numInputs + 2; inputID++) {
            MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
            numRetRows = in.getNumRows();
            numRetCols += in.getNumColumns();
            ((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
        }
    }
    allocateOutput();
    // Performs cbind (cbind (cbind ( X1, X2 ), X3 ), X4)
    double[] retData = retMB.getDenseBlockValues();
    int startColumn = 0;
    for (int inputID = 2; inputID < numInputs + 2; inputID++) {
        MatrixBlock in = ((Matrix) getFunctionInput(inputID)).getMatrixObject().acquireRead();
        if (spagetize && in.getNumRows() * in.getNumColumns() != numRetRows) {
            throw new RuntimeException("Expected the inputs to be of same size when spagetization is turned on.");
        }
        int inputNumCols = in.getNumColumns();
        if (in.isInSparseFormat()) {
            Iterator<IJV> iter = in.getSparseBlockIterator();
            while (iter.hasNext()) {
                IJV ijv = iter.next();
                if (spagetize) {
                    // Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
                    // Output Column ID = inputID-2 for all elements of inputs
                    int outputRowIndex = ijv.getI() * inputNumCols + ijv.getJ();
                    int outputColIndex = inputID - 2;
                    retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
                } else {
                    // Traditional cbind
                    // Row ID remains the same as that of input
                    int outputRowIndex = ijv.getI();
                    int outputColIndex = ijv.getJ() + startColumn;
                    retData[(int) (outputRowIndex * retMB.getNumColumns() + outputColIndex)] = ijv.getV();
                }
            }
        } else {
            double[] denseBlock = in.getDenseBlockValues();
            if (denseBlock != null) {
                if (spagetize) {
                    // Perform matrix(X1, rows=length(X1), cols=1) operation before cbind
                    // Output Column ID = inputID-2 for all elements of inputs
                    int j = inputID - 2;
                    for (int i = 0; i < numRetRows; i++) {
                        retData[(int) (i * numRetCols + j)] = denseBlock[i];
                    }
                } else {
                    // Row ID remains the same as that of input
                    for (int i = 0; i < retMB.getNumRows(); i++) {
                        for (int j = 0; j < inputNumCols; j++) {
                            int outputColIndex = j + startColumn;
                            retData[(int) (i * numRetCols + outputColIndex)] = denseBlock[i * inputNumCols + j];
                        }
                    }
                }
            }
        }
        ((Matrix) getFunctionInput(inputID)).getMatrixObject().release();
        startColumn += inputNumCols;
    }
    retMB.recomputeNonZeros();
    try {
        retMB.examSparsity();
        ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
    } catch (DMLRuntimeException e) {
        throw new RuntimeException("Error while executing MultiInputCbind", e);
    } catch (IOException e) {
        throw new RuntimeException("Error while executing MultiInputCbind", e);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IJV(org.apache.sysml.runtime.matrix.data.IJV) IOException(java.io.IOException) Scalar(org.apache.sysml.udf.Scalar) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 12 with IJV

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

the class ReblockBuffer method appendBlock.

public void appendBlock(long r_offset, long c_offset, MatrixBlock inBlk, byte index, OutputCollector<Writable, Writable> out) throws IOException {
    if (// SPARSE
    inBlk.isInSparseFormat()) {
        Iterator<IJV> iter = inBlk.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV cell = iter.next();
            long tmp = Double.doubleToRawLongBits(cell.getV());
            _buff[_count][0] = r_offset + cell.getI();
            _buff[_count][1] = c_offset + cell.getJ();
            _buff[_count][2] = tmp;
            _count++;
            // check and flush if required
            if (_count == _bufflen)
                flushBuffer(index, out);
        }
    } else // DENSE
    {
        // System.out.println("dense merge with ro="+r_offset+", co="+c_offset);
        int rlen = inBlk.getNumRows();
        int clen = inBlk.getNumColumns();
        for (int i = 0; i < rlen; i++) for (int j = 0; j < clen; j++) {
            double val = inBlk.getValueDenseUnsafe(i, j);
            if (val != 0) {
                long tmp = Double.doubleToRawLongBits(val);
                _buff[_count][0] = r_offset + i;
                _buff[_count][1] = c_offset + j;
                _buff[_count][2] = tmp;
                _count++;
                // check and flush if required
                if (_count == _bufflen)
                    flushBuffer(index, out);
            }
        }
    }
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 13 with IJV

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

the class DataConverter method convertToDoubleList.

public static List<Double> convertToDoubleList(MatrixBlock mb) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    long nnz = mb.getNonZeros();
    ArrayList<Double> ret = new ArrayList<>();
    if (mb.isInSparseFormat()) {
        Iterator<IJV> iter = mb.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV cell = iter.next();
            ret.add(cell.getV());
        }
        for (long i = nnz; i < (long) rows * cols; i++) // add remaining values
        ret.add(0d);
    } else {
        for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) ret.add(mb.getValueDenseUnsafe(i, j));
    }
    return ret;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV) ArrayList(java.util.ArrayList)

Example 14 with IJV

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

the class DataConverter method convertToDoubleVector.

public static double[] convertToDoubleVector(MatrixBlock mb, boolean deep) {
    int rows = mb.getNumRows();
    int cols = mb.getNumColumns();
    double[] ret = (!mb.isInSparseFormat() && mb.isAllocated() && !deep) ? mb.getDenseBlockValues() : // 0-initialized
    new double[rows * cols];
    if (!mb.isEmptyBlock(false)) {
        if (mb.isInSparseFormat()) {
            Iterator<IJV> iter = mb.getSparseBlockIterator();
            while (iter.hasNext()) {
                IJV cell = iter.next();
                ret[cell.getI() * cols + cell.getJ()] = cell.getV();
            }
        } else if (deep) {
            // memcopy row major representation if at least 1 non-zero
            System.arraycopy(mb.getDenseBlockValues(), 0, ret, 0, rows * cols);
        }
    }
    return ret;
}
Also used : IJV(org.apache.sysml.runtime.matrix.data.IJV)

Example 15 with IJV

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