use of org.apache.sysml.runtime.DMLScriptException in project incubator-systemml by apache.
the class ProgramBlock method executeSingleInstruction.
private void executeSingleInstruction(Instruction currInst, ExecutionContext ec) {
try {
// start time measurement for statistics
long t0 = (DMLScript.STATISTICS || LOG.isTraceEnabled()) ? System.nanoTime() : 0;
// pre-process instruction (debug state, inst patching, listeners)
Instruction tmp = currInst.preprocessInstruction(ec);
// process actual instruction
tmp.processInstruction(ec);
// post-process instruction (debug)
tmp.postprocessInstruction(ec);
// maintain aggregate statistics
if (DMLScript.STATISTICS) {
Statistics.maintainCPHeavyHitters(tmp.getExtendedOpcode(), System.nanoTime() - t0);
}
// optional trace information (instruction and runtime)
if (LOG.isTraceEnabled()) {
long t1 = System.nanoTime();
String time = String.format("%.3f", ((double) t1 - t0) / 1000000000);
LOG.trace("Instruction: " + tmp + " (executed in " + time + "s).");
}
// variables in symbol table (for tracking source of wrong representation)
if (CHECK_MATRIX_SPARSITY) {
checkSparsity(tmp, ec.getVariables());
}
} catch (Exception e) {
if (!DMLScript.ENABLE_DEBUG_MODE) {
if (e instanceof DMLScriptException)
throw (DMLScriptException) e;
else
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error evaluating instruction: " + currInst.toString(), e);
} else {
ec.handleDebugException(e);
}
}
}
use of org.apache.sysml.runtime.DMLScriptException 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.DMLScriptException in project incubator-systemml by apache.
the class ScalarBuiltinCPInstruction method processInstruction.
@Override
public void processInstruction(ExecutionContext ec) throws DMLRuntimeException {
String opcode = getOpcode();
SimpleOperator dop = (SimpleOperator) _optr;
ScalarObject sores = null;
ScalarObject so = null;
//get the scalar input
so = ec.getScalarInput(input1.getName(), input1.getValueType(), input1.isLiteral());
//core execution
if (opcode.equalsIgnoreCase("print")) {
String outString = so.getLanguageSpecificStringValue();
// The flag will be set, for example, when SystemML is invoked in fenced mode from Jaql.
if (!DMLScript.suppressPrint2Stdout())
System.out.println(outString);
// String that is printed on stdout will be inserted into symbol table (dummy, not necessary!)
sores = new StringObject(outString);
} else if (opcode.equalsIgnoreCase("stop")) {
throw new DMLScriptException(so.getStringValue());
} else {
//Inputs for all builtins other than PRINT are treated as DOUBLE.
if (so instanceof IntObject && output.getValueType() == ValueType.INT) {
long rval = (long) dop.fn.execute(so.getLongValue());
sores = (ScalarObject) new IntObject(rval);
} else {
double rval = dop.fn.execute(so.getDoubleValue());
sores = (ScalarObject) new DoubleObject(rval);
}
}
ec.setScalarOutput(output.getName(), sores);
}
Aggregations