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;
}
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;
}
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();
}
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();
}
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);
}
}
}
Aggregations