Search in sources :

Example 6 with CNode

use of org.apache.sysml.hops.codegen.cplan.CNode in project incubator-systemml by apache.

the class SpoofCompiler method cleanupCPlans.

/**
	 * Cleanup generated cplans in order to remove unnecessary inputs created
	 * during incremental construction. This is important as it avoids unnecessary 
	 * redundant computation. 
	 * 
	 * @param cplans set of cplans
	 */
private static HashMap<Long, Pair<Hop[], CNodeTpl>> cleanupCPlans(HashMap<Long, Pair<Hop[], CNodeTpl>> cplans) {
    HashMap<Long, Pair<Hop[], CNodeTpl>> cplans2 = new HashMap<Long, Pair<Hop[], CNodeTpl>>();
    for (Entry<Long, Pair<Hop[], CNodeTpl>> e : cplans.entrySet()) {
        CNodeTpl tpl = e.getValue().getValue();
        Hop[] inHops = e.getValue().getKey();
        //collect cplan leaf node names
        HashSet<Long> leafs = new HashSet<Long>();
        if (tpl instanceof CNodeMultiAgg)
            for (CNode out : ((CNodeMultiAgg) tpl).getOutputs()) rCollectLeafIDs(out, leafs);
        else
            rCollectLeafIDs(tpl.getOutput(), leafs);
        //create clean cplan w/ minimal inputs
        if (inHops.length == leafs.size())
            cplans2.put(e.getKey(), e.getValue());
        else {
            tpl.cleanupInputs(leafs);
            ArrayList<Hop> tmp = new ArrayList<Hop>();
            for (Hop hop : inHops) {
                if (hop != null && leafs.contains(hop.getHopID()))
                    tmp.add(hop);
            }
            cplans2.put(e.getKey(), new Pair<Hop[], CNodeTpl>(tmp.toArray(new Hop[0]), tpl));
        }
        //remove invalid plans with column indexing on main input
        if (tpl instanceof CNodeCell) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            if (rHasLookupRC1(tpl.getOutput(), in1) || isLookupRC1(tpl.getOutput(), in1)) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed cplan due to invalid rc1 indexing on main input.");
            }
        } else if (tpl instanceof CNodeMultiAgg) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            for (CNode output : ((CNodeMultiAgg) tpl).getOutputs()) if (rHasLookupRC1(output, in1) || isLookupRC1(output, in1)) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed cplan due to invalid rc1 indexing on main input.");
            }
        }
        //remove spurious lookups on main input of cell template
        if (tpl instanceof CNodeCell || tpl instanceof CNodeOuterProduct) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            rFindAndRemoveLookup(tpl.getOutput(), in1);
        } else if (tpl instanceof CNodeMultiAgg) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            rFindAndRemoveLookupMultiAgg((CNodeMultiAgg) tpl, in1);
        }
        //remove cplan w/ single op and w/o agg
        if ((tpl instanceof CNodeCell && ((((CNodeCell) tpl).getCellType() == CellType.NO_AGG && TemplateUtils.hasSingleOperation(tpl)) || TemplateUtils.hasNoOperation(tpl))) || tpl instanceof CNodeRow && TemplateUtils.hasSingleOperation(tpl))
            cplans2.remove(e.getKey());
        //remove cplan if empty
        if (tpl.getOutput() instanceof CNodeData)
            cplans2.remove(e.getKey());
    }
    return cplans2;
}
Also used : CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) CNodeTpl(org.apache.sysml.hops.codegen.cplan.CNodeTpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) CNodeCell(org.apache.sysml.hops.codegen.cplan.CNodeCell) CNode(org.apache.sysml.hops.codegen.cplan.CNode) CNodeOuterProduct(org.apache.sysml.hops.codegen.cplan.CNodeOuterProduct) CNodeRow(org.apache.sysml.hops.codegen.cplan.CNodeRow) CNodeMultiAgg(org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg) Pair(org.apache.sysml.runtime.matrix.data.Pair) HashSet(java.util.HashSet)

Example 7 with CNode

use of org.apache.sysml.hops.codegen.cplan.CNode in project incubator-systemml by apache.

the class Explain method explainCNode.

//////////////
// internal explain CNODE
private static String explainCNode(CNode cnode, int level) throws DMLRuntimeException {
    if (cnode.isVisited())
        return "";
    StringBuilder sb = new StringBuilder();
    String offset = createOffset(level);
    for (CNode input : cnode.getInput()) sb.append(explainCNode(input, level));
    //indentation
    sb.append(offset);
    //hop id
    if (SHOW_DATA_DEPENDENCIES)
        sb.append("(" + cnode.getID() + ") ");
    //operation string
    sb.append(cnode.toString());
    //input hop references 
    if (SHOW_DATA_DEPENDENCIES) {
        StringBuilder childs = new StringBuilder();
        childs.append(" (");
        boolean childAdded = false;
        for (CNode input : cnode.getInput()) {
            childs.append(childAdded ? "," : "");
            childs.append(input.getID());
            childAdded = true;
        }
        childs.append(")");
        if (childAdded)
            sb.append(childs.toString());
    }
    sb.append('\n');
    cnode.setVisited();
    return sb.toString();
}
Also used : CNode(org.apache.sysml.hops.codegen.cplan.CNode)

Example 8 with CNode

use of org.apache.sysml.hops.codegen.cplan.CNode in project incubator-systemml by apache.

the class TemplateMultiAgg method constructCplan.

public Pair<Hop[], CNodeTpl> constructCplan(Hop hop, CPlanMemoTable memo, boolean compileLiterals) {
    //get all root nodes for multi aggregation
    MemoTableEntry multiAgg = memo.getBest(hop.getHopID(), TemplateType.MultiAggTpl);
    ArrayList<Hop> roots = new ArrayList<Hop>();
    for (int i = 0; i < 3; i++) if (multiAgg.isPlanRef(i))
        roots.add(memo._hopRefs.get(multiAgg.input(i)));
    Hop.resetVisitStatus(roots);
    //recursively process required cplan outputs
    HashSet<Hop> inHops = new HashSet<Hop>();
    HashMap<Long, CNode> tmp = new HashMap<Long, CNode>();
    for (//use celltpl cplan construction
    Hop root : //use celltpl cplan construction
    roots) super.rConstructCplan(root, memo, tmp, inHops, compileLiterals);
    Hop.resetVisitStatus(roots);
    //reorder inputs (ensure matrices/vectors come first) and prune literals
    //note: we order by number of cells and subsequently sparsity to ensure
    //that sparse inputs are used as the main input w/o unnecessary conversion
    List<Hop> sinHops = inHops.stream().filter(h -> !(h.getDataType().isScalar() && tmp.get(h.getHopID()).isLiteral())).sorted(new HopInputComparator()).collect(Collectors.toList());
    //construct template node
    ArrayList<CNode> inputs = new ArrayList<CNode>();
    for (Hop in : sinHops) inputs.add(tmp.get(in.getHopID()));
    ArrayList<CNode> outputs = new ArrayList<CNode>();
    ArrayList<AggOp> aggOps = new ArrayList<AggOp>();
    for (Hop root : roots) {
        CNode node = tmp.get(root.getHopID());
        if (//add indexing ops for sideways data inputs
        node instanceof CNodeData && ((CNodeData) inputs.get(0)).getHopID() != ((CNodeData) node).getHopID())
            node = new CNodeUnary(node, (roots.get(0).getDim2() == 1) ? UnaryType.LOOKUP_R : UnaryType.LOOKUP_RC);
        outputs.add(node);
        aggOps.add(TemplateUtils.getAggOp(root));
    }
    CNodeMultiAgg tpl = new CNodeMultiAgg(inputs, outputs);
    tpl.setAggOps(aggOps);
    tpl.setRootNodes(roots);
    // return cplan instance
    return new Pair<Hop[], CNodeTpl>(sinHops.toArray(new Hop[0]), tpl);
}
Also used : CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) HashMap(java.util.HashMap) AggOp(org.apache.sysml.hops.Hop.AggOp) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) CNode(org.apache.sysml.hops.codegen.cplan.CNode) CNodeUnary(org.apache.sysml.hops.codegen.cplan.CNodeUnary) MemoTableEntry(org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry) CNodeMultiAgg(org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg) HashSet(java.util.HashSet) Pair(org.apache.sysml.runtime.matrix.data.Pair)

Example 9 with CNode

use of org.apache.sysml.hops.codegen.cplan.CNode in project incubator-systemml by apache.

the class TemplateRow method rConstructCplan.

private void rConstructCplan(Hop hop, CPlanMemoTable memo, HashMap<Long, CNode> tmp, HashSet<Hop> inHops, HashMap<String, Hop> inHops2, boolean compileLiterals) {
    //memoization for common subexpression elimination and to avoid redundant work 
    if (tmp.containsKey(hop.getHopID()))
        return;
    //recursively process required childs
    MemoTableEntry me = memo.getBest(hop.getHopID(), TemplateType.RowTpl);
    for (int i = 0; i < hop.getInput().size(); i++) {
        Hop c = hop.getInput().get(i);
        if (me.isPlanRef(i))
            rConstructCplan(c, memo, tmp, inHops, inHops2, 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 AggUnaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        if (((AggUnaryOp) hop).getDirection() == Direction.Row && HopRewriteUtils.isAggUnaryOp(hop, SUPPORTED_ROW_AGG)) {
            if (hop.getInput().get(0).getDim2() == 1)
                out = (cdata1.getDataType() == DataType.SCALAR) ? cdata1 : new CNodeUnary(cdata1, UnaryType.LOOKUP_R);
            else {
                String opcode = "ROW_" + ((AggUnaryOp) hop).getOp().name().toUpperCase() + "S";
                out = new CNodeUnary(cdata1, UnaryType.valueOf(opcode));
                inHops2.put("X", hop.getInput().get(0));
            }
        } else if (((AggUnaryOp) hop).getDirection() == Direction.Col && ((AggUnaryOp) hop).getOp() == AggOp.SUM) {
            //vector add without temporary copy
            if (cdata1 instanceof CNodeBinary && ((CNodeBinary) cdata1).getType().isVectorScalarPrimitive())
                out = new CNodeBinary(cdata1.getInput().get(0), cdata1.getInput().get(1), ((CNodeBinary) cdata1).getType().getVectorAddPrimitive());
            else
                out = cdata1;
        }
    } else if (hop instanceof AggBinaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        if (HopRewriteUtils.isTransposeOperation(hop.getInput().get(0))) {
            //correct input under transpose
            cdata1 = TemplateUtils.skipTranspose(cdata1, hop.getInput().get(0), tmp, compileLiterals);
            inHops.remove(hop.getInput().get(0));
            inHops.add(hop.getInput().get(0).getInput().get(0));
            //note: vectorMultAdd applicable to vector-scalar, and vector-vector
            out = new CNodeBinary(cdata1, cdata2, BinType.VECT_MULT_ADD);
            inHops2.put("X", hop.getInput().get(0).getInput().get(0));
        } else {
            if (hop.getInput().get(0).getDim2() == 1 && hop.getInput().get(1).getDim2() == 1)
                out = new CNodeBinary((cdata1.getDataType() == DataType.SCALAR) ? cdata1 : new CNodeUnary(cdata1, UnaryType.LOOKUP0), (cdata2.getDataType() == DataType.SCALAR) ? cdata2 : new CNodeUnary(cdata2, UnaryType.LOOKUP0), BinType.MULT);
            else {
                out = new CNodeBinary(cdata1, cdata2, BinType.DOT_PRODUCT);
                inHops2.put("X", hop.getInput().get(0));
            }
        }
    } else if (hop instanceof UnaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        // if one input is a matrix then we need to do vector by scalar operations
        if (hop.getInput().get(0).getDim1() > 1 && hop.getInput().get(0).getDim2() > 1) {
            if (HopRewriteUtils.isUnary(hop, SUPPORTED_VECT_UNARY)) {
                String opname = "VECT_" + ((UnaryOp) hop).getOp().name();
                out = new CNodeUnary(cdata1, UnaryType.valueOf(opname));
            } else
                throw new RuntimeException("Unsupported unary matrix " + "operation: " + ((UnaryOp) hop).getOp().name());
        } else //general scalar case
        {
            cdata1 = TemplateUtils.wrapLookupIfNecessary(cdata1, hop.getInput().get(0));
            String primitiveOpName = ((UnaryOp) hop).getOp().toString();
            out = new CNodeUnary(cdata1, UnaryType.valueOf(primitiveOpName));
        }
    } else if (hop instanceof BinaryOp) {
        CNode cdata1 = tmp.get(hop.getInput().get(0).getHopID());
        CNode cdata2 = tmp.get(hop.getInput().get(1).getHopID());
        // if one input is a matrix then we need to do vector by scalar operations
        if (hop.getInput().get(0).getDim1() > 1 && hop.getInput().get(0).getDim2() > 1) {
            if (HopRewriteUtils.isBinary(hop, SUPPORTED_VECT_BINARY)) {
                if (TemplateUtils.isMatrix(cdata1) && TemplateUtils.isMatrix(cdata2)) {
                    String opname = "VECT_" + ((BinaryOp) hop).getOp().name();
                    out = new CNodeBinary(cdata1, cdata2, BinType.valueOf(opname));
                } else {
                    String opname = "VECT_" + ((BinaryOp) hop).getOp().name() + "_SCALAR";
                    if (TemplateUtils.isColVector(cdata2))
                        cdata2 = new CNodeUnary(cdata2, UnaryType.LOOKUP_R);
                    out = new CNodeBinary(cdata1, cdata2, BinType.valueOf(opname));
                }
            } else
                throw new RuntimeException("Unsupported binary matrix " + "operation: " + ((BinaryOp) hop).getOp().name());
        } else //one input is a vector/scalar other is a scalar
        {
            String primitiveOpName = ((BinaryOp) hop).getOp().toString();
            if (TemplateUtils.isColVector(cdata1))
                cdata1 = new CNodeUnary(cdata1, UnaryType.LOOKUP_R);
            if (TemplateUtils.isColVector(cdata2))
                cdata2 = new CNodeUnary(cdata2, UnaryType.LOOKUP_R);
            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));
        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().toString()));
    } 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);
    }
    if (out == null) {
        throw new RuntimeException(hop.getHopID() + " " + hop.getOpString());
    }
    if (out.getDataType().isMatrix()) {
        out.setNumRows(hop.getDim1());
        out.setNumCols(hop.getDim2());
    }
    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) AggUnaryOp(org.apache.sysml.hops.AggUnaryOp) IndexingOp(org.apache.sysml.hops.IndexingOp) MemoTableEntry(org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry) LiteralOp(org.apache.sysml.hops.LiteralOp) AggBinaryOp(org.apache.sysml.hops.AggBinaryOp) BinaryOp(org.apache.sysml.hops.BinaryOp)

Example 10 with CNode

use of org.apache.sysml.hops.codegen.cplan.CNode in project incubator-systemml by apache.

the class TemplateRow method constructCplan.

@Override
public Pair<Hop[], CNodeTpl> constructCplan(Hop hop, CPlanMemoTable memo, boolean compileLiterals) {
    //recursively process required cplan output
    HashSet<Hop> inHops = new HashSet<Hop>();
    HashMap<String, Hop> inHops2 = new HashMap<String, Hop>();
    HashMap<Long, CNode> tmp = new HashMap<Long, CNode>();
    hop.resetVisitStatus();
    rConstructCplan(hop, memo, tmp, inHops, inHops2, compileLiterals);
    hop.resetVisitStatus();
    //reorder inputs (ensure matrix is first input, and other inputs ordered by size)
    List<Hop> sinHops = inHops.stream().filter(h -> !(h.getDataType().isScalar() && tmp.get(h.getHopID()).isLiteral())).sorted(new HopInputComparator(inHops2.get("X"))).collect(Collectors.toList());
    //construct template node
    ArrayList<CNode> inputs = new ArrayList<CNode>();
    for (Hop in : sinHops) inputs.add(tmp.get(in.getHopID()));
    CNode output = tmp.get(hop.getHopID());
    CNodeRow tpl = new CNodeRow(inputs, output);
    tpl.setRowType(TemplateUtils.getRowType(hop, sinHops.get(0)));
    tpl.setNumVectorIntermediates(TemplateUtils.countVectorIntermediates(output, new HashSet<Long>()));
    tpl.getOutput().resetVisitStatus();
    tpl.rReorderCommutativeBinaryOps(tpl.getOutput(), sinHops.get(0).getHopID());
    // return cplan instance
    return new Pair<Hop[], CNodeTpl>(sinHops.toArray(new Hop[0]), tpl);
}
Also used : HashMap(java.util.HashMap) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) CNode(org.apache.sysml.hops.codegen.cplan.CNode) CNodeRow(org.apache.sysml.hops.codegen.cplan.CNodeRow) HashSet(java.util.HashSet) Pair(org.apache.sysml.runtime.matrix.data.Pair)

Aggregations

CNode (org.apache.sysml.hops.codegen.cplan.CNode)11 Hop (org.apache.sysml.hops.Hop)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 CNodeData (org.apache.sysml.hops.codegen.cplan.CNodeData)5 CNodeUnary (org.apache.sysml.hops.codegen.cplan.CNodeUnary)5 Pair (org.apache.sysml.runtime.matrix.data.Pair)5 AggBinaryOp (org.apache.sysml.hops.AggBinaryOp)4 CNodeBinary (org.apache.sysml.hops.codegen.cplan.CNodeBinary)4 MemoTableEntry (org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry)4 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)3 BinaryOp (org.apache.sysml.hops.BinaryOp)3 UnaryOp (org.apache.sysml.hops.UnaryOp)3 IndexingOp (org.apache.sysml.hops.IndexingOp)2 LiteralOp (org.apache.sysml.hops.LiteralOp)2 ParameterizedBuiltinOp (org.apache.sysml.hops.ParameterizedBuiltinOp)2 TernaryOp (org.apache.sysml.hops.TernaryOp)2 CNodeCell (org.apache.sysml.hops.codegen.cplan.CNodeCell)2 CNodeMultiAgg (org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg)2