Search in sources :

Example 11 with ExecutionContext

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;
}
Also used : MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Data(org.apache.sysml.runtime.instructions.cp.Data)

Example 12 with ExecutionContext

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;
}
Also used : ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap)

Example 13 with ExecutionContext

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);
    }
}
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 14 with ExecutionContext

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);
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) Matrix(org.apache.sysml.udf.Matrix) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) ArrayList(java.util.ArrayList) CPOperand(org.apache.sysml.runtime.instructions.cp.CPOperand) Scalar(org.apache.sysml.udf.Scalar)

Example 15 with ExecutionContext

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;
}
Also used : ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap)

Aggregations

ExecutionContext (org.apache.sysml.runtime.controlprogram.context.ExecutionContext)17 Program (org.apache.sysml.runtime.controlprogram.Program)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)5 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)5 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)5 CompilerConfig (org.apache.sysml.conf.CompilerConfig)4 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 DMLConfig (org.apache.sysml.conf.DMLConfig)3 DMLProgram (org.apache.sysml.parser.DMLProgram)3 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)3 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)3 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)3 SparkExecutionContext (org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext)3 IOException (java.io.IOException)2 DataOp (org.apache.sysml.hops.DataOp)2 HopsException (org.apache.sysml.hops.HopsException)2 ResultVar (org.apache.sysml.parser.ParForStatementBlock.ResultVar)2 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)2