Search in sources :

Example 31 with Matrix

use of org.apache.sysml.udf.Matrix in project systemml by apache.

the class RowClassMeet method execute.

@Override
public void execute() {
    try {
        MatrixBlock A = ((Matrix) getFunctionInput(0)).getMatrixObject().acquireRead();
        MatrixBlock B = ((Matrix) getFunctionInput(1)).getMatrixObject().acquireRead();
        int nr = Math.max(A.getNumRows(), B.getNumRows());
        int nc = Math.max(A.getNumColumns(), B.getNumColumns());
        MatrixBlock C = new MatrixBlock(nr, nc, false).allocateBlock();
        MatrixBlock N = new MatrixBlock(nr, nc, false).allocateBlock();
        double[] dC = C.getDenseBlockValues();
        double[] dN = N.getDenseBlockValues();
        // wrap both A and B into side inputs for efficient sparse access
        SideInput sB = CodegenUtils.createSideInput(B);
        boolean mv = (B.getNumRows() == 1);
        int numCols = Math.min(A.getNumColumns(), B.getNumColumns());
        HashMap<ClassLabel, IntArrayList> classLabelMapping = new HashMap<>();
        for (int i = 0, ai = 0; i < A.getNumRows(); i++, ai += A.getNumColumns()) {
            classLabelMapping.clear();
            sB.reset();
            if (A.isInSparseFormat()) {
                if (A.getSparseBlock() == null || A.getSparseBlock().isEmpty(i))
                    continue;
                int alen = A.getSparseBlock().size(i);
                int apos = A.getSparseBlock().pos(i);
                int[] aix = A.getSparseBlock().indexes(i);
                double[] avals = A.getSparseBlock().values(i);
                for (int k = apos; k < apos + alen; k++) {
                    if (aix[k] >= numCols)
                        break;
                    int bval = (int) sB.getValue(mv ? 0 : i, aix[k]);
                    if (bval != 0) {
                        ClassLabel key = new ClassLabel((int) avals[k], bval);
                        if (!classLabelMapping.containsKey(key))
                            classLabelMapping.put(key, new IntArrayList());
                        classLabelMapping.get(key).appendValue(aix[k]);
                    }
                }
            } else {
                double[] denseBlk = A.getDenseBlockValues();
                if (denseBlk == null)
                    break;
                for (int j = 0; j < numCols; j++) {
                    int aVal = (int) denseBlk[ai + j];
                    int bVal = (int) sB.getValue(mv ? 0 : i, j);
                    if (aVal != 0 && bVal != 0) {
                        ClassLabel key = new ClassLabel(aVal, bVal);
                        if (!classLabelMapping.containsKey(key))
                            classLabelMapping.put(key, new IntArrayList());
                        classLabelMapping.get(key).appendValue(j);
                    }
                }
            }
            int labelID = 1;
            for (Entry<ClassLabel, IntArrayList> entry : classLabelMapping.entrySet()) {
                int nVal = entry.getValue().size();
                int[] list = entry.getValue().extractValues();
                for (int k = 0, off = i * nc; k < nVal; k++) {
                    dN[off + list[k]] = nVal;
                    dC[off + list[k]] = labelID;
                }
                labelID++;
            }
        }
        ((Matrix) getFunctionInput(0)).getMatrixObject().release();
        ((Matrix) getFunctionInput(1)).getMatrixObject().release();
        // prepare outputs
        C.recomputeNonZeros();
        C.examSparsity();
        CMat = new Matrix(createOutputFilePathAndName("TMP"), nr, nc, ValueType.Double);
        CMat.setMatrixDoubleArray(C, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
        N.recomputeNonZeros();
        N.examSparsity();
        NMat = new Matrix(createOutputFilePathAndName("TMP"), nr, nc, ValueType.Double);
        NMat.setMatrixDoubleArray(N, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
    } catch (DMLRuntimeException | IOException e) {
        throw new RuntimeException("Error while executing RowClassMeet", e);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) HashMap(java.util.HashMap) IOException(java.io.IOException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) Matrix(org.apache.sysml.udf.Matrix) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IntArrayList(org.apache.sysml.runtime.compress.utils.IntArrayList) SideInput(org.apache.sysml.runtime.codegen.SpoofOperator.SideInput)

Example 32 with Matrix

use of org.apache.sysml.udf.Matrix in project systemml by apache.

the class CumSumProd method execute.

@Override
public void execute() {
    X = ((Matrix) getFunctionInput(0)).getMatrixObject().acquireRead();
    C = ((Matrix) getFunctionInput(1)).getMatrixObject().acquireRead();
    if (X.getNumRows() != C.getNumRows())
        throw new RuntimeException("Number of rows of X and C should match");
    if (X.getNumColumns() != C.getNumColumns() && C.getNumColumns() != 1)
        throw new RuntimeException("Incorrect Number of columns of X and C (Expected C to be of same dimension or a vector)");
    start = Double.parseDouble(((Scalar) getFunctionInput(2)).getValue());
    isReverse = Boolean.parseBoolean(((Scalar) getFunctionInput(3)).getValue());
    numRetRows = X.getNumRows();
    numRetCols = X.getNumColumns();
    allocateOutput();
    // Copy X to Y
    denseBlock = retMB.getDenseBlockValues();
    if (X.isInSparseFormat()) {
        Iterator<IJV> iter = X.getSparseBlockIterator();
        while (iter.hasNext()) {
            IJV ijv = iter.next();
            denseBlock[ijv.getI() * numRetCols + ijv.getJ()] = ijv.getV();
        }
    } else {
        if (X.getDenseBlock() != null)
            System.arraycopy(X.getDenseBlockValues(), 0, denseBlock, 0, denseBlock.length);
    }
    if (!isReverse) {
        // Y [1, ] = X [1, ] + C [1, ] * start;
        // Y [i+1, ] = X [i+1, ] + C [i+1, ] * Y [i, ]
        addCNConstant(0, start);
        for (int i = 1; i < numRetRows; i++) {
            addC(i, true);
        }
    } else {
        // Y [m, ] = X [m, ] + C [m, ] * start;
        // Y [i-1, ] = X [i-1, ] + C [i-1, ] * Y [i, ]
        addCNConstant(numRetRows - 1, start);
        for (int i = numRetRows - 2; i >= 0; i--) {
            addC(i, false);
        }
    }
    ((Matrix) getFunctionInput(1)).getMatrixObject().release();
    ((Matrix) getFunctionInput(0)).getMatrixObject().release();
    retMB.recomputeNonZeros();
    try {
        retMB.examSparsity();
        ret.setMatrixDoubleArray(retMB, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
    } catch (DMLRuntimeException e) {
        throw new RuntimeException("Error while executing CumSumProd", e);
    } catch (IOException e) {
        throw new RuntimeException("Error while executing CumSumProd", e);
    }
}
Also used : Matrix(org.apache.sysml.udf.Matrix) 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 33 with Matrix

use of org.apache.sysml.udf.Matrix in project systemml by apache.

the class DynamicProjectMatrixCP method execute.

@Override
public void execute() {
    try {
        Matrix mD = (Matrix) this.getFunctionInput(0);
        Matrix mC = (Matrix) this.getFunctionInput(1);
        MatrixBlock mbD = mD.getMatrixObject().acquireRead();
        MatrixBlock mbC = mC.getMatrixObject().acquireRead();
        int rows = mbC.getNumColumns();
        int cols = mbC.getNumColumns();
        String dir = createOutputFilePathAndName(OUTPUT_FILE);
        MatrixBlock mb = null;
        if (// VECTOR
        mbD.getNumColumns() == 1) {
            cols = 1;
            mb = new MatrixBlock(rows, cols, false);
            for (int i = 0; i < rows; i++) {
                int ix1 = (int) mbC.quickGetValue(0, i) - 1;
                double val = mbD.quickGetValue(ix1, 0);
                mb.quickSetValue(i, 0, val);
            }
        } else // MATRIX
        {
            mb = new MatrixBlock(rows, cols, false);
            for (int i = 0; i < rows; i++) {
                int ix1 = (int) mbC.quickGetValue(0, i) - 1;
                for (int j = 0; j < cols; j++) {
                    int ix2 = (int) mbC.quickGetValue(0, j) - 1;
                    double val = mbD.quickGetValue(ix1, ix2);
                    mb.quickSetValue(i, j, val);
                }
            }
        }
        _ret = new Matrix(dir, rows, cols, ValueType.Double);
        _ret.setMatrixDoubleArray(mb, OutputInfo.BinaryBlockOutputInfo, InputInfo.BinaryBlockInputInfo);
        mD.getMatrixObject().release();
        mC.getMatrixObject().release();
    } catch (Exception e) {
        throw new RuntimeException("Error executing dynamic project of matrix", e);
    }
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) Matrix(org.apache.sysml.udf.Matrix)

Aggregations

Matrix (org.apache.sysml.udf.Matrix)33 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)19 Scalar (org.apache.sysml.udf.Scalar)14 IOException (java.io.IOException)8 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)8 OutputInfo (org.apache.sysml.runtime.matrix.data.OutputInfo)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 StringTokenizer (java.util.StringTokenizer)4 InputInfo (org.apache.sysml.runtime.matrix.data.InputInfo)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)3 DataOutputStream (java.io.DataOutputStream)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 LongWritable (org.apache.hadoop.io.LongWritable)2 Text (org.apache.hadoop.io.Text)2 InputSplit (org.apache.hadoop.mapred.InputSplit)2 JobConf (org.apache.hadoop.mapred.JobConf)2 TextInputFormat (org.apache.hadoop.mapred.TextInputFormat)2