Search in sources :

Example 16 with DMLProgram

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

the class PydmlSyntacticValidator method exitImportStatement.

// -----------------------------------------------------------------
// "src" statment
// -----------------------------------------------------------------
@Override
public void exitImportStatement(ImportStatementContext ctx) {
    // prepare import filepath
    String filePath = ctx.filePath.getText();
    String namespace = DMLProgram.DEFAULT_NAMESPACE;
    if (ctx.namespace != null && ctx.namespace.getText() != null && !ctx.namespace.getText().isEmpty()) {
        namespace = ctx.namespace.getText();
    }
    if ((filePath.startsWith("\"") && filePath.endsWith("\"")) || filePath.startsWith("'") && filePath.endsWith("'")) {
        filePath = filePath.substring(1, filePath.length() - 1);
    }
    File file = new File(filePath);
    if (!file.isAbsolute()) {
        // concatenate working directory to filepath
        filePath = _workingDir + File.separator + filePath;
    }
    validateNamespace(namespace, filePath, ctx);
    String scriptID = DMLProgram.constructFunctionKey(namespace, filePath);
    DMLProgram prog = null;
    if (!_scripts.get().containsKey(scriptID)) {
        _scripts.get().put(scriptID, namespace);
        try {
            prog = (new PyDMLParserWrapper()).doParse(filePath, null, getQualifiedNamespace(namespace), argVals);
        } catch (ParseException e) {
            notifyErrorListeners(e.getMessage(), ctx.start);
            return;
        }
        // Custom logic whether to proceed ahead or not. Better than the current exception handling mechanism
        if (prog == null) {
            notifyErrorListeners("One or more errors found during importing a program from file " + filePath, ctx.start);
            return;
        } else {
            ctx.info.namespaces = new HashMap<>();
            ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
            ctx.info.stmt = new ImportStatement();
            ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
            ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
            ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
        }
    } else {
        // Skip redundant parsing (to prevent potential infinite recursion) and
        // create empty program for this context to allow processing to continue.
        prog = new DMLProgram();
        ctx.info.namespaces = new HashMap<>();
        ctx.info.namespaces.put(getQualifiedNamespace(namespace), prog);
        ctx.info.stmt = new ImportStatement();
        ((ImportStatement) ctx.info.stmt).setCompletePath(filePath);
        ((ImportStatement) ctx.info.stmt).setFilePath(ctx.filePath.getText());
        ((ImportStatement) ctx.info.stmt).setNamespace(namespace);
    }
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) ParseException(org.apache.sysml.parser.ParseException) ImportStatement(org.apache.sysml.parser.ImportStatement) File(java.io.File)

Example 17 with DMLProgram

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

the class GenerateClassesForMLContext method dmlProgramFromScriptFilePath.

/**
 * Create a DMLProgram from a script file.
 *
 * @param scriptFilePath
 *            the path to a script file
 * @return the DMLProgram generated by the script file
 */
public static DMLProgram dmlProgramFromScriptFilePath(String scriptFilePath) {
    String scriptString = fileToString(scriptFilePath);
    Script script = new Script(scriptString);
    ScriptExecutor se = new ScriptExecutor() {

        @Override
        public MLResults execute(Script script) {
            setup(script);
            parseScript();
            return null;
        }
    };
    se.execute(script);
    DMLProgram dmlProgram = se.getDmlProgram();
    return dmlProgram;
}
Also used : Script(org.apache.sysml.api.mlcontext.Script) DMLScript(org.apache.sysml.api.DMLScript) DMLProgram(org.apache.sysml.parser.DMLProgram) ScriptExecutor(org.apache.sysml.api.mlcontext.ScriptExecutor)

Example 18 with DMLProgram

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

the class GenerateClassesForMLContext method addFunctionMethods.

/**
 * Add methods to a derived script class to allow invocation of script
 * functions.
 *
 * @param scriptFilePath
 *            the path to a script file
 * @param ctNewScript
 *            the javassist compile-time class representation of a script
 */
public static void addFunctionMethods(String scriptFilePath, CtClass ctNewScript) {
    try {
        DMLProgram dmlProgram = dmlProgramFromScriptFilePath(scriptFilePath);
        if (dmlProgram == null) {
            System.out.println("Could not generate DML Program for: " + scriptFilePath);
            return;
        }
        Map<String, FunctionStatementBlock> defaultNsFsbsMap = dmlProgram.getFunctionStatementBlocks(DMLProgram.DEFAULT_NAMESPACE);
        List<FunctionStatementBlock> fsbs = new ArrayList<>();
        fsbs.addAll(defaultNsFsbsMap.values());
        for (FunctionStatementBlock fsb : fsbs) {
            ArrayList<Statement> sts = fsb.getStatements();
            for (Statement st : sts) {
                if (!(st instanceof FunctionStatement)) {
                    continue;
                }
                FunctionStatement fs = (FunctionStatement) st;
                String dmlFunctionCall = generateDmlFunctionCall(scriptFilePath, fs);
                String functionCallMethod = generateFunctionCallMethod(scriptFilePath, fs, dmlFunctionCall);
                CtMethod m = CtNewMethod.make(functionCallMethod, ctNewScript);
                ctNewScript.addMethod(m);
                addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, false);
                addDescriptionFunctionCallMethod(fs, scriptFilePath, ctNewScript, true);
            }
        }
    } catch (LanguageException e) {
        System.out.println("Could not add function methods for " + ctNewScript.getName());
    } catch (CannotCompileException e) {
        System.out.println("Could not add function methods for " + ctNewScript.getName());
    } catch (RuntimeException e) {
        System.out.println("Could not add function methods for " + ctNewScript.getName());
    }
}
Also used : FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) FunctionStatement(org.apache.sysml.parser.FunctionStatement) Statement(org.apache.sysml.parser.Statement) ArrayList(java.util.ArrayList) CannotCompileException(javassist.CannotCompileException) LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatement(org.apache.sysml.parser.FunctionStatement) DMLProgram(org.apache.sysml.parser.DMLProgram) CtMethod(javassist.CtMethod)

Example 19 with DMLProgram

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

the class InterProceduralAnalysis method propagateStatisticsAcrossBlock.

/////////////////////////////
// INTRA-PROCEDURE ANALYSIS
//////	
/**
	 * Perform intra-procedural analysis (IPA) by propagating statistics
	 * across statement blocks.
	 *
	 * @param sb  DML statement blocks.
	 * @param fcand  Function candidates.
	 * @param callVars  Map of variables eligible for propagation.
	 * @param fcandSafeNNZ  Function candidate safe non-zeros.
	 * @param unaryFcands  Unary function candidates.
	 * @param fnStack  Function stack to determine current scope.
	 * @throws HopsException  If a HopsException occurs.
	 * @throws ParseException  If a ParseException occurs.
	 */
private void propagateStatisticsAcrossBlock(StatementBlock sb, Map<String, Integer> fcand, LocalVariableMap callVars, Map<String, Set<Long>> fcandSafeNNZ, Set<String> unaryFcands, Set<String> fnStack) throws HopsException, ParseException {
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        //old stats into predicate
        propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, wsb);
        //check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, wsb)) {
            //second pass if required
            propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
            for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
        }
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        //old stats into predicate
        propagateStatisticsAcrossPredicateDAG(isb.getPredicateHops(), callVars);
        //check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        LocalVariableMap callVarsElse = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : istmt.getIfBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
        for (StatementBlock sbi : istmt.getElseBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVarsElse, fcandSafeNNZ, unaryFcands, fnStack);
        callVars = Recompiler.reconcileUpdatedCallVarsIf(oldCallVars, callVars, callVarsElse, isb);
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (//incl parfor
    sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        //old stats into predicate
        propagateStatisticsAcrossPredicateDAG(fsb.getFromHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getToHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getIncrementHops(), callVars);
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, fsb);
        //check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, fsb))
            for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else //generic (last-level)
    {
        //remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
        //old stats in, new stats out if updated
        ArrayList<Hop> roots = sb.get_hops();
        DMLProgram prog = sb.getDMLProg();
        //replace scalar reads with literals
        Hop.resetVisitStatus(roots);
        propagateScalarsAcrossDAG(roots, callVars);
        //refresh stats across dag
        Hop.resetVisitStatus(roots);
        propagateStatisticsAcrossDAG(roots, callVars);
        //propagate stats into function calls
        Hop.resetVisitStatus(roots);
        propagateStatisticsIntoFunctions(prog, roots, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ArrayList(java.util.ArrayList) DMLProgram(org.apache.sysml.parser.DMLProgram) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Aggregations

DMLProgram (org.apache.sysml.parser.DMLProgram)19 LanguageException (org.apache.sysml.parser.LanguageException)8 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)7 DMLConfig (org.apache.sysml.conf.DMLConfig)5 DMLTranslator (org.apache.sysml.parser.DMLTranslator)5 ParseException (org.apache.sysml.parser.ParseException)5 ParserWrapper (org.apache.sysml.parser.ParserWrapper)5 FunctionStatement (org.apache.sysml.parser.FunctionStatement)4 ImportStatement (org.apache.sysml.parser.ImportStatement)4 StatementBlock (org.apache.sysml.parser.StatementBlock)4 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)4 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)4 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3