use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rewriteInjectSparkRepartition.
// /////
// REWRITE inject spark repartition for zipmm
// /
protected void rewriteInjectSparkRepartition(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) {
// collect candidates from zipmm spark instructions
HashSet<String> cand = new HashSet<>();
rCollectZipmmPartitioningCandidates(n, cand);
// prune updated candidates
HashSet<String> probe = new HashSet<>(pfsb.getReadOnlyParentVars());
for (String var : cand) if (probe.contains(var))
ret.add(var);
// prune small candidates
ArrayList<String> tmp = new ArrayList<>(ret);
ret.clear();
for (String var : tmp) if (vars.get(var) instanceof MatrixObject) {
MatrixObject mo = (MatrixObject) vars.get(var);
double sp = OptimizerUtils.getSparsity(mo.getNumRows(), mo.getNumColumns(), mo.getNnz());
double size = OptimizerUtils.estimateSizeExactSparsity(mo.getNumRows(), mo.getNumColumns(), sp);
if (size > OptimizerUtils.getLocalMemBudget())
ret.add(var);
}
// apply rewrite to parfor pb
if (!ret.isEmpty()) {
pfpb.setSparkRepartitionVariables(ret);
}
}
_numEvaluatedPlans++;
LOG.debug(getOptMode() + " OPT: rewrite 'inject spark input repartition' - result=" + ret.size() + " (" + ProgramConverter.serializeStringCollection(ret) + ")");
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method rewriteSetDataPartitioner.
// /////
// REWRITE set data partitioner
// /
protected boolean rewriteSetDataPartitioner(OptNode n, LocalVariableMap vars, HashMap<String, PartitionFormat> partitionedMatrices, double thetaM) {
if (n.getNodeType() != NodeType.PARFOR)
LOG.warn(getOptMode() + " OPT: Data partitioner can only be set for a ParFor node.");
boolean blockwise = false;
// preparations
long id = n.getID();
Object[] o = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
ParForStatementBlock pfsb = (ParForStatementBlock) o[0];
ParForProgramBlock pfpb = (ParForProgramBlock) o[1];
// search for candidates
boolean apply = false;
if (// only if we are allowed to recompile
OptimizerUtils.isHybridExecutionMode() && // only if beneficial wrt problem size
(_N >= PROB_SIZE_THRESHOLD_PARTITIONING || _Nmax >= PROB_SIZE_THRESHOLD_PARTITIONING)) {
HashMap<String, PartitionFormat> cand2 = new HashMap<>();
for (String c : pfsb.getReadOnlyParentVars()) {
PartitionFormat dpf = pfsb.determineDataPartitionFormat(c);
if (dpf != PartitionFormat.NONE && dpf._dpf != PDataPartitionFormat.BLOCK_WISE_M_N) {
cand2.put(c, dpf);
}
}
apply = rFindDataPartitioningCandidates(n, cand2, vars, thetaM);
if (apply)
partitionedMatrices.putAll(cand2);
}
PDataPartitioner REMOTE = OptimizerUtils.isSparkExecutionMode() ? PDataPartitioner.REMOTE_SPARK : PDataPartitioner.REMOTE_MR;
PDataPartitioner pdp = (apply) ? REMOTE : PDataPartitioner.NONE;
// NOTE: since partitioning is only applied in case of MR index access, we assume a large
// matrix and hence always apply REMOTE_MR (the benefit for large matrices outweigths
// potentially unnecessary MR jobs for smaller matrices)
// modify rtprog
pfpb.setDataPartitioner(pdp);
// modify plan
n.addParam(ParamType.DATA_PARTITIONER, pdp.toString());
_numEvaluatedPlans++;
LOG.debug(getOptMode() + " OPT: rewrite 'set data partitioner' - result=" + pdp.toString() + " (" + ProgramConverter.serializeStringCollection(partitionedMatrices.keySet()) + ")");
return blockwise;
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method executeRemoteMRParForDP.
private void executeRemoteMRParForDP(ExecutionContext ec, IntObject itervar, IntObject from, IntObject to, IntObject incr) throws IOException {
/* Step 0) check and recompile MR inst
* Step 1) serialize child PB and inst
* Step 2) create and serialize tasks
* Step 3) submit MR Jobs and wait for results
* Step 4) collect results from each parallel worker
*/
Timing time = (_monitor ? new Timing(true) : null);
// Step 0) check and compile to CP (if forced remote parfor)
boolean flagForced = checkMRAndRecompileToCP(0);
// Step 1) prepare partitioned input matrix (needs to happen before serializing the program)
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
MatrixObject inputMatrix = ec.getMatrixObject(_colocatedDPMatrix);
PartitionFormat inputDPF = sb.determineDataPartitionFormat(_colocatedDPMatrix);
// mark matrix var as partitioned
inputMatrix.setPartitioned(inputDPF._dpf, inputDPF._N);
// Step 2) init parallel workers (serialize PBs)
// NOTES: each mapper changes filenames with regard to his ID as we submit a single
// job, cannot reuse serialized string, since variables are serialized as well.
ParForBody body = new ParForBody(_childBlocks, _resultVars, ec);
String program = ProgramConverter.serializeParForBody(body);
if (_monitor)
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_INIT_PARWRK_T, time.stop());
// Step 3) create tasks
TaskPartitioner partitioner = createTaskPartitioner(from, to, incr);
String resultFile = constructResultFileName();
long numIterations = partitioner.getNumIterations();
// partitioner.createTasks().size();
long numCreatedTasks = numIterations;
if (_monitor)
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_INIT_TASKS_T, time.stop());
// write matrices to HDFS
exportMatricesToHDFS(ec);
// Step 4) submit MR job (wait for finished work)
OutputInfo inputOI = ((inputMatrix.getSparsity() < 0.1 && inputDPF == PartitionFormat.COLUMN_WISE) || (inputMatrix.getSparsity() < 0.001 && inputDPF == PartitionFormat.ROW_WISE)) ? OutputInfo.BinaryCellOutputInfo : OutputInfo.BinaryBlockOutputInfo;
RemoteParForJobReturn ret = RemoteDPParForMR.runJob(_ID, _iterPredVar, _colocatedDPMatrix, program, resultFile, inputMatrix, inputDPF, inputOI, _tSparseCol, _enableCPCaching, _numThreads, _replicationDP);
if (_monitor)
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_WAIT_EXEC_T, time.stop());
// Step 5) collecting results from each parallel worker
int numExecutedTasks = ret.getNumExecutedTasks();
int numExecutedIterations = ret.getNumExecutedIterations();
// consolidate results into global symbol table
consolidateAndCheckResults(ec, numIterations, numCreatedTasks, numExecutedIterations, numExecutedTasks, ret.getVariables());
if (// see step 0
flagForced)
releaseForcedRecompile(0);
inputMatrix.unsetPartitioned();
if (_monitor) {
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_WAIT_RESULTS_T, time.stop());
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_NUMTASKS, numExecutedTasks);
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_NUMITERS, numExecutedIterations);
}
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method getMinMemory.
private long getMinMemory(ExecutionContext ec) {
long ret = -1;
// if forced remote exec and single node
if (DMLScript.rtplatform == RUNTIME_PLATFORM.SINGLE_NODE && _execMode == PExecMode.REMOTE_MR && _optMode == POptMode.NONE) {
try {
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
OptTree tree = OptTreeConverter.createAbstractOptTree(-1, -1, sb, this, new HashSet<String>(), ec);
CostEstimator est = new CostEstimatorHops(OptTreeConverter.getAbstractPlanMapping());
double mem = est.getEstimate(TestMeasure.MEMORY_USAGE, tree.getRoot());
ret = (long) (mem * (1d / OptimizerUtils.MEM_UTIL_FACTOR));
} catch (Exception e) {
LOG.error("Failed to analyze minmum memory requirements.", e);
}
}
return ret;
}
use of org.apache.sysml.parser.ParForStatementBlock in project incubator-systemml by apache.
the class ParForProgramBlock method execute.
@Override
public void execute(ExecutionContext ec) {
ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
// evaluate from, to, incr only once (assumption: known at for entry)
IntObject from = executePredicateInstructions(1, _fromInstructions, ec);
IntObject to = executePredicateInstructions(2, _toInstructions, ec);
IntObject incr = (_incrementInstructions == null || _incrementInstructions.isEmpty()) ? new IntObject((from.getLongValue() <= to.getLongValue()) ? 1 : -1) : executePredicateInstructions(3, _incrementInstructions, ec);
if (// would produce infinite loop
incr.getLongValue() == 0)
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Expression for increment " + "of variable '" + _iterPredVar + "' must evaluate to a non-zero value.");
// early exit on num iterations = zero
_numIterations = computeNumIterations(from, to, incr);
if (_numIterations <= 0)
// avoid unnecessary optimization/initialization
return;
// /////
if (_optMode != POptMode.NONE) {
// set optimizer log level
OptimizationWrapper.setLogLevel(_optLogLevel);
// core optimize
OptimizationWrapper.optimize(_optMode, sb, this, ec, _monitor);
}
// /////
// DATA PARTITIONING of read-only parent variables of type (matrix,unpartitioned)
// /////
Timing time = _monitor ? new Timing(true) : null;
// partitioning on demand (note: for fused data partitioning and execute the optimizer set
// the data partitioner to NONE in order to prevent any side effects)
handleDataPartitioning(ec);
// repartitioning of variables for spark cpmm/zipmm in order prevent unnecessary shuffle
handleSparkRepartitioning(ec);
// eager rdd caching of variables for spark in order prevent read/write contention
handleSparkEagerCaching(ec);
if (_monitor)
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_INIT_DATA_T, time.stop());
// initialize iter var to form value
IntObject iterVar = new IntObject(from.getLongValue());
// /////
// begin PARALLEL EXECUTION of (PAR)FOR body
// /////
LOG.trace("EXECUTE PARFOR ID = " + _ID + " with mode = " + _execMode + ", numThreads = " + _numThreads + ", taskpartitioner = " + _taskPartitioner);
if (_monitor) {
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_NUMTHREADS, _numThreads);
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_TASKSIZE, _taskSize);
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_TASKPARTITIONER, _taskPartitioner.ordinal());
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_DATAPARTITIONER, _dataPartitioner.ordinal());
StatisticMonitor.putPFStat(_ID, Stat.PARFOR_EXECMODE, _execMode.ordinal());
}
// preserve shared input/result variables of cleanup
ArrayList<String> varList = ec.getVarList();
boolean[] varState = ec.pinVariables(varList);
try {
switch(_execMode) {
case // create parworkers as local threads
LOCAL:
executeLocalParFor(ec, iterVar, from, to, incr);
break;
case // create parworkers as MR tasks (one job per parfor)
REMOTE_MR:
executeRemoteMRParFor(ec, iterVar, from, to, incr);
break;
case // create parworkers as MR tasks (one job per parfor)
REMOTE_MR_DP:
executeRemoteMRParForDP(ec, iterVar, from, to, incr);
break;
case // create parworkers as Spark tasks (one job per parfor)
REMOTE_SPARK:
executeRemoteSparkParFor(ec, iterVar, from, to, incr);
break;
case // create parworkers as Spark tasks (one job per parfor)
REMOTE_SPARK_DP:
executeRemoteSparkParForDP(ec, iterVar, from, to, incr);
break;
default:
throw new DMLRuntimeException("Undefined execution mode: '" + _execMode + "'.");
}
} catch (Exception ex) {
throw new DMLRuntimeException("PARFOR: Failed to execute loop in parallel.", ex);
}
// reset state of shared input/result variables
ec.unpinVariables(varList, varState);
// cleanup unpinned shared variables
cleanupSharedVariables(ec, varState);
// set iteration var to TO value (+ increment) for FOR equivalence
// consistent with for
iterVar = new IntObject(to.getLongValue());
ec.setVariable(_iterPredVar, iterVar);
// we can replace those variables, because partitioning only applied for read-only matrices
for (String var : _variablesDPOriginal.keySet()) {
// cleanup partitioned matrix (if not reused)
if (!_variablesDPReuse.keySet().contains(var))
VariableCPInstruction.processRemoveVariableInstruction(ec, var);
// reset to original matrix
MatrixObject mo = (MatrixObject) _variablesDPOriginal.get(var);
ec.setVariable(var, mo);
}
// print profiling report (only if top-level parfor because otherwise in parallel context)
if (_monitorReport)
LOG.info("\n" + StatisticMonitor.createReport());
// TODO reset of hop parallelism constraint (e.g., ba+*)
for (// release forced exectypes
String dpvar : // release forced exectypes
_variablesDPOriginal.keySet()) ProgramRecompiler.rFindAndRecompileIndexingHOP(sb, this, dpvar, ec, false);
// release forced exectypes for fused dp/exec
if (_execMode == PExecMode.REMOTE_MR_DP || _execMode == PExecMode.REMOTE_SPARK_DP)
ProgramRecompiler.rFindAndRecompileIndexingHOP(sb, this, _colocatedDPMatrix, ec, false);
// after release, deletes dp_varnames
resetOptimizerFlags();
// execute exit instructions (usually empty)
executeInstructions(_exitInstructions, ec);
}
Aggregations