Search in sources :

Example 31 with StatementBlock

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

the class InterProceduralAnalysis method analyzeProgram.

/**
 * Main interface to perform IPA over a given DML program.
 *
 * @param repetitions number of IPA rounds
 */
public void analyzeProgram(int repetitions) {
    // sanity check for valid number of repetitions
    if (repetitions <= 0)
        throw new HopsException("Invalid number of IPA repetitions: " + repetitions);
    // perform number of requested IPA iterations
    FunctionCallSizeInfo lastSizes = null;
    for (int i = 0; i < repetitions; i++) {
        if (LOG.isDebugEnabled())
            LOG.debug("IPA: start IPA iteration " + (i + 1) + "/" + repetitions + ".");
        // get function call size infos to obtain candidates for statistics propagation
        FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph);
        if (LOG.isDebugEnabled())
            LOG.debug("IPA: Initial FunctionCallSummary: \n" + fcallSizes);
        // step 1: intra- and inter-procedural
        if (INTRA_PROCEDURAL_ANALYSIS) {
            // get unary dimension-preserving non-candidate functions
            for (String tmp : fcallSizes.getInvalidFunctions()) if (isUnarySizePreservingFunction(_prog.getFunctionStatementBlock(tmp)))
                fcallSizes.addDimsPreservingFunction(tmp);
            if (LOG.isDebugEnabled())
                LOG.debug("IPA: Extended FunctionCallSummary: \n" + fcallSizes);
            // propagate statistics and scalars into functions and across DAGs
            // (callVars used to chain outputs/inputs of multiple functions calls)
            LocalVariableMap callVars = new LocalVariableMap();
            for (// propagate stats into candidates
            StatementBlock sb : // propagate stats into candidates
            _prog.getStatementBlocks()) propagateStatisticsAcrossBlock(sb, callVars, fcallSizes, new HashSet<String>());
        }
        // step 2: apply additional IPA passes
        for (IPAPass pass : _passes) if (pass.isApplicable(_fgraph))
            pass.rewriteProgram(_prog, _fgraph, fcallSizes);
        // early abort without functions or on reached fixpoint
        if (_fgraph.getReachableFunctions().isEmpty() || (lastSizes != null && lastSizes.equals(fcallSizes))) {
            if (LOG.isDebugEnabled())
                LOG.debug("IPA: Early abort after " + (i + 1) + "/" + repetitions + " repetitions due to reached fixpoint.");
            break;
        }
    }
    // cleanup pass: remove unused functions
    FunctionCallGraph graph2 = new FunctionCallGraph(_prog);
    IPAPass rmFuns = new IPAPassRemoveUnusedFunctions();
    if (rmFuns.isApplicable(graph2))
        rmFuns.rewriteProgram(_prog, graph2, null);
}
Also used : LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HopsException(org.apache.sysml.hops.HopsException) 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) HashSet(java.util.HashSet)

Example 32 with StatementBlock

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

Example 33 with StatementBlock

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

the class SpoofCompiler method generateCode.

public static void generateCode(DMLProgram dmlprog) {
    // for each namespace, handle function statement blocks
    for (String namespaceKey : dmlprog.getNamespaces().keySet()) {
        for (String fname : dmlprog.getFunctionStatementBlocks(namespaceKey).keySet()) {
            FunctionStatementBlock fsblock = dmlprog.getFunctionStatementBlock(namespaceKey, fname);
            generateCodeFromStatementBlock(fsblock);
        }
    }
    // handle regular statement blocks in "main" method
    for (int i = 0; i < dmlprog.getNumStatementBlocks(); i++) {
        StatementBlock current = dmlprog.getStatementBlock(i);
        generateCodeFromStatementBlock(current);
    }
}
Also used : FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) 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 34 with StatementBlock

use of org.apache.sysml.parser.StatementBlock in project incubator-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 35 with StatementBlock

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

the class RewriteCompressedReblock method satisfiesAutoCompressionCondition.

private static boolean satisfiesAutoCompressionCondition(Hop hop, DMLProgram prog) {
    // check for basic compression condition
    if (!(satisfiesCompressionCondition(hop) && OptimizerUtils.isSparkExecutionMode()))
        return false;
    // determine if data size exceeds aggregate cluster storage memory
    double matrixPSize = OptimizerUtils.estimatePartitionedSizeExactSparsity(hop.getDim1(), hop.getDim2(), hop.getRowsInBlock(), hop.getColsInBlock(), hop.getNnz());
    double cacheSize = SparkExecutionContext.getDataMemoryBudget(true, true);
    boolean outOfCore = matrixPSize > cacheSize;
    // determine if matrix is ultra sparse (and hence serialized)
    double sparsity = OptimizerUtils.getSparsity(hop.getDim1(), hop.getDim2(), hop.getNnz());
    boolean ultraSparse = sparsity < MatrixBlock.ULTRA_SPARSITY_TURN_POINT;
    // but conditionally only if all other conditions are met
    if (hop.dimsKnown(true) && outOfCore && !ultraSparse) {
        // analyze program recursively, including called functions
        ProbeStatus status = new ProbeStatus(hop.getHopID(), prog);
        for (StatementBlock sb : prog.getStatementBlocks()) rAnalyzeProgram(sb, status);
        // applicable if used in loop (amortized compressed costs),
        // no conditional updates in if-else branches
        // and all operations are applicable (no decompression costs)
        boolean ret = status.foundStart && status.usedInLoop && !status.condUpdate && !status.nonApplicable;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Auto compression: " + ret + " (dimsKnown=" + hop.dimsKnown(true) + ", outOfCore=" + outOfCore + ", !ultraSparse=" + !ultraSparse + ", foundStart=" + status.foundStart + ", usedInLoop=" + status.foundStart + ", !condUpdate=" + !status.condUpdate + ", !nonApplicable=" + !status.nonApplicable + ")");
        }
        return ret;
    } else if (LOG.isDebugEnabled()) {
        LOG.debug("Auto compression: false (dimsKnown=" + hop.dimsKnown(true) + ", outOfCore=" + outOfCore + ", !ultraSparse=" + !ultraSparse + ")");
    }
    return false;
}
Also used : 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)

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