Search in sources :

Example 1 with ReduceAll

use of org.apache.sysml.runtime.functionobjects.ReduceAll in project incubator-systemml by apache.

the class ComputationSPInstruction method updateUnaryAggOutputMatrixCharacteristics.

protected void updateUnaryAggOutputMatrixCharacteristics(SparkExecutionContext sec, IndexFunction ixFn) throws DMLRuntimeException {
    MatrixCharacteristics mc1 = sec.getMatrixCharacteristics(input1.getName());
    MatrixCharacteristics mcOut = sec.getMatrixCharacteristics(output.getName());
    if (mcOut.dimsKnown())
        return;
    if (!mc1.dimsKnown()) {
        throw new DMLRuntimeException("The output dimensions are not specified and " + "cannot be inferred from input:" + mc1.toString() + " " + mcOut.toString());
    } else {
        //infer statistics from input based on operator
        if (ixFn instanceof ReduceAll)
            mcOut.set(1, 1, mc1.getRowsPerBlock(), mc1.getColsPerBlock());
        else if (ixFn instanceof ReduceCol)
            mcOut.set(mc1.getRows(), 1, mc1.getRowsPerBlock(), mc1.getColsPerBlock());
        else if (ixFn instanceof ReduceRow)
            mcOut.set(1, mc1.getCols(), mc1.getRowsPerBlock(), mc1.getColsPerBlock());
    }
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) ReduceRow(org.apache.sysml.runtime.functionobjects.ReduceRow) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 2 with ReduceAll

use of org.apache.sysml.runtime.functionobjects.ReduceAll in project incubator-systemml by apache.

the class AggregateTernarySPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
    SparkExecutionContext sec = (SparkExecutionContext) ec;
    //get inputs
    MatrixCharacteristics mcIn = sec.getMatrixCharacteristics(input1.getName());
    JavaPairRDD<MatrixIndexes, MatrixBlock> in1 = sec.getBinaryBlockRDDHandleForVariable(input1.getName());
    JavaPairRDD<MatrixIndexes, MatrixBlock> in2 = sec.getBinaryBlockRDDHandleForVariable(input2.getName());
    JavaPairRDD<MatrixIndexes, MatrixBlock> in3 = //matrix or literal 1
    input3.isLiteral() ? //matrix or literal 1
    null : sec.getBinaryBlockRDDHandleForVariable(input3.getName());
    //execute aggregate ternary operation
    AggregateTernaryOperator aggop = (AggregateTernaryOperator) _optr;
    JavaPairRDD<MatrixIndexes, MatrixBlock> out = null;
    if (in3 != null) {
        //3 inputs
        out = in1.join(in2).join(in3).mapToPair(new RDDAggregateTernaryFunction(aggop));
    } else {
        //2 inputs (third is literal 1)
        out = in1.join(in2).mapToPair(new RDDAggregateTernaryFunction2(aggop));
    }
    //aggregate partial results
    if (//tak+*
    aggop.indexFn instanceof ReduceAll) {
        //aggregate and create output (no lineage because scalar)	   
        MatrixBlock tmp = RDDAggregateUtils.sumStable(out.values());
        DoubleObject ret = new DoubleObject(tmp.getValue(0, 0));
        sec.setVariable(output.getName(), ret);
    } else if (//tack+* single block
    mcIn.dimsKnown() && mcIn.getCols() <= mcIn.getColsPerBlock()) {
        //single block aggregation and drop correction
        MatrixBlock ret = RDDAggregateUtils.aggStable(out, aggop.aggOp);
        ret.dropLastRowsOrColums(aggop.aggOp.correctionLocation);
        //put output block into symbol table (no lineage because single block)
        //this also includes implicit maintenance of matrix characteristics
        sec.setMatrixOutput(output.getName(), ret);
    } else //tack+* multi block
    {
        //multi-block aggregation and drop correction
        out = RDDAggregateUtils.aggByKeyStable(out, aggop.aggOp, false);
        out = out.mapValues(new AggregateDropCorrectionFunction(aggop.aggOp));
        //put output RDD handle into symbol table
        updateUnaryAggOutputMatrixCharacteristics(sec, aggop.indexFn);
        sec.setRDDHandleForVariable(output.getName(), out);
        sec.addLineageRDD(output.getName(), input1.getName());
        sec.addLineageRDD(output.getName(), input2.getName());
        if (in3 != null)
            sec.addLineageRDD(output.getName(), input3.getName());
    }
}
Also used : ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixIndexes(org.apache.sysml.runtime.matrix.data.MatrixIndexes) DoubleObject(org.apache.sysml.runtime.instructions.cp.DoubleObject) SparkExecutionContext(org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext) AggregateDropCorrectionFunction(org.apache.sysml.runtime.instructions.spark.functions.AggregateDropCorrectionFunction) AggregateTernaryOperator(org.apache.sysml.runtime.matrix.operators.AggregateTernaryOperator) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics)

Example 3 with ReduceAll

use of org.apache.sysml.runtime.functionobjects.ReduceAll in project incubator-systemml by apache.

the class LibMatrixAgg method aggregateTernaryDense.

private static void aggregateTernaryDense(MatrixBlock in1, MatrixBlock in2, MatrixBlock in3, MatrixBlock ret, IndexFunction ixFn, int rl, int ru) {
    //compute block operations
    KahanObject kbuff = new KahanObject(0, 0);
    KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
    double[] a = in1.denseBlock;
    double[] b1 = in2.denseBlock;
    //if null, literal 1
    double[] b2 = (in3 != null) ? in3.denseBlock : null;
    final int n = in1.clen;
    if (//tak+*
    ixFn instanceof ReduceAll) {
        for (int i = rl, ix = rl * n; i < ru; i++) for (int j = 0; j < n; j++, ix++) {
            double b2val = (b2 != null) ? b2[ix] : 1;
            double val = a[ix] * b1[ix] * b2val;
            kplus.execute2(kbuff, val);
        }
        ret.quickSetValue(0, 0, kbuff._sum);
        ret.quickSetValue(0, 1, kbuff._correction);
    } else //tack+*
    {
        double[] c = ret.getDenseBlock();
        for (int i = rl, ix = rl * n; i < ru; i++) for (int j = 0; j < n; j++, ix++) {
            double b2val = (b2 != null) ? b2[ix] : 1;
            double val = a[ix] * b1[ix] * b2val;
            kbuff._sum = c[j];
            kbuff._correction = c[j + n];
            kplus.execute2(kbuff, val);
            c[j] = kbuff._sum;
            c[j + n] = kbuff._correction;
        }
    }
}
Also used : ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus)

Example 4 with ReduceAll

use of org.apache.sysml.runtime.functionobjects.ReduceAll in project incubator-systemml by apache.

the class LibMatrixAgg method aggregateUnaryMatrixDense.

private static void aggregateUnaryMatrixDense(MatrixBlock in, MatrixBlock out, AggType optype, ValueFunction vFn, IndexFunction ixFn, int rl, int ru) throws DMLRuntimeException {
    final int m = in.rlen;
    final int n = in.clen;
    double[] a = in.getDenseBlock();
    double[] c = out.getDenseBlock();
    switch(optype) {
        case //SUM/TRACE via k+, 
        KAHAN_SUM:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (// SUM
                ixFn instanceof ReduceAll)
                    d_uakp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//ROWSUM
                ixFn instanceof ReduceCol)
                    d_uarkp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//COLSUM
                ixFn instanceof ReduceRow)
                    d_uackp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//TRACE
                ixFn instanceof ReduceDiag)
                    d_uakptrace(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                break;
            }
        case //SUM_SQ via k+,
        KAHAN_SUM_SQ:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (//SUM_SQ
                ixFn instanceof ReduceAll)
                    d_uasqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                else if (//ROWSUM_SQ
                ixFn instanceof ReduceCol)
                    d_uarsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                else if (//COLSUM_SQ
                ixFn instanceof ReduceRow)
                    d_uacsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                break;
            }
        case //CUMSUM
        CUM_KAHAN_SUM:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
                d_ucumkp(a, null, c, m, n, kbuff, kplus, rl, ru);
                break;
            }
        case //CUMPROD
        CUM_PROD:
            {
                d_ucumm(a, null, c, m, n, rl, ru);
                break;
            }
        case CUM_MIN:
        case CUM_MAX:
            {
                double init = Double.MAX_VALUE * ((optype == AggType.CUM_MAX) ? -1 : 1);
                d_ucummxx(a, null, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MIN:
        case //MAX/MIN
        MAX:
            {
                double init = Double.MAX_VALUE * ((optype == AggType.MAX) ? -1 : 1);
                if (// MIN/MAX
                ixFn instanceof ReduceAll)
                    d_uamxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                else if (//ROWMIN/ROWMAX
                ixFn instanceof ReduceCol)
                    d_uarmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                else if (//COLMIN/COLMAX
                ixFn instanceof ReduceRow)
                    d_uacmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MAX_INDEX:
            {
                double init = -Double.MAX_VALUE;
                if (//ROWINDEXMAX
                ixFn instanceof ReduceCol)
                    d_uarimxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MIN_INDEX:
            {
                double init = Double.MAX_VALUE;
                if (//ROWINDEXMIN
                ixFn instanceof ReduceCol)
                    d_uarimin(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case //MEAN
        MEAN:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (// MEAN
                ixFn instanceof ReduceAll)
                    d_uamean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                else if (//ROWMEAN
                ixFn instanceof ReduceCol)
                    d_uarmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                else if (//COLMEAN
                ixFn instanceof ReduceRow)
                    d_uacmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                break;
            }
        case //VAR
        VAR:
            {
                CM_COV_Object cbuff = new CM_COV_Object();
                if (//VAR
                ixFn instanceof ReduceAll)
                    d_uavar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                else if (//ROWVAR
                ixFn instanceof ReduceCol)
                    d_uarvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                else if (//COLVAR
                ixFn instanceof ReduceRow)
                    d_uacvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                break;
            }
        case //PROD
        PROD:
            {
                if (// PROD
                ixFn instanceof ReduceAll)
                    d_uam(a, c, m, n, rl, ru);
                break;
            }
        default:
            throw new DMLRuntimeException("Unsupported aggregation type: " + optype);
    }
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) Mean(org.apache.sysml.runtime.functionobjects.Mean) ReduceDiag(org.apache.sysml.runtime.functionobjects.ReduceDiag) CM(org.apache.sysml.runtime.functionobjects.CM) ReduceRow(org.apache.sysml.runtime.functionobjects.ReduceRow) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) KahanPlusSq(org.apache.sysml.runtime.functionobjects.KahanPlusSq) Builtin(org.apache.sysml.runtime.functionobjects.Builtin)

Example 5 with ReduceAll

use of org.apache.sysml.runtime.functionobjects.ReduceAll in project incubator-systemml by apache.

the class LibMatrixAgg method aggregateUnaryMatrixSparse.

private static void aggregateUnaryMatrixSparse(MatrixBlock in, MatrixBlock out, AggType optype, ValueFunction vFn, IndexFunction ixFn, int rl, int ru) throws DMLRuntimeException {
    final int m = in.rlen;
    final int n = in.clen;
    SparseBlock a = in.getSparseBlock();
    double[] c = out.getDenseBlock();
    switch(optype) {
        case //SUM via k+
        KAHAN_SUM:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (// SUM
                ixFn instanceof ReduceAll)
                    s_uakp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//ROWSUM
                ixFn instanceof ReduceCol)
                    s_uarkp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//COLSUM
                ixFn instanceof ReduceRow)
                    s_uackp(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                else if (//TRACE
                ixFn instanceof ReduceDiag)
                    s_uakptrace(a, c, m, n, kbuff, (KahanPlus) vFn, rl, ru);
                break;
            }
        case //SUM_SQ via k+
        KAHAN_SUM_SQ:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (//SUM_SQ
                ixFn instanceof ReduceAll)
                    s_uasqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                else if (//ROWSUM_SQ
                ixFn instanceof ReduceCol)
                    s_uarsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                else if (//COLSUM_SQ
                ixFn instanceof ReduceRow)
                    s_uacsqkp(a, c, m, n, kbuff, (KahanPlusSq) vFn, rl, ru);
                break;
            }
        case //CUMSUM
        CUM_KAHAN_SUM:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                KahanPlus kplus = KahanPlus.getKahanPlusFnObject();
                s_ucumkp(a, null, c, m, n, kbuff, kplus, rl, ru);
                break;
            }
        case //CUMPROD
        CUM_PROD:
            {
                s_ucumm(a, null, c, m, n, rl, ru);
                break;
            }
        case CUM_MIN:
        case CUM_MAX:
            {
                double init = Double.MAX_VALUE * ((optype == AggType.CUM_MAX) ? -1 : 1);
                s_ucummxx(a, null, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MIN:
        case //MAX/MIN
        MAX:
            {
                double init = Double.MAX_VALUE * ((optype == AggType.MAX) ? -1 : 1);
                if (// MIN/MAX
                ixFn instanceof ReduceAll)
                    s_uamxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                else if (//ROWMIN/ROWMAX
                ixFn instanceof ReduceCol)
                    s_uarmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                else if (//COLMIN/COLMAX
                ixFn instanceof ReduceRow)
                    s_uacmxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MAX_INDEX:
            {
                double init = -Double.MAX_VALUE;
                if (//ROWINDEXMAX
                ixFn instanceof ReduceCol)
                    s_uarimxx(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MIN_INDEX:
            {
                double init = Double.MAX_VALUE;
                if (//ROWINDEXMAX
                ixFn instanceof ReduceCol)
                    s_uarimin(a, c, m, n, init, (Builtin) vFn, rl, ru);
                break;
            }
        case MEAN:
            {
                KahanObject kbuff = new KahanObject(0, 0);
                if (// MEAN
                ixFn instanceof ReduceAll)
                    s_uamean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                else if (//ROWMEAN
                ixFn instanceof ReduceCol)
                    s_uarmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                else if (//COLMEAN
                ixFn instanceof ReduceRow)
                    s_uacmean(a, c, m, n, kbuff, (Mean) vFn, rl, ru);
                break;
            }
        case //VAR
        VAR:
            {
                CM_COV_Object cbuff = new CM_COV_Object();
                if (//VAR
                ixFn instanceof ReduceAll)
                    s_uavar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                else if (//ROWVAR
                ixFn instanceof ReduceCol)
                    s_uarvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                else if (//COLVAR
                ixFn instanceof ReduceRow)
                    s_uacvar(a, c, m, n, cbuff, (CM) vFn, rl, ru);
                break;
            }
        case //PROD
        PROD:
            {
                if (// PROD
                ixFn instanceof ReduceAll)
                    s_uam(a, c, m, n, rl, ru);
                break;
            }
        default:
            throw new DMLRuntimeException("Unsupported aggregation type: " + optype);
    }
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) ReduceAll(org.apache.sysml.runtime.functionobjects.ReduceAll) Mean(org.apache.sysml.runtime.functionobjects.Mean) ReduceDiag(org.apache.sysml.runtime.functionobjects.ReduceDiag) CM(org.apache.sysml.runtime.functionobjects.CM) ReduceRow(org.apache.sysml.runtime.functionobjects.ReduceRow) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) KahanPlusSq(org.apache.sysml.runtime.functionobjects.KahanPlusSq) Builtin(org.apache.sysml.runtime.functionobjects.Builtin)

Aggregations

ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)14 ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)10 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)7 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)7 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)6 ReduceRow (org.apache.sysml.runtime.functionobjects.ReduceRow)6 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)5 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)5 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)5 CM (org.apache.sysml.runtime.functionobjects.CM)4 Mean (org.apache.sysml.runtime.functionobjects.Mean)4 ReduceDiag (org.apache.sysml.runtime.functionobjects.ReduceDiag)4 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)4 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)3 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)2 IndexFunction (org.apache.sysml.runtime.functionobjects.IndexFunction)2 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)2 Multiply (org.apache.sysml.runtime.functionobjects.Multiply)2 CM_COV_Object (org.apache.sysml.runtime.instructions.cp.CM_COV_Object)2 DoubleObject (org.apache.sysml.runtime.instructions.cp.DoubleObject)2