use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock 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.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.
the class DMLProgram method addCleanupInstruction.
/**
* Adds the generated cleanup RMVAR instruction to the given program block.
* In case of generic (last-level) programblocks it is added to the end of
* the list of instructions, while for complex program blocks it is added to
* the end of the list of exit instructions.
*
* @param pb program block
* @param inst instruction
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
private void addCleanupInstruction(ProgramBlock pb, Instruction inst) throws DMLRuntimeException {
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
ArrayList<ProgramBlock> childs = wpb.getChildBlocks();
if (//generic last level pb
!childs.get(childs.size() - 1).getInstructions().isEmpty())
childs.get(childs.size() - 1).addInstruction(inst);
else {
ProgramBlock pbNew = new ProgramBlock(pb.getProgram());
pbNew.addInstruction(inst);
childs.add(pbNew);
}
} else if (//includes ParFORProgramBlock
pb instanceof ForProgramBlock) {
ForProgramBlock wpb = (ForProgramBlock) pb;
ArrayList<ProgramBlock> childs = wpb.getChildBlocks();
if (//generic last level pb
!childs.get(childs.size() - 1).getInstructions().isEmpty())
childs.get(childs.size() - 1).addInstruction(inst);
else {
ProgramBlock pbNew = new ProgramBlock(pb.getProgram());
pbNew.addInstruction(inst);
childs.add(pbNew);
}
} else if (pb instanceof IfProgramBlock)
((IfProgramBlock) pb).addExitInstruction(inst);
else if (//includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP)
pb instanceof FunctionProgramBlock)
//do nothing
;
else {
//add inst at end of pb
pb.addInstruction(inst);
}
}
Aggregations