use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ProgramRewriter method rewriteStatementBlock.
private ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) throws HopsException {
ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
ret.add(sb);
//recursive invocation
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
} else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wstmt.setBody(rewriteStatementBlocks(wstmt.getBody(), status));
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
istmt.setIfBody(rewriteStatementBlocks(istmt.getIfBody(), status));
istmt.setElseBody(rewriteStatementBlocks(istmt.getElseBody(), status));
} else if (//incl parfor
sb instanceof ForStatementBlock) {
//maintain parfor context information (e.g., for checkpointing)
boolean prestatus = status.isInParforContext();
if (sb instanceof ParForStatementBlock)
status.setInParforContext(true);
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
status.setInParforContext(prestatus);
}
//apply rewrite rules
for (StatementBlockRewriteRule r : _sbRuleSet) {
ArrayList<StatementBlock> tmp = new ArrayList<StatementBlock>();
for (StatementBlock sbc : ret) tmp.addAll(r.rewriteStatementBlock(sbc, status));
//take over set of rewritten sbs
ret.clear();
ret.addAll(tmp);
}
return ret;
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method removeRecursiveParFor.
protected int removeRecursiveParFor(OptNode n, HashSet<ParForProgramBlock> recPBs) {
int count = 0;
if (!n.isLeaf()) {
for (OptNode sub : n.getChilds()) {
if (sub.getNodeType() == NodeType.PARFOR) {
long id = sub.getID();
Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
if (recPBs.contains(pfpb)) {
// create for pb as replacement
Program prog = pfpb.getProgram();
ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
// replace parfor with for, and update objectmapping
OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
// update link to statement block
fpb.setStatementBlock(pfsb);
// update node
sub.setNodeType(NodeType.FOR);
sub.setK(1);
count++;
}
}
count += removeRecursiveParFor(sub, recPBs);
}
}
return count;
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method removeUnnecessaryParFor.
protected int removeUnnecessaryParFor(OptNode n) {
int count = 0;
if (!n.isLeaf()) {
for (OptNode sub : n.getChilds()) {
if (sub.getNodeType() == NodeType.PARFOR && sub.getK() == 1) {
long id = sub.getID();
Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
// create for pb as replacement
Program prog = pfpb.getProgram();
ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
// replace parfor with for, and update objectmapping
OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
// update link to statement block
fpb.setStatementBlock(pfsb);
// update node
sub.setNodeType(NodeType.FOR);
sub.setK(1);
count++;
}
count += removeUnnecessaryParFor(sub);
}
}
return count;
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rewriteSetSparkEagerRDDCaching.
// /////
// REWRITE set spark eager rdd caching
// /
protected void rewriteSetSparkEagerRDDCaching(OptNode n, LocalVariableMap vars) {
// get program blocks of root parfor
Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(n.getID());
ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
ArrayList<String> ret = new ArrayList<>();
if (// spark exec mode
OptimizerUtils.isSparkExecutionMode() && // local parfor
n.getExecType() == ExecType.CP && // at least 2 iterations
_N > 1) {
Set<String> cand = pfsb.variablesRead().getVariableNames();
Collection<String> rpVars = pfpb.getSparkRepartitionVariables();
for (String var : cand) {
Data dat = vars.get(var);
if (dat != null && dat instanceof MatrixObject && ((MatrixObject) dat).getRDDHandle() != null) {
MatrixObject mo = (MatrixObject) dat;
MatrixCharacteristics mc = mo.getMatrixCharacteristics();
RDDObject rdd = mo.getRDDHandle();
if (// not a repartition var
(rpVars == null || !rpVars.contains(var)) && // is cached rdd
rdd.rHasCheckpointRDDChilds() && // is out-of-core dataset
_lm / n.getK() < OptimizerUtils.estimateSizeExactSparsity(mc)) {
ret.add(var);
}
}
}
// apply rewrite to parfor pb
if (!ret.isEmpty()) {
pfpb.setSparkEagerCacheVariables(ret);
}
}
_numEvaluatedPlans++;
LOG.debug(getOptMode() + " OPT: rewrite 'set spark eager rdd caching' - result=" + ret.size() + " (" + ProgramConverter.serializeStringCollection(ret) + ")");
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method checkMRAndRecompileToCP.
/**
* Recompile program block hierarchy to forced CP if MR instructions or functions.
* Returns true if recompile was necessary and possible
*
* @param tid thread id
* @return true if recompile was necessary and possible
*/
private boolean checkMRAndRecompileToCP(long tid) {
// no MR instructions, ok
if (!OptTreeConverter.rContainsMRJobInstruction(this, true))
return false;
// no statement block, failed
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
if (sb == null) {
LOG.warn("Missing parfor statement block for recompile.");
return false;
}
// try recompile MR instructions to CP
HashSet<String> fnStack = new HashSet<>();
Recompiler.recompileProgramBlockHierarchy2Forced(_childBlocks, tid, fnStack, ExecType.CP);
return true;
}
Aggregations