Search in sources :

Example 11 with ReduceCol

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

the class LibMatrixAgg method getAggType.

private static AggType getAggType(AggregateUnaryOperator op) {
    ValueFunction vfn = op.aggOp.increOp.fn;
    IndexFunction ifn = op.indexFn;
    //(kahan) sum / sum squared / trace (for ReduceDiag)
    if (vfn instanceof KahanFunction && (op.aggOp.correctionLocation == CorrectionLocationType.LASTCOLUMN || op.aggOp.correctionLocation == CorrectionLocationType.LASTROW) && (ifn instanceof ReduceAll || ifn instanceof ReduceCol || ifn instanceof ReduceRow || ifn instanceof ReduceDiag)) {
        if (vfn instanceof KahanPlus)
            return AggType.KAHAN_SUM;
        else if (vfn instanceof KahanPlusSq)
            return AggType.KAHAN_SUM_SQ;
    }
    //mean
    if (vfn instanceof Mean && (op.aggOp.correctionLocation == CorrectionLocationType.LASTTWOCOLUMNS || op.aggOp.correctionLocation == CorrectionLocationType.LASTTWOROWS) && (ifn instanceof ReduceAll || ifn instanceof ReduceCol || ifn instanceof ReduceRow)) {
        return AggType.MEAN;
    }
    //variance
    if (vfn instanceof CM && ((CM) vfn).getAggOpType() == AggregateOperationTypes.VARIANCE && (op.aggOp.correctionLocation == CorrectionLocationType.LASTFOURCOLUMNS || op.aggOp.correctionLocation == CorrectionLocationType.LASTFOURROWS) && (ifn instanceof ReduceAll || ifn instanceof ReduceCol || ifn instanceof ReduceRow)) {
        return AggType.VAR;
    }
    //prod
    if (vfn instanceof Multiply && ifn instanceof ReduceAll) {
        return AggType.PROD;
    }
    //min / max
    if (vfn instanceof Builtin && (ifn instanceof ReduceAll || ifn instanceof ReduceCol || ifn instanceof ReduceRow)) {
        BuiltinCode bfcode = ((Builtin) vfn).bFunc;
        switch(bfcode) {
            case MAX:
                return AggType.MAX;
            case MIN:
                return AggType.MIN;
            case MAXINDEX:
                return AggType.MAX_INDEX;
            case MININDEX:
                return AggType.MIN_INDEX;
            //do nothing
            default:
        }
    }
    return AggType.INVALID;
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) 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) IndexFunction(org.apache.sysml.runtime.functionobjects.IndexFunction) BuiltinCode(org.apache.sysml.runtime.functionobjects.Builtin.BuiltinCode) KahanFunction(org.apache.sysml.runtime.functionobjects.KahanFunction) Multiply(org.apache.sysml.runtime.functionobjects.Multiply) KahanPlus(org.apache.sysml.runtime.functionobjects.KahanPlus) KahanPlusSq(org.apache.sysml.runtime.functionobjects.KahanPlusSq) Builtin(org.apache.sysml.runtime.functionobjects.Builtin)

Example 12 with ReduceCol

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

the class LibMatrixAgg method aggregateTernary.

public static MatrixBlock aggregateTernary(MatrixBlock in1, MatrixBlock in2, MatrixBlock in3, MatrixBlock ret, AggregateTernaryOperator op, int k) throws DMLRuntimeException {
    //fall back to sequential version if necessary
    if (k <= 1 || in1.nonZeros + in2.nonZeros < PAR_NUMCELL_THRESHOLD || in1.rlen <= k / 2 || (!(op.indexFn instanceof ReduceCol) && ret.clen * 8 * k > PAR_INTERMEDIATE_SIZE_THRESHOLD)) {
        return aggregateTernary(in1, in2, in3, ret, op);
    }
    //early abort if any block is empty
    if (in1.isEmptyBlock(false) || in2.isEmptyBlock(false) || in3 != null && in3.isEmptyBlock(false)) {
        return ret;
    }
    try {
        ExecutorService pool = Executors.newFixedThreadPool(k);
        ArrayList<AggTernaryTask> tasks = new ArrayList<AggTernaryTask>();
        int blklen = (int) (Math.ceil((double) in1.rlen / k));
        IndexFunction ixFn = op.indexFn;
        for (int i = 0; i < k & i * blklen < in1.rlen; i++) tasks.add(new AggTernaryTask(in1, in2, in3, ret, ixFn, i * blklen, Math.min((i + 1) * blklen, in1.rlen)));
        List<Future<MatrixBlock>> rtasks = pool.invokeAll(tasks);
        pool.shutdown();
        //aggregate partial results and error handling
        //for init
        ret.copy(rtasks.get(0).get());
        for (int i = 1; i < rtasks.size(); i++) aggregateFinalResult(op.aggOp, ret, rtasks.get(i).get());
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    //cleanup output and change representation (if necessary)
    ret.recomputeNonZeros();
    ret.examSparsity();
    return ret;
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) IndexFunction(org.apache.sysml.runtime.functionobjects.IndexFunction) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 13 with ReduceCol

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

the class LibMatrixAgg method aggregateUnaryMatrix.

public static void aggregateUnaryMatrix(MatrixBlock in, MatrixBlock out, AggregateUnaryOperator uaop, int k) throws DMLRuntimeException {
    //fall back to sequential version if necessary
    if (k <= 1 || (long) in.nonZeros < PAR_NUMCELL_THRESHOLD || in.rlen <= k / 2 || (!(uaop.indexFn instanceof ReduceCol) && out.clen * 8 * k > PAR_INTERMEDIATE_SIZE_THRESHOLD) || !out.isThreadSafe()) {
        aggregateUnaryMatrix(in, out, uaop);
        return;
    }
    //prepare meta data
    AggType aggtype = getAggType(uaop);
    final int m = in.rlen;
    final int m2 = out.rlen;
    final int n2 = out.clen;
    //filter empty input blocks (incl special handling for sparse-unsafe operations)
    if (in.isEmptyBlock(false)) {
        aggregateUnaryMatrixEmpty(in, out, aggtype, uaop.indexFn);
        return;
    }
    //allocate output arrays (if required)
    if (uaop.indexFn instanceof ReduceCol) {
        //always dense
        out.reset(m2, n2, false);
        out.allocateDenseBlock();
    }
    //(currently: always parallelization over number of rows)
    try {
        ExecutorService pool = Executors.newFixedThreadPool(k);
        ArrayList<AggTask> tasks = new ArrayList<AggTask>();
        int blklen = (int) (Math.ceil((double) m / k));
        for (int i = 0; i < k & i * blklen < m; i++) {
            tasks.add((uaop.indexFn instanceof ReduceCol) ? new RowAggTask(in, out, aggtype, uaop, i * blklen, Math.min((i + 1) * blklen, m)) : new PartialAggTask(in, out, aggtype, uaop, i * blklen, Math.min((i + 1) * blklen, m)));
        }
        pool.invokeAll(tasks);
        pool.shutdown();
        //aggregate partial results
        if (!(uaop.indexFn instanceof ReduceCol)) {
            //for init
            out.copy(((PartialAggTask) tasks.get(0)).getResult());
            for (int i = 1; i < tasks.size(); i++) aggregateFinalResult(uaop.aggOp, out, ((PartialAggTask) tasks.get(i)).getResult());
        }
    } catch (Exception ex) {
        throw new DMLRuntimeException(ex);
    }
    //cleanup output and change representation (if necessary)
    out.recomputeNonZeros();
    out.examSparsity();
//System.out.println("uagg k="+k+" ("+in.rlen+","+in.clen+","+in.sparse+") in "+time.stop()+"ms.");
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 14 with ReduceCol

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

the class MatrixBlock method uaggouterchainOperations.

public MatrixBlock uaggouterchainOperations(MatrixBlock mbLeft, MatrixBlock mbRight, MatrixBlock mbOut, BinaryOperator bOp, AggregateUnaryOperator uaggOp) throws DMLRuntimeException {
    double[] bv = DataConverter.convertToDoubleVector(mbRight);
    int[] bvi = null;
    //process instruction
    if (LibMatrixOuterAgg.isSupportedUaggOp(uaggOp, bOp)) {
        if ((LibMatrixOuterAgg.isRowIndexMax(uaggOp)) || (LibMatrixOuterAgg.isRowIndexMin(uaggOp))) {
            bvi = LibMatrixOuterAgg.prepareRowIndices(bv.length, bv, bOp, uaggOp);
        } else {
            Arrays.sort(bv);
        }
        int iRows = (uaggOp.indexFn instanceof ReduceCol ? mbLeft.getNumRows() : 2);
        int iCols = (uaggOp.indexFn instanceof ReduceRow ? mbLeft.getNumColumns() : 2);
        if (mbOut == null)
            // Output matrix will be dense matrix most of the time.
            mbOut = new MatrixBlock(iRows, iCols, false);
        else
            mbOut.reset(iRows, iCols, false);
        LibMatrixOuterAgg.aggregateMatrix(mbLeft, mbOut, bv, bvi, bOp, uaggOp);
    } else
        throw new DMLRuntimeException("Unsupported operator for unary aggregate operations.");
    return mbOut;
}
Also used : ReduceCol(org.apache.sysml.runtime.functionobjects.ReduceCol) ReduceRow(org.apache.sysml.runtime.functionobjects.ReduceRow) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

ReduceCol (org.apache.sysml.runtime.functionobjects.ReduceCol)14 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)11 ReduceAll (org.apache.sysml.runtime.functionobjects.ReduceAll)10 ReduceRow (org.apache.sysml.runtime.functionobjects.ReduceRow)8 Builtin (org.apache.sysml.runtime.functionobjects.Builtin)6 KahanPlus (org.apache.sysml.runtime.functionobjects.KahanPlus)5 KahanPlusSq (org.apache.sysml.runtime.functionobjects.KahanPlusSq)5 CM (org.apache.sysml.runtime.functionobjects.CM)4 IndexFunction (org.apache.sysml.runtime.functionobjects.IndexFunction)4 Mean (org.apache.sysml.runtime.functionobjects.Mean)4 ReduceDiag (org.apache.sysml.runtime.functionobjects.ReduceDiag)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)3 MatrixCharacteristics (org.apache.sysml.runtime.matrix.MatrixCharacteristics)3 Future (java.util.concurrent.Future)2 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)2 KahanFunction (org.apache.sysml.runtime.functionobjects.KahanFunction)2 Multiply (org.apache.sysml.runtime.functionobjects.Multiply)2