use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project incubator-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);
}
use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project incubator-systemml by apache.
the class SpoofCompiler method rConstructModifiedHopDag.
private static void rConstructModifiedHopDag(Hop hop, HashMap<Long, Pair<Hop[], CNodeTpl>> cplans, HashMap<Long, Pair<Hop[], Class<?>>> clas, HashSet<Long> memo) {
if (memo.contains(hop.getHopID()))
// already processed
return;
Hop hnew = hop;
if (clas.containsKey(hop.getHopID())) {
// replace sub-dag with generated operator
Pair<Hop[], Class<?>> tmpCla = clas.get(hop.getHopID());
CNodeTpl tmpCNode = cplans.get(hop.getHopID()).getValue();
hnew = new SpoofFusedOp(hop.getName(), hop.getDataType(), hop.getValueType(), tmpCla.getValue(), false, tmpCNode.getOutputDimType());
Hop[] inHops = tmpCla.getKey();
for (int i = 0; i < inHops.length; i++) {
if (tmpCNode instanceof CNodeOuterProduct && inHops[i].getHopID() == ((CNodeData) tmpCNode.getInput().get(2)).getHopID() && !TemplateUtils.hasTransposeParentUnderOuterProduct(inHops[i])) {
hnew.addInput(HopRewriteUtils.createTranspose(inHops[i]));
} else
// add inputs
hnew.addInput(inHops[i]);
}
// modify output parameters
HopRewriteUtils.setOutputParameters(hnew, hop.getDim1(), hop.getDim2(), hop.getRowsInBlock(), hop.getColsInBlock(), hop.getNnz());
if (tmpCNode instanceof CNodeOuterProduct && ((CNodeOuterProduct) tmpCNode).isTransposeOutput())
hnew = HopRewriteUtils.createTranspose(hnew);
else if (tmpCNode instanceof CNodeMultiAgg) {
ArrayList<Hop> roots = ((CNodeMultiAgg) tmpCNode).getRootNodes();
hnew.setDataType(DataType.MATRIX);
HopRewriteUtils.setOutputParameters(hnew, 1, roots.size(), inHops[0].getRowsInBlock(), inHops[0].getColsInBlock(), -1);
// inject artificial right indexing operations for all parents of all nodes
for (int i = 0; i < roots.size(); i++) {
Hop hnewi = (roots.get(i) instanceof AggUnaryOp) ? HopRewriteUtils.createScalarIndexing(hnew, 1, i + 1) : HopRewriteUtils.createIndexingOp(hnew, 1, i + 1);
HopRewriteUtils.rewireAllParentChildReferences(roots.get(i), hnewi);
}
} else if (tmpCNode instanceof CNodeCell && ((CNodeCell) tmpCNode).requiredCastDtm()) {
HopRewriteUtils.setOutputParametersForScalar(hnew);
hnew = HopRewriteUtils.createUnary(hnew, OpOp1.CAST_AS_MATRIX);
} else if (tmpCNode instanceof CNodeRow && (((CNodeRow) tmpCNode).getRowType() == RowType.NO_AGG_CONST || ((CNodeRow) tmpCNode).getRowType() == RowType.COL_AGG_CONST))
((SpoofFusedOp) hnew).setConstDim2(((CNodeRow) tmpCNode).getConstDim2());
if (!(tmpCNode instanceof CNodeMultiAgg))
HopRewriteUtils.rewireAllParentChildReferences(hop, hnew);
memo.add(hnew.getHopID());
}
// process hops recursively (parent-child links modified)
for (int i = 0; i < hnew.getInput().size(); i++) {
Hop c = hnew.getInput().get(i);
rConstructModifiedHopDag(c, cplans, clas, memo);
}
memo.add(hnew.getHopID());
}
use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project 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;
}
use of org.apache.sysml.hops.codegen.cplan.CNodeMultiAgg in project systemml by apache.
the class SpoofCompiler method rConstructModifiedHopDag.
private static void rConstructModifiedHopDag(Hop hop, HashMap<Long, Pair<Hop[], CNodeTpl>> cplans, HashMap<Long, Pair<Hop[], Class<?>>> clas, HashSet<Long> memo) {
if (memo.contains(hop.getHopID()))
// already processed
return;
Hop hnew = hop;
if (clas.containsKey(hop.getHopID())) {
// replace sub-dag with generated operator
Pair<Hop[], Class<?>> tmpCla = clas.get(hop.getHopID());
CNodeTpl tmpCNode = cplans.get(hop.getHopID()).getValue();
hnew = new SpoofFusedOp(hop.getName(), hop.getDataType(), hop.getValueType(), tmpCla.getValue(), false, tmpCNode.getOutputDimType());
Hop[] inHops = tmpCla.getKey();
for (int i = 0; i < inHops.length; i++) {
if (tmpCNode instanceof CNodeOuterProduct && inHops[i].getHopID() == ((CNodeData) tmpCNode.getInput().get(2)).getHopID() && !TemplateUtils.hasTransposeParentUnderOuterProduct(inHops[i])) {
hnew.addInput(HopRewriteUtils.createTranspose(inHops[i]));
} else
// add inputs
hnew.addInput(inHops[i]);
}
// modify output parameters
HopRewriteUtils.setOutputParameters(hnew, hop.getDim1(), hop.getDim2(), hop.getRowsInBlock(), hop.getColsInBlock(), hop.getNnz());
if (tmpCNode instanceof CNodeOuterProduct && ((CNodeOuterProduct) tmpCNode).isTransposeOutput())
hnew = HopRewriteUtils.createTranspose(hnew);
else if (tmpCNode instanceof CNodeMultiAgg) {
ArrayList<Hop> roots = ((CNodeMultiAgg) tmpCNode).getRootNodes();
hnew.setDataType(DataType.MATRIX);
HopRewriteUtils.setOutputParameters(hnew, 1, roots.size(), inHops[0].getRowsInBlock(), inHops[0].getColsInBlock(), -1);
// inject artificial right indexing operations for all parents of all nodes
for (int i = 0; i < roots.size(); i++) {
Hop hnewi = (roots.get(i) instanceof AggUnaryOp) ? HopRewriteUtils.createScalarIndexing(hnew, 1, i + 1) : HopRewriteUtils.createIndexingOp(hnew, 1, i + 1);
HopRewriteUtils.rewireAllParentChildReferences(roots.get(i), hnewi);
}
} else if (tmpCNode instanceof CNodeCell && ((CNodeCell) tmpCNode).requiredCastDtm()) {
HopRewriteUtils.setOutputParametersForScalar(hnew);
hnew = HopRewriteUtils.createUnary(hnew, OpOp1.CAST_AS_MATRIX);
} else if (tmpCNode instanceof CNodeRow && (((CNodeRow) tmpCNode).getRowType() == RowType.NO_AGG_CONST || ((CNodeRow) tmpCNode).getRowType() == RowType.COL_AGG_CONST))
((SpoofFusedOp) hnew).setConstDim2(((CNodeRow) tmpCNode).getConstDim2());
if (!(tmpCNode instanceof CNodeMultiAgg))
HopRewriteUtils.rewireAllParentChildReferences(hop, hnew);
memo.add(hnew.getHopID());
}
// process hops recursively (parent-child links modified)
for (int i = 0; i < hnew.getInput().size(); i++) {
Hop c = hnew.getInput().get(i);
rConstructModifiedHopDag(c, cplans, clas, memo);
}
memo.add(hnew.getHopID());
}
Aggregations