use of org.apache.sysml.parser.IfStatementBlock in project incubator-systemml by apache.
the class ResourceOptimizer method recompileProgramBlock.
private static void recompileProgramBlock(ProgramBlock pb, long cp, long mr) {
// init compiler memory budget
InfrastructureAnalyzer.setLocalMaxMemory(cp);
InfrastructureAnalyzer.setRemoteMaxMemoryMap(mr);
InfrastructureAnalyzer.setRemoteMaxMemoryReduce(mr);
// dependent on cp, mr
OptimizerUtils.resetDefaultSize();
// recompile instructions (incl predicates)
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
wpb.setPredicate(inst);
}
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock sb = (IfStatementBlock) ipb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
ipb.setPredicate(inst);
}
} else if (pb instanceof ForProgramBlock) {
// incl parfor
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock sb = (ForStatementBlock) fpb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null) {
if (sb.getFromHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getFromHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setFromInstructions(inst);
}
if (sb.getToHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getToHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setToInstructions(inst);
}
if (sb.getIncrementHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getIncrementHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setIncrementInstructions(inst);
}
}
} else {
// last-level program blocks
StatementBlock sb = pb.getStatementBlock();
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb, sb.getHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
pb.setInstructions(inst);
}
_cntCompilePB++;
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class Explain method explainStatementBlock.
private static String explainStatementBlock(StatementBlock sb, int level) {
StringBuilder builder = new StringBuilder();
String offset = createOffset(level);
if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
builder.append(offset);
if (!wsb.getUpdateInPlaceVars().isEmpty())
builder.append("WHILE (lines " + wsb.getBeginLine() + "-" + wsb.getEndLine() + ") [in-place=" + wsb.getUpdateInPlaceVars().toString() + "]\n");
else
builder.append("WHILE (lines " + wsb.getBeginLine() + "-" + wsb.getEndLine() + ")\n");
builder.append(explainHop(wsb.getPredicateHops(), level + 1));
WhileStatement ws = (WhileStatement) sb.getStatement(0);
for (StatementBlock current : ws.getBody()) builder.append(explainStatementBlock(current, level + 1));
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock ifsb = (IfStatementBlock) sb;
builder.append(offset);
builder.append("IF (lines " + ifsb.getBeginLine() + "-" + ifsb.getEndLine() + ")\n");
builder.append(explainHop(ifsb.getPredicateHops(), level + 1));
IfStatement ifs = (IfStatement) sb.getStatement(0);
for (StatementBlock current : ifs.getIfBody()) builder.append(explainStatementBlock(current, level + 1));
if (!ifs.getElseBody().isEmpty()) {
builder.append(offset);
builder.append("ELSE\n");
}
for (StatementBlock current : ifs.getElseBody()) builder.append(explainStatementBlock(current, level + 1));
} else if (sb instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) sb;
builder.append(offset);
if (sb instanceof ParForStatementBlock) {
if (!fsb.getUpdateInPlaceVars().isEmpty())
builder.append("PARFOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ") [in-place=" + fsb.getUpdateInPlaceVars().toString() + "]\n");
else
builder.append("PARFOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ")\n");
} else {
if (!fsb.getUpdateInPlaceVars().isEmpty())
builder.append("FOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ") [in-place=" + fsb.getUpdateInPlaceVars().toString() + "]\n");
else
builder.append("FOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ")\n");
}
if (fsb.getFromHops() != null)
builder.append(explainHop(fsb.getFromHops(), level + 1));
if (fsb.getToHops() != null)
builder.append(explainHop(fsb.getToHops(), level + 1));
if (fsb.getIncrementHops() != null)
builder.append(explainHop(fsb.getIncrementHops(), level + 1));
ForStatement fs = (ForStatement) sb.getStatement(0);
for (StatementBlock current : fs.getBody()) builder.append(explainStatementBlock(current, level + 1));
} else if (sb instanceof FunctionStatementBlock) {
FunctionStatement fsb = (FunctionStatement) sb.getStatement(0);
for (StatementBlock current : fsb.getBody()) builder.append(explainStatementBlock(current, level + 1));
} else {
// For generic StatementBlock
builder.append(offset);
builder.append("GENERIC (lines " + sb.getBeginLine() + "-" + sb.getEndLine() + ") [recompile=" + sb.requiresRecompilation() + "]\n");
ArrayList<Hop> hopsDAG = sb.getHops();
if (hopsDAG != null && !hopsDAG.isEmpty()) {
Hop.resetVisitStatus(hopsDAG);
for (Hop hop : hopsDAG) builder.append(explainHop(hop, level + 1));
Hop.resetVisitStatus(hopsDAG);
}
}
return builder.toString();
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class ResourceOptimizer method recompileProgramBlock.
private static void recompileProgramBlock(ProgramBlock pb, long cp, long mr) {
// init compiler memory budget
InfrastructureAnalyzer.setLocalMaxMemory(cp);
InfrastructureAnalyzer.setRemoteMaxMemoryMap(mr);
InfrastructureAnalyzer.setRemoteMaxMemoryReduce(mr);
// dependent on cp, mr
OptimizerUtils.resetDefaultSize();
// recompile instructions (incl predicates)
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
wpb.setPredicate(inst);
}
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock sb = (IfStatementBlock) ipb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
ipb.setPredicate(inst);
}
} else if (pb instanceof ForProgramBlock) {
// incl parfor
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock sb = (ForStatementBlock) fpb.getStatementBlock();
if (INCLUDE_PREDICATES && sb != null) {
if (sb.getFromHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getFromHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setFromInstructions(inst);
}
if (sb.getToHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getToHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setToInstructions(inst);
}
if (sb.getIncrementHops() != null) {
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getIncrementHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
fpb.setIncrementInstructions(inst);
}
}
} else {
// last-level program blocks
StatementBlock sb = pb.getStatementBlock();
ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb, sb.getHops(), new LocalVariableMap(), null, false, false, 0);
inst = annotateMRJobInstructions(inst, cp, mr);
pb.setInstructions(inst);
}
_cntCompilePB++;
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class RewriteCompressedReblock method rAnalyzeProgram.
private static void rAnalyzeProgram(StatementBlock sb, ProbeStatus status) {
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock csb : fstmt.getBody()) rAnalyzeProgram(csb, status);
} else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
for (StatementBlock csb : wstmt.getBody()) rAnalyzeProgram(csb, status);
if (wsb.variablesRead().containsAnyName(status.compMtx))
status.usedInLoop = true;
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
for (StatementBlock csb : istmt.getIfBody()) rAnalyzeProgram(csb, status);
for (StatementBlock csb : istmt.getElseBody()) rAnalyzeProgram(csb, status);
if (isb.variablesUpdated().containsAnyName(status.compMtx))
status.condUpdate = true;
} else if (sb instanceof ForStatementBlock) {
// incl parfor
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
for (StatementBlock csb : fstmt.getBody()) rAnalyzeProgram(csb, status);
if (fsb.variablesRead().containsAnyName(status.compMtx))
status.usedInLoop = true;
} else if (sb.getHops() != null) {
// generic (last-level)
ArrayList<Hop> roots = sb.getHops();
Hop.resetVisitStatus(roots);
// process entire HOP DAG starting from the roots
for (Hop root : roots) rAnalyzeHopDag(root, status);
// remove temporary variables
status.compMtx.removeIf(n -> n.startsWith(TMP_PREFIX));
Hop.resetVisitStatus(roots);
}
}
use of org.apache.sysml.parser.IfStatementBlock in project systemml by apache.
the class RewriteForLoopVectorization method rewriteStatementBlock.
@Override
public List<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus state) {
if (sb instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fs = (ForStatement) fsb.getStatement(0);
Hop from = fsb.getFromHops();
Hop to = fsb.getToHops();
Hop incr = fsb.getIncrementHops();
String iterVar = fsb.getIterPredicate().getIterVar().getName();
if (// single child block
fs.getBody() != null && fs.getBody().size() == 1) {
StatementBlock csb = (StatementBlock) fs.getBody().get(0);
if (!(// last level block
csb instanceof WhileStatementBlock || csb instanceof IfStatementBlock || csb instanceof ForStatementBlock)) {
// AUTO VECTORIZATION PATTERNS
// Note: unnecessary row or column indexing then later removed via hop rewrites
// e.g., for(i in a:b){s = s + as.scalar(X[i,2])} -> s = sum(X[a:b,2])
sb = vectorizeScalarAggregate(sb, csb, from, to, incr, iterVar);
// e.g., for(i in a:b){X[i,2] = Y[i,1] + Z[i,3]} -> X[a:b,2] = Y[a:b,1] + Z[a:b,3];
sb = vectorizeElementwiseBinary(sb, csb, from, to, incr, iterVar);
// e.g., for(i in a:b){X[i,2] = abs(Y[i,1])} -> X[a:b,2] = abs(Y[a:b,1]);
sb = vectorizeElementwiseUnary(sb, csb, from, to, incr, iterVar);
// e.g., for(i in a:b){X[7,i] = Y[1,i]} -> X[7,a:b] = Y[1,a:b];
sb = vectorizeIndexedCopy(sb, csb, from, to, incr, iterVar);
}
}
}
// that includes the equivalent vectorized operations.
return Arrays.asList(sb);
}
Aggregations