Search in sources :

Example 11 with IfStatement

use of org.apache.sysml.parser.IfStatement 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 12 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class PydmlSyntacticValidator method exitElifBranch.

@Override
public void exitElifBranch(ElifBranchContext ctx) {
    IfStatement elifStmt = new IfStatement();
    ConditionalPredicate predicate = new ConditionalPredicate(ctx.predicate.info.expr);
    elifStmt.setConditionalPredicate(predicate);
    elifStmt.setCtxValuesAndFilename(ctx, currentFile);
    if (ctx.elifBody.size() > 0) {
        for (StatementContext stmtCtx : ctx.elifBody) {
            elifStmt.addStatementBlockIfBody(getStatementBlock(stmtCtx.info.stmt));
        }
        elifStmt.mergeStatementBlocksIfBody();
    }
    ctx.info.stmt = elifStmt;
    setFileLineColumn(ctx.info.stmt, ctx);
}
Also used : IfStatement(org.apache.sysml.parser.IfStatement) ConditionalPredicate(org.apache.sysml.parser.ConditionalPredicate) FunctionStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionStatementContext) ImportStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ImportStatementContext) AssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.AssignmentStatementContext) PathStatementContext(org.apache.sysml.parser.pydml.PydmlParser.PathStatementContext) FunctionCallMultiAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallMultiAssignmentStatementContext) IfStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfStatementContext) IfdefAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.IfdefAssignmentStatementContext) ForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ForStatementContext) StatementContext(org.apache.sysml.parser.pydml.PydmlParser.StatementContext) ParForStatementContext(org.apache.sysml.parser.pydml.PydmlParser.ParForStatementContext) FunctionCallAssignmentStatementContext(org.apache.sysml.parser.pydml.PydmlParser.FunctionCallAssignmentStatementContext) WhileStatementContext(org.apache.sysml.parser.pydml.PydmlParser.WhileStatementContext)

Example 13 with IfStatement

use of org.apache.sysml.parser.IfStatement in project 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 14 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class RewriteCompressedReblock method rAnalyzeProgram.

private static void rAnalyzeProgram(StatementBlock sb, ProbeStatus status) {
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock csb : fstmt.getBody()) rAnalyzeProgram(csb, status);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        for (StatementBlock csb : wstmt.getBody()) rAnalyzeProgram(csb, status);
        if (wsb.variablesRead().containsAnyName(status.compMtx))
            status.usedInLoop = true;
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        for (StatementBlock csb : istmt.getIfBody()) rAnalyzeProgram(csb, status);
        for (StatementBlock csb : istmt.getElseBody()) rAnalyzeProgram(csb, status);
        if (isb.variablesUpdated().containsAnyName(status.compMtx))
            status.condUpdate = true;
    } else if (sb instanceof ForStatementBlock) {
        // incl parfor
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        for (StatementBlock csb : fstmt.getBody()) rAnalyzeProgram(csb, status);
        if (fsb.variablesRead().containsAnyName(status.compMtx))
            status.usedInLoop = true;
    } else if (sb.getHops() != null) {
        // generic (last-level)
        ArrayList<Hop> roots = sb.getHops();
        Hop.resetVisitStatus(roots);
        // process entire HOP DAG starting from the roots
        for (Hop root : roots) rAnalyzeHopDag(root, status);
        // remove temporary variables
        status.compMtx.removeIf(n -> n.startsWith(TMP_PREFIX));
        Hop.resetVisitStatus(roots);
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) Hop(org.apache.sysml.hops.Hop) 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)

Example 15 with IfStatement

use of org.apache.sysml.parser.IfStatement in project systemml by apache.

the class ProgramRewriter method rRewriteStatementBlock.

public ArrayList<StatementBlock> rRewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status, boolean splitDags) {
    ArrayList<StatementBlock> ret = new ArrayList<>();
    ret.add(sb);
    // recursive invocation
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        fstmt.setBody(rRewriteStatementBlocks(fstmt.getBody(), status, splitDags));
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wstmt.setBody(rRewriteStatementBlocks(wstmt.getBody(), status, splitDags));
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        istmt.setIfBody(rRewriteStatementBlocks(istmt.getIfBody(), status, splitDags));
        istmt.setElseBody(rRewriteStatementBlocks(istmt.getElseBody(), status, splitDags));
    } else if (sb instanceof ForStatementBlock) {
        // incl parfor
        // maintain parfor context information (e.g., for checkpointing)
        boolean prestatus = status.isInParforContext();
        if (sb instanceof ParForStatementBlock)
            status.setInParforContext(true);
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fstmt.setBody(rRewriteStatementBlocks(fstmt.getBody(), status, splitDags));
        status.setInParforContext(prestatus);
    }
    // apply rewrite rules to individual statement blocks
    for (StatementBlockRewriteRule r : _sbRuleSet) {
        if (!splitDags && r.createsSplitDag())
            continue;
        ArrayList<StatementBlock> tmp = new ArrayList<>();
        for (StatementBlock sbc : ret) tmp.addAll(r.rewriteStatementBlock(sbc, status));
        // take over set of rewritten sbs
        ret.clear();
        ret.addAll(tmp);
    }
    return ret;
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ArrayList(java.util.ArrayList) WhileStatement(org.apache.sysml.parser.WhileStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatement(org.apache.sysml.parser.ForStatement) 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) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Aggregations

IfStatement (org.apache.sysml.parser.IfStatement)44 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)38 StatementBlock (org.apache.sysml.parser.StatementBlock)38 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)36 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)36 ForStatement (org.apache.sysml.parser.ForStatement)33 WhileStatement (org.apache.sysml.parser.WhileStatement)33 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)28 FunctionStatement (org.apache.sysml.parser.FunctionStatement)23 Hop (org.apache.sysml.hops.Hop)16 ArrayList (java.util.ArrayList)13 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)12 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)9 ConditionalPredicate (org.apache.sysml.parser.ConditionalPredicate)6 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)6 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)6 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)6 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)6 DMLProgram (org.apache.sysml.parser.DMLProgram)5 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)5