use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class CostEstimator method rGetTimeEstimate.
private double rGetTimeEstimate(ProgramBlock pb, HashMap<String, VarStats> stats, HashSet<String> memoFunc, boolean recursive) throws DMLRuntimeException {
double ret = 0;
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock tmp = (WhileProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
ret *= DEFAULT_NUMITER;
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock tmp = (IfProgramBlock) pb;
if (recursive) {
for (ProgramBlock pb2 : tmp.getChildBlocksIfBody()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
if (tmp.getChildBlocksElseBody() != null)
for (ProgramBlock pb2 : tmp.getChildBlocksElseBody()) {
ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
//weighted sum
ret /= 2;
}
}
} else if (//includes ParFORProgramBlock
pb instanceof ForProgramBlock) {
ForProgramBlock tmp = (ForProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
ret *= getNumIterations(stats, tmp.getIterablePredicateVars());
} else if (pb instanceof FunctionProgramBlock && //see generic
!(pb instanceof ExternalFunctionProgramBlock)) {
FunctionProgramBlock tmp = (FunctionProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
} else {
ArrayList<Instruction> tmp = pb.getInstructions();
for (Instruction inst : tmp) {
if (//CP
inst instanceof CPInstruction) {
//obtain stats from createvar, cpvar, rmvar, rand
maintainCPInstVariableStatistics((CPInstruction) inst, stats);
//extract statistics (instruction-specific)
Object[] o = extractCPInstStatistics(inst, stats);
VarStats[] vs = (VarStats[]) o[0];
String[] attr = (String[]) o[1];
//if(LOG.isDebugEnabled())
// LOG.debug(inst);
//call time estimation for inst
ret += getCPInstTimeEstimate(inst, vs, attr);
if (//functions
inst instanceof FunctionCallCPInstruction) {
FunctionCallCPInstruction finst = (FunctionCallCPInstruction) inst;
String fkey = DMLProgram.constructFunctionKey(finst.getNamespace(), finst.getFunctionName());
//awareness of recursive functions, missing program
if (!memoFunc.contains(fkey) && pb.getProgram() != null) {
if (LOG.isDebugEnabled())
LOG.debug("Begin Function " + fkey);
memoFunc.add(fkey);
Program prog = pb.getProgram();
FunctionProgramBlock fpb = prog.getFunctionProgramBlock(finst.getNamespace(), finst.getFunctionName());
ret += rGetTimeEstimate(fpb, stats, memoFunc, recursive);
memoFunc.remove(fkey);
if (LOG.isDebugEnabled())
LOG.debug("End Function " + fkey);
}
}
} else if (//MR
inst instanceof MRJobInstruction) {
//obtain stats for job
maintainMRJobInstVariableStatistics(inst, stats);
//extract input statistics
Object[] o = extractMRJobInstStatistics(inst, stats);
VarStats[] vs = (VarStats[]) o[0];
if (LOG.isDebugEnabled())
LOG.debug("Begin MRJob type=" + ((MRJobInstruction) inst).getJobType());
//call time estimation for complex MR inst
ret += getMRJobInstTimeEstimate(inst, vs, null);
if (LOG.isDebugEnabled())
LOG.debug("End MRJob");
//cleanup stats for job
cleanupMRJobVariableStatistics(inst, stats);
}
}
}
return ret;
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class ProgramRecompiler method rFindAndRecompileIndexingHOP.
/**
* NOTE: if force is set, we set and recompile the respective indexing hops;
* otherwise, we release the forced exec type and recompile again. Hence,
* any changes can be exactly reverted with the same access behavior.
*
* @param sb statement block
* @param pb program block
* @param var variable
* @param ec execution context
* @param force if true, set and recompile the respective indexing hops
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
public static void rFindAndRecompileIndexingHOP(StatementBlock sb, ProgramBlock pb, String var, ExecutionContext ec, boolean force) throws DMLRuntimeException {
if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement is = (IfStatement) sb.getStatement(0);
//process if condition
if (isb.getPredicateHops() != null)
ipb.setPredicate(rFindAndRecompileIndexingHOP(isb.getPredicateHops(), ipb.getPredicate(), var, ec, force));
//process if branch
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);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
//process else branch
if (ipb.getChildBlocksElseBody() != null) {
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);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
}
} else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement ws = (WhileStatement) sb.getStatement(0);
//process while condition
if (wsb.getPredicateHops() != null)
wpb.setPredicate(rFindAndRecompileIndexingHOP(wsb.getPredicateHops(), wpb.getPredicate(), var, ec, force));
//process body
//robustness for potentially added problem blocks
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);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
} else if (//for or parfor
pb instanceof ForProgramBlock && sb instanceof ForStatementBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fs = (ForStatement) fsb.getStatement(0);
if (fsb.getFromHops() != null)
fpb.setFromInstructions(rFindAndRecompileIndexingHOP(fsb.getFromHops(), fpb.getFromInstructions(), var, ec, force));
if (fsb.getToHops() != null)
fpb.setToInstructions(rFindAndRecompileIndexingHOP(fsb.getToHops(), fpb.getToInstructions(), var, ec, force));
if (fsb.getIncrementHops() != null)
fpb.setIncrementInstructions(rFindAndRecompileIndexingHOP(fsb.getIncrementHops(), fpb.getIncrementInstructions(), var, ec, force));
//process body
//robustness for potentially added problem blocks
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);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
} else //last level program block
{
try {
//process actual hops
boolean ret = false;
Hop.resetVisitStatus(sb.get_hops());
if (force) {
//set forced execution type
for (Hop h : sb.get_hops()) ret |= rFindAndSetCPIndexingHOP(h, var);
} else {
//release forced execution type
for (Hop h : sb.get_hops()) ret |= rFindAndReleaseIndexingHOP(h, var);
}
//recompilation on-demand
if (ret) {
//construct new instructions
ArrayList<Instruction> newInst = Recompiler.recompileHopsDag(sb, sb.get_hops(), ec.getVariables(), null, true, false, 0);
pb.setInstructions(newInst);
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
}
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rIsInLoop.
/*
* This will check if candidate LeftIndexingOp are in loop (while, for or parfor).
*
* @param pn: OpNode of parfor loop
* @param uipCandHopHM: Hashmap of UIPCandidateHop with name as a key.
* @throws DMLRuntimeException
*/
private void rIsInLoop(OptNode pn, HashMap<String, ArrayList<UIPCandidateHop>> uipCandHopHM, boolean bInLoop) throws DMLRuntimeException {
if (!pn.isLeaf()) {
ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
VariableSet varUpdated = pb.getStatementBlock().variablesUpdated();
boolean bUIPCandHopUpdated = false;
for (Entry<String, ArrayList<UIPCandidateHop>> entry : uipCandHopHM.entrySet()) {
String uipCandHopID = entry.getKey();
if (varUpdated.containsVariable(uipCandHopID)) {
bUIPCandHopUpdated = true;
break;
}
}
// As none of the UIP candidates updated in this DAG, no need for further processing within this DAG
if (!bUIPCandHopUpdated)
return;
boolean bLoop = false;
if (bInLoop || pb instanceof WhileProgramBlock || (pb instanceof ParForProgramBlock && ((ParForProgramBlock) pb).getDegreeOfParallelism() == 1) || (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)))
bLoop = true;
for (OptNode optNode : pn.getChilds()) {
rIsInLoop(optNode, uipCandHopHM, bLoop);
}
} else {
Hop hop = (Hop) OptTreeConverter.getAbstractPlanMapping().getMappedHop(pn.getID());
for (Entry<String, ArrayList<UIPCandidateHop>> entry : uipCandHopHM.entrySet()) {
ArrayList<UIPCandidateHop> uipCandHopList = entry.getValue();
if (uipCandHopList != null) {
for (UIPCandidateHop uipCandHop : uipCandHopList) {
//Identify where intermediate object has been defined.
if (hop instanceof DataGenOp && hop.getName().equals(uipCandHop.getLixHop().getName())) {
uipCandHop.setHop(hop);
uipCandHop.setLocation(hop.getBeginLine());
uipCandHop.setIntermediate(true);
}
//Update if candiate hop defined outside this loop, and leftindexing is within this loop.
if ((bInLoop) && (uipCandHop.getLocation() <= hop.getBeginLine() && uipCandHop.getLixHop().getBeginLine() <= hop.getEndLine()))
uipCandHop.setIsLoopApplicable(true);
}
}
}
}
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class SpoofCompiler method generateCodeFromProgramBlock.
public static void generateCodeFromProgramBlock(ProgramBlock current) throws HopsException, DMLRuntimeException, LopsException, IOException {
if (current instanceof FunctionProgramBlock) {
FunctionProgramBlock fsb = (FunctionProgramBlock) current;
for (ProgramBlock pb : fsb.getChildBlocks()) generateCodeFromProgramBlock(pb);
} else if (current instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) current;
WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
if (wsb != null && wsb.getPredicateHops() != null)
wpb.setPredicate(generateCodeFromHopDAGsToInst(wsb.getPredicateHops()));
for (ProgramBlock sb : wpb.getChildBlocks()) generateCodeFromProgramBlock(sb);
} else if (current instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) current;
IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
if (isb != null && isb.getPredicateHops() != null)
ipb.setPredicate(generateCodeFromHopDAGsToInst(isb.getPredicateHops()));
for (ProgramBlock pb : ipb.getChildBlocksIfBody()) generateCodeFromProgramBlock(pb);
for (ProgramBlock pb : ipb.getChildBlocksElseBody()) generateCodeFromProgramBlock(pb);
} else if (//incl parfor
current instanceof ForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) current;
ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
if (fsb != null && fsb.getFromHops() != null)
fpb.setFromInstructions(generateCodeFromHopDAGsToInst(fsb.getFromHops()));
if (fsb != null && fsb.getToHops() != null)
fpb.setToInstructions(generateCodeFromHopDAGsToInst(fsb.getToHops()));
if (fsb != null && fsb.getIncrementHops() != null)
fpb.setIncrementInstructions(generateCodeFromHopDAGsToInst(fsb.getIncrementHops()));
for (ProgramBlock pb : fpb.getChildBlocks()) generateCodeFromProgramBlock(pb);
} else //generic (last-level)
{
StatementBlock sb = current.getStatementBlock();
current.setInstructions(generateCodeFromHopDAGsToInst(sb, sb.get_hops()));
}
}
use of org.apache.sysml.runtime.controlprogram.WhileProgramBlock in project incubator-systemml by apache.
the class OptTreeConverter method rCreateOptNode.
public static OptNode rCreateOptNode(ProgramBlock pb, LocalVariableMap vars, boolean topLevel, boolean storeObjs) throws DMLRuntimeException {
OptNode node = null;
if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
node = new OptNode(NodeType.IF);
if (storeObjs)
_rtMap.putMapping(ipb, node);
node.setExecType(ExecType.CP);
//process if condition
OptNode ifn = new OptNode(NodeType.GENERIC);
node.addChilds(createOptNodes(ipb.getPredicate(), vars, storeObjs));
node.addChild(ifn);
for (ProgramBlock lpb : ipb.getChildBlocksIfBody()) ifn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
//process else condition
if (ipb.getChildBlocksElseBody() != null && ipb.getChildBlocksElseBody().size() > 0) {
OptNode efn = new OptNode(NodeType.GENERIC);
node.addChild(efn);
for (ProgramBlock lpb : ipb.getChildBlocksElseBody()) efn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
}
} else if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
node = new OptNode(NodeType.WHILE);
if (storeObjs)
_rtMap.putMapping(wpb, node);
node.setExecType(ExecType.CP);
//process predicate instruction
node.addChilds(createOptNodes(wpb.getPredicate(), vars, storeObjs));
//process body
for (ProgramBlock lpb : wpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
} else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
ForProgramBlock fpb = (ForProgramBlock) pb;
node = new OptNode(NodeType.FOR);
if (storeObjs)
_rtMap.putMapping(fpb, node);
node.setExecType(ExecType.CP);
//TODO use constant value if known
node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
//process body
for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
} else if (pb instanceof ParForProgramBlock) {
ParForProgramBlock fpb = (ParForProgramBlock) pb;
node = new OptNode(NodeType.PARFOR);
if (storeObjs)
_rtMap.putMapping(fpb, node);
node.setK(fpb.getDegreeOfParallelism());
long N = fpb.getNumIterations();
node.addParam(ParamType.NUM_ITERATIONS, (N != -1) ? String.valueOf(N) : String.valueOf(CostEstimatorRuntime.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;
default:
node.setExecType(null);
}
if (!topLevel) {
node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
}
//process body
for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, false, storeObjs));
//parameters, add required parameters
} else //last level program block
{
node = new OptNode(NodeType.GENERIC);
if (storeObjs)
_rtMap.putMapping(pb, node);
node.addChilds(createOptNodes(pb.getInstructions(), vars, storeObjs));
node.setExecType(ExecType.CP);
}
return node;
}
Aggregations