use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rGetUIPConsumerList.
/*
* This will get consumer list for candidate LeftIndexingOp.
*
* @param pn: OpNode of parfor loop
* @param uipCandHopHM: Hashmap of UIPCandidateHop with name as a key.
* @throws DMLRuntimeException
*/
private void rGetUIPConsumerList(OptNode pn, HashMap<String, ArrayList<UIPCandidateHop>> uipCandHopHM) throws DMLRuntimeException {
if (!pn.isLeaf()) {
if (pn.getNodeType() == OptNode.NodeType.FUNCCALL)
return;
ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
VariableSet varRead = pb.getStatementBlock().variablesRead();
boolean bUIPCandHopRead = false;
for (Entry<String, ArrayList<UIPCandidateHop>> entry : uipCandHopHM.entrySet()) {
String uipCandHopID = entry.getKey();
if (varRead.containsVariable(uipCandHopID)) {
bUIPCandHopRead = true;
break;
}
}
// As none of the UIP candidates updated in this DAG, no need for further processing within this DAG
if (!bUIPCandHopRead)
return;
for (OptNode optNode : pn.getChilds()) rGetUIPConsumerList(optNode, uipCandHopHM);
} else {
OptTreePlanMappingAbstract map = OptTreeConverter.getAbstractPlanMapping();
long ppid = map.getMappedParentID(map.getMappedParentID(pn.getID()));
Object[] o = map.getMappedProg(ppid);
ProgramBlock pb = (ProgramBlock) o[1];
Hop hop = (Hop) OptTreeConverter.getAbstractPlanMapping().getMappedHop(pn.getID());
rGetUIPConsumerList(hop, uipCandHopHM);
if (pb instanceof IfProgramBlock || pb instanceof WhileProgramBlock || //TODO
(pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)))
rGetUIPConsumerList(pb, uipCandHopHM);
}
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rResetVisitStatus.
private void rResetVisitStatus(OptNode pn) throws DMLRuntimeException {
if (!pn.isLeaf()) {
if (pn.getNodeType() == OptNode.NodeType.FUNCCALL) {
Hop hopFunc = (Hop) OptTreeConverter.getAbstractPlanMapping().getMappedHop(pn.getID());
hopFunc.resetVisitStatus();
return;
}
ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
ArrayList<ProgramBlock> childBlocks = null;
ArrayList<ProgramBlock> elseBlocks = null;
if (pb instanceof WhileProgramBlock)
childBlocks = ((WhileProgramBlock) pb).getChildBlocks();
else if (pb instanceof ForProgramBlock)
childBlocks = ((ForProgramBlock) pb).getChildBlocks();
else if (pb instanceof IfProgramBlock) {
childBlocks = ((IfProgramBlock) pb).getChildBlocksIfBody();
elseBlocks = ((IfProgramBlock) pb).getChildBlocksElseBody();
}
if (childBlocks != null) {
for (ProgramBlock childBlock : childBlocks) {
try {
Hop.resetVisitStatus(childBlock.getStatementBlock().get_hops());
} catch (Exception e) {
throw new DMLRuntimeException(e);
}
}
}
if (elseBlocks != null) {
for (ProgramBlock childBlock : elseBlocks) {
try {
Hop.resetVisitStatus(childBlock.getStatementBlock().get_hops());
} catch (Exception e) {
throw new DMLRuntimeException(e);
}
}
}
for (OptNode optNode : pn.getChilds()) {
rResetVisitStatus(optNode);
}
} else {
Hop hop = (Hop) OptTreeConverter.getAbstractPlanMapping().getMappedHop(pn.getID());
if (hop != null) {
hop.resetVisitStatus();
}
}
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock 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.IfProgramBlock 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);
}
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-systemml by apache.
the class DMLTranslator method createRuntimeProgramBlock.
public ProgramBlock createRuntimeProgramBlock(Program prog, StatementBlock sb, DMLConfig config) {
Dag<Lop> dag = null;
Dag<Lop> pred_dag = null;
ArrayList<Instruction> instruct;
ArrayList<Instruction> pred_instruct = null;
ProgramBlock retPB = null;
// process While Statement - add runtime program blocks to program
if (sb instanceof WhileStatementBlock) {
// create DAG for loop predicates
pred_dag = new Dag<>();
((WhileStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<>();
ArrayList<Instruction> pInst = pred_dag.getJobs(null, config);
for (Instruction i : pInst) {
pred_instruct.add(i);
}
// create while program block
WhileProgramBlock rtpb = new WhileProgramBlock(prog, pred_instruct);
// // process the body of the while statement block ////
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
for (StatementBlock sblock : wstmt.getBody()) {
// process the body
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlock(childBlock);
}
retPB = rtpb;
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
} else // process If Statement - add runtime program blocks to program
if (sb instanceof IfStatementBlock) {
// create DAG for loop predicates
pred_dag = new Dag<>();
((IfStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<>();
ArrayList<Instruction> pInst = pred_dag.getJobs(null, config);
for (Instruction i : pInst) {
pred_instruct.add(i);
}
// create if program block
IfProgramBlock rtpb = new IfProgramBlock(prog, pred_instruct);
// process the body of the if statement block
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
// process the if body
for (StatementBlock sblock : istmt.getIfBody()) {
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlockIfBody(childBlock);
}
// process the else body
for (StatementBlock sblock : istmt.getElseBody()) {
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlockElseBody(childBlock);
}
retPB = rtpb;
// post processing for generating missing instructions
// retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
} else // NOTE: applies to ForStatementBlock and ParForStatementBlock
if (sb instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) sb;
// create DAGs for loop predicates
Dag<Lop> fromDag = new Dag<>();
Dag<Lop> toDag = new Dag<>();
Dag<Lop> incrementDag = new Dag<>();
if (fsb.getFromHops() != null)
fsb.getFromLops().addToDag(fromDag);
if (fsb.getToHops() != null)
fsb.getToLops().addToDag(toDag);
if (fsb.getIncrementHops() != null)
fsb.getIncrementLops().addToDag(incrementDag);
// create instructions for loop predicates
ArrayList<Instruction> fromInstructions = fromDag.getJobs(null, config);
ArrayList<Instruction> toInstructions = toDag.getJobs(null, config);
ArrayList<Instruction> incrementInstructions = incrementDag.getJobs(null, config);
// create for program block
ForProgramBlock rtpb = null;
IterablePredicate iterPred = fsb.getIterPredicate();
if (sb instanceof ParForStatementBlock && ConfigurationManager.isParallelParFor()) {
rtpb = new ParForProgramBlock(prog, iterPred.getIterVar().getName(), iterPred.getParForParams(), ((ParForStatementBlock) sb).getResultVariables());
ParForProgramBlock pfrtpb = (ParForProgramBlock) rtpb;
// used for optimization and creating unscoped variables
pfrtpb.setStatementBlock((ParForStatementBlock) sb);
} else {
// ForStatementBlock
rtpb = new ForProgramBlock(prog, iterPred.getIterVar().getName());
}
rtpb.setFromInstructions(fromInstructions);
rtpb.setToInstructions(toInstructions);
rtpb.setIncrementInstructions(incrementInstructions);
// process the body of the for statement block
ForStatement fs = (ForStatement) fsb.getStatement(0);
for (StatementBlock sblock : fs.getBody()) {
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlock(childBlock);
}
retPB = rtpb;
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
} else // process function statement block - add runtime program blocks to program
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
FunctionProgramBlock rtpb = null;
if (fstmt instanceof ExternalFunctionStatement) {
// create external function program block
String execType = ((ExternalFunctionStatement) fstmt).getOtherParams().get(ExternalFunctionStatement.EXEC_TYPE);
boolean isCP = (execType.equals(ExternalFunctionStatement.IN_MEMORY)) ? true : false;
StringBuilder buff = new StringBuilder();
buff.append(config.getTextValue(DMLConfig.SCRATCH_SPACE));
buff.append(Lop.FILE_SEPARATOR);
buff.append(Lop.PROCESS_PREFIX);
buff.append(DMLScript.getUUID());
buff.append(Lop.FILE_SEPARATOR);
buff.append(ProgramConverter.CP_ROOT_THREAD_ID);
buff.append(Lop.FILE_SEPARATOR);
buff.append("PackageSupport");
buff.append(Lop.FILE_SEPARATOR);
String basedir = buff.toString();
if (isCP) {
rtpb = new ExternalFunctionProgramBlockCP(prog, fstmt.getInputParams(), fstmt.getOutputParams(), ((ExternalFunctionStatement) fstmt).getOtherParams(), basedir);
} else {
rtpb = new ExternalFunctionProgramBlock(prog, fstmt.getInputParams(), fstmt.getOutputParams(), ((ExternalFunctionStatement) fstmt).getOtherParams(), basedir);
}
if (!fstmt.getBody().isEmpty()) {
LOG.error(fstmt.printErrorLocation() + "ExternalFunctionStatementBlock should have no statement blocks in body");
throw new LopsException(fstmt.printErrorLocation() + "ExternalFunctionStatementBlock should have no statement blocks in body");
}
} else {
// create function program block
rtpb = new FunctionProgramBlock(prog, fstmt.getInputParams(), fstmt.getOutputParams());
// process the function statement body
for (StatementBlock sblock : fstmt.getBody()) {
// process the body
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlock(childBlock);
}
}
// check there are actually Lops in to process (loop stmt body will not have any)
if (fsb.getLops() != null && !fsb.getLops().isEmpty()) {
LOG.error(fsb.printBlockErrorLocation() + "FunctionStatementBlock should have no Lops");
throw new LopsException(fsb.printBlockErrorLocation() + "FunctionStatementBlock should have no Lops");
}
retPB = rtpb;
// add location information
retPB.setParseInfo(sb);
} else {
// handle general case
ProgramBlock rtpb = new ProgramBlock(prog);
// DAGs for Lops
dag = new Dag<>();
// check there are actually Lops in to process (loop stmt body will not have any)
if (sb.getLops() != null && !sb.getLops().isEmpty()) {
for (Lop l : sb.getLops()) {
l.addToDag(dag);
}
// Instructions for Lops DAGs
instruct = dag.getJobs(sb, config);
rtpb.addInstructions(instruct);
}
retPB = rtpb;
// post processing for generating missing instructions
// retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
}
return retPB;
}
Aggregations