Search in sources :

Example 61 with IJV

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

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

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

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

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