Search in sources :

Example 41 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class DMLTranslator method processParameterizedBuiltinFunctionExpression.

/**
 * Construct Hops from parse tree : Process ParameterizedBuiltinFunction Expression in an
 * assignment statement
 *
 * @param source parameterized built-in function
 * @param target data identifier
 * @param hops map of high-level operators
 * @return high-level operator
 */
private Hop processParameterizedBuiltinFunctionExpression(ParameterizedBuiltinFunctionExpression source, DataIdentifier target, HashMap<String, Hop> hops) {
    // this expression has multiple "named" parameters
    HashMap<String, Hop> paramHops = new HashMap<>();
    // -- construct hops for all input parameters
    // -- store them in hashmap so that their "name"s are maintained
    Hop pHop = null;
    for (String paramName : source.getVarParams().keySet()) {
        pHop = processExpression(source.getVarParam(paramName), null, hops);
        paramHops.put(paramName, pHop);
    }
    Hop currBuiltinOp = null;
    if (target == null) {
        target = createTarget(source);
    }
    // construct hop based on opcode
    switch(source.getOpCode()) {
        case CDF:
        case INVCDF:
        case QNORM:
        case QT:
        case QF:
        case QCHISQ:
        case QEXP:
        case PNORM:
        case PT:
        case PF:
        case PCHISQ:
        case PEXP:
            currBuiltinOp = constructDfHop(target.getName(), target.getDataType(), target.getValueType(), source.getOpCode(), paramHops);
            break;
        case GROUPEDAGG:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.GROUPEDAGG, paramHops);
            break;
        case RMEMPTY:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.RMEMPTY, paramHops);
            break;
        case REPLACE:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.REPLACE, paramHops);
            break;
        case ORDER:
            ArrayList<Hop> inputs = new ArrayList<>();
            inputs.add(paramHops.get("target"));
            inputs.add(paramHops.get("by"));
            inputs.add(paramHops.get("decreasing"));
            inputs.add(paramHops.get("index.return"));
            currBuiltinOp = new ReorgOp(target.getName(), target.getDataType(), target.getValueType(), ReOrgOp.SORT, inputs);
            break;
        case TRANSFORMAPPLY:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.TRANSFORMAPPLY, paramHops);
            break;
        case TRANSFORMDECODE:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.TRANSFORMDECODE, paramHops);
            break;
        case TRANSFORMCOLMAP:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.TRANSFORMCOLMAP, paramHops);
            break;
        case TRANSFORMMETA:
            currBuiltinOp = new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.TRANSFORMMETA, paramHops);
            break;
        case TOSTRING:
            // check for input data type and only compile toString Hop for matrices/frames,
            // for scalars, we compile (s + "") to ensure consistent string output value types
            currBuiltinOp = !paramHops.get("target").getDataType().isScalar() ? new ParameterizedBuiltinOp(target.getName(), target.getDataType(), target.getValueType(), ParamBuiltinOp.TOSTRING, paramHops) : HopRewriteUtils.createBinary(paramHops.get("target"), new LiteralOp(""), OpOp2.PLUS);
            break;
        default:
            throw new ParseException(source.printErrorLocation() + "processParameterizedBuiltinFunctionExpression() -- Unknown operation: " + source.getOpCode());
    }
    setIdentifierParams(currBuiltinOp, source.getOutput());
    currBuiltinOp.setParseInfo(source);
    return currBuiltinOp;
}
Also used : ParameterizedBuiltinOp(org.apache.sysml.hops.ParameterizedBuiltinOp) HashMap(java.util.HashMap) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) ReorgOp(org.apache.sysml.hops.ReorgOp) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 42 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class DMLTranslator method processExpression.

/**
 * Construct Hops from parse tree : Process Expression in an assignment
 * statement
 *
 * @param source source expression
 * @param target data identifier
 * @param hops map of high-level operators
 * @return high-level operator
 */
private Hop processExpression(Expression source, DataIdentifier target, HashMap<String, Hop> hops) {
    try {
        if (source instanceof BinaryExpression)
            return processBinaryExpression((BinaryExpression) source, target, hops);
        else if (source instanceof RelationalExpression)
            return processRelationalExpression((RelationalExpression) source, target, hops);
        else if (source instanceof BooleanExpression)
            return processBooleanExpression((BooleanExpression) source, target, hops);
        else if (source instanceof BuiltinFunctionExpression)
            return processBuiltinFunctionExpression((BuiltinFunctionExpression) source, target, hops);
        else if (source instanceof ParameterizedBuiltinFunctionExpression)
            return processParameterizedBuiltinFunctionExpression((ParameterizedBuiltinFunctionExpression) source, target, hops);
        else if (source instanceof DataExpression) {
            Hop ae = (Hop) processDataExpression((DataExpression) source, target, hops);
            if (ae instanceof DataOp) {
                String formatName = ((DataExpression) source).getVarParam(DataExpression.FORMAT_TYPE).toString();
                ((DataOp) ae).setInputFormatType(Expression.convertFormatType(formatName));
            }
            return ae;
        } else if (source instanceof IndexedIdentifier)
            return processIndexingExpression((IndexedIdentifier) source, target, hops);
        else if (source instanceof IntIdentifier) {
            IntIdentifier sourceInt = (IntIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceInt.getValue());
            litop.setParseInfo(sourceInt);
            setIdentifierParams(litop, sourceInt);
            return litop;
        } else if (source instanceof DoubleIdentifier) {
            DoubleIdentifier sourceDouble = (DoubleIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceDouble.getValue());
            litop.setParseInfo(sourceDouble);
            setIdentifierParams(litop, sourceDouble);
            return litop;
        } else if (source instanceof BooleanIdentifier) {
            BooleanIdentifier sourceBoolean = (BooleanIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceBoolean.getValue());
            litop.setParseInfo(sourceBoolean);
            setIdentifierParams(litop, sourceBoolean);
            return litop;
        } else if (source instanceof StringIdentifier) {
            StringIdentifier sourceString = (StringIdentifier) source;
            LiteralOp litop = new LiteralOp(sourceString.getValue());
            litop.setParseInfo(sourceString);
            setIdentifierParams(litop, sourceString);
            return litop;
        } else if (source instanceof DataIdentifier)
            return hops.get(((DataIdentifier) source).getName());
    } catch (Exception e) {
        throw new ParseException(e.getMessage());
    }
    return null;
}
Also used : Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LopsException(org.apache.sysml.lops.LopsException) IOException(java.io.IOException) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Example 43 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationStatic method fuseLogNzUnaryOperation.

private static Hop fuseLogNzUnaryOperation(Hop parent, Hop hi, int pos) {
    // memory estimate and to prevent dense intermediates if X is ultra sparse
    if (HopRewriteUtils.isBinary(hi, OpOp2.MULT) && hi.getInput().get(0).getDataType() == DataType.MATRIX && hi.getInput().get(1).getDataType() == DataType.MATRIX && HopRewriteUtils.isUnary(hi.getInput().get(1), OpOp1.LOG)) {
        Hop pred = hi.getInput().get(0);
        Hop X = hi.getInput().get(1).getInput().get(0);
        if (HopRewriteUtils.isBinary(pred, OpOp2.NOTEQUAL) && // depend on common subexpression elimination
        pred.getInput().get(0) == X && pred.getInput().get(1) instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred.getInput().get(1)) == 0) {
            Hop hnew = HopRewriteUtils.createUnary(X, OpOp1.LOG_NZ);
            // relink new hop into original position
            HopRewriteUtils.replaceChildReference(parent, hi, hnew, pos);
            hi = hnew;
            LOG.debug("Applied fuseLogNzUnaryOperation (line " + hi.getBeginLine() + ").");
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 44 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class RewriteAlgebraicSimplificationStatic method fuseDatagenAndMinusOperation.

private static Hop fuseDatagenAndMinusOperation(Hop hi) {
    if (hi instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hi;
        Hop left = bop.getInput().get(0);
        Hop right = bop.getInput().get(1);
        if (right instanceof DataGenOp && ((DataGenOp) right).getOp() == DataGenMethod.RAND && left instanceof LiteralOp && ((LiteralOp) left).getDoubleValue() == 0.0) {
            DataGenOp inputGen = (DataGenOp) right;
            HashMap<String, Integer> params = inputGen.getParamIndexMap();
            Hop pdf = right.getInput().get(params.get(DataExpression.RAND_PDF));
            int ixMin = params.get(DataExpression.RAND_MIN);
            int ixMax = params.get(DataExpression.RAND_MAX);
            Hop min = right.getInput().get(ixMin);
            Hop max = right.getInput().get(ixMax);
            // apply rewrite under additional conditions (for simplicity)
            if (inputGen.getParent().size() == 1 && min instanceof LiteralOp && max instanceof LiteralOp && pdf instanceof LiteralOp && DataExpression.RAND_PDF_UNIFORM.equals(((LiteralOp) pdf).getStringValue())) {
                // exchange and *-1 (special case 0 stays 0 instead of -0 for consistency)
                double newMinVal = (((LiteralOp) max).getDoubleValue() == 0) ? 0 : (-1 * ((LiteralOp) max).getDoubleValue());
                double newMaxVal = (((LiteralOp) min).getDoubleValue() == 0) ? 0 : (-1 * ((LiteralOp) min).getDoubleValue());
                Hop newMin = new LiteralOp(newMinVal);
                Hop newMax = new LiteralOp(newMaxVal);
                HopRewriteUtils.removeChildReferenceByPos(inputGen, min, ixMin);
                HopRewriteUtils.addChildReference(inputGen, newMin, ixMin);
                HopRewriteUtils.removeChildReferenceByPos(inputGen, max, ixMax);
                HopRewriteUtils.addChildReference(inputGen, newMax, ixMax);
                // rewire all parents (avoid anomalies with replicated datagen)
                List<Hop> parents = new ArrayList<>(bop.getParent());
                for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, bop, inputGen);
                hi = inputGen;
                LOG.debug("Applied fuseDatagenAndMinusOperation (line " + bop.getBeginLine() + ").");
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 45 with LiteralOp

use of org.apache.sysml.hops.LiteralOp in project incubator-systemml by apache.

the class RewriteCommonSubexpressionElimination method rule_CommonSubexpressionElimination_MergeLeafs.

private int rule_CommonSubexpressionElimination_MergeLeafs(Hop hop, HashMap<String, Hop> dataops, HashMap<String, Hop> literalops) {
    int ret = 0;
    if (hop.isVisited())
        return ret;
    if (// LEAF NODE
    hop.getInput().isEmpty()) {
        if (hop instanceof LiteralOp) {
            String key = hop.getValueType() + "_" + hop.getName();
            if (!literalops.containsKey(key))
                literalops.put(key, hop);
        } else if (hop instanceof DataOp && ((DataOp) hop).isRead()) {
            if (!dataops.containsKey(hop.getName()))
                dataops.put(hop.getName(), hop);
        }
    } else // INNER NODE
    {
        // merge leaf nodes (data, literal)
        for (int i = 0; i < hop.getInput().size(); i++) {
            Hop hi = hop.getInput().get(i);
            String litKey = hi.getValueType() + "_" + hi.getName();
            if (hi instanceof DataOp && ((DataOp) hi).isRead() && dataops.containsKey(hi.getName())) {
                // replace child node ref
                Hop tmp = dataops.get(hi.getName());
                if (tmp != hi) {
                    // if required
                    tmp.getParent().add(hop);
                    tmp.setVisited();
                    hop.getInput().set(i, tmp);
                    ret++;
                }
            } else if (hi instanceof LiteralOp && literalops.containsKey(litKey)) {
                Hop tmp = literalops.get(litKey);
                // replace child node ref
                if (tmp != hi) {
                    // if required
                    tmp.getParent().add(hop);
                    tmp.setVisited();
                    hop.getInput().set(i, tmp);
                    ret++;
                }
            }
            // recursive invocation (direct return on merged nodes)
            ret += rule_CommonSubexpressionElimination_MergeLeafs(hi, dataops, literalops);
        }
    }
    hop.setVisited();
    return ret;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Aggregations

LiteralOp (org.apache.sysml.hops.LiteralOp)102 Hop (org.apache.sysml.hops.Hop)88 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)30 BinaryOp (org.apache.sysml.hops.BinaryOp)27 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)25 UnaryOp (org.apache.sysml.hops.UnaryOp)24 DataOp (org.apache.sysml.hops.DataOp)23 IndexingOp (org.apache.sysml.hops.IndexingOp)17 ArrayList (java.util.ArrayList)13 DataGenOp (org.apache.sysml.hops.DataGenOp)13 LeftIndexingOp (org.apache.sysml.hops.LeftIndexingOp)12 HashMap (java.util.HashMap)11 DataIdentifier (org.apache.sysml.parser.DataIdentifier)10 ReorgOp (org.apache.sysml.hops.ReorgOp)9 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)9 HopsException (org.apache.sysml.hops.HopsException)8 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)8 StatementBlock (org.apache.sysml.parser.StatementBlock)8 TernaryOp (org.apache.sysml.hops.TernaryOp)7 CNodeData (org.apache.sysml.hops.codegen.cplan.CNodeData)6