Search in sources :

Example 46 with LiteralOp

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

the class RewriteConstantFolding method rConstantFoldingExpression.

private Hop rConstantFoldingExpression(Hop root) {
    if (root.isVisited())
        return root;
    // no iterator in order to prevent concurrent modification
    for (int i = 0; i < root.getInput().size(); i++) {
        Hop h = root.getInput().get(i);
        rConstantFoldingExpression(h);
    }
    LiteralOp literal = null;
    // fold binary op if both are literals / unary op if literal
    if (// scalar output
    root.getDataType() == DataType.SCALAR && (isApplicableBinaryOp(root) || isApplicableUnaryOp(root))) {
        // core constant folding via runtime instructions
        try {
            literal = evalScalarOperation(root);
        } catch (Exception ex) {
            LOG.error("Failed to execute constant folding instructions. No abort.", ex);
        }
    } else // fold conjunctive predicate if at least one input is literal 'false'
    if (isApplicableFalseConjunctivePredicate(root)) {
        literal = new LiteralOp(false);
    } else // fold disjunctive predicate if at least one input is literal 'true'
    if (isApplicableTrueDisjunctivePredicate(root)) {
        literal = new LiteralOp(true);
    }
    // replace binary operator with folded constant
    if (literal != null) {
        // reverse replacement in order to keep common subexpression elimination
        int plen = root.getParent().size();
        if (// broot is NOT a DAG root
        plen > 0) {
            for (// for all parents
            int i = 0; // for all parents
            i < root.getParent().size(); // for all parents
            i++) {
                Hop parent = root.getParent().get(i);
                for (int j = 0; j < parent.getInput().size(); j++) {
                    Hop child = parent.getInput().get(j);
                    if (root == child) {
                        // replace operator
                        // root to parent link cannot be removed within this loop, as loop iterates over list containing parents.
                        parent.getInput().remove(j);
                        HopRewriteUtils.addChildReference(parent, literal, j);
                    }
                }
            }
            root.getParent().clear();
        } else // broot IS a DAG root
        {
            root = literal;
        }
    }
    // mark processed
    root.setVisited();
    return root;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) HopsException(org.apache.sysml.hops.HopsException)

Example 47 with LiteralOp

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

the class RewriteElementwiseMultChainOptimization method constructPower.

private static Hop constructPower(final Hop hop, final int cnt) {
    assert (cnt >= 1);
    // we will visit the leaves' children next
    hop.setVisited();
    if (cnt == 1)
        return hop;
    final Hop pow = HopRewriteUtils.createBinary(hop, new LiteralOp(cnt), Hop.OpOp2.POW);
    pow.setVisited();
    return pow;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 48 with LiteralOp

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

the class RewriteForLoopVectorization method vectorizeElementwiseBinary.

private static StatementBlock vectorizeElementwiseBinary(StatementBlock sb, StatementBlock csb, Hop from, Hop to, Hop increment, String itervar) {
    StatementBlock ret = sb;
    // check supported increment values
    if (!(increment instanceof LiteralOp && ((LiteralOp) increment).getDoubleValue() == 1.0)) {
        return ret;
    }
    // check for applicability
    boolean apply = false;
    // row or col
    boolean rowIx = false;
    if (csb.getHops() != null && csb.getHops().size() == 1) {
        Hop root = csb.getHops().get(0);
        if (root.getDataType() == DataType.MATRIX && root.getInput().get(0) instanceof LeftIndexingOp) {
            LeftIndexingOp lix = (LeftIndexingOp) root.getInput().get(0);
            Hop lixlhs = lix.getInput().get(0);
            Hop lixrhs = lix.getInput().get(1);
            if (lixlhs instanceof DataOp && lixrhs instanceof BinaryOp && lixrhs.getInput().get(0) instanceof IndexingOp && lixrhs.getInput().get(1) instanceof IndexingOp && lixrhs.getInput().get(0).getInput().get(0) instanceof DataOp && lixrhs.getInput().get(1).getInput().get(0) instanceof DataOp) {
                IndexingOp rix0 = (IndexingOp) lixrhs.getInput().get(0);
                IndexingOp rix1 = (IndexingOp) lixrhs.getInput().get(1);
                // check for rowwise
                if (lix.isRowLowerEqualsUpper() && rix0.isRowLowerEqualsUpper() && rix1.isRowLowerEqualsUpper() && lix.getInput().get(2).getName().equals(itervar) && rix0.getInput().get(1).getName().equals(itervar) && rix1.getInput().get(1).getName().equals(itervar)) {
                    apply = true;
                    rowIx = true;
                }
                // check for colwise
                if (lix.isColLowerEqualsUpper() && rix0.isColLowerEqualsUpper() && rix1.isColLowerEqualsUpper() && lix.getInput().get(4).getName().equals(itervar) && rix0.getInput().get(3).getName().equals(itervar) && rix1.getInput().get(3).getName().equals(itervar)) {
                    apply = true;
                    rowIx = false;
                }
            }
        }
    }
    // apply rewrite if possible
    if (apply) {
        Hop root = csb.getHops().get(0);
        LeftIndexingOp lix = (LeftIndexingOp) root.getInput().get(0);
        BinaryOp bop = (BinaryOp) lix.getInput().get(1);
        IndexingOp rix0 = (IndexingOp) bop.getInput().get(0);
        IndexingOp rix1 = (IndexingOp) bop.getInput().get(1);
        int index1 = rowIx ? 2 : 4;
        int index2 = rowIx ? 3 : 5;
        // modify left indexing bounds
        HopRewriteUtils.replaceChildReference(lix, lix.getInput().get(index1), from, index1);
        HopRewriteUtils.replaceChildReference(lix, lix.getInput().get(index2), to, index2);
        // modify both right indexing
        HopRewriteUtils.replaceChildReference(rix0, rix0.getInput().get(index1 - 1), from, index1 - 1);
        HopRewriteUtils.replaceChildReference(rix0, rix0.getInput().get(index2 - 1), to, index2 - 1);
        HopRewriteUtils.replaceChildReference(rix1, rix1.getInput().get(index1 - 1), from, index1 - 1);
        HopRewriteUtils.replaceChildReference(rix1, rix1.getInput().get(index2 - 1), to, index2 - 1);
        updateLeftAndRightIndexingSizes(rowIx, lix, rix0, rix1);
        bop.refreshSizeInformation();
        // after bop update
        lix.refreshSizeInformation();
        ret = csb;
        // ret.liveIn().removeVariable(itervar);
        LOG.debug("Applied vectorizeElementwiseBinaryForLoop.");
    }
    return ret;
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 49 with LiteralOp

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

the class RewriteIndexingVectorization method vectorizeRightIndexing.

/**
 * Note: unnecessary row or column indexing then later removed via
 * dynamic rewrites
 *
 * @param hop high-level operator
 */
@SuppressWarnings("unused")
private static void vectorizeRightIndexing(Hop hop) {
    if (// right indexing
    hop instanceof IndexingOp) {
        IndexingOp ihop0 = (IndexingOp) hop;
        boolean isSingleRow = ihop0.isRowLowerEqualsUpper();
        boolean isSingleCol = ihop0.isColLowerEqualsUpper();
        boolean appliedRow = false;
        // search for multiple indexing in same row
        if (isSingleRow && isSingleCol) {
            Hop input = ihop0.getInput().get(0);
            // find candidate set
            // dependence on common subexpression elimination to find equal input / row expression
            ArrayList<Hop> ihops = new ArrayList<Hop>();
            ihops.add(ihop0);
            for (Hop c : input.getParent()) {
                if (c != ihop0 && c instanceof IndexingOp && c.getInput().get(0) == input && ((IndexingOp) c).isRowLowerEqualsUpper() && c.getInput().get(1) == ihop0.getInput().get(1)) {
                    ihops.add(c);
                }
            }
            // apply rewrite if found candidates
            if (ihops.size() > 1) {
                // new row indexing operator
                IndexingOp newRix = new IndexingOp("tmp", input.getDataType(), input.getValueType(), input, ihop0.getInput().get(1), ihop0.getInput().get(1), new LiteralOp(1), HopRewriteUtils.createValueHop(input, false), true, false);
                HopRewriteUtils.setOutputParameters(newRix, -1, -1, input.getRowsInBlock(), input.getColsInBlock(), -1);
                newRix.refreshSizeInformation();
                // rewire current operator and all candidates
                for (Hop c : ihops) {
                    // input data
                    HopRewriteUtils.removeChildReference(c, input);
                    HopRewriteUtils.addChildReference(c, newRix, 0);
                    // row lower expr
                    HopRewriteUtils.removeChildReferenceByPos(c, c.getInput().get(1), 1);
                    HopRewriteUtils.addChildReference(c, new LiteralOp(1), 1);
                    // row upper expr
                    HopRewriteUtils.removeChildReferenceByPos(c, c.getInput().get(2), 2);
                    HopRewriteUtils.addChildReference(c, new LiteralOp(1), 2);
                    c.refreshSizeInformation();
                }
                appliedRow = true;
                LOG.debug("Applied vectorizeRightIndexingRow");
            }
        }
        // search for multiple indexing in same col
        if (isSingleRow && isSingleCol && !appliedRow) {
            Hop input = ihop0.getInput().get(0);
            // find candidate set
            // dependence on common subexpression elimination to find equal input / row expression
            ArrayList<Hop> ihops = new ArrayList<Hop>();
            ihops.add(ihop0);
            for (Hop c : input.getParent()) {
                if (c != ihop0 && c instanceof IndexingOp && c.getInput().get(0) == input && ((IndexingOp) c).isColLowerEqualsUpper() && c.getInput().get(3) == ihop0.getInput().get(3)) {
                    ihops.add(c);
                }
            }
            // apply rewrite if found candidates
            if (ihops.size() > 1) {
                // new row indexing operator
                IndexingOp newRix = new IndexingOp("tmp", input.getDataType(), input.getValueType(), input, new LiteralOp(1), HopRewriteUtils.createValueHop(input, true), ihop0.getInput().get(3), ihop0.getInput().get(3), false, true);
                HopRewriteUtils.setOutputParameters(newRix, -1, -1, input.getRowsInBlock(), input.getColsInBlock(), -1);
                newRix.refreshSizeInformation();
                // rewire current operator and all candidates
                for (Hop c : ihops) {
                    // input data
                    HopRewriteUtils.removeChildReference(c, input);
                    HopRewriteUtils.addChildReference(c, newRix, 0);
                    // col lower expr
                    HopRewriteUtils.replaceChildReference(c, c.getInput().get(3), new LiteralOp(1), 3);
                    // col upper expr
                    HopRewriteUtils.replaceChildReference(c, c.getInput().get(4), new LiteralOp(1), 4);
                    c.refreshSizeInformation();
                }
                LOG.debug("Applied vectorizeRightIndexingCol");
            }
        }
    }
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 50 with LiteralOp

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

the class RewriteSplitDagUnknownCSVRead method rewriteStatementBlock.

@Override
public List<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus state) {
    // DAG splits not required for forced single node
    if (DMLScript.rtplatform == RUNTIME_PLATFORM.SINGLE_NODE || !HopRewriteUtils.isLastLevelStatementBlock(sb))
        return Arrays.asList(sb);
    ArrayList<StatementBlock> ret = new ArrayList<>();
    // collect all unknown csv reads hops
    ArrayList<Hop> cand = new ArrayList<>();
    collectCSVReadHopsUnknownSize(sb.getHops(), cand);
    // split hop dag on demand
    if (!cand.isEmpty()) {
        try {
            // duplicate sb incl live variable sets
            StatementBlock sb1 = new StatementBlock();
            sb1.setDMLProg(sb.getDMLProg());
            sb1.setParseInfo(sb);
            sb1.setLiveIn(new VariableSet());
            sb1.setLiveOut(new VariableSet());
            // move csv reads incl reblock to new statement block
            // (and replace original persistent read with transient read)
            ArrayList<Hop> sb1hops = new ArrayList<>();
            for (Hop reblock : cand) {
                long rlen = reblock.getDim1();
                long clen = reblock.getDim2();
                long nnz = reblock.getNnz();
                UpdateType update = reblock.getUpdateType();
                int brlen = reblock.getRowsInBlock();
                int bclen = reblock.getColsInBlock();
                // (otherwise, for instance, literal ops are shared across dags)
                for (int i = 0; i < reblock.getInput().size(); i++) if (reblock.getInput().get(i) instanceof LiteralOp)
                    HopRewriteUtils.replaceChildReference(reblock, reblock.getInput().get(i), new LiteralOp((LiteralOp) reblock.getInput().get(i)));
                // create new transient read
                DataOp tread = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), DataOpTypes.TRANSIENTREAD, null, rlen, clen, nnz, update, brlen, bclen);
                HopRewriteUtils.copyLineNumbers(reblock, tread);
                // replace reblock with transient read
                ArrayList<Hop> parents = new ArrayList<>(reblock.getParent());
                for (int i = 0; i < parents.size(); i++) {
                    Hop parent = parents.get(i);
                    HopRewriteUtils.replaceChildReference(parent, reblock, tread);
                }
                // add reblock sub dag to first statement block
                DataOp twrite = new DataOp(reblock.getName(), reblock.getDataType(), reblock.getValueType(), reblock, DataOpTypes.TRANSIENTWRITE, null);
                twrite.setOutputParams(rlen, clen, nnz, update, brlen, bclen);
                HopRewriteUtils.copyLineNumbers(reblock, twrite);
                sb1hops.add(twrite);
                // update live in and out of new statement block (for piggybacking)
                DataIdentifier diVar = sb.variablesRead().getVariable(reblock.getName());
                if (diVar != null) {
                    // var read should always exist because persistent read
                    sb1.liveOut().addVariable(reblock.getName(), new DataIdentifier(diVar));
                    sb.liveIn().addVariable(reblock.getName(), new DataIdentifier(diVar));
                }
            }
            sb1.setHops(sb1hops);
            sb1.updateRecompilationFlag();
            // statement block with csv reblocks
            ret.add(sb1);
            // statement block with remaining hops
            ret.add(sb);
            // avoid later merge by other rewrites
            sb.setSplitDag(true);
        } catch (Exception ex) {
            throw new HopsException("Failed to split hops dag for csv read with unknown size.", ex);
        }
        LOG.debug("Applied splitDagUnknownCSVRead.");
    } else // keep original hop dag
    {
        ret.add(sb);
    }
    return ret;
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) HopsException(org.apache.sysml.hops.HopsException) UpdateType(org.apache.sysml.runtime.controlprogram.caching.MatrixObject.UpdateType) HopsException(org.apache.sysml.hops.HopsException) VariableSet(org.apache.sysml.parser.VariableSet) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) StatementBlock(org.apache.sysml.parser.StatementBlock)

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