Search in sources :

Example 11 with FunctionStatementBlock

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

the class DMLParserWrapper method createDMLProgram.

private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
    DMLProgram dmlPgm = new DMLProgram();
    String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
    dmlPgm.getNamespaces().put(namespace, dmlPgm);
    // First add all the functions
    for (FunctionStatementContext fn : ast.functionBlocks) {
        FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
        functionStmtBlk.addStatement(fn.info.stmt);
        try {
            dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
        } catch (LanguageException e) {
            LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
            return null;
        }
    }
    // Then add all the statements
    for (StatementContext stmtCtx : ast.blocks) {
        org.apache.sysml.parser.Statement current = stmtCtx.info.stmt;
        if (current == null) {
            LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
            return null;
        }
        if (current instanceof ImportStatement) {
            // Handle import statements separately
            if (stmtCtx.info.namespaces != null) {
                // Add the DMLProgram entries into current program
                for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
                    // TODO handle namespace key already exists for different program value instead of overwriting
                    DMLProgram prog = entry.getValue();
                    if (prog != null && prog.getNamespaces().size() > 0) {
                        dmlPgm.getNamespaces().put(entry.getKey(), prog);
                    }
                    // Add dependent programs (handle imported script that also imports scripts)
                    for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
                        String depNamespace = dependency.getKey();
                        DMLProgram depProgram = dependency.getValue();
                        if (dmlPgm.getNamespaces().get(depNamespace) == null) {
                            dmlPgm.getNamespaces().put(depNamespace, depProgram);
                        }
                    }
                }
            } else {
                LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
                return null;
            }
        }
        // Now wrap statement into individual statement block
        // merge statement will take care of merging these blocks
        dmlPgm.addStatementBlock(getStatementBlock(current));
    }
    // post-processing
    dmlPgm.hoistFunctionCallsFromExpressions();
    dmlPgm.mergeStatementBlocks();
    return dmlPgm;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.dml.DmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.dml.DmlParser.StatementContext)

Example 12 with FunctionStatementBlock

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

the class PyDMLParserWrapper method createDMLProgram.

private static DMLProgram createDMLProgram(ProgramrootContext ast, String sourceNamespace) {
    DMLProgram dmlPgm = new DMLProgram();
    String namespace = (sourceNamespace != null && sourceNamespace.length() > 0) ? sourceNamespace : DMLProgram.DEFAULT_NAMESPACE;
    dmlPgm.getNamespaces().put(namespace, dmlPgm);
    // First add all the functions
    for (FunctionStatementContext fn : ast.functionBlocks) {
        FunctionStatementBlock functionStmtBlk = new FunctionStatementBlock();
        functionStmtBlk.addStatement(fn.info.stmt);
        try {
            dmlPgm.addFunctionStatementBlock(namespace, fn.info.functionName, functionStmtBlk);
        } catch (LanguageException e) {
            LOG.error("line: " + fn.start.getLine() + ":" + fn.start.getCharPositionInLine() + " cannot process the function " + fn.info.functionName);
            return null;
        }
    }
    // Then add all the statements
    for (StatementContext stmtCtx : ast.blocks) {
        Statement current = stmtCtx.info.stmt;
        if (current == null) {
            LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the statement");
            return null;
        }
        // Ignore Newline logic
        if (current.isEmptyNewLineStatement()) {
            continue;
        }
        if (current instanceof ImportStatement) {
            // Handle import statements separately
            if (stmtCtx.info.namespaces != null) {
                // Add the DMLProgram entries into current program
                for (Map.Entry<String, DMLProgram> entry : stmtCtx.info.namespaces.entrySet()) {
                    // TODO handle namespace key already exists for different program value instead of overwriting
                    DMLProgram prog = entry.getValue();
                    if (prog != null && prog.getNamespaces().size() > 0) {
                        dmlPgm.getNamespaces().put(entry.getKey(), prog);
                    }
                    // Add dependent programs (handle imported script that also imports scripts)
                    for (Map.Entry<String, DMLProgram> dependency : entry.getValue().getNamespaces().entrySet()) {
                        String depNamespace = dependency.getKey();
                        DMLProgram depProgram = dependency.getValue();
                        if (dmlPgm.getNamespaces().get(depNamespace) == null) {
                            dmlPgm.getNamespaces().put(depNamespace, depProgram);
                        }
                    }
                }
            } else {
                LOG.error("line: " + stmtCtx.start.getLine() + ":" + stmtCtx.start.getCharPositionInLine() + " cannot process the import statement");
                return null;
            }
        }
        // Now wrap statement into individual statement block
        // merge statement will take care of merging these blocks
        dmlPgm.addStatementBlock(getStatementBlock(current));
    }
    // post-processing
    dmlPgm.hoistFunctionCallsFromExpressions();
    dmlPgm.mergeStatementBlocks();
    return dmlPgm;
}
Also used : LanguageException(org.apache.sysml.parser.LanguageException) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ImportStatement(org.apache.sysml.parser.ImportStatement) Statement(org.apache.sysml.parser.Statement) DMLProgram(org.apache.sysml.parser.DMLProgram) ImportStatement(org.apache.sysml.parser.ImportStatement) Map(java.util.Map) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext)

Example 13 with FunctionStatementBlock

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

the class Explain method getHopDAG.

public static String getHopDAG(DMLProgram prog, ArrayList<Integer> lines, boolean withSubgraph) {
    StringBuilder sb = new StringBuilder();
    StringBuilder nodes = new StringBuilder();
    // create header
    sb.append("digraph {");
    // Explain functions (if exists)
    if (prog.hasFunctionStatementBlocks()) {
        // show individual functions
        for (String namespace : prog.getNamespaces().keySet()) {
            for (String fname : prog.getFunctionStatementBlocks(namespace).keySet()) {
                FunctionStatementBlock fsb = prog.getFunctionStatementBlock(namespace, fname);
                FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
                String fkey = DMLProgram.constructFunctionKey(namespace, fname);
                if (!(fstmt instanceof ExternalFunctionStatement)) {
                    addSubGraphHeader(sb, withSubgraph);
                    for (StatementBlock current : fstmt.getBody()) sb.append(getHopDAG(current, nodes, lines, withSubgraph));
                    String label = "FUNCTION " + fkey + " recompile=" + fsb.isRecompileOnce() + "\n";
                    addSubGraphFooter(sb, withSubgraph, label);
                }
            }
        }
    }
    // Explain main program
    for (StatementBlock sblk : prog.getStatementBlocks()) sb.append(getHopDAG(sblk, nodes, lines, withSubgraph));
    sb.append(nodes);
    sb.append("rankdir = \"BT\"\n");
    sb.append("}\n");
    return sb.toString();
}
Also used : ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock)

Example 14 with FunctionStatementBlock

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

the class Explain method explainStatementBlock.

private static String explainStatementBlock(StatementBlock sb, int level) {
    StringBuilder builder = new StringBuilder();
    String offset = createOffset(level);
    if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        builder.append(offset);
        if (!wsb.getUpdateInPlaceVars().isEmpty())
            builder.append("WHILE (lines " + wsb.getBeginLine() + "-" + wsb.getEndLine() + ") [in-place=" + wsb.getUpdateInPlaceVars().toString() + "]\n");
        else
            builder.append("WHILE (lines " + wsb.getBeginLine() + "-" + wsb.getEndLine() + ")\n");
        builder.append(explainHop(wsb.getPredicateHops(), level + 1));
        WhileStatement ws = (WhileStatement) sb.getStatement(0);
        for (StatementBlock current : ws.getBody()) builder.append(explainStatementBlock(current, level + 1));
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock ifsb = (IfStatementBlock) sb;
        builder.append(offset);
        builder.append("IF (lines " + ifsb.getBeginLine() + "-" + ifsb.getEndLine() + ")\n");
        builder.append(explainHop(ifsb.getPredicateHops(), level + 1));
        IfStatement ifs = (IfStatement) sb.getStatement(0);
        for (StatementBlock current : ifs.getIfBody()) builder.append(explainStatementBlock(current, level + 1));
        if (!ifs.getElseBody().isEmpty()) {
            builder.append(offset);
            builder.append("ELSE\n");
        }
        for (StatementBlock current : ifs.getElseBody()) builder.append(explainStatementBlock(current, level + 1));
    } else if (sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        builder.append(offset);
        if (sb instanceof ParForStatementBlock) {
            if (!fsb.getUpdateInPlaceVars().isEmpty())
                builder.append("PARFOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ") [in-place=" + fsb.getUpdateInPlaceVars().toString() + "]\n");
            else
                builder.append("PARFOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ")\n");
        } else {
            if (!fsb.getUpdateInPlaceVars().isEmpty())
                builder.append("FOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ") [in-place=" + fsb.getUpdateInPlaceVars().toString() + "]\n");
            else
                builder.append("FOR (lines " + fsb.getBeginLine() + "-" + fsb.getEndLine() + ")\n");
        }
        if (fsb.getFromHops() != null)
            builder.append(explainHop(fsb.getFromHops(), level + 1));
        if (fsb.getToHops() != null)
            builder.append(explainHop(fsb.getToHops(), level + 1));
        if (fsb.getIncrementHops() != null)
            builder.append(explainHop(fsb.getIncrementHops(), level + 1));
        ForStatement fs = (ForStatement) sb.getStatement(0);
        for (StatementBlock current : fs.getBody()) builder.append(explainStatementBlock(current, level + 1));
    } else if (sb instanceof FunctionStatementBlock) {
        FunctionStatement fsb = (FunctionStatement) sb.getStatement(0);
        for (StatementBlock current : fsb.getBody()) builder.append(explainStatementBlock(current, level + 1));
    } else {
        // For generic StatementBlock
        builder.append(offset);
        builder.append("GENERIC (lines " + sb.getBeginLine() + "-" + sb.getEndLine() + ") [recompile=" + sb.requiresRecompilation() + "]\n");
        ArrayList<Hop> hopsDAG = sb.getHops();
        if (hopsDAG != null && !hopsDAG.isEmpty()) {
            Hop.resetVisitStatus(hopsDAG);
            for (Hop hop : hopsDAG) builder.append(explainHop(hop, level + 1));
            Hop.resetVisitStatus(hopsDAG);
        }
    }
    return builder.toString();
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) WhileStatement(org.apache.sysml.parser.WhileStatement) IfStatement(org.apache.sysml.parser.IfStatement) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatement(org.apache.sysml.parser.ForStatement) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 15 with FunctionStatementBlock

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

the class IPAPassPropagateReplaceLiterals method rewriteProgram.

@Override
public void rewriteProgram(DMLProgram prog, FunctionCallGraph fgraph, FunctionCallSizeInfo fcallSizes) {
    for (String fkey : fgraph.getReachableFunctions()) {
        FunctionOp first = fgraph.getFunctionCalls(fkey).get(0);
        // propagate and replace amenable literals into function
        if (fcallSizes.hasSafeLiterals(fkey)) {
            FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fkey);
            FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
            ArrayList<DataIdentifier> finputs = fstmt.getInputParams();
            // populate call vars with amenable literals
            LocalVariableMap callVars = new LocalVariableMap();
            for (int j = 0; j < finputs.size(); j++) if (fcallSizes.isSafeLiteral(fkey, j)) {
                LiteralOp lit = (LiteralOp) first.getInput().get(j);
                callVars.put(finputs.get(j).getName(), ScalarObjectFactory.createScalarObject(lit.getValueType(), lit));
            }
            // propagate and replace literals
            for (StatementBlock sb : fstmt.getBody()) rReplaceLiterals(sb, callVars);
        }
    }
}
Also used : FunctionStatement(org.apache.sysml.parser.FunctionStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) FunctionOp(org.apache.sysml.hops.FunctionOp) LiteralOp(org.apache.sysml.hops.LiteralOp) 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)

Aggregations

FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)33 FunctionStatement (org.apache.sysml.parser.FunctionStatement)24 StatementBlock (org.apache.sysml.parser.StatementBlock)24 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)22 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)22 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)22 IfStatement (org.apache.sysml.parser.IfStatement)14 ForStatement (org.apache.sysml.parser.ForStatement)12 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)12 WhileStatement (org.apache.sysml.parser.WhileStatement)12 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)11 Hop (org.apache.sysml.hops.Hop)10 ArrayList (java.util.ArrayList)9 FunctionOp (org.apache.sysml.hops.FunctionOp)9 DMLProgram (org.apache.sysml.parser.DMLProgram)7 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)6 LanguageException (org.apache.sysml.parser.LanguageException)4 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)4 HashSet (java.util.HashSet)3 LiteralOp (org.apache.sysml.hops.LiteralOp)3