use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class GraphBuilder method constructGDFGraph.
@SuppressWarnings("unchecked")
private static void constructGDFGraph(ProgramBlock pb, HashMap<String, GDFNode> roots) throws DMLRuntimeException, HopsException {
if (pb instanceof FunctionProgramBlock) {
throw new DMLRuntimeException("FunctionProgramBlocks not implemented yet.");
} else if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
//construct predicate node (conceptually sequence of from/to/incr)
GDFNode pred = constructGDFGraph(wsb.getPredicateHops(), wpb, new HashMap<Long, GDFNode>(), roots);
HashMap<String, GDFNode> inputs = constructLoopInputNodes(wpb, wsb, roots);
HashMap<String, GDFNode> lroots = (HashMap<String, GDFNode>) inputs.clone();
//process childs blocks
for (ProgramBlock pbc : wpb.getChildBlocks()) constructGDFGraph(pbc, lroots);
HashMap<String, GDFNode> outputs = constructLoopOutputNodes(wsb, lroots);
GDFLoopNode lnode = new GDFLoopNode(wpb, pred, inputs, outputs);
//construct crossblock nodes
constructLoopOutputCrossBlockNodes(wsb, lnode, outputs, roots, wpb);
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
//construct predicate
if (isb.getPredicateHops() != null) {
Hop pred = isb.getPredicateHops();
roots.put(pred.getName(), constructGDFGraph(pred, ipb, new HashMap<Long, GDFNode>(), roots));
}
//construct if and else branch separately
HashMap<String, GDFNode> ifRoots = (HashMap<String, GDFNode>) roots.clone();
HashMap<String, GDFNode> elseRoots = (HashMap<String, GDFNode>) roots.clone();
for (ProgramBlock pbc : ipb.getChildBlocksIfBody()) constructGDFGraph(pbc, ifRoots);
if (ipb.getChildBlocksElseBody() != null)
for (ProgramBlock pbc : ipb.getChildBlocksElseBody()) constructGDFGraph(pbc, elseRoots);
//merge data flow roots (if no else, elseRoots refer to original roots)
reconcileMergeIfProgramBlockOutputs(ifRoots, elseRoots, roots, ipb);
} else if (//incl parfor
pb instanceof ForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
//construct predicate node (conceptually sequence of from/to/incr)
GDFNode pred = constructForPredicateNode(fpb, fsb, roots);
HashMap<String, GDFNode> inputs = constructLoopInputNodes(fpb, fsb, roots);
HashMap<String, GDFNode> lroots = (HashMap<String, GDFNode>) inputs.clone();
//process childs blocks
for (ProgramBlock pbc : fpb.getChildBlocks()) constructGDFGraph(pbc, lroots);
HashMap<String, GDFNode> outputs = constructLoopOutputNodes(fsb, lroots);
GDFLoopNode lnode = new GDFLoopNode(fpb, pred, inputs, outputs);
//construct crossblock nodes
constructLoopOutputCrossBlockNodes(fsb, lnode, outputs, roots, fpb);
} else //last-level program block
{
StatementBlock sb = pb.getStatementBlock();
ArrayList<Hop> hops = sb.get_hops();
if (hops != null) {
//create new local memo structure for local dag
HashMap<Long, GDFNode> lmemo = new HashMap<Long, GDFNode>();
for (Hop hop : hops) {
//recursively construct GDF graph for hop dag root
GDFNode root = constructGDFGraph(hop, pb, lmemo, roots);
if (root == null)
throw new HopsException("GDFGraphBuilder: failed to constuct dag root for: " + Explain.explain(hop));
//create cross block nodes for all transient writes
if (hop instanceof DataOp && ((DataOp) hop).getDataOpType() == DataOpTypes.TRANSIENTWRITE)
root = new GDFCrossBlockNode(hop, pb, root, hop.getName());
//add GDF root node to global roots
roots.put(hop.getName(), root);
}
}
}
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class GraphBuilder method constructLoopOutputNodes.
private static HashMap<String, GDFNode> constructLoopOutputNodes(StatementBlock fsb, HashMap<String, GDFNode> roots) throws HopsException {
HashMap<String, GDFNode> ret = new HashMap<String, GDFNode>();
Set<String> outvars = fsb.variablesUpdated().getVariableNames();
for (String var : outvars) {
GDFNode node = roots.get(var);
//handle non-existing nodes
if (node == null) {
if (!IGNORE_UNBOUND_UPDATED_VARS)
throw new HopsException("GDFGraphBuilder: failed to constuct loop output for variable: " + var);
else
//skip unbound updated variables
continue;
}
//add existing node to loop outputs
ret.put(var, node);
}
return ret;
}
use of org.apache.sysml.hops.HopsException in project incubator-systemml by apache.
the class GDFEnumOptimizer method costRuntimePlan.
private static double costRuntimePlan(Plan p) throws DMLRuntimeException {
Program prog = p.getNode().getProgram();
if (prog == null)
throw new DMLRuntimeException("Program not available for runtime plan costing.");
//put data flow configuration into program
rSetRuntimePlanConfig(p, new HashMap<Long, Plan>());
double costs = -1;
if (COST_FULL_PROGRAMS || (p.getNode().getHop() == null || p.getNode().getProgramBlock() == null)) {
//recompile entire runtime program
Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
_compiledPlans++;
//cost entire runtime program
ExecutionContext ec = ExecutionContextFactory.createContext(prog);
costs = CostEstimationWrapper.getTimeEstimate(prog, ec);
} else {
Hop currentHop = p.getNode().getHop();
ProgramBlock pb = p.getNode().getProgramBlock();
try {
//keep the old dag roots
ArrayList<Hop> oldRoots = pb.getStatementBlock().get_hops();
Hop tmpHop = null;
if (!(currentHop instanceof DataOp && ((DataOp) currentHop).isWrite())) {
ArrayList<Hop> newRoots = new ArrayList<Hop>();
tmpHop = new DataOp("_tmp", currentHop.getDataType(), currentHop.getValueType(), currentHop, DataOpTypes.TRANSIENTWRITE, "tmp");
//ensure recursive visitstatus reset on recompile
tmpHop.setVisited();
newRoots.add(tmpHop);
pb.getStatementBlock().set_hops(newRoots);
}
//recompile modified runtime program
Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
_compiledPlans++;
//cost partial runtime program up to current hop
ExecutionContext ec = ExecutionContextFactory.createContext(prog);
costs = CostEstimationWrapper.getTimeEstimate(prog, ec);
//restore original hop dag
if (tmpHop != null)
HopRewriteUtils.removeChildReference(tmpHop, currentHop);
pb.getStatementBlock().set_hops(oldRoots);
} catch (HopsException ex) {
throw new DMLRuntimeException(ex);
}
}
//release forced data flow configuration from program
rResetRuntimePlanConfig(p, new HashMap<Long, Plan>());
_costedPlans++;
return costs;
}
Aggregations