use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class Recompiler method recompileProgramBlockInstructions.
/**
* This method does NO full program block recompile (no stats update, no rewrites, no recursion) but
* only regenerates lops and instructions. The primary use case is recompilation after are hop configuration
* changes which allows to preserve statistics (e.g., propagated worst case stats from other program blocks)
* and better performance for recompiling individual program blocks.
*
* @param pb program block
* @throws IOException if IOException occurs
*/
public static void recompileProgramBlockInstructions(ProgramBlock pb) throws IOException {
if (pb instanceof WhileProgramBlock) {
// recompile while predicate instructions
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
if (wsb != null && wsb.getPredicateHops() != null)
wpb.setPredicate(recompileHopsDagInstructions(wsb.getPredicateHops()));
} else if (pb instanceof IfProgramBlock) {
// recompile if predicate instructions
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
if (isb != null && isb.getPredicateHops() != null)
ipb.setPredicate(recompileHopsDagInstructions(isb.getPredicateHops()));
} else if (pb instanceof ForProgramBlock) {
// recompile for/parfor predicate instructions
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
if (fsb != null && fsb.getFromHops() != null)
fpb.setFromInstructions(recompileHopsDagInstructions(fsb.getFromHops()));
if (fsb != null && fsb.getToHops() != null)
fpb.setToInstructions(recompileHopsDagInstructions(fsb.getToHops()));
if (fsb != null && fsb.getIncrementHops() != null)
fpb.setIncrementInstructions(recompileHopsDagInstructions(fsb.getIncrementHops()));
} else {
// recompile last-level program block instructions
StatementBlock sb = pb.getStatementBlock();
if (sb != null && sb.getHops() != null) {
pb.setInstructions(recompileHopsDagInstructions(sb, sb.getHops()));
}
}
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class ProgramConverter method rcreateDeepCopyProgramBlocks.
/**
* This recursively creates a deep copy of program blocks and transparently replaces filenames according to the
* specified parallel worker in order to avoid conflicts between parworkers. This happens recursively in order
* to support arbitrary control-flow constructs within a parfor.
*
* @param childBlocks child program blocks
* @param pid ?
* @param IDPrefix ?
* @param fnStack ?
* @param fnCreated ?
* @param plain if true, full deep copy without id replacement
* @param forceDeepCopy if true, force deep copy
* @return list of program blocks
*/
public static ArrayList<ProgramBlock> rcreateDeepCopyProgramBlocks(ArrayList<ProgramBlock> childBlocks, long pid, int IDPrefix, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) {
ArrayList<ProgramBlock> tmp = new ArrayList<>();
for (ProgramBlock pb : childBlocks) {
Program prog = pb.getProgram();
ProgramBlock tmpPB = null;
if (pb instanceof WhileProgramBlock) {
tmpPB = createDeepCopyWhileProgramBlock((WhileProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
tmpPB = createDeepCopyForProgramBlock((ForProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof ParForProgramBlock) {
ParForProgramBlock pfpb = (ParForProgramBlock) pb;
if (ParForProgramBlock.ALLOW_NESTED_PARALLELISM)
tmpPB = createDeepCopyParForProgramBlock(pfpb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
else
tmpPB = createDeepCopyForProgramBlock((ForProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof IfProgramBlock) {
tmpPB = createDeepCopyIfProgramBlock((IfProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else // last-level program block
{
// general case use for most PBs
tmpPB = new ProgramBlock(prog);
// for recompile in the master node JVM
tmpPB.setStatementBlock(createStatementBlockCopy(pb.getStatementBlock(), pid, plain, forceDeepCopy));
// tmpPB.setStatementBlock(pb.getStatementBlock());
tmpPB.setThreadID(pid);
}
// copy instructions
tmpPB.setInstructions(createDeepCopyInstructionSet(pb.getInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
// copy symbol table
// tmpPB.setVariables( pb.getVariables() ); //implicit cloning
tmp.add(tmpPB);
}
return tmp;
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class ProgramConverter method rParseWhileProgramBlock.
private static WhileProgramBlock rParseWhileProgramBlock(String in, Program prog, int id) {
String lin = in.substring(PARFOR_PB_WHILE.length(), in.length() - PARFOR_PB_END.length());
HierarchyAwareStringTokenizer st = new HierarchyAwareStringTokenizer(lin, COMPONENTS_DELIM);
// predicate instructions
ArrayList<Instruction> inst = parseInstructions(st.nextToken(), id);
// exit instructions
ArrayList<Instruction> exit = parseInstructions(st.nextToken(), id);
// program blocks
ArrayList<ProgramBlock> pbs = rParseProgramBlocks(st.nextToken(), prog, id);
WhileProgramBlock wpb = new WhileProgramBlock(prog, inst);
wpb.setExitInstructions2(exit);
wpb.setChildBlocks(pbs);
return wpb;
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class OptTreeConverter method rCreateAbstractOptNode.
public static OptNode rCreateAbstractOptNode(StatementBlock sb, ProgramBlock pb, LocalVariableMap vars, boolean topLevel, Set<String> memo) {
OptNode node = null;
if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement is = (IfStatement) isb.getStatement(0);
node = new OptNode(NodeType.IF);
_hlMap.putProgMapping(sb, pb, node);
node.setExecType(ExecType.CP);
node.setLineNumbers(isb.getBeginLine(), isb.getEndLine());
// handle predicate
isb.getPredicateHops().resetVisitStatus();
node.addChilds(rCreateAbstractOptNodes(isb.getPredicateHops(), vars, memo));
// process if branch
OptNode ifn = new OptNode(NodeType.GENERIC);
_hlMap.putProgMapping(sb, pb, ifn);
ifn.setExecType(ExecType.CP);
node.addChild(ifn);
int len = is.getIfBody().size();
for (int i = 0; i < ipb.getChildBlocksIfBody().size() && i < len; i++) {
ProgramBlock lpb = ipb.getChildBlocksIfBody().get(i);
StatementBlock lsb = is.getIfBody().get(i);
ifn.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
// process else branch
if (ipb.getChildBlocksElseBody() != null) {
OptNode efn = new OptNode(NodeType.GENERIC);
_hlMap.putProgMapping(sb, pb, efn);
efn.setExecType(ExecType.CP);
node.addChild(efn);
int len2 = is.getElseBody().size();
for (int i = 0; i < ipb.getChildBlocksElseBody().size() && i < len2; i++) {
ProgramBlock lpb = ipb.getChildBlocksElseBody().get(i);
StatementBlock lsb = is.getElseBody().get(i);
efn.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
}
} else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement ws = (WhileStatement) wsb.getStatement(0);
node = new OptNode(NodeType.WHILE);
_hlMap.putProgMapping(sb, pb, node);
node.setExecType(ExecType.CP);
node.setLineNumbers(wsb.getBeginLine(), wsb.getEndLine());
// handle predicate
wsb.getPredicateHops().resetVisitStatus();
node.addChilds(rCreateAbstractOptNodes(wsb.getPredicateHops(), vars, memo));
// process body
int len = ws.getBody().size();
for (int i = 0; i < wpb.getChildBlocks().size() && i < len; i++) {
ProgramBlock lpb = wpb.getChildBlocks().get(i);
StatementBlock lsb = ws.getBody().get(i);
node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
} else if (pb instanceof ForProgramBlock && sb instanceof ForStatementBlock && !(pb instanceof ParForProgramBlock)) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fs = (ForStatement) fsb.getStatement(0);
node = new OptNode(NodeType.FOR);
_hlMap.putProgMapping(sb, pb, node);
node.setExecType(ExecType.CP);
node.setLineNumbers(fsb.getBeginLine(), fsb.getEndLine());
// determine number of iterations
long N = OptimizerUtils.getNumIterations(fpb, vars, CostEstimator.FACTOR_NUM_ITERATIONS);
node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(N));
// handle predicate
fsb.getFromHops().resetVisitStatus();
fsb.getToHops().resetVisitStatus();
if (fsb.getIncrementHops() != null)
fsb.getIncrementHops().resetVisitStatus();
node.addChilds(rCreateAbstractOptNodes(fsb.getFromHops(), vars, memo));
node.addChilds(rCreateAbstractOptNodes(fsb.getToHops(), vars, memo));
if (fsb.getIncrementHops() != null)
node.addChilds(rCreateAbstractOptNodes(fsb.getIncrementHops(), vars, memo));
// process body
int len = fs.getBody().size();
for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
ProgramBlock lpb = fpb.getChildBlocks().get(i);
StatementBlock lsb = fs.getBody().get(i);
node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
} else if (pb instanceof ParForProgramBlock && sb instanceof ParForStatementBlock) {
ParForProgramBlock fpb = (ParForProgramBlock) pb;
ParForStatementBlock fsb = (ParForStatementBlock) sb;
ParForStatement fs = (ParForStatement) fsb.getStatement(0);
node = new OptNode(NodeType.PARFOR);
node.setLineNumbers(fsb.getBeginLine(), fsb.getEndLine());
_hlMap.putProgMapping(sb, pb, node);
node.setK(fpb.getDegreeOfParallelism());
long N = fpb.getNumIterations();
node.addParam(ParamType.NUM_ITERATIONS, (N != -1) ? String.valueOf(N) : String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
switch(fpb.getExecMode()) {
case LOCAL:
node.setExecType(ExecType.CP);
break;
case REMOTE_MR:
case REMOTE_MR_DP:
node.setExecType(ExecType.MR);
break;
case REMOTE_SPARK:
case REMOTE_SPARK_DP:
node.setExecType(ExecType.SPARK);
break;
case UNSPECIFIED:
node.setExecType(null);
}
if (!topLevel) {
fsb.getFromHops().resetVisitStatus();
fsb.getToHops().resetVisitStatus();
if (fsb.getIncrementHops() != null)
fsb.getIncrementHops().resetVisitStatus();
node.addChilds(rCreateAbstractOptNodes(fsb.getFromHops(), vars, memo));
node.addChilds(rCreateAbstractOptNodes(fsb.getToHops(), vars, memo));
if (fsb.getIncrementHops() != null)
node.addChilds(rCreateAbstractOptNodes(fsb.getIncrementHops(), vars, memo));
}
// process body
int len = fs.getBody().size();
for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
ProgramBlock lpb = fpb.getChildBlocks().get(i);
StatementBlock lsb = fs.getBody().get(i);
node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
// parameters, add required parameters
Map<String, String> lparams = fpb.getParForParams();
node.addParam(ParamType.DATA_PARTITIONER, lparams.get(ParForStatementBlock.DATA_PARTITIONER));
node.addParam(ParamType.TASK_PARTITIONER, lparams.get(ParForStatementBlock.TASK_PARTITIONER));
node.addParam(ParamType.RESULT_MERGE, lparams.get(ParForStatementBlock.RESULT_MERGE));
// TODO task size
} else // last level program block
{
sb = pb.getStatementBlock();
// process all hops
node = new OptNode(NodeType.GENERIC);
_hlMap.putProgMapping(sb, pb, node);
node.addChilds(createAbstractOptNodes(sb.getHops(), vars, memo));
node.setExecType(ExecType.CP);
node.setLineNumbers(sb.getBeginLine(), sb.getEndLine());
// TODO remove this workaround once this information can be obtained from hops/lops compiler
if (node.isCPOnly()) {
boolean isSparkExec = OptimizerUtils.isSparkExecutionMode();
if (!isSparkExec && containsMRJobInstruction(pb, false, false))
node.setExecType(ExecType.MR);
else if (isSparkExec && containsMRJobInstruction(pb, false, true))
node.setExecType(ExecType.SPARK);
}
}
// final cleanup
// NOTE: required because this function is also used to create subtrees
node.checkAndCleanupLeafNodes();
return node;
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class OptTreeConverter method rContainsMRJobInstruction.
public static boolean rContainsMRJobInstruction(ProgramBlock pb, boolean inclFunctions) {
boolean ret = false;
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock tmp = (WhileProgramBlock) pb;
ret = containsMRJobInstruction(tmp.getPredicate(), true, true);
if (ret)
return ret;
for (ProgramBlock pb2 : tmp.getChildBlocks()) {
ret = rContainsMRJobInstruction(pb2, inclFunctions);
if (ret)
return ret;
}
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock tmp = (IfProgramBlock) pb;
ret = containsMRJobInstruction(tmp.getPredicate(), true, true);
if (ret)
return ret;
for (ProgramBlock pb2 : tmp.getChildBlocksIfBody()) {
ret = rContainsMRJobInstruction(pb2, inclFunctions);
if (ret)
return ret;
}
for (ProgramBlock pb2 : tmp.getChildBlocksElseBody()) {
ret = rContainsMRJobInstruction(pb2, inclFunctions);
if (ret)
return ret;
}
} else if (// includes ParFORProgramBlock
pb instanceof ForProgramBlock) {
ForProgramBlock tmp = (ForProgramBlock) pb;
ret = containsMRJobInstruction(tmp.getFromInstructions(), true, true);
ret |= containsMRJobInstruction(tmp.getToInstructions(), true, true);
ret |= containsMRJobInstruction(tmp.getIncrementInstructions(), true, true);
if (ret)
return ret;
for (ProgramBlock pb2 : tmp.getChildBlocks()) {
ret = rContainsMRJobInstruction(pb2, inclFunctions);
if (ret)
return ret;
}
} else if (// includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP)
pb instanceof FunctionProgramBlock) {
// do nothing
} else {
ret = containsMRJobInstruction(pb, true, true) || (inclFunctions && containsFunctionCallInstruction(pb));
}
return ret;
}
Aggregations