Search in sources :

Example 16 with WhileStatementBlock

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

the class Recompiler method rRecompileProgramBlock.

// ////////////////////////////
// private helper functions //
// ////////////////////////////
private static void rRecompileProgramBlock(ProgramBlock pb, LocalVariableMap vars, RecompileStatus status, long tid, ResetType resetRecompile) {
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
        // recompile predicate
        recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
        // remove updated scalars because in loop
        removeUpdatedScalars(vars, wsb);
        // copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, wsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, wsb)) {
            // second pass with unknowns if required
            recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, wsb);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
        // recompile predicate
        recompileIfPredicate(ipb, isb, vars, status, tid, resetRecompile);
        // copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        LocalVariableMap varsElse = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        RecompileStatus statusElse = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : ipb.getChildBlocksIfBody()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        for (ProgramBlock pb2 : ipb.getChildBlocksElseBody()) rRecompileProgramBlock(pb2, varsElse, statusElse, tid, resetRecompile);
        reconcileUpdatedCallVarsIf(oldVars, vars, varsElse, isb);
        reconcileUpdatedCallVarsIf(oldStatus, status, statusElse, isb);
        removeUpdatedScalars(vars, ipb.getStatementBlock());
    } else if (// includes ParFORProgramBlock
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
        // recompile predicates
        recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
        // remove updated scalars because in loop
        removeUpdatedScalars(vars, fpb.getStatementBlock());
        // copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, fsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, fsb)) {
            // second pass with unknowns if required
            recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, fpb.getStatementBlock());
    } else if (// includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP
    pb instanceof FunctionProgramBlock) {
    // do nothing
    } else {
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Instruction> tmp = pb.getInstructions();
        if (sb == null)
            return;
        // recompile all for stats propagation and recompile flags
        tmp = Recompiler.recompileHopsDag(sb, sb.getHops(), vars, status, true, false, tid);
        pb.setInstructions(tmp);
        // propagate stats across hops (should be executed on clone of vars)
        Recompiler.extractDAGOutputStatistics(sb.getHops(), vars);
        // reset recompilation flags (w/ special handling functions)
        if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.getHops()) && resetRecompile.isReset()) {
            Hop.resetRecompilationFlag(sb.getHops(), ExecType.CP, resetRecompile);
            sb.updateRecompilationFlag();
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ArrayList(java.util.ArrayList) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) 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 17 with WhileStatementBlock

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

the class ProgramRewriter method rRewriteStatementBlockHopDAGs.

public void rRewriteStatementBlockHopDAGs(StatementBlock current, ProgramRewriteStatus state) {
    // ensure robustness for calls from outside
    if (state == null)
        state = new ProgramRewriteStatus();
    if (current instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) current;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        for (StatementBlock sb : fstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
    } else if (current instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) current;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wsb.setPredicateHops(rewriteHopDAG(wsb.getPredicateHops(), state));
        for (StatementBlock sb : wstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
    } else if (current instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) current;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        isb.setPredicateHops(rewriteHopDAG(isb.getPredicateHops(), state));
        for (StatementBlock sb : istmt.getIfBody()) rRewriteStatementBlockHopDAGs(sb, state);
        for (StatementBlock sb : istmt.getElseBody()) rRewriteStatementBlockHopDAGs(sb, state);
    } else if (// incl parfor
    current instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) current;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fsb.setFromHops(rewriteHopDAG(fsb.getFromHops(), state));
        fsb.setToHops(rewriteHopDAG(fsb.getToHops(), state));
        fsb.setIncrementHops(rewriteHopDAG(fsb.getIncrementHops(), state));
        for (StatementBlock sb : fstmt.getBody()) rRewriteStatementBlockHopDAGs(sb, state);
    } else // generic (last-level)
    {
        current.setHops(rewriteHopDAG(current.getHops(), state));
    }
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) 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) 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)

Example 18 with WhileStatementBlock

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

the class WhileProgramBlock method executePredicate.

private BooleanObject executePredicate(ExecutionContext ec) {
    BooleanObject result = null;
    try {
        if (_sb != null) {
            if (// set program block specific remote memory
            DMLScript.isActiveAM())
                DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
            WhileStatementBlock wsb = (WhileStatementBlock) _sb;
            Hop predicateOp = wsb.getPredicateHops();
            boolean recompile = wsb.requiresPredicateRecompilation();
            result = (BooleanObject) executePredicate(_predicate, predicateOp, recompile, ValueType.BOOLEAN, ec);
        } else
            result = (BooleanObject) executePredicate(_predicate, null, false, ValueType.BOOLEAN, ec);
    } catch (Exception ex) {
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Failed to evaluate the while predicate.", ex);
    }
    // (guaranteed to be non-null, see executePredicate/getScalarInput)
    return result;
}
Also used : Hop(org.apache.sysml.hops.Hop) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) BooleanObject(org.apache.sysml.runtime.instructions.cp.BooleanObject) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 19 with WhileStatementBlock

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

the class RewriteInjectSparkLoopCheckpointing method rewriteStatementBlock.

@Override
public List<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) {
    if (!OptimizerUtils.isSparkExecutionMode()) {
        // nothing to do here, return original statement block
        return Arrays.asList(sb);
    }
    // 1) We currently add checkpoint operations without information about the global program structure,
    // this assumes that redundant checkpointing is prevented at runtime level (instruction-level)
    // 2) Also, we do not take size information into account right now. This means that all candidates
    // are checkpointed even if they are only used by CP operations.
    ArrayList<StatementBlock> ret = new ArrayList<>();
    // block size set by reblock rewrite
    int blocksize = status.getBlocksize();
    // optimization because otherwise we would prevent remote parfor)
    if (// incl parfor
    (sb instanceof WhileStatementBlock || sb instanceof ForStatementBlock) && (_checkCtx ? !status.isInParforContext() : true)) {
        // step 1: determine checkpointing candidates
        ArrayList<String> candidates = new ArrayList<>();
        VariableSet read = sb.variablesRead();
        VariableSet updated = sb.variablesUpdated();
        for (String rvar : read.getVariableNames()) if (!updated.containsVariable(rvar) && read.getVariable(rvar).getDataType() == DataType.MATRIX)
            candidates.add(rvar);
        // step 2: insert statement block with checkpointing operations
        if (// existing candidates
        !candidates.isEmpty()) {
            StatementBlock sb0 = new StatementBlock();
            sb0.setDMLProg(sb.getDMLProg());
            sb0.setParseInfo(sb);
            ArrayList<Hop> hops = new ArrayList<>();
            VariableSet livein = new VariableSet();
            VariableSet liveout = new VariableSet();
            for (String var : candidates) {
                DataIdentifier dat = read.getVariable(var);
                long dim1 = (dat instanceof IndexedIdentifier) ? ((IndexedIdentifier) dat).getOrigDim1() : dat.getDim1();
                long dim2 = (dat instanceof IndexedIdentifier) ? ((IndexedIdentifier) dat).getOrigDim2() : dat.getDim2();
                DataOp tread = new DataOp(var, DataType.MATRIX, ValueType.DOUBLE, DataOpTypes.TRANSIENTREAD, dat.getFilename(), dim1, dim2, dat.getNnz(), blocksize, blocksize);
                tread.setRequiresCheckpoint(true);
                DataOp twrite = new DataOp(var, DataType.MATRIX, ValueType.DOUBLE, tread, DataOpTypes.TRANSIENTWRITE, null);
                HopRewriteUtils.setOutputParameters(twrite, dim1, dim2, blocksize, blocksize, dat.getNnz());
                hops.add(twrite);
                livein.addVariable(var, read.getVariable(var));
                liveout.addVariable(var, read.getVariable(var));
            }
            sb0.setHops(hops);
            sb0.setLiveIn(livein);
            sb0.setLiveOut(liveout);
            sb0.setSplitDag(true);
            ret.add(sb0);
            // maintain rewrite status
            status.setInjectedCheckpoints();
        }
    }
    // add original statement block to end
    ret.add(sb);
    return ret;
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) DataIdentifier(org.apache.sysml.parser.DataIdentifier) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) VariableSet(org.apache.sysml.parser.VariableSet) DataOp(org.apache.sysml.hops.DataOp) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) StatementBlock(org.apache.sysml.parser.StatementBlock) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock) IndexedIdentifier(org.apache.sysml.parser.IndexedIdentifier)

Example 20 with WhileStatementBlock

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

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