Search in sources :

Example 11 with CMOperator

use of org.apache.sysml.runtime.matrix.operators.CMOperator in project incubator-systemml by apache.

the class PerformGroupByAggInCombiner method call.

@Override
public WeightedCell call(WeightedCell value1, WeightedCell value2) throws Exception {
    WeightedCell outCell = new WeightedCell();
    CM_COV_Object cmObj = new CM_COV_Object();
    if (// everything except sum
    _op instanceof CMOperator) {
        if (((CMOperator) _op).isPartialAggregateOperator()) {
            cmObj.reset();
            // cmFn.get(key.getTag());
            CM lcmFn = CM.getCMFnObject(((CMOperator) _op).aggOpType);
            // partial aggregate cm operator
            lcmFn.execute(cmObj, value1.getValue(), value1.getWeight());
            lcmFn.execute(cmObj, value2.getValue(), value2.getWeight());
            outCell.setValue(cmObj.getRequiredPartialResult(_op));
            outCell.setWeight(cmObj.getWeight());
        } else // forward tuples to reducer
        {
            throw new DMLRuntimeException("Incorrect usage, should have used PerformGroupByAggInReducer");
        }
    } else if (// sum
    _op instanceof AggregateOperator) {
        AggregateOperator aggop = (AggregateOperator) _op;
        if (aggop.correctionExists) {
            KahanObject buffer = new KahanObject(aggop.initialValue, 0);
            KahanPlus.getKahanPlusFnObject();
            // partial aggregate with correction
            aggop.increOp.fn.execute(buffer, value1.getValue() * value1.getWeight());
            aggop.increOp.fn.execute(buffer, value2.getValue() * value2.getWeight());
            outCell.setValue(buffer._sum);
            outCell.setWeight(1);
        } else // no correction
        {
            double v = aggop.initialValue;
            // partial aggregate without correction
            v = aggop.increOp.fn.execute(v, value1.getValue() * value1.getWeight());
            v = aggop.increOp.fn.execute(v, value2.getValue() * value2.getWeight());
            outCell.setValue(v);
            outCell.setWeight(1);
        }
    } else
        throw new DMLRuntimeException("Unsupported operator in grouped aggregate instruction:" + _op);
    return outCell;
}
Also used : WeightedCell(org.apache.sysml.runtime.matrix.data.WeightedCell) CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) AggregateOperator(org.apache.sysml.runtime.matrix.operators.AggregateOperator) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) CM(org.apache.sysml.runtime.functionobjects.CM) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 12 with CMOperator

use of org.apache.sysml.runtime.matrix.operators.CMOperator in project incubator-systemml by apache.

the class PerformGroupByAggInReducer method call.

@Override
public WeightedCell call(Iterable<WeightedCell> kv) throws Exception {
    WeightedCell outCell = new WeightedCell();
    CM_COV_Object cmObj = new CM_COV_Object();
    if (// everything except sum
    op instanceof CMOperator) {
        cmObj.reset();
        // cmFn.get(key.getTag());
        CM lcmFn = CM.getCMFnObject(((CMOperator) op).aggOpType);
        if (((CMOperator) op).isPartialAggregateOperator()) {
            throw new DMLRuntimeException("Incorrect usage, should have used PerformGroupByAggInCombiner");
        } else // forward tuples to reducer
        {
            for (WeightedCell value : kv) lcmFn.execute(cmObj, value.getValue(), value.getWeight());
            outCell.setValue(cmObj.getRequiredResult(op));
            outCell.setWeight(1);
        }
    } else if (// sum
    op instanceof AggregateOperator) {
        AggregateOperator aggop = (AggregateOperator) op;
        if (aggop.correctionExists) {
            KahanObject buffer = new KahanObject(aggop.initialValue, 0);
            KahanPlus.getKahanPlusFnObject();
            // partial aggregate with correction
            for (WeightedCell value : kv) aggop.increOp.fn.execute(buffer, value.getValue() * value.getWeight());
            outCell.setValue(buffer._sum);
            outCell.setWeight(1);
        } else // no correction
        {
            double v = aggop.initialValue;
            // partial aggregate without correction
            for (WeightedCell value : kv) v = aggop.increOp.fn.execute(v, value.getValue() * value.getWeight());
            outCell.setValue(v);
            outCell.setWeight(1);
        }
    } else
        throw new DMLRuntimeException("Unsupported operator in grouped aggregate instruction:" + op);
    return outCell;
}
Also used : WeightedCell(org.apache.sysml.runtime.matrix.data.WeightedCell) CM_COV_Object(org.apache.sysml.runtime.instructions.cp.CM_COV_Object) AggregateOperator(org.apache.sysml.runtime.matrix.operators.AggregateOperator) KahanObject(org.apache.sysml.runtime.instructions.cp.KahanObject) CM(org.apache.sysml.runtime.functionobjects.CM) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 13 with CMOperator

use of org.apache.sysml.runtime.matrix.operators.CMOperator in project incubator-systemml by apache.

the class CentralMomentCPInstruction method parseInstruction.

public static CentralMomentCPInstruction parseInstruction(String str) {
    CPOperand in1 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    CPOperand in2 = null;
    CPOperand in3 = null;
    CPOperand out = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
    String opcode = parts[0];
    // check supported opcode
    if (!opcode.equalsIgnoreCase("cm")) {
        throw new DMLRuntimeException("Unsupported opcode " + opcode);
    }
    if (parts.length == 4) {
        // Example: CP.cm.mVar0.Var1.mVar2; (without weights)
        in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
        parseUnaryInstruction(str, in1, in2, out);
    } else if (parts.length == 5) {
        // CP.cm.mVar0.mVar1.Var2.mVar3; (with weights)
        in2 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
        in3 = new CPOperand("", ValueType.UNKNOWN, DataType.UNKNOWN);
        parseUnaryInstruction(str, in1, in2, in3, out);
    }
    /* 
		 * Exact order of the central moment MAY NOT be known at compilation time.
		 * We first try to parse the second argument as an integer, and if we fail, 
		 * we simply pass -1 so that getCMAggOpType() picks up AggregateOperationTypes.INVALID.
		 * It must be updated at run time in processInstruction() method.
		 */
    int cmOrder;
    try {
        if (in3 == null) {
            cmOrder = Integer.parseInt(in2.getName());
        } else {
            cmOrder = Integer.parseInt(in3.getName());
        }
    } catch (NumberFormatException e) {
        // unknown at compilation time
        cmOrder = -1;
    }
    AggregateOperationTypes opType = CMOperator.getCMAggOpType(cmOrder);
    CMOperator cm = new CMOperator(CM.getCMFnObject(opType), opType);
    return new CentralMomentCPInstruction(cm, in1, in2, in3, out, opcode, str);
}
Also used : AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 14 with CMOperator

use of org.apache.sysml.runtime.matrix.operators.CMOperator in project incubator-systemml by apache.

the class CentralMomentCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String output_name = output.getName();
    /*
		 * The "order" of the central moment in the instruction can 
		 * be set to INVALID when the exact value is unknown at 
		 * compilation time. We first need to determine the exact 
		 * order and update the CMOperator, if needed.
		 */
    MatrixBlock matBlock = ec.getMatrixInput(input1.getName(), getExtendedOpcode());
    CPOperand scalarInput = (input3 == null ? input2 : input3);
    ScalarObject order = ec.getScalarInput(scalarInput.getName(), scalarInput.getValueType(), scalarInput.isLiteral());
    CMOperator cm_op = ((CMOperator) _optr);
    if (cm_op.getAggOpType() == AggregateOperationTypes.INVALID)
        cm_op = cm_op.setCMAggOp((int) order.getLongValue());
    CM_COV_Object cmobj = null;
    if (input3 == null) {
        cmobj = matBlock.cmOperations(cm_op);
    } else {
        MatrixBlock wtBlock = ec.getMatrixInput(input2.getName(), getExtendedOpcode());
        cmobj = matBlock.cmOperations(cm_op, wtBlock);
        ec.releaseMatrixInput(input2.getName(), getExtendedOpcode());
    }
    ec.releaseMatrixInput(input1.getName(), getExtendedOpcode());
    double val = cmobj.getRequiredResult(cm_op);
    ec.setScalarOutput(output_name, new DoubleObject(val));
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator)

Example 15 with CMOperator

use of org.apache.sysml.runtime.matrix.operators.CMOperator in project incubator-systemml by apache.

the class MatrixBlock method groupedAggOperations.

public MatrixBlock groupedAggOperations(MatrixValue tgt, MatrixValue wghts, MatrixValue ret, int ngroups, Operator op, int k) {
    // setup input matrices
    MatrixBlock target = checkType(tgt);
    MatrixBlock weights = checkType(wghts);
    // check valid dimensions
    boolean validMatrixOp = (weights == null && ngroups >= 1);
    if (this.getNumColumns() != 1 || (weights != null && weights.getNumColumns() != 1))
        throw new DMLRuntimeException("groupedAggregate can only operate on 1-dimensional column matrices for groups and weights.");
    if (target.getNumColumns() != 1 && op instanceof CMOperator && !validMatrixOp)
        throw new DMLRuntimeException("groupedAggregate can only operate on 1-dimensional column matrices for target (for this aggregation function).");
    if (target.getNumColumns() != 1 && target.getNumRows() != 1 && !validMatrixOp)
        throw new DMLRuntimeException("groupedAggregate can only operate on 1-dimensional column or row matrix for target.");
    if (this.getNumRows() != target.getNumRows() && this.getNumRows() != Math.max(target.getNumRows(), target.getNumColumns()) || (weights != null && this.getNumRows() != weights.getNumRows()))
        throw new DMLRuntimeException("groupedAggregate can only operate on matrices with equal dimensions.");
    // obtain numGroups from instruction, if provided
    if (ngroups > 0)
        numGroups = ngroups;
    // Determine the number of groups
    if (numGroups <= 0) {
        // reuse if available
        double min = this.min();
        double max = this.max();
        if (min <= 0)
            throw new DMLRuntimeException("Invalid value (" + min + ") encountered in 'groups' while computing groupedAggregate");
        if (max <= 0)
            throw new DMLRuntimeException("Invalid value (" + max + ") encountered in 'groups' while computing groupedAggregate.");
        numGroups = (int) max;
    }
    // Allocate result matrix
    boolean rowVector = (target.getNumRows() == 1 && target.getNumColumns() > 1);
    MatrixBlock result = checkType(ret);
    boolean result_sparsity = estimateSparsityOnGroupedAgg(rlen, numGroups);
    if (result == null)
        result = new MatrixBlock(numGroups, rowVector ? 1 : target.getNumColumns(), result_sparsity);
    else
        result.reset(numGroups, rowVector ? 1 : target.getNumColumns(), result_sparsity);
    // execute grouped aggregate operation
    if (k > 1)
        LibMatrixAgg.groupedAggregate(this, target, weights, result, numGroups, op, k);
    else
        LibMatrixAgg.groupedAggregate(this, target, weights, result, numGroups, op);
    return result;
}
Also used : CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

CMOperator (org.apache.sysml.runtime.matrix.operators.CMOperator)17 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)11 KahanObject (org.apache.sysml.runtime.instructions.cp.KahanObject)7 IOException (java.io.IOException)6 AggregateOperator (org.apache.sysml.runtime.matrix.operators.AggregateOperator)6 CM_COV_Object (org.apache.sysml.runtime.instructions.cp.CM_COV_Object)5 CM (org.apache.sysml.runtime.functionobjects.CM)4 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)4 WeightedCell (org.apache.sysml.runtime.matrix.data.WeightedCell)4 AggregateOperationTypes (org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes)4 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)3 GroupedAggregateInstruction (org.apache.sysml.runtime.instructions.mr.GroupedAggregateInstruction)3 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)2 MatrixIndexes (org.apache.sysml.runtime.matrix.data.MatrixIndexes)2 COVOperator (org.apache.sysml.runtime.matrix.operators.COVOperator)2 Operator (org.apache.sysml.runtime.matrix.operators.Operator)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1