use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock 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.FunctionProgramBlock in project incubator-systemml by apache.
the class Explain method explain.
public static String explain(Program rtprog) throws HopsException {
//counts number of instructions
boolean sparkExec = OptimizerUtils.isSparkExecutionMode();
ExplainCounts counts = new ExplainCounts();
countCompiledInstructions(rtprog, counts, !sparkExec, true, sparkExec);
StringBuilder sb = new StringBuilder();
//create header
sb.append("\nPROGRAM ( size CP/" + (sparkExec ? "SP" : "MR") + " = ");
sb.append(counts.numCPInst);
sb.append("/");
sb.append(counts.numJobs);
sb.append(" )\n");
//explain functions (if exists)
Map<String, FunctionProgramBlock> funcMap = rtprog.getFunctionProgramBlocks();
if (funcMap != null && !funcMap.isEmpty()) {
sb.append("--FUNCTIONS\n");
//show function call graph
if (!rtprog.getProgramBlocks().isEmpty() && rtprog.getProgramBlocks().get(0).getStatementBlock() != null) {
sb.append("----FUNCTION CALL GRAPH\n");
sb.append("------MAIN PROGRAM\n");
DMLProgram prog = rtprog.getProgramBlocks().get(0).getStatementBlock().getDMLProg();
FunctionCallGraph fgraph = new FunctionCallGraph(prog);
sb.append(explainFunctionCallGraph(fgraph, new HashSet<String>(), null, 3));
}
//show individual functions
for (Entry<String, FunctionProgramBlock> e : funcMap.entrySet()) {
String fkey = e.getKey();
FunctionProgramBlock fpb = e.getValue();
if (fpb instanceof ExternalFunctionProgramBlock)
sb.append("----EXTERNAL FUNCTION " + fkey + "\n");
else {
sb.append("----FUNCTION " + fkey + " [recompile=" + fpb.isRecompileOnce() + "]\n");
for (ProgramBlock pb : fpb.getChildBlocks()) sb.append(explainProgramBlock(pb, 3));
}
}
}
//explain main program
sb.append("--MAIN PROGRAM\n");
for (ProgramBlock pb : rtprog.getProgramBlocks()) sb.append(explainProgramBlock(pb, 2));
return sb.toString();
}
use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.
the class DMLProgram method createRuntimeProgramBlock.
public ProgramBlock createRuntimeProgramBlock(Program prog, StatementBlock sb, DMLConfig config) throws IOException, LopsException, DMLRuntimeException {
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<Lop>();
((WhileStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<Instruction>();
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);
if (rtpb.getPredicateResultVar() == null) {
// e.g case : WHILE(continue)
if (((WhileStatementBlock) sb).get_predicateLops().getExecLocation() == LopProperties.ExecLocation.Data) {
String resultVar = ((WhileStatementBlock) sb).get_predicateLops().getOutputParameters().getLabel();
rtpb.setPredicateResultVar(resultVar);
} else {
LOG.error(sb.printBlockErrorLocation() + "Error in translating the WHILE predicate.");
throw new LopsException(sb.printBlockErrorLocation() + "Error in translating the WHILE predicate.");
}
}
//// process the body of the while statement block ////
WhileStatementBlock wsb = (WhileStatementBlock) sb;
if (wsb.getNumStatements() > 1) {
LOG.error(wsb.printBlockErrorLocation() + "WhileStatementBlock should only have 1 statement");
throw new LopsException(wsb.printBlockErrorLocation() + "WhileStatementBlock should only have 1 statement");
}
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
for (StatementBlock sblock : wstmt.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 (wsb.getLops() != null && !wsb.getLops().isEmpty()) {
LOG.error(wsb.printBlockErrorLocation() + "WhileStatementBlock should have no Lops");
throw new LopsException(wsb.printBlockErrorLocation() + "WhileStatementBlock should have no Lops");
}
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.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
} else // process If Statement - add runtime program blocks to program
if (sb instanceof IfStatementBlock) {
// create DAG for loop predicates
pred_dag = new Dag<Lop>();
((IfStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<Instruction>();
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);
if (rtpb.getPredicateResultVar() == null) {
// e.g case : If(continue)
if (((IfStatementBlock) sb).get_predicateLops().getExecLocation() == LopProperties.ExecLocation.Data) {
String resultVar = ((IfStatementBlock) sb).get_predicateLops().getOutputParameters().getLabel();
rtpb.setPredicateResultVar(resultVar);
} else {
LOG.error(sb.printBlockErrorLocation() + "Error in translating the IF predicate.");
throw new LopsException(sb.printBlockErrorLocation() + "Error in translating the IF predicate.");
}
}
// process the body of the if statement block
IfStatementBlock isb = (IfStatementBlock) sb;
if (isb.getNumStatements() > 1) {
LOG.error(isb.printBlockErrorLocation() + "IfStatementBlock should have only 1 statement");
throw new LopsException(isb.printBlockErrorLocation() + "IfStatementBlock should have only 1 statement");
}
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);
}
// check there are actually Lops in to process (loop stmt body will not have any)
if (isb.getLops() != null && !isb.getLops().isEmpty()) {
LOG.error(isb.printBlockErrorLocation() + "IfStatementBlock should have no Lops");
throw new LopsException(isb.printBlockErrorLocation() + "IfStatementBlock should have no Lops");
}
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.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
} 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<Lop>();
Dag<Lop> toDag = new Dag<Lop>();
Dag<Lop> incrementDag = new Dag<Lop>();
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
String sbName = null;
ForProgramBlock rtpb = null;
IterablePredicate iterPred = fsb.getIterPredicate();
String[] iterPredData = IterablePredicate.createIterablePredicateVariables(iterPred.getIterVar().getName(), fsb.getFromLops(), fsb.getToLops(), fsb.getIncrementLops());
if (sb instanceof ParForStatementBlock) {
sbName = "ParForStatementBlock";
rtpb = new ParForProgramBlock(prog, iterPredData, iterPred.getParForParams());
ParForProgramBlock pfrtpb = (ParForProgramBlock) rtpb;
pfrtpb.setResultVariables(((ParForStatementBlock) sb).getResultVariables());
//used for optimization and creating unscoped variables
pfrtpb.setStatementBlock((ParForStatementBlock) sb);
} else //ForStatementBlock
{
sbName = "ForStatementBlock";
rtpb = new ForProgramBlock(prog, iterPredData);
}
rtpb.setFromInstructions(fromInstructions);
rtpb.setToInstructions(toInstructions);
rtpb.setIncrementInstructions(incrementInstructions);
rtpb.setIterablePredicateVars(iterPredData);
// process the body of the for statement block
if (fsb.getNumStatements() > 1) {
LOG.error(fsb.printBlockErrorLocation() + " " + sbName + " should have 1 statement");
throw new LopsException(fsb.printBlockErrorLocation() + " " + sbName + " should have 1 statement");
}
ForStatement fs = (ForStatement) fsb.getStatement(0);
for (StatementBlock sblock : fs.getBody()) {
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() + sbName + " should have no Lops");
throw new LopsException(fsb.printBlockErrorLocation() + sbName + " should have no Lops");
}
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.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
} else // process function statement block - add runtime program blocks to program
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
if (fsb.getNumStatements() > 1) {
LOG.error(fsb.printBlockErrorLocation() + "FunctionStatementBlock should only have 1 statement");
throw new LopsException(fsb.printBlockErrorLocation() + "FunctionStatementBlock should only have 1 statement");
}
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;
String scratchSpaceLoc = null;
try {
scratchSpaceLoc = config.getTextValue(DMLConfig.SCRATCH_SPACE);
} catch (Exception e) {
LOG.error(fsb.printBlockErrorLocation() + "could not retrieve parameter " + DMLConfig.SCRATCH_SPACE + " from DMLConfig");
}
StringBuilder buff = new StringBuilder();
buff.append(scratchSpaceLoc);
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.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
} else {
// handle general case
ProgramBlock rtpb = new ProgramBlock(prog);
// DAGs for Lops
dag = new Dag<Lop>();
// 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 Lobs DAGs
instruct = dag.getJobs(sb, config);
rtpb.addInstructions(instruct);
}
/*// TODO: check with Doug
// add instruction for a function call
if (sb.getFunctionCallInst() != null){
rtpb.addInstruction(sb.getFunctionCallInst());
}*/
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.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
}
return retPB;
}
use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.
the class DMLProgram method getRuntimeProgram.
public Program getRuntimeProgram(DMLConfig config) throws IOException, LanguageException, DMLRuntimeException, LopsException {
// constructor resets the set of registered functions
Program rtprog = new Program();
// for all namespaces, translate function statement blocks into function program blocks
for (String namespace : _namespaces.keySet()) {
for (String fname : getFunctionStatementBlocks(namespace).keySet()) {
// add program block to program
FunctionStatementBlock fsb = getFunctionStatementBlocks(namespace).get(fname);
FunctionProgramBlock rtpb = (FunctionProgramBlock) createRuntimeProgramBlock(rtprog, fsb, config);
rtprog.addFunctionProgramBlock(namespace, fname, rtpb);
rtpb.setRecompileOnce(fsb.isRecompileOnce());
}
}
// for each top-level block
for (StatementBlock sb : _blocks) {
// add program block to program
ProgramBlock rtpb = createRuntimeProgramBlock(rtprog, sb, config);
rtprog.addProgramBlock(rtpb);
}
return rtprog;
}
use of org.apache.sysml.runtime.controlprogram.FunctionProgramBlock in project incubator-systemml by apache.
the class PreparedScript method enableFunctionRecompile.
/**
* Enables function recompilation, selectively for the given functions.
* If dynamic recompilation is globally enabled this has no additional
* effect; otherwise the given functions are dynamically recompiled once
* on every entry but not at the granularity of individually last-level
* program blocks. Use this fine-grained recompilation option for important
* functions in small-data scenarios where dynamic recompilation overheads
* might not be amortized.
*
* @param fnamespace function namespace, null for default namespace
* @param fnames function name
*/
public void enableFunctionRecompile(String fnamespace, String... fnames) {
// handle default name space
if (fnamespace == null)
fnamespace = DMLProgram.DEFAULT_NAMESPACE;
// enable dynamic recompilation (note that this does not globally enable
// dynamic recompilation because the program has been compiled already)
CompilerConfig cconf = ConfigurationManager.getCompilerConfig();
cconf.set(ConfigType.ALLOW_DYN_RECOMPILATION, true);
ConfigurationManager.setLocalConfig(cconf);
// build function call graph (to probe for recursive functions)
FunctionCallGraph fgraph = _prog.getProgramBlocks().isEmpty() ? null : new FunctionCallGraph(_prog.getProgramBlocks().get(0).getStatementBlock().getDMLProg());
// enable requested functions for recompile once
for (String fname : fnames) {
String fkey = DMLProgram.constructFunctionKey(fnamespace, fname);
if (fgraph != null && !fgraph.isRecursiveFunction(fkey)) {
FunctionProgramBlock fpb = _prog.getFunctionProgramBlock(fnamespace, fname);
if (fpb != null)
fpb.setRecompileOnce(true);
else
LOG.warn("Failed to enable function recompile for non-existing '" + fkey + "'.");
} else if (fgraph != null) {
LOG.warn("Failed to enable function recompile for recursive '" + fkey + "'.");
}
}
}
Aggregations