use of org.apache.sysml.parser.ExternalFunctionStatement in project incubator-systemml by apache.
the class DmlSyntacticValidator method exitExternalFunctionDefExpression.
@Override
public void exitExternalFunctionDefExpression(ExternalFunctionDefExpressionContext ctx) {
ExternalFunctionStatement functionStmt = new ExternalFunctionStatement();
ArrayList<DataIdentifier> functionInputs = getFunctionParameters(ctx.inputParams);
functionStmt.setInputParams(functionInputs);
// set function outputs
ArrayList<DataIdentifier> functionOutputs = getFunctionParameters(ctx.outputParams);
functionStmt.setOutputParams(functionOutputs);
// set function name
functionStmt.setName(ctx.name.getText());
// set other parameters
HashMap<String, String> otherParams = new HashMap<String, String>();
boolean atleastOneClassName = false;
for (StrictParameterizedKeyValueStringContext otherParamCtx : ctx.otherParams) {
String paramName = otherParamCtx.paramName.getText();
String val = "";
String text = otherParamCtx.paramVal.getText();
// First unquote the string
if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("\'") && text.endsWith("\'"))) {
if (text.length() > 2) {
val = text.substring(1, text.length() - 1);
}
// Empty value allowed
} else {
notifyErrorListeners("the value of user parameter for external function should be of type string", ctx.start);
return;
}
otherParams.put(paramName, val);
if (paramName.equals("classname")) {
atleastOneClassName = true;
}
}
functionStmt.setOtherParams(otherParams);
if (!atleastOneClassName) {
notifyErrorListeners("the parameter \'className\' needs to be passed for externalFunction", ctx.start);
return;
}
ctx.info.stmt = functionStmt;
setFileLineColumn(ctx.info.stmt, ctx);
ctx.info.functionName = ctx.name.getText();
}
use of org.apache.sysml.parser.ExternalFunctionStatement in project incubator-systemml by apache.
the class PydmlSyntacticValidator method exitExternalFunctionDefExpression.
@Override
public void exitExternalFunctionDefExpression(ExternalFunctionDefExpressionContext ctx) {
ExternalFunctionStatement functionStmt = new ExternalFunctionStatement();
ArrayList<DataIdentifier> functionInputs = getFunctionParameters(ctx.inputParams);
functionStmt.setInputParams(functionInputs);
// set function outputs
ArrayList<DataIdentifier> functionOutputs = getFunctionParameters(ctx.outputParams);
functionStmt.setOutputParams(functionOutputs);
// set function name
functionStmt.setName(ctx.name.getText());
// set other parameters
HashMap<String, String> otherParams = new HashMap<String, String>();
boolean atleastOneClassName = false;
for (StrictParameterizedKeyValueStringContext otherParamCtx : ctx.otherParams) {
String paramName = otherParamCtx.paramName.getText();
String val = "";
String text = otherParamCtx.paramVal.getText();
// First unquote the string
if ((text.startsWith("\"") && text.endsWith("\"")) || (text.startsWith("\'") && text.endsWith("\'"))) {
if (text.length() > 2) {
val = text.substring(1, text.length() - 1);
}
// Empty value allowed
} else {
notifyErrorListeners("the value of user parameter for external function should be of type str", ctx.start);
return;
}
otherParams.put(paramName, val);
if (paramName.equals("classname")) {
atleastOneClassName = true;
}
}
functionStmt.setOtherParams(otherParams);
if (!atleastOneClassName) {
notifyErrorListeners("the parameter \'className\' needs to be passed for defExternal", ctx.start);
return;
}
ctx.info.stmt = functionStmt;
setFileLineColumn(ctx.info.stmt, ctx);
ctx.info.functionName = ctx.name.getText();
}
use of org.apache.sysml.parser.ExternalFunctionStatement in project incubator-systemml by apache.
the class Explain method explain.
public static String explain(DMLProgram prog) throws HopsException, DMLRuntimeException, LanguageException {
StringBuilder sb = new StringBuilder();
//create header
sb.append("\nPROGRAM\n");
// Explain functions (if exists)
if (prog.hasFunctionStatementBlocks()) {
sb.append("--FUNCTIONS\n");
//show function call graph
sb.append("----FUNCTION CALL GRAPH\n");
sb.append("------MAIN PROGRAM\n");
FunctionCallGraph fgraph = new FunctionCallGraph(prog);
sb.append(explainFunctionCallGraph(fgraph, new HashSet<String>(), null, 3));
//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);
if (fstmt instanceof ExternalFunctionStatement)
sb.append("----EXTERNAL FUNCTION " + namespace + "::" + fname + "\n");
else {
sb.append("----FUNCTION " + namespace + "::" + fname + " [recompile=" + fsb.isRecompileOnce() + "]\n");
for (StatementBlock current : fstmt.getBody()) sb.append(explainStatementBlock(current, 3));
}
}
}
}
// Explain main program
sb.append("--MAIN PROGRAM\n");
for (StatementBlock sblk : prog.getStatementBlocks()) sb.append(explainStatementBlock(sblk, 2));
return sb.toString();
}
use of org.apache.sysml.parser.ExternalFunctionStatement in project incubator-systemml by apache.
the class InterProceduralAnalysis method propagateStatisticsIntoFunctions.
/**
* Propagate statistics from the calling program into a function
* block.
*
* @param prog The DML program.
* @param hop HOP to propagate statistics into.
* @param fcand Function candidates.
* @param callVars Calling program's 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 propagateStatisticsIntoFunctions(DMLProgram prog, Hop hop, Map<String, Integer> fcand, LocalVariableMap callVars, Map<String, Set<Long>> fcandSafeNNZ, Set<String> unaryFcands, Set<String> fnStack) throws HopsException, ParseException {
if (hop.isVisited())
return;
for (Hop c : hop.getInput()) propagateStatisticsIntoFunctions(prog, c, fcand, callVars, fcandSafeNNZ, unaryFcands, fnStack);
if (hop instanceof FunctionOp) {
//maintain counters and investigate functions if not seen so far
FunctionOp fop = (FunctionOp) hop;
String fkey = DMLProgram.constructFunctionKey(fop.getFunctionNamespace(), fop.getFunctionName());
if (fop.getFunctionType() == FunctionType.DML) {
FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fop.getFunctionNamespace(), fop.getFunctionName());
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
if (fcand.containsKey(fkey) && //prevent recursion
!fnStack.contains(fkey)) {
//maintain function call stack
fnStack.add(fkey);
//create mapping and populate symbol table for refresh
LocalVariableMap tmpVars = new LocalVariableMap();
populateLocalVariableMapForFunctionCall(fstmt, fop, callVars, tmpVars, fcandSafeNNZ.get(fkey), fcand.get(fkey));
//recursively propagate statistics
propagateStatisticsAcrossBlock(fsb, fcand, tmpVars, fcandSafeNNZ, unaryFcands, fnStack);
//extract vars from symbol table, re-map and refresh main program
extractFunctionCallReturnStatistics(fstmt, fop, tmpVars, callVars, true);
//maintain function call stack
fnStack.remove(fkey);
} else if (unaryFcands.contains(fkey)) {
extractFunctionCallEquivalentReturnStatistics(fstmt, fop, callVars);
} else {
extractFunctionCallUnknownReturnStatistics(fstmt, fop, callVars);
}
} else if (fop.getFunctionType() == FunctionType.EXTERNAL_FILE || fop.getFunctionType() == FunctionType.EXTERNAL_MEM) {
//infer output size for known external functions
FunctionStatementBlock fsb = prog.getFunctionStatementBlock(fop.getFunctionNamespace(), fop.getFunctionName());
ExternalFunctionStatement fstmt = (ExternalFunctionStatement) fsb.getStatement(0);
if (PROPAGATE_KNOWN_UDF_STATISTICS)
extractExternalFunctionCallReturnStatistics(fstmt, fop, callVars);
else
extractFunctionCallUnknownReturnStatistics(fstmt, fop, callVars);
}
}
hop.setVisited();
}
Aggregations