use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method analyzeSubProgram.
public Set<String> analyzeSubProgram() {
DMLTranslator.resetHopsDAGVisitStatus(_sb);
// get function call size infos to obtain candidates for statistics propagation
FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph);
// (callVars used to chain outputs/inputs of multiple functions calls)
if (!fcallSizes.getValidFunctions().isEmpty()) {
LocalVariableMap callVars = new LocalVariableMap();
propagateStatisticsAcrossBlock(_sb, callVars, fcallSizes, new HashSet<String>());
}
return fcallSizes.getValidFunctions();
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method analyzeProgram.
/**
* Main interface to perform IPA over a given DML program.
*
* @param repetitions number of IPA rounds
*/
public void analyzeProgram(int repetitions) {
// sanity check for valid number of repetitions
if (repetitions <= 0)
throw new HopsException("Invalid number of IPA repetitions: " + repetitions);
// perform number of requested IPA iterations
FunctionCallSizeInfo lastSizes = null;
for (int i = 0; i < repetitions; i++) {
if (LOG.isDebugEnabled())
LOG.debug("IPA: start IPA iteration " + (i + 1) + "/" + repetitions + ".");
// get function call size infos to obtain candidates for statistics propagation
FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph);
if (LOG.isDebugEnabled())
LOG.debug("IPA: Initial FunctionCallSummary: \n" + fcallSizes);
// step 1: intra- and inter-procedural
if (INTRA_PROCEDURAL_ANALYSIS) {
// get unary dimension-preserving non-candidate functions
for (String tmp : fcallSizes.getInvalidFunctions()) if (isUnarySizePreservingFunction(_prog.getFunctionStatementBlock(tmp)))
fcallSizes.addDimsPreservingFunction(tmp);
if (LOG.isDebugEnabled())
LOG.debug("IPA: Extended FunctionCallSummary: \n" + fcallSizes);
// propagate statistics and scalars into functions and across DAGs
// (callVars used to chain outputs/inputs of multiple functions calls)
LocalVariableMap callVars = new LocalVariableMap();
for (// propagate stats into candidates
StatementBlock sb : // propagate stats into candidates
_prog.getStatementBlocks()) propagateStatisticsAcrossBlock(sb, callVars, fcallSizes, new HashSet<String>());
}
// step 2: apply additional IPA passes
for (IPAPass pass : _passes) if (pass.isApplicable(_fgraph))
pass.rewriteProgram(_prog, _fgraph, fcallSizes);
// early abort without functions or on reached fixpoint
if (_fgraph.getReachableFunctions().isEmpty() || (lastSizes != null && lastSizes.equals(fcallSizes))) {
if (LOG.isDebugEnabled())
LOG.debug("IPA: Early abort after " + (i + 1) + "/" + repetitions + " repetitions due to reached fixpoint.");
break;
}
}
// cleanup pass: remove unused functions
FunctionCallGraph graph2 = new FunctionCallGraph(_prog);
IPAPass rmFuns = new IPAPassRemoveUnusedFunctions();
if (rmFuns.isApplicable(graph2))
rmFuns.rewriteProgram(_prog, graph2, null);
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method propagateStatisticsAcrossBlock.
// ///////////////////////////
// INTRA-PROCEDURE ANALYSIS
// ////
private void propagateStatisticsAcrossBlock(StatementBlock sb, LocalVariableMap callVars, FunctionCallSizeInfo fcallSizes, Set<String> fnStack) {
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
} else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
// old stats into predicate
propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, wsb);
// check and propagate stats into body
LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, wsb)) {
// second pass if required
propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
}
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, sb);
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
// old stats into predicate
propagateStatisticsAcrossPredicateDAG(isb.getPredicateHops(), callVars);
// check and propagate stats into body
LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
LocalVariableMap callVarsElse = (LocalVariableMap) callVars.clone();
for (StatementBlock sbi : istmt.getIfBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
for (StatementBlock sbi : istmt.getElseBody()) propagateStatisticsAcrossBlock(sbi, callVarsElse, fcallSizes, fnStack);
callVars = Recompiler.reconcileUpdatedCallVarsIf(oldCallVars, callVars, callVarsElse, isb);
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, sb);
} else if (// incl parfor
sb instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
// old stats into predicate
propagateStatisticsAcrossPredicateDAG(fsb.getFromHops(), callVars);
propagateStatisticsAcrossPredicateDAG(fsb.getToHops(), callVars);
propagateStatisticsAcrossPredicateDAG(fsb.getIncrementHops(), callVars);
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, fsb);
// check and propagate stats into body
LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, fsb))
for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, sb);
} else // generic (last-level)
{
// remove updated constant scalars
Recompiler.removeUpdatedScalars(callVars, sb);
// old stats in, new stats out if updated
ArrayList<Hop> roots = sb.getHops();
DMLProgram prog = sb.getDMLProg();
// replace scalar reads with literals
Hop.resetVisitStatus(roots);
propagateScalarsAcrossDAG(roots, callVars);
// refresh stats across dag
Hop.resetVisitStatus(roots);
propagateStatisticsAcrossDAG(roots, callVars);
// propagate stats into function calls
Hop.resetVisitStatus(roots);
propagateStatisticsIntoFunctions(prog, roots, callVars, fcallSizes, fnStack);
}
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class InterProceduralAnalysis method isUnarySizePreservingFunction.
private boolean isUnarySizePreservingFunction(FunctionStatementBlock fsb) {
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
// check unary functions over matrices
boolean ret = (fstmt.getInputParams().size() == 1 && fstmt.getInputParams().get(0).getDataType() == DataType.MATRIX && fstmt.getOutputParams().size() == 1 && fstmt.getOutputParams().get(0).getDataType() == DataType.MATRIX);
// check size-preserving characteristic
if (ret) {
FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph, false);
HashSet<String> fnStack = new HashSet<>();
LocalVariableMap callVars = new LocalVariableMap();
// populate input
MatrixObject mo = createOutputMatrix(7777, 3333, -1);
callVars.put(fstmt.getInputParams().get(0).getName(), mo);
// propagate statistics
for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
// compare output
MatrixObject mo2 = (MatrixObject) callVars.get(fstmt.getOutputParams().get(0).getName());
ret &= mo.getNumRows() == mo2.getNumRows() && mo.getNumColumns() == mo2.getNumColumns();
// reset function
mo.getMatrixCharacteristics().setDimension(-1, -1);
for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
}
return ret;
}
use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.
the class CachedReuseVariables method reuseVariables.
public synchronized void reuseVariables(long pfid, LocalVariableMap vars, Collection<String> blacklist) {
// check for existing reuse map
LocalVariableMap tmp = null;
if (_data.containsKey(pfid))
tmp = _data.get(pfid).get();
// build reuse map if not created yet or evicted
if (tmp == null) {
tmp = new LocalVariableMap(vars);
tmp.removeAllIn((blacklist instanceof HashSet) ? (HashSet<String>) blacklist : new HashSet<>(blacklist));
_data.put(pfid, new SoftReference<>(tmp));
} else // reuse existing reuse map
{
for (String varName : tmp.keySet()) vars.put(varName, tmp.get(varName));
}
}
Aggregations