Search in sources :

Example 1 with IndexingOp

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

the class HopRewriteUtils method createScalarIndexing.

public static Hop createScalarIndexing(Hop input, long rix, long cix) {
    LiteralOp row = new LiteralOp(rix);
    LiteralOp col = new LiteralOp(cix);
    IndexingOp ix = new IndexingOp("tmp", DataType.MATRIX, ValueType.DOUBLE, input, row, row, col, col, true, true);
    ix.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
    copyLineNumbers(input, ix);
    ix.refreshSizeInformation();
    return createUnary(ix, OpOp1.CAST_AS_SCALAR);
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp) LiteralOp(org.apache.sysml.hops.LiteralOp)

Example 2 with IndexingOp

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

the class CPlanMemoTable method add.

public void add(Hop hop, TemplateType type, long in1, long in2, long in3) {
    int size = (hop instanceof IndexingOp) ? 1 : hop.getInput().size();
    add(hop, new MemoTableEntry(type, in1, in2, in3, size));
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) InterestingPoint(org.apache.sysml.hops.codegen.opt.InterestingPoint)

Example 3 with IndexingOp

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

the class TemplateCell method rConstructCplan.

protected void rConstructCplan(Hop hop, CPlanMemoTable memo, HashMap<Long, CNode> tmp, HashSet<Hop> inHops, boolean compileLiterals) {
    // memoization for common subexpression elimination and to avoid redundant work
    if (tmp.containsKey(hop.getHopID()))
        return;
    MemoTableEntry me = memo.getBest(hop.getHopID(), TemplateType.CELL);
    // recursively process required childs
    if (me != null && me.type.isIn(TemplateType.ROW, TemplateType.OUTER)) {
        CNodeData cdata = TemplateUtils.createCNodeData(hop, compileLiterals);
        tmp.put(hop.getHopID(), cdata);
        inHops.add(hop);
        return;
    }
    for (int i = 0; i < hop.getInput().size(); i++) {
        Hop c = hop.getInput().get(i);
        if (me != null && me.isPlanRef(i) && !(c instanceof DataOp) && (me.type != TemplateType.MAGG || memo.contains(c.getHopID(), TemplateType.CELL)))
            rConstructCplan(c, memo, tmp, inHops, compileLiterals);
        else if (me != null && (me.type == TemplateType.MAGG || me.type == TemplateType.CELL) && HopRewriteUtils.isMatrixMultiply(hop) && // skip transpose
        i == 0)
            rConstructCplan(c.getInput().get(0), memo, tmp, inHops, compileLiterals);
        else {
            CNodeData cdata = TemplateUtils.createCNodeData(c, compileLiterals);
            tmp.put(c.getHopID(), cdata);
            inHops.add(c);
        }
    }
    // construct cnode for current hop
    CNode out = null;
    if (hop instanceof UnaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        String primitiveOpName = ((UnaryOp) hop).getOp().name();
        out = new CNodeUnary(cdata1, UnaryType.valueOf(primitiveOpName));
    } else if (hop instanceof BinaryOp) {
        BinaryOp bop = (BinaryOp) hop;
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        String primitiveOpName = bop.getOp().name();
        // add lookups if required
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        cdata2 = TemplateUtils.wrapLookupIfNecessary(cdata2, hop.getInput().get(1));
        // construct binary cnode
        out = new CNodeBinary(cdata1, cdata2, BinType.valueOf(primitiveOpName));
    } else if (hop instanceof TernaryOp) {
        TernaryOp top = (TernaryOp) hop;
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        CNode cdata3 = tmp.get(hop.getInput().get(2).getHopID());
        // add lookups if required
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        cdata2 = TemplateUtils.wrapLookupIfNecessary(cdata2, hop.getInput().get(1));
        cdata3 = TemplateUtils.wrapLookupIfNecessary(cdata3, hop.getInput().get(2));
        // construct ternary cnode, primitive operation derived from OpOp3
        out = new CNodeTernary(cdata1, cdata2, cdata3, TernaryType.valueOf(top.getOp().name()));
    } else if (hop instanceof ParameterizedBuiltinOp) {
        CNode cdata1 = tmp.get(((ParameterizedBuiltinOp) hop).getTargetHop().getHopID());
        cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
        CNode cdata2 = tmp.get(((ParameterizedBuiltinOp) hop).getParameterHop("pattern").getHopID());
        CNode cdata3 = tmp.get(((ParameterizedBuiltinOp) hop).getParameterHop("replacement").getHopID());
        TernaryType ttype = (cdata2.isLiteral() && cdata2.getVarname().equals("Double.NaN")) ? TernaryType.REPLACE_NAN : TernaryType.REPLACE;
        out = new CNodeTernary(cdata1, cdata2, cdata3, ttype);
    } else if (hop instanceof IndexingOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        out = new CNodeTernary(cdata1, TemplateUtils.createCNodeData(new LiteralOp(hop.getInput().get(0).getDim2()), true), TemplateUtils.createCNodeData(hop.getInput().get(4), true), TernaryType.LOOKUP_RC1);
    } else if (HopRewriteUtils.isTransposeOperation(hop)) {
        out = TemplateUtils.skipTranspose(tmp.get(hop.getHopID()), hop, tmp, compileLiterals);
        // correct indexing types of existing lookups
        if (!HopRewriteUtils.containsOp(hop.getParent(), AggBinaryOp.class))
            TemplateUtils.rFlipVectorLookups(out);
        // maintain input hops
        if (out instanceof CNodeData && !inHops.contains(hop.getInput().get(0)))
            inHops.add(hop.getInput().get(0));
    } else if (hop instanceof AggUnaryOp) {
        // aggregation handled in template implementation (note: we do not compile
        // ^2 of SUM_SQ into the operator to simplify the detection of single operators)
        out = tmp.get(hop.getInput().get(0).getHopID());
    } else if (hop instanceof AggBinaryOp) {
        // (1) t(X)%*%X -> sum(X^2) and t(X) %*% Y -> sum(X*Y)
        if (HopRewriteUtils.isTransposeOfItself(hop.getInput().get(0), hop.getInput().get(1))) {
            CNode cdata1 = tmp.get(hop.getInput().get(1).getHopID());
            if (TemplateUtils.isColVector(cdata1))
                cdata1 = new CNodeUnary(cdata1, UnaryType.LOOKUP_R);
            out = new CNodeUnary(cdata1, UnaryType.POW2);
        } else {
            CNode cdata1 = TemplateUtils.skipTranspose(tmp.get(hop.getInput().get(0).getHopID()), hop.getInput().get(0), tmp, compileLiterals);
            if (cdata1 instanceof CNodeData && !inHops.contains(hop.getInput().get(0).getInput().get(0)))
                inHops.add(hop.getInput().get(0).getInput().get(0));
            if (TemplateUtils.isColVector(cdata1))
                cdata1 = new CNodeUnary(cdata1, UnaryType.LOOKUP_R);
            CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
            if (TemplateUtils.isColVector(cdata2))
                cdata2 = new CNodeUnary(cdata2, UnaryType.LOOKUP_R);
            out = new CNodeBinary(cdata1, cdata2, BinType.MULT);
        }
    }
    tmp.put(hop.getHopID(), out);
}
Also used : TernaryType(org.apache.sysml.hops.codegen.cplan.CNodeTernary.TernaryType) CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) UnaryOp(org.apache.sysml.hops.UnaryOp) CNodeTernary(org.apache.sysml.hops.codegen.cplan.CNodeTernary) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop) CNodeBinary(org.apache.sysml.hops.codegen.cplan.CNodeBinary) TernaryOp(org.apache.sysml.hops.TernaryOp) CNode(org.apache.sysml.hops.codegen.cplan.CNode) ParameterizedBuiltinOp(org.apache.sysml.hops.ParameterizedBuiltinOp) CNodeUnary(org.apache.sysml.hops.codegen.cplan.CNodeUnary) IndexingOp(org.apache.sysml.hops.IndexingOp) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) MemoTableEntry(org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 4 with IndexingOp

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

the class TemplateUtils method getRowType.

public static RowType getRowType(Hop output, Hop... inputs) {
    Hop X = inputs[0];
    Hop B1 = (inputs.length > 1) ? inputs[1] : null;
    if ((X != null && HopRewriteUtils.isEqualSize(output, X)) || X == null || !X.dimsKnown())
        return RowType.NO_AGG;
    else if (((B1 != null && output.getDim1() == X.getDim1() && output.getDim2() == B1.getDim2()) || (output instanceof IndexingOp && HopRewriteUtils.isColumnRangeIndexing((IndexingOp) output))) && !(output instanceof AggBinaryOp && HopRewriteUtils.isTransposeOfItself(output.getInput().get(0), X)))
        return RowType.NO_AGG_B1;
    else if (output.getDim1() == X.getDim1() && (output.getDim2() == 1) && !(output instanceof AggBinaryOp && HopRewriteUtils.isTransposeOfItself(output.getInput().get(0), X)))
        return RowType.ROW_AGG;
    else if (output instanceof AggUnaryOp && ((AggUnaryOp) output).getDirection() == Direction.RowCol)
        return RowType.FULL_AGG;
    else if (output.getDim1() == X.getDim2() && output.getDim2() == 1)
        return RowType.COL_AGG_T;
    else if (output.getDim1() == 1 && output.getDim2() == X.getDim2())
        return RowType.COL_AGG;
    else if (B1 != null && output.getDim1() == X.getDim2() && output.getDim2() == B1.getDim2())
        return RowType.COL_AGG_B1_T;
    else if (B1 != null && output.getDim1() == B1.getDim2() && output.getDim2() == X.getDim2())
        return RowType.COL_AGG_B1;
    else if (B1 != null && output.getDim1() == 1 && B1.getDim2() == output.getDim2())
        return RowType.COL_AGG_B1R;
    else if (X.getDim1() == output.getDim1() && X.getDim2() != output.getDim2())
        return RowType.NO_AGG_CONST;
    else if (output.getDim1() == 1 && X.getDim2() != output.getDim2())
        return RowType.COL_AGG_CONST;
    else
        throw new RuntimeException("Unknown row type for hop " + output.getHopID() + ".");
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) Hop(org.apache.sysml.hops.Hop)

Example 5 with IndexingOp

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

the class HopRewriteUtils method createIndexingOp.

public static IndexingOp createIndexingOp(Hop input, Hop rl, Hop ru, Hop cl, Hop cu) {
    IndexingOp ix = new IndexingOp("tmp", DataType.MATRIX, ValueType.DOUBLE, input, rl, ru, cl, cu, rl == ru, cl == cu);
    ix.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
    copyLineNumbers(input, ix);
    ix.refreshSizeInformation();
    return ix;
}
Also used : IndexingOp(org.apache.sysml.hops.IndexingOp) LeftIndexingOp(org.apache.sysml.hops.LeftIndexingOp)

Aggregations

IndexingOp (org.apache.sysml.hops.IndexingOp)42 Hop (org.apache.sysml.hops.Hop)35 LiteralOp (org.apache.sysml.hops.LiteralOp)32 LeftIndexingOp (org.apache.sysml.hops.LeftIndexingOp)23 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)19 DataOp (org.apache.sysml.hops.DataOp)16 UnaryOp (org.apache.sysml.hops.UnaryOp)15 BinaryOp (org.apache.sysml.hops.BinaryOp)11 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)9 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)8 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)8 StatementBlock (org.apache.sysml.parser.StatementBlock)8 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)8 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)7 TernaryOp (org.apache.sysml.hops.TernaryOp)7 ArrayList (java.util.ArrayList)6 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)6 ReorgOp (org.apache.sysml.hops.ReorgOp)5 CNode (org.apache.sysml.hops.codegen.cplan.CNode)4 CNodeBinary (org.apache.sysml.hops.codegen.cplan.CNodeBinary)4