Search in sources :

Example 91 with LiteralOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyReplaceZeroOperation.

// Patterns: X + (X==0) * s -> replace(X, 0, s)
private static Hop simplifyReplaceZeroOperation(Hop parent, Hop hi, int pos) {
    if (HopRewriteUtils.isBinary(hi, OpOp2.PLUS) && hi.getInput().get(0).isMatrix() && HopRewriteUtils.isBinary(hi.getInput().get(1), OpOp2.MULT) && hi.getInput().get(1).getInput().get(1).isScalar() && HopRewriteUtils.isBinaryMatrixScalar(hi.getInput().get(1).getInput().get(0), OpOp2.EQUAL, 0) && hi.getInput().get(1).getInput().get(0).getInput().contains(hi.getInput().get(0))) {
        HashMap<String, Hop> args = new HashMap<>();
        args.put("target", hi.getInput().get(0));
        args.put("pattern", new LiteralOp(0));
        args.put("replacement", hi.getInput().get(1).getInput().get(1));
        Hop replace = HopRewriteUtils.createParameterizedBuiltinOp(hi.getInput().get(0), args, ParamBuiltinOp.REPLACE);
        HopRewriteUtils.replaceChildReference(parent, hi, replace, pos);
        hi = replace;
        LOG.debug("Applied simplifyReplaceZeroOperation (line " + hi.getBeginLine() + ").");
    }
    return hi;
}
Also used : HashMap(java.util.HashMap) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 92 with LiteralOp

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

the class RewriteAlgebraicSimplificationStatic method simplifyConstantSort.

private static Hop simplifyConstantSort(Hop parent, Hop hi, int pos) {
    // order(matrix(7), indexreturn=TRUE) -> seq(1,nrow(X),1)
    if (// order
    hi instanceof ReorgOp && ((ReorgOp) hi).getOp() == ReOrgOp.SORT) {
        Hop hi2 = hi.getInput().get(0);
        if (hi2 instanceof DataGenOp && ((DataGenOp) hi2).getOp() == DataGenMethod.RAND && ((DataGenOp) hi2).hasConstantValue() && // known indexreturn
        hi.getInput().get(3) instanceof LiteralOp) {
            if (HopRewriteUtils.getBooleanValue((LiteralOp) hi.getInput().get(3))) {
                // order(matrix(7), indexreturn=TRUE) -> seq(1,nrow(X),1)
                Hop seq = HopRewriteUtils.createSeqDataGenOp(hi2);
                seq.refreshSizeInformation();
                HopRewriteUtils.replaceChildReference(parent, hi, seq, pos);
                HopRewriteUtils.cleanupUnreferenced(hi);
                hi = seq;
                LOG.debug("Applied simplifyConstantSort1.");
            } else {
                // order(matrix(7), indexreturn=FALSE) -> matrix(7)
                HopRewriteUtils.replaceChildReference(parent, hi, hi2, pos);
                HopRewriteUtils.cleanupUnreferenced(hi);
                hi = hi2;
                LOG.debug("Applied simplifyConstantSort2.");
            }
        }
    }
    return hi;
}
Also used : DataGenOp(org.apache.sysml.hops.DataGenOp) ReorgOp(org.apache.sysml.hops.ReorgOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 93 with LiteralOp

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

the class RewriteAlgebraicSimplificationStatic method removeUnnecessaryBinaryOperation.

/**
 * handle removal of unnecessary binary operations
 *
 * X/1 or X*1 or 1*X or X-0 -> X
 * -1*X or X*-1-> -X
 *
 * @param parent parent high-level operator
 * @param hi high-level operator
 * @param pos position
 * @return high-level operator
 */
private static Hop removeUnnecessaryBinaryOperation(Hop parent, Hop hi, int pos) {
    if (hi instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hi;
        Hop left = bop.getInput().get(0);
        Hop right = bop.getInput().get(1);
        // X/1 or X*1 -> X
        if (left.getDataType() == DataType.MATRIX && right instanceof LiteralOp && ((LiteralOp) right).getDoubleValue() == 1.0) {
            if (bop.getOp() == OpOp2.DIV || bop.getOp() == OpOp2.MULT) {
                HopRewriteUtils.replaceChildReference(parent, bop, left, pos);
                hi = left;
                LOG.debug("Applied removeUnnecessaryBinaryOperation1 (line " + bop.getBeginLine() + ")");
            }
        } else // X-0 -> X
        if (left.getDataType() == DataType.MATRIX && right instanceof LiteralOp && ((LiteralOp) right).getDoubleValue() == 0.0) {
            if (bop.getOp() == OpOp2.MINUS) {
                HopRewriteUtils.replaceChildReference(parent, bop, left, pos);
                hi = left;
                LOG.debug("Applied removeUnnecessaryBinaryOperation2 (line " + bop.getBeginLine() + ")");
            }
        } else // 1*X -> X
        if (right.getDataType() == DataType.MATRIX && left instanceof LiteralOp && ((LiteralOp) left).getDoubleValue() == 1.0) {
            if (bop.getOp() == OpOp2.MULT) {
                HopRewriteUtils.replaceChildReference(parent, bop, right, pos);
                hi = right;
                LOG.debug("Applied removeUnnecessaryBinaryOperation3 (line " + bop.getBeginLine() + ")");
            }
        } else // -X to -1*X due to mechanical reasons
        if (right.getDataType() == DataType.MATRIX && left instanceof LiteralOp && ((LiteralOp) left).getDoubleValue() == -1.0) {
            if (bop.getOp() == OpOp2.MULT) {
                bop.setOp(OpOp2.MINUS);
                HopRewriteUtils.replaceChildReference(bop, left, new LiteralOp(0), 0);
                hi = bop;
                LOG.debug("Applied removeUnnecessaryBinaryOperation4 (line " + bop.getBeginLine() + ")");
            }
        } else // X*-1 -> -X (see comment above)
        if (left.getDataType() == DataType.MATRIX && right instanceof LiteralOp && ((LiteralOp) right).getDoubleValue() == -1.0) {
            if (bop.getOp() == OpOp2.MULT) {
                bop.setOp(OpOp2.MINUS);
                HopRewriteUtils.removeChildReferenceByPos(bop, right, 1);
                HopRewriteUtils.addChildReference(bop, new LiteralOp(0), 0);
                hi = bop;
                LOG.debug("Applied removeUnnecessaryBinaryOperation5 (line " + bop.getBeginLine() + ")");
            }
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 94 with LiteralOp

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

the class OptTreeConverter method rCreateAbstractOptNodes.

public static ArrayList<OptNode> rCreateAbstractOptNodes(Hop hop, LocalVariableMap vars, Set<String> memo) {
    ArrayList<OptNode> ret = new ArrayList<>();
    ArrayList<Hop> in = hop.getInput();
    if (hop.isVisited())
        return ret;
    // general case
    if (!(hop instanceof DataOp || hop instanceof LiteralOp || hop instanceof FunctionOp)) {
        OptNode node = new OptNode(NodeType.HOP);
        String opstr = hop.getOpString();
        node.addParam(ParamType.OPSTRING, opstr);
        // handle execution type
        LopProperties.ExecType et = (hop.getExecType() != null) ? hop.getExecType() : LopProperties.ExecType.CP;
        switch(et) {
            case CP:
            case GPU:
                node.setExecType(ExecType.CP);
                break;
            case SPARK:
                node.setExecType(ExecType.SPARK);
                break;
            case MR:
                node.setExecType(ExecType.MR);
                break;
            default:
                throw new DMLRuntimeException("Unsupported optnode exec type: " + et);
        }
        // handle degree of parallelism
        if (et == LopProperties.ExecType.CP && hop instanceof MultiThreadedHop) {
            MultiThreadedHop mtop = (MultiThreadedHop) hop;
            node.setK(OptimizerUtils.getConstrainedNumThreads(mtop.getMaxNumThreads()));
        }
        // assign node to return
        _hlMap.putHopMapping(hop, node);
        ret.add(node);
    } else // process function calls
    if (hop instanceof FunctionOp && INCLUDE_FUNCTIONS) {
        FunctionOp fhop = (FunctionOp) hop;
        String fname = fhop.getFunctionName();
        String fnspace = fhop.getFunctionNamespace();
        String fKey = fhop.getFunctionKey();
        Object[] prog = _hlMap.getRootProgram();
        OptNode node = new OptNode(NodeType.FUNCCALL);
        _hlMap.putHopMapping(fhop, node);
        node.setExecType(ExecType.CP);
        node.addParam(ParamType.OPSTRING, fKey);
        if (!fnspace.equals(DMLProgram.INTERNAL_NAMESPACE)) {
            FunctionProgramBlock fpb = ((Program) prog[1]).getFunctionProgramBlock(fnspace, fname);
            FunctionStatementBlock fsb = ((DMLProgram) prog[0]).getFunctionStatementBlock(fnspace, fname);
            FunctionStatement fs = (FunctionStatement) fsb.getStatement(0);
            // process body; NOTE: memo prevents inclusion of functions multiple times
            if (!memo.contains(fKey)) {
                memo.add(fKey);
                int len = fs.getBody().size();
                for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
                    ProgramBlock lpb = fpb.getChildBlocks().get(i);
                    StatementBlock lsb = fs.getBody().get(i);
                    node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
                }
                memo.remove(fKey);
            } else
                node.addParam(ParamType.RECURSIVE_CALL, "true");
        }
        ret.add(node);
    }
    if (in != null)
        for (Hop hin : in) if (// no need for opt nodes
        !(hin instanceof DataOp || hin instanceof LiteralOp))
            ret.addAll(rCreateAbstractOptNodes(hin, vars, memo));
    hop.setVisited();
    return ret;
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) FunctionOp(org.apache.sysml.hops.FunctionOp) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) FunctionStatement(org.apache.sysml.parser.FunctionStatement) LopProperties(org.apache.sysml.lops.LopProperties) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock)

Example 95 with LiteralOp

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

the class OptimizerRuleBased method isResultFullReplace.

protected boolean isResultFullReplace(OptNode n, String resultVar, String iterVarname, MatrixObject mo) {
    // check left indexing operator
    String opStr = n.getParam(ParamType.OPSTRING);
    if (opStr == null || !opStr.equals(LeftIndexingOp.OPSTRING))
        return false;
    Hop h = OptTreeConverter.getAbstractPlanMapping().getMappedHop(n.getID());
    Hop base = h.getInput().get(0);
    // check result variable
    if (!resultVar.equals(base.getName()))
        return false;
    // check access pattern, memory budget
    Hop inpRowL = h.getInput().get(2);
    Hop inpRowU = h.getInput().get(3);
    Hop inpColL = h.getInput().get(4);
    Hop inpColU = h.getInput().get(5);
    // check for rowwise overwrite
    if ((inpRowL.getName().equals(iterVarname) && inpRowU.getName().equals(iterVarname)) && inpColL instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) inpColL) == 1 && inpColU instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) inpColU) == mo.getNumColumns()) {
        return true;
    }
    // check for colwise overwrite
    if ((inpColL.getName().equals(iterVarname) && inpColU.getName().equals(iterVarname)) && inpRowL instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) inpRowL) == 1 && inpRowU instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) inpRowU) == mo.getNumRows()) {
        return true;
    }
    return false;
}
Also used : Hop(org.apache.sysml.hops.Hop) MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) LiteralOp(org.apache.sysml.hops.LiteralOp)

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