Search in sources :

Example 26 with StatementBlock

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

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

the class ResourceOptimizer method recompileProgramBlock.

private static void recompileProgramBlock(ProgramBlock pb, long cp, long mr) {
    // init compiler memory budget
    InfrastructureAnalyzer.setLocalMaxMemory(cp);
    InfrastructureAnalyzer.setRemoteMaxMemoryMap(mr);
    InfrastructureAnalyzer.setRemoteMaxMemoryReduce(mr);
    // dependent on cp, mr
    OptimizerUtils.resetDefaultSize();
    // recompile instructions (incl predicates)
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
            ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
            inst = annotateMRJobInstructions(inst, cp, mr);
            wpb.setPredicate(inst);
        }
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock sb = (IfStatementBlock) ipb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null && sb.getPredicateHops() != null) {
            ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getPredicateHops(), new LocalVariableMap(), null, false, false, 0);
            inst = annotateMRJobInstructions(inst, cp, mr);
            ipb.setPredicate(inst);
        }
    } else if (pb instanceof ForProgramBlock) {
        // incl parfor
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock sb = (ForStatementBlock) fpb.getStatementBlock();
        if (INCLUDE_PREDICATES && sb != null) {
            if (sb.getFromHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getFromHops(), new LocalVariableMap(), null, false, false, 0);
                inst = annotateMRJobInstructions(inst, cp, mr);
                fpb.setFromInstructions(inst);
            }
            if (sb.getToHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getToHops(), new LocalVariableMap(), null, false, false, 0);
                inst = annotateMRJobInstructions(inst, cp, mr);
                fpb.setToInstructions(inst);
            }
            if (sb.getIncrementHops() != null) {
                ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb.getIncrementHops(), new LocalVariableMap(), null, false, false, 0);
                inst = annotateMRJobInstructions(inst, cp, mr);
                fpb.setIncrementInstructions(inst);
            }
        }
    } else {
        // last-level program blocks
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Instruction> inst = Recompiler.recompileHopsDag(sb, sb.getHops(), new LocalVariableMap(), null, false, false, 0);
        inst = annotateMRJobInstructions(inst, cp, mr);
        pb.setInstructions(inst);
    }
    _cntCompilePB++;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ArrayList(java.util.ArrayList) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) 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 28 with StatementBlock

use of org.apache.sysml.parser.StatementBlock 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)

Example 29 with StatementBlock

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

the class IPAPassRemoveConstantBinaryOps method rRemoveConstantBinaryOp.

private static void rRemoveConstantBinaryOp(StatementBlock sb, HashMap<String, Hop> mOnes) {
    if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        for (StatementBlock c : istmt.getIfBody()) rRemoveConstantBinaryOp(c, mOnes);
        if (istmt.getElseBody() != null)
            for (StatementBlock c : istmt.getElseBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        for (StatementBlock c : wstmt.getBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else if (sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        for (StatementBlock c : fstmt.getBody()) rRemoveConstantBinaryOp(c, mOnes);
    } else {
        if (sb.getHops() != null) {
            Hop.resetVisitStatus(sb.getHops());
            for (Hop hop : sb.getHops()) rRemoveConstantBinaryOp(hop, mOnes);
        }
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) IfStatement(org.apache.sysml.parser.IfStatement) Hop(org.apache.sysml.hops.Hop) WhileStatement(org.apache.sysml.parser.WhileStatement) ForStatement(org.apache.sysml.parser.ForStatement) 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 30 with StatementBlock

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

the class IPAPassRemoveUnnecessaryCheckpoints method moveCheckpointAfterUpdate.

private static void moveCheckpointAfterUpdate(DMLProgram dmlp) {
    // approach: scan over top-level program (guaranteed to be unconditional),
    // collect checkpoints; determine if used before update; move first checkpoint
    // after update if not used before update (best effort move which often avoids
    // the second checkpoint on loops even though used in between)
    HashMap<String, Hop> chkpointCand = new HashMap<>();
    for (StatementBlock sb : dmlp.getStatementBlocks()) {
        // prune candidates (used before updated)
        Set<String> cands = new HashSet<>(chkpointCand.keySet());
        for (String cand : cands) if (sb.variablesRead().containsVariable(cand) && !sb.variablesUpdated().containsVariable(cand)) {
            // note: variableRead might include false positives due to meta
            // data operations like nrow(X) or operations removed by rewrites
            // double check hops on basic blocks; otherwise worst-case
            boolean skipRemove = false;
            if (sb.getHops() != null) {
                Hop.resetVisitStatus(sb.getHops());
                skipRemove = true;
                for (Hop root : sb.getHops()) skipRemove &= !HopRewriteUtils.rContainsRead(root, cand, false);
            }
            if (!skipRemove)
                chkpointCand.remove(cand);
        }
        // prune candidates (updated in conditional control flow)
        Set<String> cands2 = new HashSet<>(chkpointCand.keySet());
        if (sb instanceof IfStatementBlock || sb instanceof WhileStatementBlock || sb instanceof ForStatementBlock) {
            for (String cand : cands2) if (sb.variablesUpdated().containsVariable(cand)) {
                chkpointCand.remove(cand);
            }
        } else // move checkpoint after update with simple read chain
        // (note: right now this only applies if the checkpoints comes from a previous
        // statement block, within-dag checkpoints should be handled during injection)
        {
            for (String cand : cands2) if (sb.variablesUpdated().containsVariable(cand) && sb.getHops() != null) {
                Hop.resetVisitStatus(sb.getHops());
                for (Hop root : sb.getHops()) if (root.getName().equals(cand)) {
                    if (HopRewriteUtils.rHasSimpleReadChain(root, cand)) {
                        chkpointCand.get(cand).setRequiresCheckpoint(false);
                        root.getInput().get(0).setRequiresCheckpoint(true);
                        chkpointCand.put(cand, root.getInput().get(0));
                    } else
                        chkpointCand.remove(cand);
                }
            }
        }
        // collect checkpoints
        if (HopRewriteUtils.isLastLevelStatementBlock(sb)) {
            ArrayList<Hop> tmp = collectCheckpoints(sb.getHops());
            for (Hop chkpoint : tmp) chkpointCand.put(chkpoint.getName(), chkpoint);
        }
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) HashMap(java.util.HashMap) Hop(org.apache.sysml.hops.Hop) 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) HashSet(java.util.HashSet) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Aggregations

StatementBlock (org.apache.sysml.parser.StatementBlock)67 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)57 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)57 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)57 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)39 Hop (org.apache.sysml.hops.Hop)28 ArrayList (java.util.ArrayList)24 FunctionStatement (org.apache.sysml.parser.FunctionStatement)22 IfStatement (org.apache.sysml.parser.IfStatement)22 ForStatement (org.apache.sysml.parser.ForStatement)20 WhileStatement (org.apache.sysml.parser.WhileStatement)19 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)18 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)18 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)16 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)16 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)13 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)13 HashSet (java.util.HashSet)11 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)11 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)11