use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method consolidateAndCheckResults.
private void consolidateAndCheckResults(ExecutionContext ec, long expIters, long expTasks, long numIters, long numTasks, LocalVariableMap[] results) {
Timing time = new Timing(true);
// result merge
if (checkParallelRemoteResultMerge()) {
// execute result merge in parallel for all result vars
int par = Math.min(_resultVars.size(), InfrastructureAnalyzer.getLocalParallelism());
if (InfrastructureAnalyzer.isLocalMode()) {
int parmem = (int) Math.floor(OptimizerUtils.getLocalMemBudget() / InfrastructureAnalyzer.getRemoteMaxMemorySortBuffer());
// reduce k if necessary
par = Math.min(par, Math.max(parmem, 1));
}
try {
// enqueue all result vars as tasks
LocalTaskQueue<ResultVar> q = new LocalTaskQueue<>();
for (ResultVar var : _resultVars) {
// foreach non-local write
if (// robustness scalars
ec.getVariable(var._name) instanceof MatrixObject)
q.enqueueTask(var);
}
q.closeInput();
// run result merge workers
ResultMergeWorker[] rmWorkers = new ResultMergeWorker[par];
for (int i = 0; i < par; i++) rmWorkers[i] = new ResultMergeWorker(q, results, ec);
for (// start all
int i = 0; // start all
i < par; // start all
i++) rmWorkers[i].start();
for (int i = 0; i < par; i++) {
// wait for all
rmWorkers[i].join();
if (!rmWorkers[i].finishedNoError())
throw new DMLRuntimeException("Error occured in parallel result merge worker.");
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
} else {
// execute result merge sequentially for all result vars
for (// foreach non-local write
ResultVar var : // foreach non-local write
_resultVars) {
Data dat = ec.getVariable(var._name);
if (// robustness scalars
dat instanceof MatrixObject) {
MatrixObject out = (MatrixObject) dat;
MatrixObject[] in = new MatrixObject[results.length];
for (int i = 0; i < results.length; i++) in[i] = (MatrixObject) results[i].get(var._name);
String fname = constructResultMergeFileName();
ResultMerge rm = createResultMerge(_resultMerge, out, in, fname, var._isAccum, ec);
MatrixObject outNew = null;
if (USE_PARALLEL_RESULT_MERGE)
outNew = rm.executeParallelMerge(_numThreads);
else
outNew = rm.executeSerialMerge();
// cleanup existing var
Data exdata = ec.removeVariable(var._name);
if (exdata != null && exdata != outNew && exdata instanceof MatrixObject)
ec.cleanupCacheableData((MatrixObject) exdata);
// cleanup of intermediate result variables
cleanWorkerResultVariables(ec, out, in);
// set merged result variable
ec.setVariable(var._name, outNew);
}
}
}
// handle unscoped variables (vars created in parfor, but potentially used afterwards)
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
if (// sb might be null for nested parallelism
CREATE_UNSCOPED_RESULTVARS && sb != null && ec.getVariables() != null)
createEmptyUnscopedVariables(ec.getVariables(), sb);
// check expected counters
if (// consistency check
numTasks != expTasks || numIters != expIters)
throw new DMLRuntimeException("PARFOR: Number of executed tasks does not match the number of created tasks: tasks " + numTasks + "/" + expTasks + ", iters " + numIters + "/" + expIters + ".");
if (DMLScript.STATISTICS)
Statistics.incrementParForMergeTime((long) time.stop());
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method exportMatricesToHDFS.
private void exportMatricesToHDFS(ExecutionContext ec, String... blacklistNames) {
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
Set<String> blacklist = UtilFunctions.asSet(blacklistNames);
if (LIVEVAR_AWARE_EXPORT && sb != null) {
// optimization to prevent unnecessary export of matrices
// export only variables that are read in the body
VariableSet varsRead = sb.variablesRead();
for (String key : ec.getVariables().keySet()) {
if (varsRead.containsVariable(key) && !blacklist.contains(key)) {
Data d = ec.getVariable(key);
if (d.getDataType() == DataType.MATRIX)
((MatrixObject) d).exportData(_replicationExport);
}
}
} else {
// export all matrices in symbol table
for (String key : ec.getVariables().keySet()) {
if (!blacklist.contains(key)) {
Data d = ec.getVariable(key);
if (d.getDataType() == DataType.MATRIX)
((MatrixObject) d).exportData(_replicationExport);
}
}
}
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-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.ParForStatementBlock in project incubator-systemml by apache.
the class ProgramRewriter method rRewriteStatementBlock.
public ArrayList<StatementBlock> rRewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status, boolean splitDags) {
ArrayList<StatementBlock> ret = new ArrayList<>();
ret.add(sb);
// recursive invocation
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
fstmt.setBody(rRewriteStatementBlocks(fstmt.getBody(), status, splitDags));
} else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wstmt.setBody(rRewriteStatementBlocks(wstmt.getBody(), status, splitDags));
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
istmt.setIfBody(rRewriteStatementBlocks(istmt.getIfBody(), status, splitDags));
istmt.setElseBody(rRewriteStatementBlocks(istmt.getElseBody(), status, splitDags));
} else if (sb instanceof ForStatementBlock) {
// incl parfor
// 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(rRewriteStatementBlocks(fstmt.getBody(), status, splitDags));
status.setInParforContext(prestatus);
}
// apply rewrite rules to individual statement blocks
for (StatementBlockRewriteRule r : _sbRuleSet) {
if (!splitDags && r.createsSplitDag())
continue;
ArrayList<StatementBlock> tmp = new ArrayList<>();
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 ProgramConverter method createForStatementBlockCopy.
public static ForStatementBlock createForStatementBlockCopy(ForStatementBlock sb, long pid, boolean plain, boolean forceDeepCopy) {
ForStatementBlock ret = null;
try {
if (ConfigurationManager.getCompilerConfigFlag(ConfigType.ALLOW_PARALLEL_DYN_RECOMPILATION) && sb != null && (Recompiler.requiresRecompilation(sb.getFromHops()) || Recompiler.requiresRecompilation(sb.getToHops()) || Recompiler.requiresRecompilation(sb.getIncrementHops()) || forceDeepCopy)) {
ret = (sb instanceof ParForStatementBlock) ? new ParForStatementBlock() : new ForStatementBlock();
// create new statement (shallow copy livein/liveout for recompile, line numbers for explain)
ret.setDMLProg(sb.getDMLProg());
ret.setParseInfo(sb);
ret.setLiveIn(sb.liveIn());
ret.setLiveOut(sb.liveOut());
ret.setUpdatedVariables(sb.variablesUpdated());
ret.setReadVariables(sb.variablesRead());
ret.setUpdateInPlaceVars(sb.getUpdateInPlaceVars());
// shallow copy child statements
ret.setStatements(sb.getStatements());
// deep copy predicate hops dag for concurrent recompile
if (sb.requiresFromRecompilation()) {
Hop hops = Recompiler.deepCopyHopsDag(sb.getFromHops());
ret.setFromHops(hops);
}
if (sb.requiresToRecompilation()) {
Hop hops = Recompiler.deepCopyHopsDag(sb.getToHops());
ret.setToHops(hops);
}
if (sb.requiresIncrementRecompilation()) {
Hop hops = Recompiler.deepCopyHopsDag(sb.getIncrementHops());
ret.setIncrementHops(hops);
}
ret.updatePredicateRecompilationFlags();
} else {
ret = sb;
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
return ret;
}
Aggregations