use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.
the class ProgramConverter method createDeepCopyExecutionContext.
// //////////////////////////////
// CREATION of DEEP COPIES
// //////////////////////////////
/**
* Creates a deep copy of the given execution context.
* For rt_platform=Hadoop, execution context has a symbol table.
*
* @param ec execution context
* @return execution context
* @throws CloneNotSupportedException if CloneNotSupportedException occurs
*/
public static ExecutionContext createDeepCopyExecutionContext(ExecutionContext ec) throws CloneNotSupportedException {
ExecutionContext cpec = ExecutionContextFactory.createContext(false, ec.getProgram());
cpec.setVariables((LocalVariableMap) ec.getVariables().clone());
// (each worker requires its own copy of the empty matrix object)
for (String var : cpec.getVariables().keySet()) {
Data dat = cpec.getVariables().get(var);
if (dat instanceof MatrixObject && ((MatrixObject) dat).getUpdateType().isInPlace()) {
MatrixObject mo = (MatrixObject) dat;
MatrixObject moNew = new MatrixObject(mo);
if (mo.getNnz() != 0) {
// If output matrix is not empty (NNZ != 0), then local copy is created so that
// update in place operation can be applied.
MatrixBlock mbVar = mo.acquireRead();
moNew.acquireModify(new MatrixBlock(mbVar));
mo.release();
} else {
// create empty matrix block w/ dense representation (preferred for update in-place)
// Creating a dense matrix block is valid because empty block not allocated and transfer
// to sparse representation happens in left indexing in place operation.
moNew.acquireModify(new MatrixBlock((int) mo.getNumRows(), (int) mo.getNumColumns(), false));
}
moNew.release();
cpec.setVariable(var, moNew);
}
}
return cpec;
}
use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.
the class ProgramConverter method parseExecutionContext.
private static ExecutionContext parseExecutionContext(String in, Program prog) {
ExecutionContext ec = null;
String lin = in.substring(PARFOR_EC_BEGIN.length(), in.length() - PARFOR_EC_END.length()).trim();
if (!lin.equals(EMPTY)) {
LocalVariableMap vars = parseVariables(lin);
ec = ExecutionContextFactory.createContext(false, prog);
ec.setVariables(vars);
}
return ec;
}
use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.
the class FunctionCallCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) {
if (LOG.isTraceEnabled()) {
LOG.trace("Executing instruction : " + this.toString());
}
// get the function program block (stored in the Program object)
FunctionProgramBlock fpb = ec.getProgram().getFunctionProgramBlock(_namespace, _functionName);
// sanity check number of function parameters
if (_boundInputs.length < fpb.getInputParams().size()) {
throw new DMLRuntimeException("Number of bound input parameters does not match the function signature " + "(" + _boundInputs.length + ", but " + fpb.getInputParams().size() + " expected)");
}
// create bindings to formal parameters for given function call
// These are the bindings passed to the FunctionProgramBlock for function execution
LocalVariableMap functionVariables = new LocalVariableMap();
for (int i = 0; i < fpb.getInputParams().size(); i++) {
// error handling non-existing variables
CPOperand input = _boundInputs[i];
if (!input.isLiteral() && !ec.containsVariable(input.getName())) {
throw new DMLRuntimeException("Input variable '" + input.getName() + "' not existing on call of " + DMLProgram.constructFunctionKey(_namespace, _functionName) + " (line " + getLineNum() + ").");
}
// get input matrix/frame/scalar
DataIdentifier currFormalParam = fpb.getInputParams().get(i);
Data value = ec.getVariable(input);
// graceful value type conversion for scalar inputs with wrong type
if (value.getDataType() == DataType.SCALAR && value.getValueType() != currFormalParam.getValueType()) {
value = ScalarObjectFactory.createScalarObject(currFormalParam.getValueType(), (ScalarObject) value);
}
// set input parameter
functionVariables.put(currFormalParam.getName(), value);
}
// Pin the input variables so that they do not get deleted
// from pb's symbol table at the end of execution of function
boolean[] pinStatus = ec.pinVariables(_boundInputNames);
// Create a symbol table under a new execution context for the function invocation,
// and copy the function arguments into the created table.
ExecutionContext fn_ec = ExecutionContextFactory.createContext(false, ec.getProgram());
if (DMLScript.USE_ACCELERATOR) {
fn_ec.setGPUContexts(ec.getGPUContexts());
fn_ec.getGPUContext(0).initializeThread();
}
fn_ec.setVariables(functionVariables);
// execute the function block
try {
fpb._functionName = this._functionName;
fpb._namespace = this._namespace;
fpb.execute(fn_ec);
} catch (DMLScriptException e) {
throw e;
} catch (Exception e) {
String fname = DMLProgram.constructFunctionKey(_namespace, _functionName);
throw new DMLRuntimeException("error executing function " + fname, e);
}
// cleanup all returned variables w/o binding
HashSet<String> expectRetVars = new HashSet<>();
for (DataIdentifier di : fpb.getOutputParams()) expectRetVars.add(di.getName());
LocalVariableMap retVars = fn_ec.getVariables();
for (Entry<String, Data> var : retVars.entrySet()) {
if (expectRetVars.contains(var.getKey()))
continue;
// cleanup unexpected return values to avoid leaks
if (var.getValue() instanceof CacheableData)
fn_ec.cleanupCacheableData((CacheableData<?>) var.getValue());
}
// Unpin the pinned variables
ec.unpinVariables(_boundInputNames, pinStatus);
// add the updated binding for each return variable to the variables in original symbol table
for (int i = 0; i < fpb.getOutputParams().size(); i++) {
String boundVarName = _boundOutputNames.get(i);
Data boundValue = retVars.get(fpb.getOutputParams().get(i).getName());
if (boundValue == null)
throw new DMLRuntimeException(boundVarName + " was not assigned a return value");
// cleanup existing data bound to output variable name
Data exdata = ec.removeVariable(boundVarName);
if (exdata != null && exdata instanceof CacheableData && exdata != boundValue) {
ec.cleanupCacheableData((CacheableData<?>) exdata);
}
// add/replace data in symbol table
ec.setVariable(boundVarName, boundValue);
}
}
use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.
the class EvalFunction method execute.
@Override
public void execute(ExecutionContext ec) {
String fname = ((Scalar) getFunctionInput(0)).getValue();
MatrixObject in = ((Matrix) getFunctionInput(1)).getMatrixObject();
ArrayList<String> inputs = new ArrayList<>();
inputs.add("A");
ArrayList<String> outputs = new ArrayList<>();
outputs.add("B");
ExecutionContext ec2 = ExecutionContextFactory.createContext(ec.getProgram());
CPOperand inName = new CPOperand("TMP", org.apache.sysml.parser.Expression.ValueType.DOUBLE, DataType.MATRIX);
ec2.setVariable("TMP", in);
FunctionCallCPInstruction fcpi = new FunctionCallCPInstruction(null, fname, new CPOperand[] { inName }, inputs, outputs, "eval func");
fcpi.processInstruction(ec2);
MatrixObject out = (MatrixObject) ec2.getVariable("B");
_ret = new Matrix(out, ValueType.Double);
}
use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.
the class ResourceOptimizer method getProgramCosts.
private static double getProgramCosts(ProgramBlock pb) {
double val = 0;
if (COST_INDIVIDUAL_BLOCKS) {
LocalVariableMap vars = new LocalVariableMap();
collectReadVariables(pb.getStatementBlock().getHops(), vars);
ExecutionContext ec = ExecutionContextFactory.createContext(false, null);
ec.setVariables(vars);
val = CostEstimationWrapper.getTimeEstimate(pb, ec, false);
} else {
// we need to cost the entire program in order to take in-memory status into account
ExecutionContext ec = ExecutionContextFactory.createContext();
val = CostEstimationWrapper.getTimeEstimate(pb.getProgram(), ec);
}
_cntCostPB++;
return val;
}
Aggregations