use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class SpoofCompiler method generateCodeFromStatementBlock.
public static void generateCodeFromStatementBlock(StatementBlock current) {
if (current instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) current;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock sb : fstmt.getBody()) generateCodeFromStatementBlock(sb);
} else if (current instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) current;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wsb.setPredicateHops(optimize(wsb.getPredicateHops(), false));
for (StatementBlock sb : wstmt.getBody()) generateCodeFromStatementBlock(sb);
} else if (current instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) current;
IfStatement istmt = (IfStatement) isb.getStatement(0);
isb.setPredicateHops(optimize(isb.getPredicateHops(), false));
for (StatementBlock sb : istmt.getIfBody()) generateCodeFromStatementBlock(sb);
for (StatementBlock sb : istmt.getElseBody()) generateCodeFromStatementBlock(sb);
} else if (// incl parfor
current instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) current;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
fsb.setFromHops(optimize(fsb.getFromHops(), false));
fsb.setToHops(optimize(fsb.getToHops(), false));
fsb.setIncrementHops(optimize(fsb.getIncrementHops(), false));
for (StatementBlock sb : fstmt.getBody()) generateCodeFromStatementBlock(sb);
} else // generic (last-level)
{
current.setHops(generateCodeFromHopDAGs(current.getHops()));
current.updateRecompilationFlag();
}
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class IPAPassFlagFunctionsRecompileOnce method rFlagFunctionForRecompileOnce.
/**
* Returns true if this statementblock requires recompilation inside a
* loop statement block.
*
* @param sb statement block
* @param inLoop true if in loop
* @return true if statement block requires recompilation inside a loop statement block
*/
public boolean rFlagFunctionForRecompileOnce(StatementBlock sb, boolean inLoop) {
boolean ret = false;
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock c : fstmt.getBody()) ret |= rFlagFunctionForRecompileOnce(c, inLoop);
} else if (sb instanceof WhileStatementBlock) {
// recompilation information not available at this point
// hence, mark any loop statement block
ret = true;
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
ret |= (inLoop && isb.requiresPredicateRecompilation());
for (StatementBlock c : istmt.getIfBody()) ret |= rFlagFunctionForRecompileOnce(c, inLoop);
for (StatementBlock c : istmt.getElseBody()) ret |= rFlagFunctionForRecompileOnce(c, inLoop);
} else if (sb instanceof ForStatementBlock) {
// recompilation information not available at this point
// hence, mark any loop statement block
ret = true;
} else {
ret |= (inLoop && sb.requiresRecompilation());
}
return ret;
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class IPAPassRemoveUnnecessaryCheckpoints method moveCheckpointAfterUpdate.
private static void moveCheckpointAfterUpdate(DMLProgram dmlp) {
// approach: scan over top-level program (guaranteed to be unconditional),
// collect checkpoints; determine if used before update; move first checkpoint
// after update if not used before update (best effort move which often avoids
// the second checkpoint on loops even though used in between)
HashMap<String, Hop> chkpointCand = new HashMap<>();
for (StatementBlock sb : dmlp.getStatementBlocks()) {
// prune candidates (used before updated)
Set<String> cands = new HashSet<>(chkpointCand.keySet());
for (String cand : cands) if (sb.variablesRead().containsVariable(cand) && !sb.variablesUpdated().containsVariable(cand)) {
// note: variableRead might include false positives due to meta
// data operations like nrow(X) or operations removed by rewrites
// double check hops on basic blocks; otherwise worst-case
boolean skipRemove = false;
if (sb.getHops() != null) {
Hop.resetVisitStatus(sb.getHops());
skipRemove = true;
for (Hop root : sb.getHops()) skipRemove &= !HopRewriteUtils.rContainsRead(root, cand, false);
}
if (!skipRemove)
chkpointCand.remove(cand);
}
// prune candidates (updated in conditional control flow)
Set<String> cands2 = new HashSet<>(chkpointCand.keySet());
if (sb instanceof IfStatementBlock || sb instanceof WhileStatementBlock || sb instanceof ForStatementBlock) {
for (String cand : cands2) if (sb.variablesUpdated().containsVariable(cand)) {
chkpointCand.remove(cand);
}
} else // move checkpoint after update with simple read chain
// (note: right now this only applies if the checkpoints comes from a previous
// statement block, within-dag checkpoints should be handled during injection)
{
for (String cand : cands2) if (sb.variablesUpdated().containsVariable(cand) && sb.getHops() != null) {
Hop.resetVisitStatus(sb.getHops());
for (Hop root : sb.getHops()) if (root.getName().equals(cand)) {
if (HopRewriteUtils.rHasSimpleReadChain(root, cand)) {
chkpointCand.get(cand).setRequiresCheckpoint(false);
root.getInput().get(0).setRequiresCheckpoint(true);
chkpointCand.put(cand, root.getInput().get(0));
} else
chkpointCand.remove(cand);
}
}
}
// collect checkpoints
if (HopRewriteUtils.isLastLevelStatementBlock(sb)) {
ArrayList<Hop> tmp = collectCheckpoints(sb.getHops());
for (Hop chkpoint : tmp) chkpointCand.put(chkpoint.getName(), chkpoint);
}
}
}
use of org.apache.sysml.parser.IfStatementBlock in project 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.parser.IfStatementBlock in project systemml by apache.
the class ProgramRewriter method rRewriteStatementBlockHopDAGs.
public void rRewriteStatementBlockHopDAGs(StatementBlock current, ProgramRewriteStatus state) {
// ensure robustness for calls from outside
if (state == null)
state = new ProgramRewriteStatus();
if (current instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) current;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock sb : fstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
} else if (current instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) current;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wsb.setPredicateHops(rewriteHopDAG(wsb.getPredicateHops(), state));
for (StatementBlock sb : wstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
} else if (current instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) current;
IfStatement istmt = (IfStatement) isb.getStatement(0);
isb.setPredicateHops(rewriteHopDAG(isb.getPredicateHops(), state));
for (StatementBlock sb : istmt.getIfBody()) rRewriteStatementBlockHopDAGs(sb, state);
for (StatementBlock sb : istmt.getElseBody()) rRewriteStatementBlockHopDAGs(sb, state);
} else if (// incl parfor
current instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) current;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
fsb.setFromHops(rewriteHopDAG(fsb.getFromHops(), state));
fsb.setToHops(rewriteHopDAG(fsb.getToHops(), state));
fsb.setIncrementHops(rewriteHopDAG(fsb.getIncrementHops(), state));
for (StatementBlock sb : fstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
} else // generic (last-level)
{
current.setHops(rewriteHopDAG(current.getHops(), state));
}
}
Aggregations