Search in sources :

Example 6 with LeftIndexingOp

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

the class RewriteAlgebraicSimplificationDynamic method fuseLeftIndexingChainToAppend.

private static Hop fuseLeftIndexingChainToAppend(Hop parent, Hop hi, int pos) {
    boolean applied = false;
    // pattern1: X[,1]=A; X[,2]=B -> X=cbind(A,B); matrix / frame
    if (// first lix
    hi instanceof LeftIndexingOp && HopRewriteUtils.isFullColumnIndexing((LeftIndexingOp) hi) && // second lix
    hi.getInput().get(0) instanceof LeftIndexingOp && HopRewriteUtils.isFullColumnIndexing((LeftIndexingOp) hi.getInput().get(0)) && // first lix is single consumer
    hi.getInput().get(0).getParent().size() == 1 && // two column matrix
    hi.getInput().get(0).getInput().get(0).getDim2() == 2) {
        // rhs matrix
        Hop input2 = hi.getInput().get(1);
        // cl=cu
        Hop pred2 = hi.getInput().get(4);
        // lhs matrix
        Hop input1 = hi.getInput().get(0).getInput().get(1);
        // cl=cu
        Hop pred1 = hi.getInput().get(0).getInput().get(4);
        if (pred1 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred1) == 1 && pred2 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred2) == 2 && input1.getDataType() != DataType.SCALAR && input2.getDataType() != DataType.SCALAR) {
            // create new cbind operation and rewrite inputs
            BinaryOp bop = HopRewriteUtils.createBinary(input1, input2, OpOp2.CBIND);
            HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
            hi = bop;
            applied = true;
        }
    }
    // pattern1: X[1,]=A; X[2,]=B -> X=rbind(A,B)
    if (// first lix
    !applied && hi instanceof LeftIndexingOp && HopRewriteUtils.isFullRowIndexing((LeftIndexingOp) hi) && // second lix
    hi.getInput().get(0) instanceof LeftIndexingOp && HopRewriteUtils.isFullRowIndexing((LeftIndexingOp) hi.getInput().get(0)) && // first lix is single consumer
    hi.getInput().get(0).getParent().size() == 1 && // two column matrix
    hi.getInput().get(0).getInput().get(0).getDim1() == 2) {
        // rhs matrix
        Hop input2 = hi.getInput().get(1);
        // rl=ru
        Hop pred2 = hi.getInput().get(2);
        // lhs matrix
        Hop input1 = hi.getInput().get(0).getInput().get(1);
        // rl=ru
        Hop pred1 = hi.getInput().get(0).getInput().get(2);
        if (pred1 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred1) == 1 && pred2 instanceof LiteralOp && HopRewriteUtils.getDoubleValueSafe((LiteralOp) pred2) == 2 && input1.getDataType() != DataType.SCALAR && input2.getDataType() != DataType.SCALAR) {
            // create new cbind operation and rewrite inputs
            BinaryOp bop = HopRewriteUtils.createBinary(input1, input2, OpOp2.RBIND);
            HopRewriteUtils.replaceChildReference(parent, hi, bop, pos);
            hi = bop;
            applied = true;
            LOG.debug("Applied fuseLeftIndexingChainToAppend2 (line " + hi.getBeginLine() + ")");
        }
    }
    return hi;
}
Also used : Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 7 with LeftIndexingOp

use of org.apache.sysml.hops.LeftIndexingOp 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 8 with LeftIndexingOp

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

the class RewriteIndexingVectorization method isConsecutiveLeftRightIndexing.

private static boolean isConsecutiveLeftRightIndexing(LeftIndexingOp lix, IndexingOp rix, Hop input) {
    if (!(input instanceof LeftIndexingOp && input.getInput().get(1) instanceof IndexingOp))
        return false;
    boolean row = lix.isRowLowerEqualsUpper();
    LeftIndexingOp lix2 = (LeftIndexingOp) input;
    IndexingOp rix2 = (IndexingOp) input.getInput().get(1);
    // check row/column access with full row/column indexing
    boolean access = (row ? HopRewriteUtils.isFullRowIndexing(lix2) && HopRewriteUtils.isFullRowIndexing(rix2) : HopRewriteUtils.isFullColumnIndexing(lix2) && HopRewriteUtils.isFullColumnIndexing(rix2));
    // check equivalent right indexing inputs
    boolean rixInputs = (rix.getInput().get(0) == rix2.getInput().get(0));
    // check consecutive access
    boolean consecutive = (row ? HopRewriteUtils.isConsecutiveIndex(lix2.getInput().get(2), lix.getInput().get(2)) && HopRewriteUtils.isConsecutiveIndex(rix2.getInput().get(1), rix.getInput().get(1)) : HopRewriteUtils.isConsecutiveIndex(lix2.getInput().get(4), lix.getInput().get(4)) && HopRewriteUtils.isConsecutiveIndex(rix2.getInput().get(3), rix.getInput().get(3)));
    return access && rixInputs && consecutive;
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp)

Example 9 with LeftIndexingOp

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

the class DMLTranslator method processLeftIndexedExpression.

private Hop processLeftIndexedExpression(Expression source, IndexedIdentifier target, HashMap<String, Hop> hops) {
    // process target indexed expressions
    Hop rowLowerHops = null, rowUpperHops = null, colLowerHops = null, colUpperHops = null;
    if (target.getRowLowerBound() != null)
        rowLowerHops = processExpression(target.getRowLowerBound(), null, hops);
    else
        rowLowerHops = new LiteralOp(1);
    if (target.getRowUpperBound() != null)
        rowUpperHops = processExpression(target.getRowUpperBound(), null, hops);
    else {
        if (target.getDim1() != -1)
            rowUpperHops = new LiteralOp(target.getOrigDim1());
        else {
            rowUpperHops = new UnaryOp(target.getName(), DataType.SCALAR, ValueType.INT, Hop.OpOp1.NROW, hops.get(target.getName()));
            rowUpperHops.setParseInfo(target);
        }
    }
    if (target.getColLowerBound() != null)
        colLowerHops = processExpression(target.getColLowerBound(), null, hops);
    else
        colLowerHops = new LiteralOp(1);
    if (target.getColUpperBound() != null)
        colUpperHops = processExpression(target.getColUpperBound(), null, hops);
    else {
        if (target.getDim2() != -1)
            colUpperHops = new LiteralOp(target.getOrigDim2());
        else
            colUpperHops = new UnaryOp(target.getName(), DataType.SCALAR, ValueType.INT, Hop.OpOp1.NCOL, hops.get(target.getName()));
    }
    // process the source expression to get source Hops
    Hop sourceOp = processExpression(source, target, hops);
    // process the target to get targetHops
    Hop targetOp = hops.get(target.getName());
    if (targetOp == null) {
        LOG.error(target.printErrorLocation() + " must define matrix " + target.getName() + " before indexing operations are allowed ");
        throw new ParseException(target.printErrorLocation() + " must define matrix " + target.getName() + " before indexing operations are allowed ");
    }
    if (sourceOp.getDataType().isMatrix() && source.getOutput().getDataType().isScalar())
        sourceOp.setDataType(DataType.SCALAR);
    Hop leftIndexOp = new LeftIndexingOp(target.getName(), target.getDataType(), ValueType.DOUBLE, targetOp, sourceOp, rowLowerHops, rowUpperHops, colLowerHops, colUpperHops, target.getRowLowerEqualsUpper(), target.getColLowerEqualsUpper());
    setIdentifierParams(leftIndexOp, target);
    leftIndexOp.setParseInfo(target);
    leftIndexOp.setDim1(target.getOrigDim1());
    leftIndexOp.setDim2(target.getOrigDim2());
    return leftIndexOp;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) Hop(org.apache.sysml.hops.Hop) LiteralOp(org.apache.sysml.hops.LiteralOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp)

Example 10 with LeftIndexingOp

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

the class RewriteForLoopVectorization method vectorizeElementwiseUnary.

private static StatementBlock vectorizeElementwiseUnary(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 UnaryOp && lixrhs.getInput().get(0) instanceof IndexingOp && lixrhs.getInput().get(0).getInput().get(0) instanceof DataOp) {
                boolean[] tmp = checkLeftAndRightIndexing(lix, (IndexingOp) lixrhs.getInput().get(0), itervar);
                apply = tmp[0];
                rowIx = tmp[1];
            }
        }
    }
    // apply rewrite if possible
    if (apply) {
        Hop root = csb.getHops().get(0);
        LeftIndexingOp lix = (LeftIndexingOp) root.getInput().get(0);
        UnaryOp uop = (UnaryOp) lix.getInput().get(1);
        IndexingOp rix = (IndexingOp) uop.getInput().get(0);
        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 right indexing
        HopRewriteUtils.replaceChildReference(rix, rix.getInput().get(index1 - 1), from, index1 - 1);
        HopRewriteUtils.replaceChildReference(rix, rix.getInput().get(index2 - 1), to, index2 - 1);
        updateLeftAndRightIndexingSizes(rowIx, lix, rix);
        uop.refreshSizeInformation();
        // after uop update
        lix.refreshSizeInformation();
        ret = csb;
        LOG.debug("Applied vectorizeElementwiseUnaryForLoop.");
    }
    return ret;
}
Also used : AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) 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)

Aggregations

LeftIndexingOp (org.apache.sysml.hops.LeftIndexingOp)16 Hop (org.apache.sysml.hops.Hop)10 LiteralOp (org.apache.sysml.hops.LiteralOp)7 IndexingOp (org.apache.sysml.hops.IndexingOp)6 DataOp (org.apache.sysml.hops.DataOp)4 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)4 ArrayList (java.util.ArrayList)3 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)3 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)3 StatementBlock (org.apache.sysml.parser.StatementBlock)3 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)3 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)2 BinaryOp (org.apache.sysml.hops.BinaryOp)2 UnaryOp (org.apache.sysml.hops.UnaryOp)2 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)1 FunctionOp (org.apache.sysml.hops.FunctionOp)1 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)1 ReorgOp (org.apache.sysml.hops.ReorgOp)1