Search in sources :

Example 1 with CNodeMultiAgg

use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg 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 2 with CNodeMultiAgg

use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg 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 memo memoization table
 * @param cplans set of cplans
 */
private static HashMap<Long, Pair<Hop[], CNodeTpl>> cleanupCPlans(CPlanMemoTable memo, HashMap<Long, Pair<Hop[], CNodeTpl>> cplans) {
    HashMap<Long, Pair<Hop[], CNodeTpl>> cplans2 = new HashMap<>();
    CPlanOpRewriter rewriter = new CPlanOpRewriter();
    CPlanCSERewriter cse = new CPlanCSERewriter();
    for (Entry<Long, Pair<Hop[], CNodeTpl>> e : cplans.entrySet()) {
        CNodeTpl tpl = e.getValue().getValue();
        Hop[] inHops = e.getValue().getKey();
        // remove invalid plans with null inputs
        if (Arrays.stream(inHops).anyMatch(h -> (h == null)))
            continue;
        // perform simplifications and cse rewrites
        tpl = rewriter.simplifyCPlan(tpl);
        tpl = cse.eliminateCommonSubexpressions(tpl);
        // update input hops (order-preserving)
        HashSet<Long> inputHopIDs = tpl.getInputHopIDs(false);
        inHops = Arrays.stream(inHops).filter(p -> p != null && inputHopIDs.contains(p.getHopID())).toArray(Hop[]::new);
        cplans2.put(e.getKey(), new Pair<>(inHops, tpl));
        // remove invalid plans with column indexing on main input
        if (tpl instanceof CNodeCell || tpl instanceof CNodeRow) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            boolean inclRC1 = !(tpl instanceof CNodeRow);
            if (rHasLookupRC1(tpl.getOutput(), in1, inclRC1) || isLookupRC1(tpl.getOutput(), in1, inclRC1)) {
                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, true) || isLookupRC1(output, in1, true)) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed cplan due to invalid rc1 indexing on main input.");
            }
        }
        // remove invalid lookups on main input (all templates)
        CNodeData in1 = (CNodeData) tpl.getInput().get(0);
        if (tpl instanceof CNodeMultiAgg)
            rFindAndRemoveLookupMultiAgg((CNodeMultiAgg) tpl, in1);
        else
            rFindAndRemoveLookup(tpl.getOutput(), in1, !(tpl instanceof CNodeRow));
        // remove invalid row templates (e.g., unsatisfied blocksize constraint)
        if (tpl instanceof CNodeRow) {
            // check for invalid row cplan over column vector
            if (((CNodeRow) tpl).getRowType() == RowType.NO_AGG && tpl.getOutput().getDataType().isScalar()) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed invalid row cplan w/o agg on column vector.");
            } else if (OptimizerUtils.isSparkExecutionMode()) {
                Hop hop = memo.getHopRefs().get(e.getKey());
                boolean isSpark = DMLScript.rtplatform == RUNTIME_PLATFORM.SPARK || OptimizerUtils.getTotalMemEstimate(inHops, hop, true) > OptimizerUtils.getLocalMemBudget();
                boolean invalidNcol = hop.getDataType().isMatrix() && (HopRewriteUtils.isTransposeOperation(hop) ? hop.getDim1() > hop.getRowsInBlock() : hop.getDim2() > hop.getColsInBlock());
                for (Hop in : inHops) invalidNcol |= (in.getDataType().isMatrix() && in.getDim2() > in.getColsInBlock());
                if (isSpark && invalidNcol) {
                    cplans2.remove(e.getKey());
                    if (LOG.isTraceEnabled())
                        LOG.trace("Removed invalid row cplan w/ ncol>ncolpb.");
                }
            }
        }
        // remove cplan w/ single op and w/o agg
        if ((tpl instanceof CNodeCell && ((CNodeCell) tpl).getCellType() == CellType.NO_AGG && TemplateUtils.hasSingleOperation(tpl)) || (tpl instanceof CNodeRow && (((CNodeRow) tpl).getRowType() == RowType.NO_AGG || ((CNodeRow) tpl).getRowType() == RowType.NO_AGG_B1 || ((CNodeRow) tpl).getRowType() == RowType.ROW_AGG) && TemplateUtils.hasSingleOperation(tpl)) || TemplateUtils.hasNoOperation(tpl)) {
            cplans2.remove(e.getKey());
            if (LOG.isTraceEnabled())
                LOG.trace("Removed cplan with single operation.");
        }
        // remove cplan if empty
        if (tpl.getOutput() instanceof CNodeData) {
            cplans2.remove(e.getKey());
            if (LOG.isTraceEnabled())
                LOG.trace("Removed empty cplan.");
        }
        // rename inputs (for codegen and plan caching)
        tpl.renameInputs();
    }
    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) CPlanCSERewriter(org.apache.sysml.hops.codegen.template.CPlanCSERewriter) CNodeCell(org.apache.sysml.hops.codegen.cplan.CNodeCell) CNode(org.apache.sysml.hops.codegen.cplan.CNode) CNodeRow(org.apache.sysml.hops.codegen.cplan.CNodeRow) CNodeMultiAgg(org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg) CPlanOpRewriter(org.apache.sysml.hops.codegen.template.CPlanOpRewriter) Pair(org.apache.sysml.runtime.matrix.data.Pair)

Example 3 with CNodeMultiAgg

use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project systemml by apache.

the class TemplateMultiAgg method constructCplan.

@Override
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.MAGG);
    ArrayList<Hop> roots = new ArrayList<>();
    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<>();
    HashMap<Long, CNode> tmp = new HashMap<>();
    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
    Hop shared = getSparseSafeSharedInput(roots, inHops);
    Hop[] sinHops = inHops.stream().filter(h -> !(h.getDataType().isScalar() && tmp.get(h.getHopID()).isLiteral())).sorted(new HopInputComparator(shared)).toArray(Hop[]::new);
    // construct template node
    ArrayList<CNode> inputs = new ArrayList<>();
    for (Hop in : sinHops) inputs.add(tmp.get(in.getHopID()));
    ArrayList<CNode> outputs = new ArrayList<>();
    ArrayList<AggOp> aggOps = new ArrayList<>();
    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.setSparseSafe(isSparseSafe(roots, sinHops[0], tpl.getOutputs(), tpl.getAggOps(), true));
    tpl.setRootNodes(roots);
    tpl.setBeginLine(hop.getBeginLine());
    // return cplan instance
    return new Pair<>(sinHops, tpl);
}
Also used : CNodeData(org.apache.sysml.hops.codegen.cplan.CNodeData) HashMap(java.util.HashMap) AggOp(org.apache.sysml.hops.Hop.AggOp) Hop(org.apache.sysml.hops.Hop) ArrayList(java.util.ArrayList) 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 4 with CNodeMultiAgg

use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project 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 memo memoization table
 * @param cplans set of cplans
 */
private static HashMap<Long, Pair<Hop[], CNodeTpl>> cleanupCPlans(CPlanMemoTable memo, HashMap<Long, Pair<Hop[], CNodeTpl>> cplans) {
    HashMap<Long, Pair<Hop[], CNodeTpl>> cplans2 = new HashMap<>();
    CPlanOpRewriter rewriter = new CPlanOpRewriter();
    CPlanCSERewriter cse = new CPlanCSERewriter();
    for (Entry<Long, Pair<Hop[], CNodeTpl>> e : cplans.entrySet()) {
        CNodeTpl tpl = e.getValue().getValue();
        Hop[] inHops = e.getValue().getKey();
        // remove invalid plans with null inputs
        if (Arrays.stream(inHops).anyMatch(h -> (h == null)))
            continue;
        // perform simplifications and cse rewrites
        tpl = rewriter.simplifyCPlan(tpl);
        tpl = cse.eliminateCommonSubexpressions(tpl);
        // update input hops (order-preserving)
        HashSet<Long> inputHopIDs = tpl.getInputHopIDs(false);
        inHops = Arrays.stream(inHops).filter(p -> p != null && inputHopIDs.contains(p.getHopID())).toArray(Hop[]::new);
        cplans2.put(e.getKey(), new Pair<>(inHops, tpl));
        // remove invalid plans with column indexing on main input
        if (tpl instanceof CNodeCell || tpl instanceof CNodeRow) {
            CNodeData in1 = (CNodeData) tpl.getInput().get(0);
            boolean inclRC1 = !(tpl instanceof CNodeRow);
            if (rHasLookupRC1(tpl.getOutput(), in1, inclRC1) || isLookupRC1(tpl.getOutput(), in1, inclRC1)) {
                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, true) || isLookupRC1(output, in1, true)) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed cplan due to invalid rc1 indexing on main input.");
            }
        }
        // remove invalid lookups on main input (all templates)
        CNodeData in1 = (CNodeData) tpl.getInput().get(0);
        if (tpl instanceof CNodeMultiAgg)
            rFindAndRemoveLookupMultiAgg((CNodeMultiAgg) tpl, in1);
        else
            rFindAndRemoveLookup(tpl.getOutput(), in1, !(tpl instanceof CNodeRow));
        // remove invalid row templates (e.g., unsatisfied blocksize constraint)
        if (tpl instanceof CNodeRow) {
            // check for invalid row cplan over column vector
            if (((CNodeRow) tpl).getRowType() == RowType.NO_AGG && tpl.getOutput().getDataType().isScalar()) {
                cplans2.remove(e.getKey());
                if (LOG.isTraceEnabled())
                    LOG.trace("Removed invalid row cplan w/o agg on column vector.");
            } else if (OptimizerUtils.isSparkExecutionMode()) {
                Hop hop = memo.getHopRefs().get(e.getKey());
                boolean isSpark = DMLScript.rtplatform == RUNTIME_PLATFORM.SPARK || OptimizerUtils.getTotalMemEstimate(inHops, hop, true) > OptimizerUtils.getLocalMemBudget();
                boolean invalidNcol = hop.getDataType().isMatrix() && (HopRewriteUtils.isTransposeOperation(hop) ? hop.getDim1() > hop.getRowsInBlock() : hop.getDim2() > hop.getColsInBlock());
                for (Hop in : inHops) invalidNcol |= (in.getDataType().isMatrix() && in.getDim2() > in.getColsInBlock());
                if (isSpark && invalidNcol) {
                    cplans2.remove(e.getKey());
                    if (LOG.isTraceEnabled())
                        LOG.trace("Removed invalid row cplan w/ ncol>ncolpb.");
                }
            }
        }
        // remove cplan w/ single op and w/o agg
        if ((tpl instanceof CNodeCell && ((CNodeCell) tpl).getCellType() == CellType.NO_AGG && TemplateUtils.hasSingleOperation(tpl)) || (tpl instanceof CNodeRow && (((CNodeRow) tpl).getRowType() == RowType.NO_AGG || ((CNodeRow) tpl).getRowType() == RowType.NO_AGG_B1 || ((CNodeRow) tpl).getRowType() == RowType.ROW_AGG) && TemplateUtils.hasSingleOperation(tpl)) || TemplateUtils.hasNoOperation(tpl)) {
            cplans2.remove(e.getKey());
            if (LOG.isTraceEnabled())
                LOG.trace("Removed cplan with single operation.");
        }
        // remove cplan if empty
        if (tpl.getOutput() instanceof CNodeData) {
            cplans2.remove(e.getKey());
            if (LOG.isTraceEnabled())
                LOG.trace("Removed empty cplan.");
        }
        // rename inputs (for codegen and plan caching)
        tpl.renameInputs();
    }
    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) CPlanCSERewriter(org.apache.sysml.hops.codegen.template.CPlanCSERewriter) CNodeCell(org.apache.sysml.hops.codegen.cplan.CNodeCell) CNode(org.apache.sysml.hops.codegen.cplan.CNode) CNodeRow(org.apache.sysml.hops.codegen.cplan.CNodeRow) CNodeMultiAgg(org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg) CPlanOpRewriter(org.apache.sysml.hops.codegen.template.CPlanOpRewriter) Pair(org.apache.sysml.runtime.matrix.data.Pair)

Example 5 with CNodeMultiAgg

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

the class CPlanCSERewriter method eliminateCommonSubexpressions.

public CNodeTpl eliminateCommonSubexpressions(CNodeTpl tpl) {
    // Note: Compared to our traditional common subexpression elimination, on cplans,
    // we don't have any parent references, and hence cannot use a collect-merge approach.
    // In contrast, we exploit the hash signatures of cnodes as used in the plan cache.
    // However, note that these signatures ignore input hops by default (for better plan
    // cache hit rates), but are temporarily set to strict evaluation for this rewrite.
    List<CNode> outputs = (tpl instanceof CNodeMultiAgg) ? ((CNodeMultiAgg) tpl).getOutputs() : Collections.singletonList(tpl.getOutput());
    // step 1: set data nodes to strict comparison
    tpl.resetVisitStatusOutputs();
    for (CNode out : outputs) rSetStrictDataNodeComparision(out, true);
    // step 2: perform common subexpression elimination
    HashMap<CNode, CNode> cseSet = new HashMap<>();
    tpl.resetVisitStatusOutputs();
    for (CNode out : outputs) rEliminateCommonSubexpression(out, cseSet);
    // step 3: reset data nodes to imprecise comparison
    tpl.resetVisitStatusOutputs();
    for (CNode out : outputs) rSetStrictDataNodeComparision(out, false);
    tpl.resetVisitStatusOutputs();
    return tpl;
}
Also used : CNode(org.apache.sysml.hops.codegen.cplan.CNode) HashMap(java.util.HashMap) CNodeMultiAgg(org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg)

Aggregations

CNodeMultiAgg (org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg)9 HashMap (java.util.HashMap)7 Hop (org.apache.sysml.hops.Hop)7 CNode (org.apache.sysml.hops.codegen.cplan.CNode)7 ArrayList (java.util.ArrayList)5 CNodeCell (org.apache.sysml.hops.codegen.cplan.CNodeCell)5 CNodeData (org.apache.sysml.hops.codegen.cplan.CNodeData)5 CNodeRow (org.apache.sysml.hops.codegen.cplan.CNodeRow)5 CNodeTpl (org.apache.sysml.hops.codegen.cplan.CNodeTpl)5 Pair (org.apache.sysml.runtime.matrix.data.Pair)5 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 CNodeOuterProduct (org.apache.sysml.hops.codegen.cplan.CNodeOuterProduct)3 AggUnaryOp (org.apache.sysml.hops.AggUnaryOp)2 AggOp (org.apache.sysml.hops.Hop.AggOp)2 CNodeUnary (org.apache.sysml.hops.codegen.cplan.CNodeUnary)2 CPlanCSERewriter (org.apache.sysml.hops.codegen.template.CPlanCSERewriter)2 MemoTableEntry (org.apache.sysml.hops.codegen.template.CPlanMemoTable.MemoTableEntry)2 CPlanOpRewriter (org.apache.sysml.hops.codegen.template.CPlanOpRewriter)2