Search in sources :

Example 11 with SimpleOperator

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

the class BuiltinNaryCPInstruction method parseInstruction.

public static BuiltinNaryCPInstruction parseInstruction(String str) {
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
    String opcode = parts[0];
    CPOperand outputOperand = new CPOperand(parts[parts.length - 1]);
    CPOperand[] inputOperands = null;
    if (parts.length > 2) {
        inputOperands = new CPOperand[parts.length - 2];
        for (int i = 1; i < parts.length - 1; i++) inputOperands[i - 1] = new CPOperand(parts[i]);
    }
    if (Nary.OperationType.PRINTF.name().equalsIgnoreCase(opcode)) {
        ValueFunction func = Builtin.getBuiltinFnObject(opcode);
        return new ScalarBuiltinNaryCPInstruction(new SimpleOperator(func), opcode, str, outputOperand, inputOperands);
    } else if (opcode.equals("cbind") || opcode.equals("rbind")) {
        return new MatrixBuiltinNaryCPInstruction(null, opcode, str, outputOperand, inputOperands);
    } else if (Nary.OperationType.EVAL.name().equalsIgnoreCase(opcode)) {
        return new EvalNaryCPInstruction(null, opcode, str, outputOperand, inputOperands);
    }
    throw new DMLRuntimeException("Opcode (" + opcode + ") not recognized in BuiltinMultipleCPInstruction");
}
Also used : ValueFunction(org.apache.sysml.runtime.functionobjects.ValueFunction) SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 12 with SimpleOperator

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

the class CtableCPInstruction method parseInstruction.

public static CtableCPInstruction parseInstruction(String inst) {
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
    InstructionUtils.checkNumFields(parts, 7);
    String opcode = parts[0];
    // handle opcode
    if (!(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand"))) {
        throw new DMLRuntimeException("Unexpected opcode in TertiaryCPInstruction: " + inst);
    }
    boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
    // handle operands
    CPOperand in1 = new CPOperand(parts[1]);
    CPOperand in2 = new CPOperand(parts[2]);
    CPOperand in3 = new CPOperand(parts[3]);
    // handle known dimension information
    String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
    String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
    CPOperand out = new CPOperand(parts[6]);
    boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
    // ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
    return new CtableCPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
}
Also used : SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 13 with SimpleOperator

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

the class ParameterizedBuiltinCPInstruction method processInstruction.

@Override
public void processInstruction(ExecutionContext ec) {
    String opcode = getOpcode();
    ScalarObject sores = null;
    if (opcode.equalsIgnoreCase("cdf")) {
        SimpleOperator op = (SimpleOperator) _optr;
        double result = op.fn.execute(params);
        sores = new DoubleObject(result);
        ec.setScalarOutput(output.getName(), sores);
    } else if (opcode.equalsIgnoreCase("invcdf")) {
        SimpleOperator op = (SimpleOperator) _optr;
        double result = op.fn.execute(params);
        sores = new DoubleObject(result);
        ec.setScalarOutput(output.getName(), sores);
    } else if (opcode.equalsIgnoreCase("groupedagg")) {
        // acquire locks
        MatrixBlock target = ec.getMatrixInput(params.get(Statement.GAGG_TARGET), getExtendedOpcode());
        MatrixBlock groups = ec.getMatrixInput(params.get(Statement.GAGG_GROUPS), getExtendedOpcode());
        MatrixBlock weights = null;
        if (params.get(Statement.GAGG_WEIGHTS) != null)
            weights = ec.getMatrixInput(params.get(Statement.GAGG_WEIGHTS), getExtendedOpcode());
        int ngroups = -1;
        if (params.get(Statement.GAGG_NUM_GROUPS) != null) {
            ngroups = (int) Double.parseDouble(params.get(Statement.GAGG_NUM_GROUPS));
        }
        // compute the result
        // num threads
        int k = Integer.parseInt(params.get("k"));
        MatrixBlock soresBlock = groups.groupedAggOperations(target, weights, new MatrixBlock(), ngroups, _optr, k);
        ec.setMatrixOutput(output.getName(), soresBlock, getExtendedOpcode());
        // release locks
        target = groups = weights = null;
        ec.releaseMatrixInput(params.get(Statement.GAGG_TARGET), getExtendedOpcode());
        ec.releaseMatrixInput(params.get(Statement.GAGG_GROUPS), getExtendedOpcode());
        if (params.get(Statement.GAGG_WEIGHTS) != null)
            ec.releaseMatrixInput(params.get(Statement.GAGG_WEIGHTS), getExtendedOpcode());
    } else if (opcode.equalsIgnoreCase("rmempty")) {
        String margin = params.get("margin");
        if (!(margin.equals("rows") || margin.equals("cols")))
            throw new DMLRuntimeException("Unspupported margin identifier '" + margin + "'.");
        // acquire locks
        MatrixBlock target = ec.getMatrixInput(params.get("target"), getExtendedOpcode());
        MatrixBlock select = params.containsKey("select") ? ec.getMatrixInput(params.get("select"), getExtendedOpcode()) : null;
        // compute the result
        boolean emptyReturn = Boolean.parseBoolean(params.get("empty.return").toLowerCase());
        MatrixBlock soresBlock = target.removeEmptyOperations(new MatrixBlock(), margin.equals("rows"), emptyReturn, select);
        // release locks
        ec.setMatrixOutput(output.getName(), soresBlock, getExtendedOpcode());
        ec.releaseMatrixInput(params.get("target"), getExtendedOpcode());
        if (params.containsKey("select"))
            ec.releaseMatrixInput(params.get("select"), getExtendedOpcode());
    } else if (opcode.equalsIgnoreCase("replace")) {
        // acquire locks
        MatrixBlock target = ec.getMatrixInput(params.get("target"), getExtendedOpcode());
        // compute the result
        double pattern = Double.parseDouble(params.get("pattern"));
        double replacement = Double.parseDouble(params.get("replacement"));
        MatrixBlock ret = (MatrixBlock) target.replaceOperations(new MatrixBlock(), pattern, replacement);
        // release locks
        ec.setMatrixOutput(output.getName(), ret, getExtendedOpcode());
        ec.releaseMatrixInput(params.get("target"), getExtendedOpcode());
    } else if (opcode.equalsIgnoreCase("rexpand")) {
        // acquire locks
        MatrixBlock target = ec.getMatrixInput(params.get("target"), getExtendedOpcode());
        // compute the result
        double maxVal = Double.parseDouble(params.get("max"));
        boolean dirVal = params.get("dir").equals("rows");
        boolean cast = Boolean.parseBoolean(params.get("cast"));
        boolean ignore = Boolean.parseBoolean(params.get("ignore"));
        int numThreads = Integer.parseInt(params.get("k"));
        MatrixBlock ret = (MatrixBlock) target.rexpandOperations(new MatrixBlock(), maxVal, dirVal, cast, ignore, numThreads);
        // release locks
        ec.setMatrixOutput(output.getName(), ret, getExtendedOpcode());
        ec.releaseMatrixInput(params.get("target"), getExtendedOpcode());
    } else if (opcode.equalsIgnoreCase("transformapply")) {
        // acquire locks
        FrameBlock data = ec.getFrameInput(params.get("target"));
        FrameBlock meta = ec.getFrameInput(params.get("meta"));
        String[] colNames = data.getColumnNames();
        // compute transformapply
        Encoder encoder = EncoderFactory.createEncoder(params.get("spec"), colNames, data.getNumColumns(), meta);
        MatrixBlock mbout = encoder.apply(data, new MatrixBlock(data.getNumRows(), data.getNumColumns(), false));
        // release locks
        ec.setMatrixOutput(output.getName(), mbout, getExtendedOpcode());
        ec.releaseFrameInput(params.get("target"));
        ec.releaseFrameInput(params.get("meta"));
    } else if (opcode.equalsIgnoreCase("transformdecode")) {
        // acquire locks
        MatrixBlock data = ec.getMatrixInput(params.get("target"), getExtendedOpcode());
        FrameBlock meta = ec.getFrameInput(params.get("meta"));
        String[] colnames = meta.getColumnNames();
        // compute transformdecode
        Decoder decoder = DecoderFactory.createDecoder(getParameterMap().get("spec"), colnames, null, meta);
        FrameBlock fbout = decoder.decode(data, new FrameBlock(decoder.getSchema()));
        fbout.setColumnNames(Arrays.copyOfRange(colnames, 0, fbout.getNumColumns()));
        // release locks
        ec.setFrameOutput(output.getName(), fbout);
        ec.releaseMatrixInput(params.get("target"), getExtendedOpcode());
        ec.releaseFrameInput(params.get("meta"));
    } else if (opcode.equalsIgnoreCase("transformcolmap")) {
        // acquire locks
        FrameBlock meta = ec.getFrameInput(params.get("target"));
        String[] colNames = meta.getColumnNames();
        // compute transformapply
        Encoder encoder = EncoderFactory.createEncoder(params.get("spec"), colNames, meta.getNumColumns(), null);
        MatrixBlock mbout = encoder.getColMapping(meta, new MatrixBlock(meta.getNumColumns(), 3, false));
        // release locks
        ec.setMatrixOutput(output.getName(), mbout, getExtendedOpcode());
        ec.releaseFrameInput(params.get("target"));
    } else if (opcode.equalsIgnoreCase("transformmeta")) {
        // get input spec and path
        String spec = getParameterMap().get("spec");
        String path = getParameterMap().get(ParameterizedBuiltinFunctionExpression.TF_FN_PARAM_MTD);
        String delim = getParameterMap().containsKey("sep") ? getParameterMap().get("sep") : TfUtils.TXMTD_SEP;
        // execute transform meta data read
        FrameBlock meta = null;
        try {
            meta = TfMetaUtils.readTransformMetaDataFromFile(spec, path, delim);
        } catch (Exception ex) {
            throw new DMLRuntimeException(ex);
        }
        // release locks
        ec.setFrameOutput(output.getName(), meta);
    } else if (opcode.equalsIgnoreCase("toString")) {
        // handle input parameters
        int rows = (getParam("rows") != null) ? Integer.parseInt(getParam("rows")) : TOSTRING_MAXROWS;
        int cols = (getParam("cols") != null) ? Integer.parseInt(getParam("cols")) : TOSTRING_MAXCOLS;
        int decimal = (getParam("decimal") != null) ? Integer.parseInt(getParam("decimal")) : TOSTRING_DECIMAL;
        boolean sparse = (getParam("sparse") != null) ? Boolean.parseBoolean(getParam("sparse")) : TOSTRING_SPARSE;
        String separator = (getParam("sep") != null) ? getParam("sep") : TOSTRING_SEPARATOR;
        String lineseparator = (getParam("linesep") != null) ? getParam("linesep") : TOSTRING_LINESEPARATOR;
        // get input matrix/frame and convert to string
        CacheableData<?> data = ec.getCacheableData(getParam("target"));
        String out = null;
        if (data instanceof MatrixObject) {
            MatrixBlock matrix = (MatrixBlock) data.acquireRead();
            warnOnTrunction(matrix, rows, cols);
            out = DataConverter.toString(matrix, sparse, separator, lineseparator, rows, cols, decimal);
        } else if (data instanceof FrameObject) {
            FrameBlock frame = (FrameBlock) data.acquireRead();
            warnOnTrunction(frame, rows, cols);
            out = DataConverter.toString(frame, sparse, separator, lineseparator, rows, cols, decimal);
        } else {
            throw new DMLRuntimeException("toString only converts matrix or frames to string");
        }
        ec.releaseCacheableData(getParam("target"));
        ec.setScalarOutput(output.getName(), new StringObject(out));
    } else {
        throw new DMLRuntimeException("Unknown opcode : " + opcode);
    }
}
Also used : SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) FrameObject(org.apache.sysml.runtime.controlprogram.caching.FrameObject) Decoder(org.apache.sysml.runtime.transform.decode.Decoder) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) Encoder(org.apache.sysml.runtime.transform.encode.Encoder)

Example 14 with SimpleOperator

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

the class CtableSPInstruction method parseInstruction.

public static CtableSPInstruction parseInstruction(String inst) {
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(inst);
    InstructionUtils.checkNumFields(parts, 7);
    String opcode = parts[0];
    // handle opcode
    if (!(opcode.equalsIgnoreCase("ctable") || opcode.equalsIgnoreCase("ctableexpand"))) {
        throw new DMLRuntimeException("Unexpected opcode in TertiarySPInstruction: " + inst);
    }
    boolean isExpand = opcode.equalsIgnoreCase("ctableexpand");
    // handle operands
    CPOperand in1 = new CPOperand(parts[1]);
    CPOperand in2 = new CPOperand(parts[2]);
    CPOperand in3 = new CPOperand(parts[3]);
    // handle known dimension information
    String[] dim1Fields = parts[4].split(Instruction.LITERAL_PREFIX);
    String[] dim2Fields = parts[5].split(Instruction.LITERAL_PREFIX);
    CPOperand out = new CPOperand(parts[6]);
    boolean ignoreZeros = Boolean.parseBoolean(parts[7]);
    // ctable does not require any operator, so we simply pass-in a dummy operator with null functionobject
    return new CtableSPInstruction(new SimpleOperator(null), in1, in2, in3, out, dim1Fields[0], Boolean.parseBoolean(dim1Fields[1]), dim2Fields[0], Boolean.parseBoolean(dim2Fields[1]), isExpand, ignoreZeros, opcode, inst);
}
Also used : SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 15 with SimpleOperator

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

the class IndexingSPInstruction method parseInstruction.

public static IndexingSPInstruction parseInstruction(String str) {
    String[] parts = InstructionUtils.getInstructionPartsWithValueType(str);
    String opcode = parts[0];
    if (opcode.equalsIgnoreCase(RightIndex.OPCODE)) {
        if (parts.length == 8) {
            CPOperand in = new CPOperand(parts[1]);
            CPOperand rl = new CPOperand(parts[2]);
            CPOperand ru = new CPOperand(parts[3]);
            CPOperand cl = new CPOperand(parts[4]);
            CPOperand cu = new CPOperand(parts[5]);
            CPOperand out = new CPOperand(parts[6]);
            SparkAggType aggtype = SparkAggType.valueOf(parts[7]);
            if (in.getDataType() == DataType.MATRIX)
                return new MatrixIndexingSPInstruction(new SimpleOperator(null), in, rl, ru, cl, cu, out, aggtype, opcode, str);
            else
                return new FrameIndexingSPInstruction(new SimpleOperator(null), in, rl, ru, cl, cu, out, aggtype, opcode, str);
        } else {
            throw new DMLRuntimeException("Invalid number of operands in instruction: " + str);
        }
    } else if (opcode.equalsIgnoreCase(LeftIndex.OPCODE) || opcode.equalsIgnoreCase("mapLeftIndex")) {
        if (parts.length == 9) {
            CPOperand lhsInput = new CPOperand(parts[1]);
            CPOperand rhsInput = new CPOperand(parts[2]);
            CPOperand rl = new CPOperand(parts[3]);
            CPOperand ru = new CPOperand(parts[4]);
            CPOperand cl = new CPOperand(parts[5]);
            CPOperand cu = new CPOperand(parts[6]);
            CPOperand out = new CPOperand(parts[7]);
            LixCacheType lixtype = LixCacheType.valueOf(parts[8]);
            if (lhsInput.getDataType() == DataType.MATRIX)
                return new MatrixIndexingSPInstruction(new SimpleOperator(null), lhsInput, rhsInput, rl, ru, cl, cu, out, lixtype, opcode, str);
            else
                return new FrameIndexingSPInstruction(new SimpleOperator(null), lhsInput, rhsInput, rl, ru, cl, cu, out, opcode, str);
        } else {
            throw new DMLRuntimeException("Invalid number of operands in instruction: " + str);
        }
    } else {
        throw new DMLRuntimeException("Unknown opcode while parsing a IndexingSPInstruction: " + str);
    }
}
Also used : SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) SparkAggType(org.apache.sysml.hops.AggBinaryOp.SparkAggType) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) LixCacheType(org.apache.sysml.lops.LeftIndex.LixCacheType) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

SimpleOperator (org.apache.sysml.runtime.matrix.operators.SimpleOperator)19 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)14 CPOperand (org.apache.sysml.runtime.instructions.cp.CPOperand)7 ValueFunction (org.apache.sysml.runtime.functionobjects.ValueFunction)6 Operator (org.apache.sysml.runtime.matrix.operators.Operator)2 HashMap (java.util.HashMap)1 SparkAggType (org.apache.sysml.hops.AggBinaryOp.SparkAggType)1 LixCacheType (org.apache.sysml.lops.LeftIndex.LixCacheType)1 DMLScriptException (org.apache.sysml.runtime.DMLScriptException)1 FrameObject (org.apache.sysml.runtime.controlprogram.caching.FrameObject)1 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)1 FrameBlock (org.apache.sysml.runtime.matrix.data.FrameBlock)1 MatrixBlock (org.apache.sysml.runtime.matrix.data.MatrixBlock)1 AggregateOperator (org.apache.sysml.runtime.matrix.operators.AggregateOperator)1 AggregateUnaryOperator (org.apache.sysml.runtime.matrix.operators.AggregateUnaryOperator)1 CMOperator (org.apache.sysml.runtime.matrix.operators.CMOperator)1 UnaryOperator (org.apache.sysml.runtime.matrix.operators.UnaryOperator)1 Decoder (org.apache.sysml.runtime.transform.decode.Decoder)1 Encoder (org.apache.sysml.runtime.transform.encode.Encoder)1