Search in sources :

Example 16 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class InterProceduralAnalysis method analyzeSubProgram.

public Set<String> analyzeSubProgram() {
    DMLTranslator.resetHopsDAGVisitStatus(_sb);
    // get function call size infos to obtain candidates for statistics propagation
    FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph);
    // (callVars used to chain outputs/inputs of multiple functions calls)
    if (!fcallSizes.getValidFunctions().isEmpty()) {
        LocalVariableMap callVars = new LocalVariableMap();
        propagateStatisticsAcrossBlock(_sb, callVars, fcallSizes, new HashSet<String>());
    }
    return fcallSizes.getValidFunctions();
}
Also used : LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap)

Example 17 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap 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 18 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class InterProceduralAnalysis method propagateStatisticsAcrossBlock.

// ///////////////////////////
// INTRA-PROCEDURE ANALYSIS
// ////
private void propagateStatisticsAcrossBlock(StatementBlock sb, LocalVariableMap callVars, FunctionCallSizeInfo fcallSizes, Set<String> fnStack) {
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, wsb);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, wsb)) {
            // second pass if required
            propagateStatisticsAcrossPredicateDAG(wsb.getPredicateHops(), callVars);
            for (StatementBlock sbi : wstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        }
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(isb.getPredicateHops(), callVars);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        LocalVariableMap callVarsElse = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : istmt.getIfBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        for (StatementBlock sbi : istmt.getElseBody()) propagateStatisticsAcrossBlock(sbi, callVarsElse, fcallSizes, fnStack);
        callVars = Recompiler.reconcileUpdatedCallVarsIf(oldCallVars, callVars, callVarsElse, isb);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else if (// incl parfor
    sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        // old stats into predicate
        propagateStatisticsAcrossPredicateDAG(fsb.getFromHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getToHops(), callVars);
        propagateStatisticsAcrossPredicateDAG(fsb.getIncrementHops(), callVars);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, fsb);
        // check and propagate stats into body
        LocalVariableMap oldCallVars = (LocalVariableMap) callVars.clone();
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        if (Recompiler.reconcileUpdatedCallVarsLoops(oldCallVars, callVars, fsb))
            for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
    } else // generic (last-level)
    {
        // remove updated constant scalars
        Recompiler.removeUpdatedScalars(callVars, sb);
        // old stats in, new stats out if updated
        ArrayList<Hop> roots = sb.getHops();
        DMLProgram prog = sb.getDMLProg();
        // replace scalar reads with literals
        Hop.resetVisitStatus(roots);
        propagateScalarsAcrossDAG(roots, callVars);
        // refresh stats across dag
        Hop.resetVisitStatus(roots);
        propagateStatisticsAcrossDAG(roots, callVars);
        // propagate stats into function calls
        Hop.resetVisitStatus(roots);
        propagateStatisticsIntoFunctions(prog, roots, callVars, fcallSizes, fnStack);
    }
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ArrayList(java.util.ArrayList) DMLProgram(org.apache.sysml.parser.DMLProgram) 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 19 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class InterProceduralAnalysis method isUnarySizePreservingFunction.

private boolean isUnarySizePreservingFunction(FunctionStatementBlock fsb) {
    FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
    // check unary functions over matrices
    boolean ret = (fstmt.getInputParams().size() == 1 && fstmt.getInputParams().get(0).getDataType() == DataType.MATRIX && fstmt.getOutputParams().size() == 1 && fstmt.getOutputParams().get(0).getDataType() == DataType.MATRIX);
    // check size-preserving characteristic
    if (ret) {
        FunctionCallSizeInfo fcallSizes = new FunctionCallSizeInfo(_fgraph, false);
        HashSet<String> fnStack = new HashSet<>();
        LocalVariableMap callVars = new LocalVariableMap();
        // populate input
        MatrixObject mo = createOutputMatrix(7777, 3333, -1);
        callVars.put(fstmt.getInputParams().get(0).getName(), mo);
        // propagate statistics
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
        // compare output
        MatrixObject mo2 = (MatrixObject) callVars.get(fstmt.getOutputParams().get(0).getName());
        ret &= mo.getNumRows() == mo2.getNumRows() && mo.getNumColumns() == mo2.getNumColumns();
        // reset function
        mo.getMatrixCharacteristics().setDimension(-1, -1);
        for (StatementBlock sbi : fstmt.getBody()) propagateStatisticsAcrossBlock(sbi, callVars, fcallSizes, fnStack);
    }
    return ret;
}
Also used : ExternalFunctionStatement(org.apache.sysml.parser.ExternalFunctionStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) 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 20 with LocalVariableMap

use of org.apache.sysml.runtime.controlprogram.LocalVariableMap in project incubator-systemml by apache.

the class CachedReuseVariables method reuseVariables.

public synchronized void reuseVariables(long pfid, LocalVariableMap vars, Collection<String> blacklist) {
    // check for existing reuse map
    LocalVariableMap tmp = null;
    if (_data.containsKey(pfid))
        tmp = _data.get(pfid).get();
    // build reuse map if not created yet or evicted
    if (tmp == null) {
        tmp = new LocalVariableMap(vars);
        tmp.removeAllIn((blacklist instanceof HashSet) ? (HashSet<String>) blacklist : new HashSet<>(blacklist));
        _data.put(pfid, new SoftReference<>(tmp));
    } else // reuse existing reuse map
    {
        for (String varName : tmp.keySet()) vars.put(varName, tmp.get(varName));
    }
}
Also used : LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) HashSet(java.util.HashSet)

Aggregations

LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)36 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)11 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)11 StatementBlock (org.apache.sysml.parser.StatementBlock)11 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)11 ArrayList (java.util.ArrayList)9 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)9 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)7 FunctionStatement (org.apache.sysml.parser.FunctionStatement)6 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)6 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)6 FunctionOp (org.apache.sysml.hops.FunctionOp)5 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)5 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)5 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)5 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)5 ExecutionContext (org.apache.sysml.runtime.controlprogram.context.ExecutionContext)5 Path (org.apache.hadoop.fs.Path)4