Search in sources :

Example 1 with AggregateOperationTypes

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

the class CM_N_COVInstruction method parseInstruction.

public static CM_N_COVInstruction parseInstruction(String str) {
    String[] parts = InstructionUtils.getInstructionParts(str);
    byte in, out;
    int cst;
    String opcode = parts[0];
    if (opcode.equalsIgnoreCase("cm")) {
        in = Byte.parseByte(parts[1]);
        cst = Integer.parseInt(parts[2]);
        out = Byte.parseByte(parts[3]);
        if (cst > 4 || cst < 0 || cst == 1)
            throw new DMLRuntimeException("constant for central moment has to be 0, 2, 3, or 4");
        AggregateOperationTypes opType = CMOperator.getCMAggOpType(cst);
        CMOperator cm = new CMOperator(CM.getCMFnObject(opType), opType);
        return new CM_N_COVInstruction(cm, in, out, str);
    } else if (opcode.equalsIgnoreCase("cov")) {
        in = Byte.parseByte(parts[1]);
        out = Byte.parseByte(parts[2]);
        COVOperator cov = new COVOperator(COV.getCOMFnObject());
        return new CM_N_COVInstruction(cov, in, out, str);
    } else
        throw new DMLRuntimeException("unknown opcode " + opcode);
}
Also used : COVOperator(org.apache.sysml.runtime.matrix.operators.COVOperator) AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 2 with AggregateOperationTypes

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

the class CentralMomentSPInstruction method parseInstruction.

public static CentralMomentSPInstruction 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 CentralMomentSPInstruction(cm, in1, in2, in3, out, opcode, str);
}
Also used : AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 3 with AggregateOperationTypes

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

the class CostEstimator method extractCPInstStatistics.

private static Object[] extractCPInstStatistics(Instruction inst, HashMap<String, VarStats> stats) {
    // stats, attrs
    Object[] ret = new Object[2];
    VarStats[] vs = new VarStats[3];
    String[] attr = null;
    if (inst instanceof UnaryCPInstruction) {
        if (inst instanceof DataGenCPInstruction) {
            DataGenCPInstruction rinst = (DataGenCPInstruction) inst;
            vs[0] = _unknownStats;
            vs[1] = _unknownStats;
            vs[2] = stats.get(rinst.output.getName());
            // prepare attributes for cost estimation
            // full rand
            int type = 2;
            if (rinst.getMinValue() == 0.0 && rinst.getMaxValue() == 0.0)
                type = 0;
            else if (rinst.getSparsity() == 1.0 && rinst.getMinValue() == rinst.getMaxValue())
                type = 1;
            attr = new String[] { String.valueOf(type) };
        } else if (inst instanceof StringInitCPInstruction) {
            StringInitCPInstruction rinst = (StringInitCPInstruction) inst;
            vs[0] = _unknownStats;
            vs[1] = _unknownStats;
            vs[2] = stats.get(rinst.output.getName());
        } else // general unary
        {
            UnaryCPInstruction uinst = (UnaryCPInstruction) inst;
            vs[0] = stats.get(uinst.input1.getName());
            vs[1] = _unknownStats;
            vs[2] = stats.get(uinst.output.getName());
            if (// scalar input, e.g., print
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar output
            vs[2] == null)
                vs[2] = _scalarStats;
            if (inst instanceof MMTSJCPInstruction) {
                String type = ((MMTSJCPInstruction) inst).getMMTSJType().toString();
                attr = new String[] { type };
            } else if (inst instanceof AggregateUnaryCPInstruction) {
                String[] parts = InstructionUtils.getInstructionParts(inst.toString());
                String opcode = parts[0];
                if (opcode.equals("cm"))
                    attr = new String[] { parts[parts.length - 2] };
            }
        }
    } else if (inst instanceof BinaryCPInstruction) {
        BinaryCPInstruction binst = (BinaryCPInstruction) inst;
        vs[0] = stats.get(binst.input1.getName());
        vs[1] = stats.get(binst.input2.getName());
        vs[2] = stats.get(binst.output.getName());
        if (// scalar input,
        vs[0] == null)
            vs[0] = _scalarStats;
        if (// scalar input,
        vs[1] == null)
            vs[1] = _scalarStats;
        if (// scalar output
        vs[2] == null)
            vs[2] = _scalarStats;
    } else if (inst instanceof AggregateTernaryCPInstruction) {
        AggregateTernaryCPInstruction binst = (AggregateTernaryCPInstruction) inst;
        // of same dimension anyway but missing third input
        vs[0] = stats.get(binst.input1.getName());
        vs[1] = stats.get(binst.input2.getName());
        vs[2] = stats.get(binst.output.getName());
        if (// scalar input,
        vs[0] == null)
            vs[0] = _scalarStats;
        if (// scalar input,
        vs[1] == null)
            vs[1] = _scalarStats;
        if (// scalar output
        vs[2] == null)
            vs[2] = _scalarStats;
    } else if (inst instanceof ParameterizedBuiltinCPInstruction) {
        // ParameterizedBuiltinCPInstruction pinst = (ParameterizedBuiltinCPInstruction) inst;
        String[] parts = InstructionUtils.getInstructionParts(inst.toString());
        String opcode = parts[0];
        if (opcode.equals("groupedagg")) {
            HashMap<String, String> paramsMap = ParameterizedBuiltinCPInstruction.constructParameterMap(parts);
            String fn = paramsMap.get("fn");
            String order = paramsMap.get("order");
            AggregateOperationTypes type = CMOperator.getAggOpType(fn, order);
            attr = new String[] { String.valueOf(type.ordinal()) };
        } else if (opcode.equals("rmempty")) {
            HashMap<String, String> paramsMap = ParameterizedBuiltinCPInstruction.constructParameterMap(parts);
            attr = new String[] { String.valueOf(paramsMap.get("margin").equals("rows") ? 0 : 1) };
        }
        vs[0] = stats.get(parts[1].substring(7).replaceAll(Lop.VARIABLE_NAME_PLACEHOLDER, ""));
        // TODO
        vs[1] = _unknownStats;
        vs[2] = stats.get(parts[parts.length - 1]);
        if (// scalar input
        vs[0] == null)
            vs[0] = _scalarStats;
        if (// scalar output
        vs[2] == null)
            vs[2] = _scalarStats;
    } else if (inst instanceof MultiReturnBuiltinCPInstruction) {
        // applies to qr, lu, eigen (cost computation on input1)
        MultiReturnBuiltinCPInstruction minst = (MultiReturnBuiltinCPInstruction) inst;
        vs[0] = stats.get(minst.input1.getName());
        vs[1] = stats.get(minst.getOutput(0).getName());
        vs[2] = stats.get(minst.getOutput(1).getName());
    } else if (inst instanceof VariableCPInstruction) {
        setUnknownStats(vs);
        VariableCPInstruction varinst = (VariableCPInstruction) inst;
        if (varinst.getOpcode().equals("write")) {
            // special handling write of matrix objects (non existing if scalar)
            if (stats.containsKey(varinst.getInput1().getName()))
                vs[0] = stats.get(varinst.getInput1().getName());
            attr = new String[] { varinst.getInput3().getName() };
        }
    } else {
        setUnknownStats(vs);
    }
    // maintain var status (CP output always inmem)
    vs[2]._inmem = true;
    ret[0] = vs;
    ret[1] = attr;
    return ret;
}
Also used : StringInitCPInstruction(org.apache.sysml.runtime.instructions.cp.StringInitCPInstruction) ParameterizedBuiltinCPInstruction(org.apache.sysml.runtime.instructions.cp.ParameterizedBuiltinCPInstruction) AggregateUnaryCPInstruction(org.apache.sysml.runtime.instructions.cp.AggregateUnaryCPInstruction) HashMap(java.util.HashMap) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) MMTSJCPInstruction(org.apache.sysml.runtime.instructions.cp.MMTSJCPInstruction) MultiReturnBuiltinCPInstruction(org.apache.sysml.runtime.instructions.cp.MultiReturnBuiltinCPInstruction) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) AggregateTernaryCPInstruction(org.apache.sysml.runtime.instructions.cp.AggregateTernaryCPInstruction) DataGenCPInstruction(org.apache.sysml.runtime.instructions.cp.DataGenCPInstruction) AggregateUnaryCPInstruction(org.apache.sysml.runtime.instructions.cp.AggregateUnaryCPInstruction) UnaryCPInstruction(org.apache.sysml.runtime.instructions.cp.UnaryCPInstruction) BinaryCPInstruction(org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction)

Example 4 with AggregateOperationTypes

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

the class CostEstimatorStaticRuntime method extractMRInstStatistics.

private Object[] extractMRInstStatistics(String inst, VarStats[] stats) {
    // stats, attrs
    Object[] ret = new Object[2];
    VarStats[] vs = new VarStats[3];
    String[] attr = null;
    String[] parts = InstructionUtils.getInstructionParts(inst);
    String opcode = parts[0];
    if (opcode.equals(DataGen.RAND_OPCODE)) {
        vs[0] = _unknownStats;
        vs[1] = _unknownStats;
        vs[2] = stats[Integer.parseInt(parts[2])];
        int type = 2;
        // awareness of instruction patching min/max
        if (!parts[7].contains(Lop.VARIABLE_NAME_PLACEHOLDER) && !parts[8].contains(Lop.VARIABLE_NAME_PLACEHOLDER)) {
            double minValue = Double.parseDouble(parts[7]);
            double maxValue = Double.parseDouble(parts[8]);
            double sparsity = Double.parseDouble(parts[9]);
            if (minValue == 0.0 && maxValue == 0.0)
                type = 0;
            else if (sparsity == 1.0 && minValue == maxValue)
                type = 1;
        }
        attr = new String[] { String.valueOf(type) };
    }
    if (opcode.equals(DataGen.SEQ_OPCODE)) {
        vs[0] = _unknownStats;
        vs[1] = _unknownStats;
        vs[2] = stats[Integer.parseInt(parts[2])];
    } else // general case
    {
        String inst2 = replaceInstructionPatch(inst);
        MRInstruction mrinst = MRInstructionParser.parseSingleInstruction(inst2);
        if (mrinst instanceof UnaryMRInstructionBase) {
            UnaryMRInstructionBase uinst = (UnaryMRInstructionBase) mrinst;
            vs[0] = uinst.input >= 0 ? stats[uinst.input] : _unknownStats;
            vs[1] = _unknownStats;
            vs[2] = stats[uinst.output];
            if (// scalar input, e.g., print
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar output
            vs[2] == null)
                vs[2] = _scalarStats;
            if (mrinst instanceof MMTSJMRInstruction) {
                String type = ((MMTSJMRInstruction) mrinst).getMMTSJType().toString();
                attr = new String[] { type };
            } else if (mrinst instanceof CM_N_COVInstruction) {
                if (opcode.equals("cm"))
                    attr = new String[] { parts[parts.length - 2] };
            } else if (mrinst instanceof GroupedAggregateInstruction) {
                if (opcode.equals("groupedagg")) {
                    AggregateOperationTypes type = CMOperator.getAggOpType(parts[2], parts[3]);
                    attr = new String[] { String.valueOf(type.ordinal()) };
                }
            }
        } else if (mrinst instanceof BinaryMRInstructionBase) {
            BinaryMRInstructionBase binst = (BinaryMRInstructionBase) mrinst;
            vs[0] = stats[binst.input1];
            vs[1] = stats[binst.input2];
            vs[2] = stats[binst.output];
            if (// scalar input,
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar input,
            vs[1] == null)
                vs[1] = _scalarStats;
            if (// scalar output
            vs[2] == null)
                vs[2] = _scalarStats;
            if (opcode.equals("rmempty")) {
                RemoveEmptyMRInstruction rbinst = (RemoveEmptyMRInstruction) mrinst;
                attr = new String[] { rbinst.isRemoveRows() ? "0" : "1" };
            }
        } else if (mrinst instanceof TernaryInstruction) {
            TernaryInstruction tinst = (TernaryInstruction) mrinst;
            byte[] ix = tinst.getAllIndexes();
            for (int i = 0; i < ix.length - 1; i++) vs[0] = stats[ix[i]];
            vs[2] = stats[ix[ix.length - 1]];
            if (// scalar input,
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar input,
            vs[1] == null)
                vs[1] = _scalarStats;
            if (// scalar output
            vs[2] == null)
                vs[2] = _scalarStats;
        } else if (mrinst instanceof CtableInstruction) {
            CtableInstruction tinst = (CtableInstruction) mrinst;
            vs[0] = stats[tinst.input1];
            vs[1] = stats[tinst.input2];
            vs[2] = stats[tinst.input3];
            if (// scalar input,
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar input,
            vs[1] == null)
                vs[1] = _scalarStats;
            if (// scalar input
            vs[2] == null)
                vs[2] = _scalarStats;
        } else if (mrinst instanceof PickByCountInstruction) {
            PickByCountInstruction pinst = (PickByCountInstruction) mrinst;
            vs[0] = stats[pinst.input1];
            vs[2] = stats[pinst.output];
            if (// scalar input,
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar input,
            vs[1] == null)
                vs[1] = _scalarStats;
            if (// scalar input
            vs[2] == null)
                vs[2] = _scalarStats;
        } else if (mrinst instanceof MapMultChainInstruction) {
            MapMultChainInstruction minst = (MapMultChainInstruction) mrinst;
            vs[0] = stats[minst.getInput1()];
            vs[1] = stats[minst.getInput2()];
            if (minst.getInput3() >= 0)
                vs[2] = stats[minst.getInput3()];
            if (// scalar input,
            vs[0] == null)
                vs[0] = _scalarStats;
            if (// scalar input,
            vs[1] == null)
                vs[1] = _scalarStats;
            if (// scalar input
            vs[2] == null)
                vs[2] = _scalarStats;
        }
    }
    // maintain var status (CP output always inmem)
    vs[2]._inmem = true;
    ret[0] = vs;
    ret[1] = attr;
    return ret;
}
Also used : CM_N_COVInstruction(org.apache.sysml.runtime.instructions.mr.CM_N_COVInstruction) BinaryMRInstructionBase(org.apache.sysml.runtime.instructions.mr.BinaryMRInstructionBase) PickByCountInstruction(org.apache.sysml.runtime.instructions.mr.PickByCountInstruction) TernaryInstruction(org.apache.sysml.runtime.instructions.mr.TernaryInstruction) AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) RemoveEmptyMRInstruction(org.apache.sysml.runtime.instructions.mr.RemoveEmptyMRInstruction) CtableInstruction(org.apache.sysml.runtime.instructions.mr.CtableInstruction) UnaryMRInstructionBase(org.apache.sysml.runtime.instructions.mr.UnaryMRInstructionBase) MapMultChainInstruction(org.apache.sysml.runtime.instructions.mr.MapMultChainInstruction) MMTSJMRInstruction(org.apache.sysml.runtime.instructions.mr.MMTSJMRInstruction) DataGenMRInstruction(org.apache.sysml.runtime.instructions.mr.DataGenMRInstruction) MMTSJMRInstruction(org.apache.sysml.runtime.instructions.mr.MMTSJMRInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction) RemoveEmptyMRInstruction(org.apache.sysml.runtime.instructions.mr.RemoveEmptyMRInstruction) GroupedAggregateInstruction(org.apache.sysml.runtime.instructions.mr.GroupedAggregateInstruction)

Example 5 with AggregateOperationTypes

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

the class BasicMatrixCentralMomentTest method runMatrixAppendTest.

private static void runMatrixAppendTest(SparsityType sptype, ValueType vtype, boolean compress) {
    try {
        // prepare sparsity for input data
        double sparsity = -1;
        switch(sptype) {
            case DENSE:
                sparsity = sparsity1;
                break;
            case SPARSE:
                sparsity = sparsity2;
                break;
            case EMPTY:
                sparsity = sparsity3;
                break;
        }
        // generate input data
        double min = (vtype == ValueType.CONST) ? 10 : -10;
        double[][] input = TestUtils.generateTestMatrix(rows, cols, min, 10, sparsity, 7);
        if (vtype == ValueType.RAND_ROUND_OLE || vtype == ValueType.RAND_ROUND_DDC) {
            CompressedMatrixBlock.ALLOW_DDC_ENCODING = (vtype == ValueType.RAND_ROUND_DDC);
            input = TestUtils.round(input);
        }
        MatrixBlock mb = DataConverter.convertToMatrixBlock(input);
        // compress given matrix block
        CompressedMatrixBlock cmb = new CompressedMatrixBlock(mb);
        if (compress)
            cmb.compress();
        // quantile uncompressed
        AggregateOperationTypes opType = CMOperator.getCMAggOpType(2);
        CMOperator cm = new CMOperator(CM.getCMFnObject(opType), opType);
        double ret1 = mb.cmOperations(cm).getRequiredResult(opType);
        // quantile compressed
        double ret2 = cmb.cmOperations(cm).getRequiredResult(opType);
        // compare result with input
        TestUtils.compareScalars(ret1, ret2, 0.0000001);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        CompressedMatrixBlock.ALLOW_DDC_ENCODING = true;
    }
}
Also used : CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) CompressedMatrixBlock(org.apache.sysml.runtime.compress.CompressedMatrixBlock) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) AggregateOperationTypes(org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes) CMOperator(org.apache.sysml.runtime.matrix.operators.CMOperator)

Aggregations

AggregateOperationTypes (org.apache.sysml.runtime.matrix.operators.CMOperator.AggregateOperationTypes)12 CMOperator (org.apache.sysml.runtime.matrix.operators.CMOperator)8 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 HashMap (java.util.HashMap)2 CompressedMatrixBlock (org.apache.sysml.runtime.compress.CompressedMatrixBlock)2 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)2 AggregateTernaryCPInstruction (org.apache.sysml.runtime.instructions.cp.AggregateTernaryCPInstruction)2 AggregateUnaryCPInstruction (org.apache.sysml.runtime.instructions.cp.AggregateUnaryCPInstruction)2 BinaryCPInstruction (org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction)2 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)2 DataGenCPInstruction (org.apache.sysml.runtime.instructions.cp.DataGenCPInstruction)2 MMTSJCPInstruction (org.apache.sysml.runtime.instructions.cp.MMTSJCPInstruction)2 MultiReturnBuiltinCPInstruction (org.apache.sysml.runtime.instructions.cp.MultiReturnBuiltinCPInstruction)2 ParameterizedBuiltinCPInstruction (org.apache.sysml.runtime.instructions.cp.ParameterizedBuiltinCPInstruction)2 StringInitCPInstruction (org.apache.sysml.runtime.instructions.cp.StringInitCPInstruction)2 UnaryCPInstruction (org.apache.sysml.runtime.instructions.cp.UnaryCPInstruction)2 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)2 BinaryMRInstructionBase (org.apache.sysml.runtime.instructions.mr.BinaryMRInstructionBase)2 CM_N_COVInstruction (org.apache.sysml.runtime.instructions.mr.CM_N_COVInstruction)2 CtableInstruction (org.apache.sysml.runtime.instructions.mr.CtableInstruction)2