Search in sources :

Example 1 with DMLTranslator

use of org.apache.sysml.parser.DMLTranslator 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 DMLTranslator

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

the class ScriptExecutor method liveVariableAnalysis.

/**
 * Liveness analysis is performed on the program, obtaining sets of live-in
 * and live-out variables by forward and backward passes over the program.
 */
protected void liveVariableAnalysis() {
    try {
        dmlTranslator = new DMLTranslator(dmlProgram);
        dmlTranslator.liveVariableAnalysis(dmlProgram);
    } catch (DMLRuntimeException e) {
        throw new MLContextException("Exception occurred during live variable analysis", e);
    } catch (LanguageException e) {
        throw new MLContextException("Exception occurred during live variable analysis", e);
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) DMLTranslator(org.apache.sysml.parser.DMLTranslator) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 3 with DMLTranslator

use of org.apache.sysml.parser.DMLTranslator 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 4 with DMLTranslator

use of org.apache.sysml.parser.DMLTranslator 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 5 with DMLTranslator

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

the class ProgramRecompiler method generatePartitialRuntimeProgram.

public static ArrayList<ProgramBlock> generatePartitialRuntimeProgram(Program rtprog, ArrayList<StatementBlock> sbs) {
    ArrayList<ProgramBlock> ret = new ArrayList<>();
    DMLConfig config = ConfigurationManager.getDMLConfig();
    // construct lops from hops if not existing
    DMLTranslator dmlt = new DMLTranslator(sbs.get(0).getDMLProg());
    for (StatementBlock sb : sbs) {
        dmlt.constructLops(sb);
    }
    // construct runtime program from lops
    for (StatementBlock sb : sbs) {
        ret.add(dmlt.createRuntimeProgramBlock(rtprog, sb, config));
    }
    // enhance runtime program by automatic operator fusion
    if (ConfigurationManager.isCodegenEnabled() && SpoofCompiler.INTEGRATION == IntegrationType.RUNTIME) {
        for (ProgramBlock pb : ret) dmlt.codgenHopsDAG(pb);
    }
    return ret;
}
Also used : DMLConfig(org.apache.sysml.conf.DMLConfig) ArrayList(java.util.ArrayList) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) DMLTranslator(org.apache.sysml.parser.DMLTranslator) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock)

Aggregations

DMLTranslator (org.apache.sysml.parser.DMLTranslator)14 DMLConfig (org.apache.sysml.conf.DMLConfig)12 DMLProgram (org.apache.sysml.parser.DMLProgram)10 ParserWrapper (org.apache.sysml.parser.ParserWrapper)10 LanguageException (org.apache.sysml.parser.LanguageException)8 BufferedReader (java.io.BufferedReader)6 FileReader (java.io.FileReader)6 HashMap (java.util.HashMap)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 DMLException (org.apache.sysml.api.DMLException)4 CompilerConfig (org.apache.sysml.conf.CompilerConfig)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 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Map (java.util.Map)2