use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method analyzeSubProgram.
public Set<String> analyzeSubProgram(StatementBlock sb) throws HopsException, ParseException {
DMLTranslator.resetHopsDAGVisitStatus(sb);
//step 1: get candidates for statistics propagation into functions (if required)
Map<String, Integer> fcandCounts = new HashMap<String, Integer>();
Map<String, FunctionOp> fcandHops = new HashMap<String, FunctionOp>();
Map<String, Set<Long>> fcandSafeNNZ = new HashMap<String, Set<Long>>();
Set<String> allFCandKeys = new HashSet<String>();
getFunctionCandidatesForStatisticPropagation(sb, fcandCounts, fcandHops);
//cp before pruning
allFCandKeys.addAll(fcandCounts.keySet());
pruneFunctionCandidatesForStatisticPropagation(fcandCounts, fcandHops);
determineFunctionCandidatesNNZPropagation(fcandHops, fcandSafeNNZ);
DMLTranslator.resetHopsDAGVisitStatus(sb);
if (!fcandCounts.isEmpty()) {
//step 2: propagate statistics into functions and across DAGs
//(callVars used to chain outputs/inputs of multiple functions calls)
LocalVariableMap callVars = new LocalVariableMap();
propagateStatisticsAcrossBlock(sb, fcandCounts, callVars, fcandSafeNNZ, new HashSet<String>(), new HashSet<String>());
}
return fcandCounts.keySet();
}
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, boolean resetRecompile) throws HopsException, DMLRuntimeException, LopsException, IOException {
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 (//recompile all for stats propagation and recompile flags
sb != null) //&& Recompiler.requiresRecompilation( sb.get_hops() )
/*&& !Recompiler.containsNonRecompileInstructions(tmp)*/
{
tmp = Recompiler.recompileHopsDag(sb, sb.get_hops(), vars, status, true, false, tid);
pb.setInstructions(tmp);
//propagate stats across hops (should be executed on clone of vars)
Recompiler.extractDAGOutputStatistics(sb.get_hops(), vars);
//reset recompilation flags (w/ special handling functions)
if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.get_hops()) && resetRecompile) {
Hop.resetRecompilationFlag(sb.get_hops(), ExecType.CP);
sb.updateRecompilationFlag();
}
}
}
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class GDFEnumOptimizer method optimize.
@Override
public GDFGraph optimize(GDFGraph gdfgraph, Summary summary) throws DMLRuntimeException, HopsException, LopsException {
Timing time = new Timing(true);
Program prog = gdfgraph.getRuntimeProgram();
ExecutionContext ec = ExecutionContextFactory.createContext(prog);
ArrayList<GDFNode> roots = gdfgraph.getGraphRootNodes();
//Step 1: baseline costing for branch and bound costs
double initCosts = Double.MAX_VALUE;
if (BRANCH_AND_BOUND_PRUNING) {
initCosts = CostEstimationWrapper.getTimeEstimate(prog, ec);
initCosts = initCosts * (1 + BRANCH_AND_BOUND_REL_THRES);
}
//Step 2: dynamic programming plan generation
//(finally, pick optimal root plans over all interesting property sets)
ArrayList<Plan> rootPlans = new ArrayList<Plan>();
for (GDFNode node : roots) {
PlanSet ps = enumOpt(node, _memo, initCosts);
Plan optPlan = ps.getPlanWithMinCosts();
rootPlans.add(optPlan);
}
long enumPlanMismatch = getPlanMismatches();
//check for final containment of independent roots and pick optimal
HashMap<Long, Plan> memo = new HashMap<Long, Plan>();
resetPlanMismatches();
for (Plan p : rootPlans) rSetRuntimePlanConfig(p, memo);
long finalPlanMismatch = getPlanMismatches();
//generate final runtime plan (w/ optimal config)
Recompiler.recompileProgramBlockHierarchy(prog.getProgramBlocks(), new LocalVariableMap(), 0, false);
ec = ExecutionContextFactory.createContext(prog);
double optCosts = CostEstimationWrapper.getTimeEstimate(prog, ec);
//maintain optimization summary statistics
summary.setCostsInitial(initCosts);
summary.setCostsOptimal(optCosts);
summary.setNumEnumPlans(_enumeratedPlans);
summary.setNumPrunedInvalidPlans(_prunedInvalidPlans);
summary.setNumPrunedSuboptPlans(_prunedSuboptimalPlans);
summary.setNumCompiledPlans(_compiledPlans);
summary.setNumCostedPlans(_costedPlans);
summary.setNumEnumPlanMismatch(enumPlanMismatch);
summary.setNumFinalPlanMismatch(finalPlanMismatch);
summary.setTimeOptim(time.stop());
return gdfgraph;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class ScriptExecutor method createAndInitializeExecutionContext.
/**
* Create an execution context and set its variables to be the symbol table
* of the script.
*/
protected void createAndInitializeExecutionContext() {
executionContext = ExecutionContextFactory.createContext(runtimeProgram);
LocalVariableMap symbolTable = script.getSymbolTable();
if (symbolTable != null)
executionContext.setVariables(symbolTable);
// attach registered outputs (for dynamic recompile)
executionContext.getVariables().setRegisteredOutputs(new HashSet<String>(script.getOutputVariables()));
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class ScriptExecutor method rewritePersistentReadsAndWrites.
/**
* Replace persistent reads and writes with transient reads and writes in
* the symbol table.
*/
protected void rewritePersistentReadsAndWrites() {
LocalVariableMap symbolTable = script.getSymbolTable();
if (symbolTable != null) {
String[] inputs = (script.getInputVariables() == null) ? new String[0] : script.getInputVariables().toArray(new String[0]);
String[] outputs = (script.getOutputVariables() == null) ? new String[0] : script.getOutputVariables().toArray(new String[0]);
RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs, script.getSymbolTable());
ProgramRewriter programRewriter = new ProgramRewriter(rewrite);
try {
programRewriter.rewriteProgramHopDAGs(dmlProgram);
} catch (LanguageException | HopsException e) {
throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
}
}
}
Aggregations