Search in sources :

Example 1 with ParserWrapper

use of org.apache.sysml.parser.ParserWrapper in project incubator-systemml by apache.

the class Connection method prepareScript.

/**
 * Prepares (precompiles) a script, sets input parameter values, and registers input and output variables.
 *
 * @param script string representing the DML or PyDML script
 * @param args map of input parameters ($) and their values
 * @param inputs string array of input variables to register
 * @param outputs string array of output variables to register
 * @param parsePyDML {@code true} if PyDML, {@code false} if DML
 * @return PreparedScript object representing the precompiled script
 */
public PreparedScript prepareScript(String script, Map<String, String> args, String[] inputs, String[] outputs, boolean parsePyDML) {
    DMLScript.SCRIPT_TYPE = parsePyDML ? ScriptType.PYDML : ScriptType.DML;
    // check for valid names of passed arguments
    String[] invalidArgs = args.keySet().stream().filter(k -> k == null || !k.startsWith("$")).toArray(String[]::new);
    if (invalidArgs.length > 0)
        throw new LanguageException("Invalid argument names: " + Arrays.toString(invalidArgs));
    // check for valid names of input and output variables
    String[] invalidVars = UtilFunctions.asSet(inputs, outputs).stream().filter(k -> k == null || k.startsWith("$")).toArray(String[]::new);
    if (invalidVars.length > 0)
        throw new LanguageException("Invalid variable names: " + Arrays.toString(invalidVars));
    setLocalConfigs();
    // simplified compilation chain
    Program rtprog = null;
    try {
        // parsing
        ParserWrapper parser = ParserFactory.createParser(parsePyDML ? ScriptType.PYDML : ScriptType.DML);
        DMLProgram prog = parser.parse(null, script, args);
        // language validate
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.liveVariableAnalysis(prog);
        dmlt.validateParseTree(prog);
        // hop construct/rewrite
        dmlt.constructHops(prog);
        dmlt.rewriteHopsDAG(prog);
        // rewrite persistent reads/writes
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs);
        ProgramRewriter rewriter2 = new ProgramRewriter(rewrite);
        rewriter2.rewriteProgramHopDAGs(prog);
        // lop construct and runtime prog generation
        dmlt.constructLops(prog);
        rtprog = dmlt.getRuntimeProgram(prog, _dmlconf);
        // final cleanup runtime prog
        JMLCUtils.cleanupRuntimeProgram(rtprog, outputs);
    } catch (ParseException pe) {
        // don't chain ParseException (for cleaner error output)
        throw pe;
    } catch (Exception ex) {
        throw new DMLException(ex);
    }
    // return newly create precompiled script
    return new PreparedScript(rtprog, inputs, outputs, _dmlconf, _cconf);
}
Also used : ParserFactory(org.apache.sysml.parser.ParserFactory) CacheableData(org.apache.sysml.runtime.controlprogram.caching.CacheableData) Arrays(java.util.Arrays) DMLTranslator(org.apache.sysml.parser.DMLTranslator) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite) FileSystem(org.apache.hadoop.fs.FileSystem) SpoofCompiler(org.apache.sysml.hops.codegen.SpoofCompiler) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) FrameReader(org.apache.sysml.runtime.io.FrameReader) HashMap(java.util.HashMap) DataConverter(org.apache.sysml.runtime.util.DataConverter) CompilerConfig(org.apache.sysml.conf.CompilerConfig) DMLException(org.apache.sysml.api.DMLException) MatrixBlock(org.apache.sysml.runtime.matrix.data.MatrixBlock) TfMetaUtils(org.apache.sysml.runtime.transform.meta.TfMetaUtils) TfUtils(org.apache.sysml.runtime.transform.TfUtils) MatrixReader(org.apache.sysml.runtime.io.MatrixReader) FrameBlock(org.apache.sysml.runtime.matrix.data.FrameBlock) InputInfo(org.apache.sysml.runtime.matrix.data.InputInfo) Map(java.util.Map) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) Path(org.apache.hadoop.fs.Path) DMLProgram(org.apache.sysml.parser.DMLProgram) DMLConfig(org.apache.sysml.conf.DMLConfig) Program(org.apache.sysml.runtime.controlprogram.Program) LanguageException(org.apache.sysml.parser.LanguageException) ParserWrapper(org.apache.sysml.parser.ParserWrapper) RUNTIME_PLATFORM(org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM) ConfigurationManager(org.apache.sysml.conf.ConfigurationManager) MatrixReaderFactory(org.apache.sysml.runtime.io.MatrixReaderFactory) FrameReaderFactory(org.apache.sysml.runtime.io.FrameReaderFactory) IOException(java.io.IOException) ScriptType(org.apache.sysml.api.mlcontext.ScriptType) IOUtilFunctions(org.apache.sysml.runtime.io.IOUtilFunctions) InputStreamReader(java.io.InputStreamReader) ConfigType(org.apache.sysml.conf.CompilerConfig.ConfigType) Closeable(java.io.Closeable) JSONObject(org.apache.wink.json4j.JSONObject) DMLScript(org.apache.sysml.api.DMLScript) ParseException(org.apache.sysml.parser.ParseException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) UtilFunctions(org.apache.sysml.runtime.util.UtilFunctions) InputStream(java.io.InputStream) DataExpression(org.apache.sysml.parser.DataExpression) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) DMLException(org.apache.sysml.api.DMLException) DMLTranslator(org.apache.sysml.parser.DMLTranslator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException) LanguageException(org.apache.sysml.parser.LanguageException) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Example 2 with ParserWrapper

use of org.apache.sysml.parser.ParserWrapper in project incubator-systemml by apache.

the class DMLScript method launchDebugger.

/**
 * Launcher for DML debugger. This method should be called after
 * execution and debug properties have been correctly set, and customized parameters
 *
 * @param dmlScriptStr DML script contents (including new lines)
 * @param fnameOptConfig Full path of configuration file for SystemML
 * @param argVals Key-value pairs defining arguments of DML script
 * @param scriptType type of script (DML or PyDML)
 * @throws IOException if IOException occurs
 */
private static void launchDebugger(String dmlScriptStr, String fnameOptConfig, Map<String, String> argVals, ScriptType scriptType) throws IOException {
    DMLDebuggerProgramInfo dbprog = new DMLDebuggerProgramInfo();
    // Step 1: parse configuration files
    DMLConfig conf = DMLConfig.readConfigurationFile(fnameOptConfig);
    ConfigurationManager.setGlobalConfig(conf);
    // Step 2: parse dml script
    ParserWrapper parser = ParserFactory.createParser(scriptType);
    DMLProgram prog = parser.parse(DML_FILE_PATH_ANTLR_PARSER, dmlScriptStr, argVals);
    // Step 3: construct HOP DAGs (incl LVA and validate)
    DMLTranslator dmlt = new DMLTranslator(prog);
    dmlt.liveVariableAnalysis(prog);
    dmlt.validateParseTree(prog);
    dmlt.constructHops(prog);
    // Step 4: rewrite HOP DAGs (incl IPA and memory estimates)
    dmlt.rewriteHopsDAG(prog);
    // Step 5: construct LOP DAGs
    dmlt.constructLops(prog);
    // Step 6: generate runtime program
    dbprog.rtprog = dmlt.getRuntimeProgram(prog, conf);
    try {
        // set execution environment
        initHadoopExecution(conf);
        // initialize an instance of SystemML debugger
        DMLDebugger SystemMLdb = new DMLDebugger(dbprog, dmlScriptStr);
        // run SystemML debugger
        SystemMLdb.runSystemMLDebugger();
    } finally {
        // cleanup scratch_space and all working dirs
        cleanupHadoopExecution(conf);
    }
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) DMLDebuggerProgramInfo(org.apache.sysml.debug.DMLDebuggerProgramInfo) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) DMLDebugger(org.apache.sysml.debug.DMLDebugger) DMLTranslator(org.apache.sysml.parser.DMLTranslator)

Example 3 with ParserWrapper

use of org.apache.sysml.parser.ParserWrapper in project incubator-systemml by apache.

the class ParForDependencyAnalysisTest method runTest.

private void runTest(String scriptFilename, boolean expectedException) {
    boolean raisedException = false;
    try {
        // Tell the superclass about the name of this test, so that the superclass can
        // create temporary directories.
        int index = scriptFilename.lastIndexOf(".dml");
        String testName = scriptFilename.substring(0, index > 0 ? index : scriptFilename.length());
        TestConfiguration testConfig = new TestConfiguration(TEST_CLASS_DIR, testName, new String[] {});
        addTestConfiguration(testName, testConfig);
        loadTestConfiguration(testConfig);
        DMLConfig conf = new DMLConfig(getCurConfigFile().getPath());
        ConfigurationManager.setLocalConfig(conf);
        String dmlScriptString = "";
        HashMap<String, String> argVals = new HashMap<String, String>();
        // read script
        try (BufferedReader in = new BufferedReader(new FileReader(HOME + scriptFilename))) {
            String s1 = null;
            while ((s1 = in.readLine()) != null) dmlScriptString += s1 + "\n";
        }
        // parsing and dependency analysis
        ParserWrapper parser = ParserFactory.createParser(org.apache.sysml.api.mlcontext.ScriptType.DML);
        DMLProgram prog = parser.parse(DMLScript.DML_FILE_PATH_ANTLR_PARSER, dmlScriptString, argVals);
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.validateParseTree(prog);
    } catch (LanguageException ex) {
        raisedException = true;
        if (raisedException != expectedException)
            ex.printStackTrace();
    } catch (Exception ex2) {
        ex2.printStackTrace();
        throw new RuntimeException(ex2);
    // Assert.fail( "Unexpected exception occured during test run." );
    }
    // check correctness
    Assert.assertEquals(expectedException, raisedException);
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) HashMap(java.util.HashMap) TestConfiguration(org.apache.sysml.test.integration.TestConfiguration) DMLTranslator(org.apache.sysml.parser.DMLTranslator) LanguageException(org.apache.sysml.parser.LanguageException) LanguageException(org.apache.sysml.parser.LanguageException) BufferedReader(java.io.BufferedReader) DMLProgram(org.apache.sysml.parser.DMLProgram) FileReader(java.io.FileReader) ParserWrapper(org.apache.sysml.parser.ParserWrapper)

Example 4 with ParserWrapper

use of org.apache.sysml.parser.ParserWrapper in project 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)

Example 5 with ParserWrapper

use of org.apache.sysml.parser.ParserWrapper in project systemml by apache.

the class ScriptExecutor method parseScript.

/**
 * Parse the script into an ANTLR parse tree, and convert this parse tree
 * into a SystemML program. Parsing includes lexical/syntactic analysis.
 */
protected void parseScript() {
    try {
        ParserWrapper parser = ParserFactory.createParser(script.getScriptType());
        Map<String, Object> inputParameters = script.getInputParameters();
        Map<String, String> inputParametersStringMaps = MLContextUtil.convertInputParametersForParser(inputParameters, script.getScriptType());
        String scriptExecutionString = script.getScriptExecutionString();
        dmlProgram = parser.parse(null, scriptExecutionString, inputParametersStringMaps);
    } catch (ParseException e) {
        throw new MLContextException("Exception occurred while parsing script", e);
    }
}
Also used : ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException)

Aggregations

ParserWrapper (org.apache.sysml.parser.ParserWrapper)12 DMLConfig (org.apache.sysml.conf.DMLConfig)10 DMLProgram (org.apache.sysml.parser.DMLProgram)10 DMLTranslator (org.apache.sysml.parser.DMLTranslator)10 BufferedReader (java.io.BufferedReader)6 FileReader (java.io.FileReader)6 HashMap (java.util.HashMap)6 LanguageException (org.apache.sysml.parser.LanguageException)6 DMLException (org.apache.sysml.api.DMLException)4 CompilerConfig (org.apache.sysml.conf.CompilerConfig)4 ParseException (org.apache.sysml.parser.ParseException)4 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)4 Program (org.apache.sysml.runtime.controlprogram.Program)4 TestConfiguration (org.apache.sysml.test.integration.TestConfiguration)4 Closeable (java.io.Closeable)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2