use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method propagateStatisticsIntoFunctions.
/**
* Propagate statistics from the calling program into a function
* block.
*
* @param prog The DML program.
* @param hop HOP to propagate statistics into.
* @param callVars Calling program's map of variables eligible for propagation.
* @param fcallSizes function call summary
* @param fnStack Function stack to determine current scope.
*/
private void propagateStatisticsIntoFunctions(DMLProgram prog, Hop hop, LocalVariableMap callVars, FunctionCallSizeInfo fcallSizes, Set<String> fnStack) {
if (hop.isVisited())
return;
for (Hop c : hop.getInput()) propagateStatisticsIntoFunctions(prog, c, callVars, fcallSizes, fnStack);
if (hop instanceof FunctionOp) {
// maintain counters and investigate functions if not seen so far
FunctionOp fop = (FunctionOp) hop;
String fkey = fop.getFunctionKey();
if (fop.getFunctionType() == FunctionType.DML) {
FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fop.getFunctionNamespace(), fop.getFunctionName());
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
if (fcallSizes.isValidFunction(fkey) && // prevent recursion
!fnStack.contains(fkey)) {
// maintain function call stack
fnStack.add(fkey);
// create mapping and populate symbol table for refresh
LocalVariableMap tmpVars = new LocalVariableMap();
populateLocalVariableMapForFunctionCall(fstmt, fop, callVars, tmpVars, fcallSizes);
// recursively propagate statistics
propagateStatisticsAcrossBlock(fsb, tmpVars, fcallSizes, fnStack);
// extract vars from symbol table, re-map and refresh main program
extractFunctionCallReturnStatistics(fstmt, fop, tmpVars, callVars, true);
// maintain function call stack
fnStack.remove(fkey);
} else if (fcallSizes.isDimsPreservingFunction(fkey)) {
extractFunctionCallEquivalentReturnStatistics(fstmt, fop, callVars);
} else {
extractFunctionCallUnknownReturnStatistics(fstmt, fop, callVars);
}
} else if (fop.getFunctionType() == FunctionType.EXTERNAL_FILE || fop.getFunctionType() == FunctionType.EXTERNAL_MEM) {
// infer output size for known external functions
FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fop.getFunctionNamespace(), fop.getFunctionName());
ExternalFunctionStatement fstmt = (ExternalFunctionStatement) fsb.getStatement(0);
if (PROPAGATE_KNOWN_UDF_STATISTICS)
extractExternalFunctionCallReturnStatistics(fstmt, fop, callVars);
else
extractFunctionCallUnknownReturnStatistics(fstmt, fop, callVars);
}
}
hop.setVisited();
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class Recompiler method rRecompileProgramBlock.
// ////////////////////////////
// private helper functions //
// ////////////////////////////
private static void rRecompileProgramBlock(ProgramBlock pb, LocalVariableMap vars, RecompileStatus status, long tid, ResetType resetRecompile) {
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
// recompile predicate
recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
// remove updated scalars because in loop
removeUpdatedScalars(vars, wsb);
// copy vars for later compare
LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
RecompileStatus oldStatus = (RecompileStatus) status.clone();
for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
if (reconcileUpdatedCallVarsLoops(oldVars, vars, wsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, wsb)) {
// second pass with unknowns if required
recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
}
removeUpdatedScalars(vars, wsb);
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
// recompile predicate
recompileIfPredicate(ipb, isb, vars, status, tid, resetRecompile);
// copy vars for later compare
LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
LocalVariableMap varsElse = (LocalVariableMap) vars.clone();
RecompileStatus oldStatus = (RecompileStatus) status.clone();
RecompileStatus statusElse = (RecompileStatus) status.clone();
for (ProgramBlock pb2 : ipb.getChildBlocksIfBody()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
for (ProgramBlock pb2 : ipb.getChildBlocksElseBody()) rRecompileProgramBlock(pb2, varsElse, statusElse, tid, resetRecompile);
reconcileUpdatedCallVarsIf(oldVars, vars, varsElse, isb);
reconcileUpdatedCallVarsIf(oldStatus, status, statusElse, isb);
removeUpdatedScalars(vars, ipb.getStatementBlock());
} else if (// includes ParFORProgramBlock
pb instanceof ForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
// recompile predicates
recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
// remove updated scalars because in loop
removeUpdatedScalars(vars, fpb.getStatementBlock());
// copy vars for later compare
LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
RecompileStatus oldStatus = (RecompileStatus) status.clone();
for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
if (reconcileUpdatedCallVarsLoops(oldVars, vars, fsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, fsb)) {
// second pass with unknowns if required
recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
}
removeUpdatedScalars(vars, fpb.getStatementBlock());
} else if (// includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP
pb instanceof FunctionProgramBlock) {
// do nothing
} else {
StatementBlock sb = pb.getStatementBlock();
ArrayList<Instruction> tmp = pb.getInstructions();
if (sb == null)
return;
// recompile all for stats propagation and recompile flags
tmp = Recompiler.recompileHopsDag(sb, sb.getHops(), vars, status, true, false, tid);
pb.setInstructions(tmp);
// propagate stats across hops (should be executed on clone of vars)
Recompiler.extractDAGOutputStatistics(sb.getHops(), vars);
// reset recompilation flags (w/ special handling functions)
if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.getHops()) && resetRecompile.isReset()) {
Hop.resetRecompilationFlag(sb.getHops(), ExecType.CP, resetRecompile);
sb.updateRecompilationFlag();
}
}
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class ResourceOptimizer method recompileProgramBlock.
private static void recompileProgramBlock(ProgramBlock pb, long cp, long mr) {
// init compiler memory budget
InfrastructureAnalyzer.setLocalMaxMemory(cp);
InfrastructureAnalyzer.setRemoteMaxMemoryMap(mr);
InfrastructureAnalyzer.setRemoteMaxMemoryReduce(mr);
// dependent on cp, mr
OptimizerUtils.resetDefaultSize();
// recompile instructions (incl predicates)
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
wpb.setPredicate(inst);
}
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock sb = (IfStatementBlock) ipb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
ipb.setPredicate(inst);
}
} else if (pb instanceof ForProgramBlock) {
// incl parfor
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock sb = (ForStatementBlock) fpb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null) {
if (sb.getFromHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getFromHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setFromInstructions(inst);
}
if (sb.getToHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getToHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setToInstructions(inst);
}
if (sb.getIncrementHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getIncrementHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setIncrementInstructions(inst);
}
}
} else {
// last-level program blocks
StatementBlock sb = pb.getStatementBlock();
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb, sb.getHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
pb.setInstructions(inst);
}
_cntCompilePB++;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class CostEstimationWrapper method getTimeEstimate.
public static double getTimeEstimate(Program rtprog, ExecutionContext ec) {
Timing time = new Timing(true);
HashMap<String, VarStats> stats = new HashMap<>();
LocalVariableMap vars = (ec != null) ? ec.getVariables() : new LocalVariableMap();
double costs = _costEstim.getTimeEstimate(rtprog, vars, stats);
LOG.debug("Finished estimation in " + time.stop() + "ms.");
return costs;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class IPAPassPropagateReplaceLiterals method rewriteProgram.
@Override
public void rewriteProgram(DMLProgram prog, FunctionCallGraph fgraph, FunctionCallSizeInfo fcallSizes) {
for (String fkey : fgraph.getReachableFunctions()) {
FunctionOp first = fgraph.getFunctionCalls(fkey).get(0);
// propagate and replace amenable literals into function
if (fcallSizes.hasSafeLiterals(fkey)) {
FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fkey);
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
ArrayList<DataIdentifier> finputs = fstmt.getInputParams();
// populate call vars with amenable literals
LocalVariableMap callVars = new LocalVariableMap();
for (int j = 0; j < finputs.size(); j++) if (fcallSizes.isSafeLiteral(fkey, j)) {
LiteralOp lit = (LiteralOp) first.getInput().get(j);
callVars.put(finputs.get(j).getName(), ScalarObjectFactory.createScalarObject(lit.getValueType(), lit));
}
// propagate and replace literals
for (StatementBlock sb : fstmt.getBody()) rReplaceLiterals(sb, callVars);
}
}
}
Aggregations