Search in sources :

Example 6 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(Program prog) {
    // we need to cost the entire program in order to take in-memory status into account
    ExecutionContext ec = ExecutionContextFactory.createContext();
    double val = CostEstimationWrapper.getTimeEstimate(prog, ec);
    _cntCostPB++;
    return val;
}
Also used : ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext)

Example 7 with ExecutionContext

use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.

the class ResourceOptimizer method initLocalMemoTable.

private static double[][] initLocalMemoTable(ArrayList<ProgramBlock> Bp, double min) {
    // allocate memo structure
    int len = Bp.size();
    double[][] memo = new double[len][2];
    // init with min resource and current costs
    for (int i = 0; i < len; i++) {
        ProgramBlock pb = Bp.get(i);
        ExecutionContext ec = ExecutionContextFactory.createContext();
        memo[i][0] = min;
        memo[i][1] = CostEstimationWrapper.getTimeEstimate(pb.getProgram(), ec);
    }
    return memo;
}
Also used : ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock)

Example 8 with ExecutionContext

use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.

the class PreparedScript method executeScript.

/**
 * Executes the prepared script over the bound inputs, creating the
 * result variables according to bound and registered outputs.
 *
 * @return ResultVariables object encapsulating output results
 */
public ResultVariables executeScript() {
    // add reused variables
    _vars.putAll(_inVarReuse);
    // set thread-local configurations
    ConfigurationManager.setLocalConfig(_dmlconf);
    ConfigurationManager.setLocalConfig(_cconf);
    // create and populate execution context
    ExecutionContext ec = ExecutionContextFactory.createContext(_vars, _prog);
    // core execute runtime program
    _prog.execute(ec);
    // cleanup unnecessary outputs
    _vars.removeAllNotIn(_outVarnames);
    // construct results
    ResultVariables rvars = new ResultVariables();
    for (String ovar : _outVarnames) {
        Data tmpVar = _vars.get(ovar);
        if (tmpVar != null)
            rvars.addResult(ovar, tmpVar);
    }
    // clear thread-local configurations
    ConfigurationManager.clearLocalConfigs();
    return rvars;
}
Also used : ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) Data(org.apache.sysml.runtime.instructions.cp.Data)

Example 9 with ExecutionContext

use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.

the class RewriteConstantFolding method evalScalarOperation.

/**
 * In order to (1) prevent unexpected side effects from constant folding and
 * (2) for simplicity with regard to arbitrary value type combinations,
 * we use the same compilation and runtime for constant folding as we would
 * use for actual instruction execution.
 *
 * @param bop high-level operator
 * @return literal op
 */
private LiteralOp evalScalarOperation(Hop bop) {
    // Timing time = new Timing( true );
    DataOp tmpWrite = new DataOp(TMP_VARNAME, bop.getDataType(), bop.getValueType(), bop, DataOpTypes.TRANSIENTWRITE, TMP_VARNAME);
    // generate runtime instruction
    Dag<Lop> dag = new Dag<>();
    // prevent lops reuse
    Recompiler.rClearLops(tmpWrite);
    // reconstruct lops
    Lop lops = tmpWrite.constructLops();
    lops.addToDag(dag);
    ArrayList<Instruction> inst = dag.getJobs(null, ConfigurationManager.getDMLConfig());
    // execute instructions
    ExecutionContext ec = getExecutionContext();
    ProgramBlock pb = getProgramBlock();
    pb.setInstructions(inst);
    pb.execute(ec);
    // get scalar result (check before invocation) and create literal according
    // to observed scalar output type (not hop type) for runtime consistency
    ScalarObject so = (ScalarObject) ec.getVariable(TMP_VARNAME);
    LiteralOp literal = null;
    switch(so.getValueType()) {
        case DOUBLE:
            literal = new LiteralOp(so.getDoubleValue());
            break;
        case INT:
            literal = new LiteralOp(so.getLongValue());
            break;
        case BOOLEAN:
            literal = new LiteralOp(so.getBooleanValue());
            break;
        case STRING:
            literal = new LiteralOp(so.getStringValue());
            break;
        default:
            throw new HopsException("Unsupported literal value type: " + bop.getValueType());
    }
    // cleanup
    tmpWrite.getInput().clear();
    bop.getParent().remove(tmpWrite);
    pb.setInstructions(null);
    ec.getVariables().removeAll();
    // set literal properties (scalar)
    HopRewriteUtils.setOutputParametersForScalar(literal);
    return literal;
}
Also used : ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) Dag(org.apache.sysml.lops.compile.Dag) HopsException(org.apache.sysml.hops.HopsException) Lop(org.apache.sysml.lops.Lop) Instruction(org.apache.sysml.runtime.instructions.Instruction) LiteralOp(org.apache.sysml.hops.LiteralOp) DataOp(org.apache.sysml.hops.DataOp)

Example 10 with ExecutionContext

use of org.apache.sysml.runtime.controlprogram.context.ExecutionContext in project incubator-systemml by apache.

the class DMLScript method execute.

// /////////////////////////////
// private internal interface
// (core compilation and execute)
// //////
/**
 * The running body of DMLScript execution. This method should be called after execution properties have been correctly set,
 * and customized parameters have been put into _argVals
 *
 * @param dmlScriptStr DML script string
 * @param fnameOptConfig configuration file
 * @param argVals map of argument values
 * @param allArgs arguments
 * @param scriptType type of script (DML or PyDML)
 * @throws IOException if IOException occurs
 */
private static void execute(String dmlScriptStr, String fnameOptConfig, Map<String, String> argVals, String[] allArgs, ScriptType scriptType) throws IOException {
    SCRIPT_TYPE = scriptType;
    // print basic time and environment info
    printStartExecInfo(dmlScriptStr);
    // Step 1: parse configuration files & write any configuration specific global variables
    DMLConfig dmlconf = DMLConfig.readConfigurationFile(fnameOptConfig);
    ConfigurationManager.setGlobalConfig(dmlconf);
    CompilerConfig cconf = OptimizerUtils.constructCompilerConfig(dmlconf);
    ConfigurationManager.setGlobalConfig(cconf);
    LOG.debug("\nDML config: \n" + dmlconf.getConfigInfo());
    // Sets the GPUs to use for this process (a range, all GPUs, comma separated list or a specific GPU)
    GPUContextPool.AVAILABLE_GPUS = dmlconf.getTextValue(DMLConfig.AVAILABLE_GPUS);
    String evictionPolicy = dmlconf.getTextValue(DMLConfig.GPU_EVICTION_POLICY).toUpperCase();
    try {
        DMLScript.GPU_EVICTION_POLICY = EvictionPolicy.valueOf(evictionPolicy);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException("Unsupported eviction policy:" + evictionPolicy);
    }
    // Step 2: set local/remote memory if requested (for compile in AM context)
    if (dmlconf.getBooleanValue(DMLConfig.YARN_APPMASTER)) {
        DMLAppMasterUtils.setupConfigRemoteMaxMemory(dmlconf);
    }
    // Step 3: parse dml script
    Statistics.startCompileTimer();
    ParserWrapper parser = ParserFactory.createParser(scriptType);
    DMLProgram prog = parser.parse(DML_FILE_PATH_ANTLR_PARSER, dmlScriptStr, argVals);
    // Step 4: construct HOP DAGs (incl LVA, validate, and setup)
    DMLTranslator dmlt = new DMLTranslator(prog);
    dmlt.liveVariableAnalysis(prog);
    dmlt.validateParseTree(prog);
    dmlt.constructHops(prog);
    // init working directories (before usage by following compilation steps)
    initHadoopExecution(dmlconf);
    // Step 5: rewrite HOP DAGs (incl IPA and memory estimates)
    dmlt.rewriteHopsDAG(prog);
    // Step 6: construct lops (incl exec type and op selection)
    dmlt.constructLops(prog);
    if (LOG.isDebugEnabled()) {
        LOG.debug("\n********************** LOPS DAG *******************");
        dmlt.printLops(prog);
        dmlt.resetLopsDAGVisitStatus(prog);
    }
    // Step 7: generate runtime program, incl codegen
    Program rtprog = dmlt.getRuntimeProgram(prog, dmlconf);
    // launch SystemML appmaster (if requested and not already in launched AM)
    if (dmlconf.getBooleanValue(DMLConfig.YARN_APPMASTER)) {
        if (!isActiveAM() && DMLYarnClientProxy.launchDMLYarnAppmaster(dmlScriptStr, dmlconf, allArgs, rtprog))
            // if AM launch unsuccessful, fall back to normal execute
            return;
        if (// in AM context (not failed AM launch)
        isActiveAM())
            DMLAppMasterUtils.setupProgramMappingRemoteMaxMemory(rtprog);
    }
    // Step 9: prepare statistics [and optional explain output]
    // count number compiled MR jobs / SP instructions
    ExplainCounts counts = Explain.countDistributedOperations(rtprog);
    Statistics.resetNoOfCompiledJobs(counts.numJobs);
    // explain plan of program (hops or runtime)
    if (EXPLAIN != ExplainType.NONE)
        LOG.info(Explain.display(prog, rtprog, EXPLAIN, counts));
    Statistics.stopCompileTimer();
    // double costs = CostEstimationWrapper.getTimeEstimate(rtprog, ExecutionContextFactory.createContext());
    // System.out.println("Estimated costs: "+costs);
    // Step 10: execute runtime program
    ExecutionContext ec = null;
    try {
        ec = ExecutionContextFactory.createContext(rtprog);
        ScriptExecutorUtils.executeRuntimeProgram(rtprog, ec, dmlconf, STATISTICS ? STATISTICS_COUNT : 0);
    } finally {
        if (ec != null && ec instanceof SparkExecutionContext)
            ((SparkExecutionContext) ec).close();
        LOG.info("END DML run " + getDateTime());
        // cleanup scratch_space and all working dirs
        cleanupHadoopExecution(dmlconf);
    }
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) SparkExecutionContext(org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext) ExecutionContext(org.apache.sysml.runtime.controlprogram.context.ExecutionContext) ExplainCounts(org.apache.sysml.utils.Explain.ExplainCounts) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) SparkExecutionContext(org.apache.sysml.runtime.controlprogram.context.SparkExecutionContext) CompilerConfig(org.apache.sysml.conf.CompilerConfig) DMLTranslator(org.apache.sysml.parser.DMLTranslator)

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