use of org.apache.sysml.parser.IfStatement 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
*/
public static void rFindAndRecompileIndexingHOP(StatementBlock sb, ProgramBlock pb, String var, ExecutionContext ec, boolean force) {
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.getHops());
if (force) {
// set forced execution type
for (Hop h : sb.getHops()) ret |= rFindAndSetCPIndexingHOP(h, var);
} else {
// release forced execution type
for (Hop h : sb.getHops()) ret |= rFindAndReleaseIndexingHOP(h, var);
}
// recompilation on-demand
if (ret) {
// construct new instructions
ArrayList<Instruction> newInst = Recompiler.recompileHopsDag(sb, sb.getHops(), ec.getVariables(), null, true, false, 0);
pb.setInstructions(newInst);
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
}
}
use of org.apache.sysml.parser.IfStatement in project incubator-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.IfStatement in project incubator-systemml by apache.
the class IPAPassPropagateReplaceLiterals method rReplaceLiterals.
private void rReplaceLiterals(StatementBlock sb, LocalVariableMap constants) {
// remove updated literals
for (String varname : sb.variablesUpdated().getVariableNames()) if (constants.keySet().contains(varname))
constants.remove(varname);
// propagate and replace literals
if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement ws = (WhileStatement) sb.getStatement(0);
replaceLiterals(wsb.getPredicateHops(), constants);
for (StatementBlock current : ws.getBody()) rReplaceLiterals(current, constants);
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement ifs = (IfStatement) sb.getStatement(0);
replaceLiterals(isb.getPredicateHops(), constants);
for (StatementBlock current : ifs.getIfBody()) rReplaceLiterals(current, constants);
for (StatementBlock current : ifs.getElseBody()) rReplaceLiterals(current, constants);
} else if (sb instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fs = (ForStatement) sb.getStatement(0);
replaceLiterals(fsb.getFromHops(), constants);
replaceLiterals(fsb.getToHops(), constants);
replaceLiterals(fsb.getIncrementHops(), constants);
for (StatementBlock current : fs.getBody()) rReplaceLiterals(current, constants);
} else {
replaceLiterals(sb.getHops(), constants);
}
}
use of org.apache.sysml.parser.IfStatement in project incubator-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));
}
}
use of org.apache.sysml.parser.IfStatement in project incubator-systemml by apache.
the class DmlSyntacticValidator method exitIfStatement.
@Override
public void exitIfStatement(IfStatementContext ctx) {
IfStatement ifStmt = new IfStatement();
ConditionalPredicate predicate = new ConditionalPredicate(ctx.predicate.info.expr);
ifStmt.setConditionalPredicate(predicate);
ifStmt.setCtxValuesAndFilename(ctx, currentFile);
if (ctx.ifBody.size() > 0) {
for (StatementContext stmtCtx : ctx.ifBody) {
ifStmt.addStatementBlockIfBody(getStatementBlock(stmtCtx.info.stmt));
}
ifStmt.mergeStatementBlocksIfBody();
}
if (ctx.elseBody.size() > 0) {
for (StatementContext stmtCtx : ctx.elseBody) {
ifStmt.addStatementBlockElseBody(getStatementBlock(stmtCtx.info.stmt));
}
ifStmt.mergeStatementBlocksElseBody();
}
ctx.info.stmt = ifStmt;
setFileLineColumn(ctx.info.stmt, ctx);
}
Aggregations