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;
}
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;
}
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.");
}
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;
}
Aggregations