Search in sources :

Example 6 with ForStatementBlock

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

the class ProgramRewriter method rewriteStatementBlockHopDAGs.

public void rewriteStatementBlockHopDAGs(StatementBlock current, ProgramRewriteStatus state) throws LanguageException, HopsException {
    //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()) rewriteStatementBlockHopDAGs(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()) rewriteStatementBlockHopDAGs(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()) rewriteStatementBlockHopDAGs(sb, state);
        for (StatementBlock sb : istmt.getElseBody()) rewriteStatementBlockHopDAGs(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()) rewriteStatementBlockHopDAGs(sb, state);
    } else //generic (last-level)
    {
        current.set_hops(rewriteHopDAGs(current.get_hops(), 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 7 with ForStatementBlock

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

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

the class ForProgramBlock method executePredicateInstructions.

protected IntObject executePredicateInstructions(int pos, ArrayList<Instruction> instructions, ExecutionContext ec) throws DMLRuntimeException {
    ScalarObject tmp = null;
    IntObject ret = null;
    try {
        if (_iterablePredicateVars[pos] != null) {
            //probe for scalar variables
            Data ldat = ec.getVariable(_iterablePredicateVars[pos]);
            if (ldat != null && ldat instanceof ScalarObject)
                tmp = (ScalarObject) ldat;
            else
                //handle literals
                tmp = new IntObject(UtilFunctions.parseToLong(_iterablePredicateVars[pos]));
        } else {
            if (_sb != null) {
                if (//set program block specific remote memory
                DMLScript.isActiveAM())
                    DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
                ForStatementBlock fsb = (ForStatementBlock) _sb;
                Hop predHops = null;
                boolean recompile = false;
                if (pos == 1) {
                    predHops = fsb.getFromHops();
                    recompile = fsb.requiresFromRecompilation();
                } else if (pos == 2) {
                    predHops = fsb.getToHops();
                    recompile = fsb.requiresToRecompilation();
                } else if (pos == 3) {
                    predHops = fsb.getIncrementHops();
                    recompile = fsb.requiresIncrementRecompilation();
                }
                tmp = (IntObject) executePredicate(instructions, predHops, recompile, ValueType.INT, ec);
            } else
                tmp = (IntObject) executePredicate(instructions, null, false, ValueType.INT, ec);
        }
    } catch (Exception ex) {
        String predNameStr = null;
        if (pos == 1)
            predNameStr = "from";
        else if (pos == 2)
            predNameStr = "to";
        else if (pos == 3)
            predNameStr = "increment";
        throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error evaluating '" + predNameStr + "' predicate", ex);
    }
    //final check of resulting int object (guaranteed to be non-null, see executePredicate)
    if (tmp instanceof IntObject)
        ret = (IntObject) tmp;
    else
        //downcast to int if necessary
        ret = new IntObject(tmp.getName(), tmp.getLongValue());
    return ret;
}
Also used : ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ScalarObject(org.apache.sysml.runtime.instructions.cp.ScalarObject) IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) Hop(org.apache.sysml.hops.Hop) Data(org.apache.sysml.runtime.instructions.cp.Data) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 9 with ForStatementBlock

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

the class ResourceConfig method addProgramBlock.

private void addProgramBlock(ProgramBlock pb, long init) throws HopsException {
    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 10 with ForStatementBlock

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

the class ResourceOptimizer method recompileProgramBlock.

private static void recompileProgramBlock(ProgramBlock pb, long cp, long mr) throws DMLRuntimeException, HopsException, LopsException, IOException {
    //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 (//incl parfor
    pb instanceof ForProgramBlock) {
        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.get_hops(), 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)

Aggregations

ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)31 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)27 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)26 StatementBlock (org.apache.sysml.parser.StatementBlock)26 ForStatement (org.apache.sysml.parser.ForStatement)14 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)13 IfStatement (org.apache.sysml.parser.IfStatement)13 WhileStatement (org.apache.sysml.parser.WhileStatement)13 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)13 ArrayList (java.util.ArrayList)12 Hop (org.apache.sysml.hops.Hop)12 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)12 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)12 FunctionStatement (org.apache.sysml.parser.FunctionStatement)8 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)8 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)7 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)7 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)6 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)5 ExternalFunctionStatement (org.apache.sysml.parser.ExternalFunctionStatement)4