Search in sources :

Example 11 with ProgramBlock

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

the class OptTreeConverter method rCreateOptNode.

public static OptNode rCreateOptNode(ProgramBlock pb, LocalVariableMap vars, boolean topLevel, boolean storeObjs) {
    OptNode node = null;
    if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        node = new OptNode(NodeType.IF);
        if (storeObjs)
            _rtMap.putMapping(ipb, node);
        node.setExecType(ExecType.CP);
        // process if condition
        OptNode ifn = new OptNode(NodeType.GENERIC);
        node.addChilds(createOptNodes(ipb.getPredicate(), vars, storeObjs));
        node.addChild(ifn);
        for (ProgramBlock lpb : ipb.getChildBlocksIfBody()) ifn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
        // process else condition
        if (ipb.getChildBlocksElseBody() != null && ipb.getChildBlocksElseBody().size() > 0) {
            OptNode efn = new OptNode(NodeType.GENERIC);
            node.addChild(efn);
            for (ProgramBlock lpb : ipb.getChildBlocksElseBody()) efn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
        }
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        node = new OptNode(NodeType.WHILE);
        if (storeObjs)
            _rtMap.putMapping(wpb, node);
        node.setExecType(ExecType.CP);
        // process predicate instruction
        node.addChilds(createOptNodes(wpb.getPredicate(), vars, storeObjs));
        // process body
        for (ProgramBlock lpb : wpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
    } else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        node = new OptNode(NodeType.FOR);
        if (storeObjs)
            _rtMap.putMapping(fpb, node);
        node.setExecType(ExecType.CP);
        // determine number of iterations
        long N = OptimizerUtils.getNumIterations(fpb, vars, CostEstimator.FACTOR_NUM_ITERATIONS);
        node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(N));
        node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
        node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
        node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
        // process body
        for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
    } else if (pb instanceof ParForProgramBlock) {
        ParForProgramBlock fpb = (ParForProgramBlock) pb;
        node = new OptNode(NodeType.PARFOR);
        if (storeObjs)
            _rtMap.putMapping(fpb, node);
        node.setK(fpb.getDegreeOfParallelism());
        long N = fpb.getNumIterations();
        node.addParam(ParamType.NUM_ITERATIONS, (N != -1) ? String.valueOf(N) : String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
        switch(fpb.getExecMode()) {
            case LOCAL:
                node.setExecType(ExecType.CP);
                break;
            case REMOTE_MR:
            case REMOTE_MR_DP:
                node.setExecType(ExecType.MR);
                break;
            case REMOTE_SPARK:
            case REMOTE_SPARK_DP:
                node.setExecType(ExecType.SPARK);
                break;
            default:
                node.setExecType(null);
        }
        if (!topLevel) {
            node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
            node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
            node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
        }
        // process body
        for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, false, storeObjs));
    // parameters, add required parameters
    } else // last level program block
    {
        node = new OptNode(NodeType.GENERIC);
        if (storeObjs)
            _rtMap.putMapping(pb, node);
        node.addChilds(createOptNodes(pb.getInstructions(), vars, storeObjs));
        node.setExecType(ExecType.CP);
    }
    return node;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 12 with ProgramBlock

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

the class OptimizerRuleBased method rReplaceFunctionNames.

protected void rReplaceFunctionNames(OptNode n, String oldName, String newName) {
    if (n.getNodeType() == NodeType.FUNCCALL) {
        FunctionOp fop = (FunctionOp) OptTreeConverter.getAbstractPlanMapping().getMappedHop(n.getID());
        String[] names = n.getParam(ParamType.OPSTRING).split(Program.KEY_DELIM);
        String fnamespace = names[0];
        String fname = names[1];
        if (// newName if shared hop
        fname.equals(oldName) || fname.equals(newName)) {
            // set opttree function name
            n.addParam(ParamType.OPSTRING, DMLProgram.constructFunctionKey(fnamespace, newName));
            // set instruction function name
            long parentID = OptTreeConverter.getAbstractPlanMapping().getMappedParentID(n.getID());
            ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(parentID)[1];
            ArrayList<Instruction> instArr = pb.getInstructions();
            for (int i = 0; i < instArr.size(); i++) {
                Instruction inst = instArr.get(i);
                if (inst instanceof FunctionCallCPInstruction) {
                    FunctionCallCPInstruction fci = (FunctionCallCPInstruction) inst;
                    if (oldName.equals(fci.getFunctionName()))
                        instArr.set(i, FunctionCallCPInstruction.parseInstruction(fci.toString().replaceAll(oldName, newName)));
                }
            }
            // set hop name (for recompile)
            if (fop.getFunctionName().equals(oldName))
                fop.setFunctionName(newName);
        }
    }
    // recursive invocation
    if (!n.isLeaf())
        for (OptNode c : n.getChilds()) rReplaceFunctionNames(c, oldName, newName);
}
Also used : FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) FunctionOp(org.apache.sysml.hops.FunctionOp) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) Instruction(org.apache.sysml.runtime.instructions.Instruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)

Example 13 with ProgramBlock

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

the class ProgramRecompiler method rFindAndRecompileIndexingHOP.

/**
 * NOTE: if force is set, we set and recompile the respective indexing hops;
 * otherwise, we release the forced exec type and recompile again. Hence,
 * any changes can be exactly reverted with the same access behavior.
 *
 * @param sb statement block
 * @param pb program block
 * @param var variable
 * @param ec execution context
 * @param force if true, set and recompile the respective indexing hops
 */
public static void rFindAndRecompileIndexingHOP(StatementBlock sb, ProgramBlock pb, String var, ExecutionContext ec, boolean force) {
    if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement is = (IfStatement) sb.getStatement(0);
        // process if condition
        if (isb.getPredicateHops() != null)
            ipb.setPredicate(rFindAndRecompileIndexingHOP(isb.getPredicateHops(), ipb.getPredicate(), var, ec, force));
        // process if branch
        int len = is.getIfBody().size();
        for (int i = 0; i < ipb.getChildBlocksIfBody().size() && i < len; i++) {
            ProgramBlock lpb = ipb.getChildBlocksIfBody().get(i);
            StatementBlock lsb = is.getIfBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
        // process else branch
        if (ipb.getChildBlocksElseBody() != null) {
            int len2 = is.getElseBody().size();
            for (int i = 0; i < ipb.getChildBlocksElseBody().size() && i < len2; i++) {
                ProgramBlock lpb = ipb.getChildBlocksElseBody().get(i);
                StatementBlock lsb = is.getElseBody().get(i);
                rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
            }
        }
    } else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement ws = (WhileStatement) sb.getStatement(0);
        // process while condition
        if (wsb.getPredicateHops() != null)
            wpb.setPredicate(rFindAndRecompileIndexingHOP(wsb.getPredicateHops(), wpb.getPredicate(), var, ec, force));
        // process body
        // robustness for potentially added problem blocks
        int len = ws.getBody().size();
        for (int i = 0; i < wpb.getChildBlocks().size() && i < len; i++) {
            ProgramBlock lpb = wpb.getChildBlocks().get(i);
            StatementBlock lsb = ws.getBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
    } else if (// for or parfor
    pb instanceof ForProgramBlock && sb instanceof ForStatementBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) fsb.getStatement(0);
        if (fsb.getFromHops() != null)
            fpb.setFromInstructions(rFindAndRecompileIndexingHOP(fsb.getFromHops(), fpb.getFromInstructions(), var, ec, force));
        if (fsb.getToHops() != null)
            fpb.setToInstructions(rFindAndRecompileIndexingHOP(fsb.getToHops(), fpb.getToInstructions(), var, ec, force));
        if (fsb.getIncrementHops() != null)
            fpb.setIncrementInstructions(rFindAndRecompileIndexingHOP(fsb.getIncrementHops(), fpb.getIncrementInstructions(), var, ec, force));
        // process body
        // robustness for potentially added problem blocks
        int len = fs.getBody().size();
        for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
            ProgramBlock lpb = fpb.getChildBlocks().get(i);
            StatementBlock lsb = fs.getBody().get(i);
            rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
        }
    } else // last level program block
    {
        try {
            // process actual hops
            boolean ret = false;
            Hop.resetVisitStatus(sb.getHops());
            if (force) {
                // set forced execution type
                for (Hop h : sb.getHops()) ret |= rFindAndSetCPIndexingHOP(h, var);
            } else {
                // release forced execution type
                for (Hop h : sb.getHops()) ret |= rFindAndReleaseIndexingHOP(h, var);
            }
            // recompilation on-demand
            if (ret) {
                // construct new instructions
                ArrayList<Instruction> newInst = Recompiler.recompileHopsDag(sb, sb.getHops(), ec.getVariables(), null, true, false, 0);
                pb.setInstructions(newInst);
            }
        } catch (Exception ex) {
            throw new DMLRuntimeException(ex);
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) Hop(org.apache.sysml.hops.Hop) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileStatement(org.apache.sysml.parser.WhileStatement) BinaryCPInstruction(org.apache.sysml.runtime.instructions.cp.BinaryCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) IfStatement(org.apache.sysml.parser.IfStatement) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ForStatement(org.apache.sysml.parser.ForStatement) 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 14 with ProgramBlock

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

the class ParWorker method executeRangeTask.

private void executeRangeTask(Task task) {
    // monitoring start
    Timing time1 = null, time2 = null;
    if (_monitor) {
        time1 = new Timing(true);
        time2 = new Timing(true);
    }
    // core execution
    List<IntObject> tmp = task.getIterations();
    String lVarName = task.getVarName();
    long lFrom = tmp.get(0).getLongValue();
    long lTo = tmp.get(1).getLongValue();
    long lIncr = tmp.get(2).getLongValue();
    for (long i = lFrom; i <= lTo; i += lIncr) {
        // set index values
        _ec.setVariable(lVarName, new IntObject(i));
        // for each program block
        for (ProgramBlock pb : _childBlocks) pb.execute(_ec);
        _numIters++;
        if (_monitor)
            StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_ITER_T, time1.stop());
    }
    _numTasks++;
    // monitoring end
    if (_monitor) {
        StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_TASKSIZE, task.size());
        StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_TASK_T, time2.stop());
    }
}
Also used : IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Example 15 with ProgramBlock

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

the class ParWorker method executeSetTask.

private void executeSetTask(Task task) {
    // monitoring start
    Timing time1 = null, time2 = null;
    if (_monitor) {
        time1 = new Timing(true);
        time2 = new Timing(true);
    }
    // core execution
    // foreach iteration in task, execute iteration body
    String lVarName = task.getVarName();
    for (IntObject indexVal : task.getIterations()) {
        // System.out.println(" EXECUTE ITERATION: "+indexVal.getName()+"="+indexVal.getIntValue());
        // set index values
        _ec.setVariable(lVarName, indexVal);
        // for each program block
        for (ProgramBlock pb : _childBlocks) pb.execute(_ec);
        _numIters++;
        if (_monitor)
            StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_ITER_T, time1.stop());
    }
    _numTasks++;
    // monitoring end
    if (_monitor) {
        StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_TASKSIZE, task.size());
        StatisticMonitor.putPWStat(_workerID, Stat.PARWRK_TASK_T, time2.stop());
    }
}
Also used : IntObject(org.apache.sysml.runtime.instructions.cp.IntObject) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) Timing(org.apache.sysml.runtime.controlprogram.parfor.stat.Timing)

Aggregations

ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)64 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)58 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)56 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)54 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)54 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)38 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)26 ArrayList (java.util.ArrayList)20 Instruction (org.apache.sysml.runtime.instructions.Instruction)18 StatementBlock (org.apache.sysml.parser.StatementBlock)13 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)13 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)11 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)11 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)11 Hop (org.apache.sysml.hops.Hop)10 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)10 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 Program (org.apache.sysml.runtime.controlprogram.Program)9 DMLProgram (org.apache.sysml.parser.DMLProgram)8 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)7