Search in sources :

Example 1 with ParForStatementBlock

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

the class ProgramRewriter method rewriteStatementBlock.

private ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) throws HopsException {
    ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
    ret.add(sb);
    //recursive invocation
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
    } else if (sb instanceof WhileStatementBlock) {
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        wstmt.setBody(rewriteStatementBlocks(wstmt.getBody(), status));
    } else if (sb instanceof IfStatementBlock) {
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        istmt.setIfBody(rewriteStatementBlocks(istmt.getIfBody(), status));
        istmt.setElseBody(rewriteStatementBlocks(istmt.getElseBody(), status));
    } else if (//incl parfor
    sb instanceof ForStatementBlock) {
        //maintain parfor context information (e.g., for checkpointing)
        boolean prestatus = status.isInParforContext();
        if (sb instanceof ParForStatementBlock)
            status.setInParforContext(true);
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fstmt = (ForStatement) fsb.getStatement(0);
        fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
        status.setInParforContext(prestatus);
    }
    //apply rewrite rules
    for (StatementBlockRewriteRule r : _sbRuleSet) {
        ArrayList<StatementBlock> tmp = new ArrayList<StatementBlock>();
        for (StatementBlock sbc : ret) tmp.addAll(r.rewriteStatementBlock(sbc, status));
        //take over set of rewritten sbs		
        ret.clear();
        ret.addAll(tmp);
    }
    return ret;
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionStatementBlock(org.apache.sysml.parser.FunctionStatementBlock) ArrayList(java.util.ArrayList) WhileStatement(org.apache.sysml.parser.WhileStatement) FunctionStatement(org.apache.sysml.parser.FunctionStatement) IfStatement(org.apache.sysml.parser.IfStatement) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) 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 2 with ParForStatementBlock

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

the class OptimizerRuleBased method removeRecursiveParFor.

protected int removeRecursiveParFor(OptNode n, HashSet<ParForProgramBlock> recPBs) {
    int count = 0;
    if (!n.isLeaf()) {
        for (OptNode sub : n.getChilds()) {
            if (sub.getNodeType() == NodeType.PARFOR) {
                long id = sub.getID();
                Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
                ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
                ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
                if (recPBs.contains(pfpb)) {
                    // create for pb as replacement
                    Program prog = pfpb.getProgram();
                    ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
                    // replace parfor with for, and update objectmapping
                    OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
                    // update link to statement block
                    fpb.setStatementBlock(pfsb);
                    // update node
                    sub.setNodeType(NodeType.FOR);
                    sub.setK(1);
                    count++;
                }
            }
            count += removeRecursiveParFor(sub, recPBs);
        }
    }
    return count;
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 3 with ParForStatementBlock

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

the class OptimizerRuleBased method removeUnnecessaryParFor.

protected int removeUnnecessaryParFor(OptNode n) {
    int count = 0;
    if (!n.isLeaf()) {
        for (OptNode sub : n.getChilds()) {
            if (sub.getNodeType() == NodeType.PARFOR && sub.getK() == 1) {
                long id = sub.getID();
                Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
                ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
                ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
                // create for pb as replacement
                Program prog = pfpb.getProgram();
                ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
                // replace parfor with for, and update objectmapping
                OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
                // update link to statement block
                fpb.setStatementBlock(pfsb);
                // update node
                sub.setNodeType(NodeType.FOR);
                sub.setK(1);
                count++;
            }
            count += removeUnnecessaryParFor(sub);
        }
    }
    return count;
}
Also used : DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 4 with ParForStatementBlock

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

the class OptimizerRuleBased method rewriteSetSparkEagerRDDCaching.

// /////
// REWRITE set spark eager rdd caching
// /
protected void rewriteSetSparkEagerRDDCaching(OptNode n, LocalVariableMap vars) {
    // get program blocks of root parfor
    Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(n.getID());
    ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
    ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
    ArrayList<String> ret = new ArrayList<>();
    if (// spark exec mode
    OptimizerUtils.isSparkExecutionMode() && // local parfor
    n.getExecType() == ExecType.CP && // at least 2 iterations
    _N > 1) {
        Set<String> cand = pfsb.variablesRead().getVariableNames();
        Collection<String> rpVars = pfpb.getSparkRepartitionVariables();
        for (String var : cand) {
            Data dat = vars.get(var);
            if (dat != null && dat instanceof MatrixObject && ((MatrixObject) dat).getRDDHandle() != null) {
                MatrixObject mo = (MatrixObject) dat;
                MatrixCharacteristics mc = mo.getMatrixCharacteristics();
                RDDObject rdd = mo.getRDDHandle();
                if (// not a repartition var
                (rpVars == null || !rpVars.contains(var)) && // is cached rdd
                rdd.rHasCheckpointRDDChilds() && // is out-of-core dataset
                _lm / n.getK() < OptimizerUtils.estimateSizeExactSparsity(mc)) {
                    ret.add(var);
                }
            }
        }
        // apply rewrite to parfor pb
        if (!ret.isEmpty()) {
            pfpb.setSparkEagerCacheVariables(ret);
        }
    }
    _numEvaluatedPlans++;
    LOG.debug(getOptMode() + " OPT: rewrite 'set spark eager rdd caching' - result=" + ret.size() + " (" + ProgramConverter.serializeStringCollection(ret) + ")");
}
Also used : MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) ArrayList(java.util.ArrayList) Data(org.apache.sysml.runtime.instructions.cp.Data) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) MatrixCharacteristics(org.apache.sysml.runtime.matrix.MatrixCharacteristics) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject) MatrixObject(org.apache.sysml.runtime.controlprogram.caching.MatrixObject) RDDObject(org.apache.sysml.runtime.instructions.spark.data.RDDObject)

Example 5 with ParForStatementBlock

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

the class ParForProgramBlock method checkMRAndRecompileToCP.

/**
 * Recompile program block hierarchy to forced CP if MR instructions or functions.
 * Returns true if recompile was necessary and possible
 *
 * @param tid thread id
 * @return true if recompile was necessary and possible
 */
private boolean checkMRAndRecompileToCP(long tid) {
    // no MR instructions, ok
    if (!OptTreeConverter.rContainsMRJobInstruction(this, true))
        return false;
    // no statement block, failed
    ParForStatementBlock sb = (ParForStatementBlock) getStatementBlock();
    if (sb == null) {
        LOG.warn("Missing parfor statement block for recompile.");
        return false;
    }
    // try recompile MR instructions to CP
    HashSet<String> fnStack = new HashSet<>();
    Recompiler.recompileProgramBlockHierarchy2Forced(_childBlocks, tid, fnStack, ExecType.CP);
    return true;
}
Also used : ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) HashSet(java.util.HashSet)

Aggregations

ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)39 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)22 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)14 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)12 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)12 ArrayList (java.util.ArrayList)11 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)11 Timing (org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)10 ForStatement (org.apache.sysml.parser.ForStatement)9 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)9 IfStatement (org.apache.sysml.parser.IfStatement)9 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)9 StatementBlock (org.apache.sysml.parser.StatementBlock)9 WhileStatement (org.apache.sysml.parser.WhileStatement)9 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)9 Data (org.apache.sysml.runtime.instructions.cp.Data)8 FunctionStatement (org.apache.sysml.parser.FunctionStatement)7 IOException (java.io.IOException)6 Hop (org.apache.sysml.hops.Hop)6 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)6