Search in sources :

Example 1 with RewriteRemovePersistentReadWrite

use of org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite 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 RewriteRemovePersistentReadWrite

use of org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite in project incubator-systemml by apache.

the class ScriptExecutor method rewritePersistentReadsAndWrites.

/**
 * Replace persistent reads and writes with transient reads and writes in
 * the symbol table.
 */
protected void rewritePersistentReadsAndWrites() {
    LocalVariableMap symbolTable = script.getSymbolTable();
    if (symbolTable != null) {
        String[] inputs = (script.getInputVariables() == null) ? new String[0] : script.getInputVariables().toArray(new String[0]);
        String[] outputs = (script.getOutputVariables() == null) ? new String[0] : script.getOutputVariables().toArray(new String[0]);
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs, script.getSymbolTable());
        ProgramRewriter programRewriter = new ProgramRewriter(rewrite);
        try {
            programRewriter.rewriteProgramHopDAGs(dmlProgram);
        } catch (LanguageException | HopsException e) {
            throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
        }
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HopsException(org.apache.sysml.hops.HopsException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Example 3 with RewriteRemovePersistentReadWrite

use of org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite in project systemml by apache.

the class ScriptExecutor method rewritePersistentReadsAndWrites.

/**
 * Replace persistent reads and writes with transient reads and writes in
 * the symbol table.
 */
protected void rewritePersistentReadsAndWrites() {
    LocalVariableMap symbolTable = script.getSymbolTable();
    if (symbolTable != null) {
        String[] inputs = (script.getInputVariables() == null) ? new String[0] : script.getInputVariables().toArray(new String[0]);
        String[] outputs = (script.getOutputVariables() == null) ? new String[0] : script.getOutputVariables().toArray(new String[0]);
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs, script.getSymbolTable());
        ProgramRewriter programRewriter = new ProgramRewriter(rewrite);
        try {
            programRewriter.rewriteProgramHopDAGs(dmlProgram);
        } catch (LanguageException | HopsException e) {
            throw new MLContextException("Exception occurred while rewriting persistent reads and writes", e);
        }
    }
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HopsException(org.apache.sysml.hops.HopsException) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)

Example 4 with RewriteRemovePersistentReadWrite

use of org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite in project 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)

Aggregations

ProgramRewriter (org.apache.sysml.hops.rewrite.ProgramRewriter)4 RewriteRemovePersistentReadWrite (org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite)4 LanguageException (org.apache.sysml.parser.LanguageException)4 BufferedReader (java.io.BufferedReader)2 Closeable (java.io.Closeable)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Path (org.apache.hadoop.fs.Path)2 DMLException (org.apache.sysml.api.DMLException)2 DMLScript (org.apache.sysml.api.DMLScript)2 RUNTIME_PLATFORM (org.apache.sysml.api.DMLScript.RUNTIME_PLATFORM)2 ScriptType (org.apache.sysml.api.mlcontext.ScriptType)2 CompilerConfig (org.apache.sysml.conf.CompilerConfig)2 ConfigType (org.apache.sysml.conf.CompilerConfig.ConfigType)2