Search in sources :

Example 21 with WhileStatementBlock

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

the class ResourceConfig method addProgramBlock.

private void addProgramBlock(ProgramBlock pb, long init) {
    if (pb instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock fpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && wsb != null && wsb.getPredicateHops() != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock fpb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && isb != null && isb.getPredicateHops() != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocksIfBody(), init);
        addProgramBlocks(fpb.getChildBlocksElseBody(), init);
    } else if (// incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
        if (ResourceOptimizer.INCLUDE_PREDICATES && fsb != null)
            _mrres.add(init);
        addProgramBlocks(fpb.getChildBlocks(), init);
    } else {
        // for objects hash is unique because memory location used
        _mrres.add(init);
    }
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IfStatementBlock(org.apache.sysml.parser.IfStatementBlock)

Example 22 with WhileStatementBlock

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

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

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

Example 25 with WhileStatementBlock

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

the class SpoofCompiler method generateCodeFromStatementBlock.

public static void generateCodeFromStatementBlock(StatementBlock current) {
    if (current instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) current;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sb : fstmt.getBody()) generateCodeFromStatementBlock(sb);
    } else if (current instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) current;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wsb.setPredicateHops(optimize(wsb.getPredicateHops(), false));
        for (StatementBlock sb : wstmt.getBody()) generateCodeFromStatementBlock(sb);
    } else if (current instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) current;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        isb.setPredicateHops(optimize(isb.getPredicateHops(), false));
        for (StatementBlock sb : istmt.getIfBody()) generateCodeFromStatementBlock(sb);
        for (StatementBlock sb : istmt.getElseBody()) generateCodeFromStatementBlock(sb);
    } else if (// incl parfor
    current instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) current;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fsb.setFromHops(optimize(fsb.getFromHops(), false));
        fsb.setToHops(optimize(fsb.getToHops(), false));
        fsb.setIncrementHops(optimize(fsb.getIncrementHops(), false));
        for (StatementBlock sb : fstmt.getBody()) generateCodeFromStatementBlock(sb);
    } else // generic (last-level)
    {
        current.setHops(generateCodeFromHopDAGs(current.getHops()));
        current.updateRecompilationFlag();
    }
}
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) 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)

Aggregations

WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)41 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)38 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)36 StatementBlock (org.apache.sysml.parser.StatementBlock)36 ForStatement (org.apache.sysml.parser.ForStatement)21 IfStatement (org.apache.sysml.parser.IfStatement)21 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)20 WhileStatement (org.apache.sysml.parser.WhileStatement)20 Hop (org.apache.sysml.hops.Hop)17 ArrayList (java.util.ArrayList)15 FunctionStatement (org.apache.sysml.parser.FunctionStatement)14 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)14 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)13 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)13 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)9 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)8 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)7 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)7 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)6 HashMap (java.util.HashMap)5