Search in sources :

Example 1 with ExternalFunctionStatement

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();
}
Also used : StrictParameterizedKeyValueStringContext(org.apache.sysml.parser.dml.DmlParser.StrictParameterizedKeyValueStringContext) DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement)

Example 2 with ExternalFunctionStatement

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();
}
Also used : StrictParameterizedKeyValueStringContext(org.apache.sysml.parser.pydml.PydmlParser.StrictParameterizedKeyValueStringContext) DataIdentifier(org.apache.sysml.parser.DataIdentifier) HashMap(java.util.HashMap) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement)

Example 3 with ExternalFunctionStatement

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();
}
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) FunctionCallGraph(org.apache.sysml.hops.ipa.FunctionCallGraph) 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) HashSet(java.util.HashSet)

Example 4 with ExternalFunctionStatement

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();
}
Also used : ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) Hop(org.apache.sysml.hops.Hop) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionOp(org.apache.sysml.hops.FunctionOp)

Aggregations

ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)4 HashMap (java.util.HashMap)2 DataIdentifier (org.apache.sysml.parser.DataIdentifier)2 FunctionStatement (org.apache.sysml.parser.FunctionStatement)2 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)2 HashSet (java.util.HashSet)1 FunctionOp (org.apache.sysml.hops.FunctionOp)1 Hop (org.apache.sysml.hops.Hop)1 FunctionCallGraph (org.apache.sysml.hops.ipa.FunctionCallGraph)1 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)1 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)1 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)1 StatementBlock (org.apache.sysml.parser.StatementBlock)1 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)1 StrictParameterizedKeyValueStringContext (org.apache.sysml.parser.dml.DmlParser.StrictParameterizedKeyValueStringContext)1 StrictParameterizedKeyValueStringContext (org.apache.sysml.parser.pydml.PydmlParser.StrictParameterizedKeyValueStringContext)1 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)1