Search in sources :

Example 11 with DMLScriptException

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);
        }
    }
}
Also used : DMLScriptException(org.apache.sysml.runtime.DMLScriptException) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 12 with DMLScriptException

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);
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) HashSet(java.util.HashSet)

Example 13 with DMLScriptException

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);
}
Also used : SimpleOperator(org.apache.sysml.runtime.matrix.operators.SimpleOperator) DMLScriptException(org.apache.sysml.runtime.DMLScriptException)

Aggregations

DMLScriptException (org.apache.sysml.runtime.DMLScriptException)13 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 IOException (java.io.IOException)3 GenericOptionsParser (org.apache.hadoop.util.GenericOptionsParser)2 FinalApplicationStatus (org.apache.hadoop.yarn.api.records.FinalApplicationStatus)2 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)2 ParseException (org.apache.sysml.parser.ParseException)2 UpdateType (org.apache.sysml.runtime.controlprogram.caching.MatrixObject.UpdateType)2 HashSet (java.util.HashSet)1 AlreadySelectedException (org.apache.commons.cli.AlreadySelectedException)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 Options (org.apache.commons.cli.Options)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 Configuration (org.apache.hadoop.conf.Configuration)1 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)1 ApplicationReport (org.apache.hadoop.yarn.api.records.ApplicationReport)1 ApplicationSubmissionContext (org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext)1 ContainerId (org.apache.hadoop.yarn.api.records.ContainerId)1 ContainerLaunchContext (org.apache.hadoop.yarn.api.records.ContainerLaunchContext)1 LocalResource (org.apache.hadoop.yarn.api.records.LocalResource)1