Search in sources :

Example 26 with IfProgramBlock

use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.

the class ProgramConverter method rSerializeProgramBlock.

private static String rSerializeProgramBlock(ProgramBlock pb, HashMap<String, byte[]> clsMap) {
    StringBuilder sb = new StringBuilder();
    // handle header
    if (pb instanceof WhileProgramBlock)
        sb.append(PARFOR_PB_WHILE);
    else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock))
        sb.append(PARFOR_PB_FOR);
    else if (pb instanceof ParForProgramBlock)
        sb.append(PARFOR_PB_PARFOR);
    else if (pb instanceof IfProgramBlock)
        sb.append(PARFOR_PB_IF);
    else if (pb instanceof FunctionProgramBlock && !(pb instanceof ExternalFunctionProgramBlock))
        sb.append(PARFOR_PB_FC);
    else if (pb instanceof ExternalFunctionProgramBlock)
        sb.append(PARFOR_PB_EFC);
    else
        // all generic program blocks
        sb.append(PARFOR_PB_BEGIN);
    // handle body
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(wpb.getPredicate(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(wpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(wpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        sb.append(fpb.getIterVar());
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getFromInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getToInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getIncrementInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof ParForProgramBlock) {
        ParForProgramBlock pfpb = (ParForProgramBlock) pb;
        // check for nested remote ParFOR
        if (PExecMode.valueOf(pfpb.getParForParams().get(ParForStatementBlock.EXEC_MODE)) == PExecMode.REMOTE_MR)
            throw new DMLRuntimeException(NOT_SUPPORTED_MR_PARFOR);
        sb.append(pfpb.getIterVar());
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeResultVariables(pfpb.getResultVariables()));
        sb.append(COMPONENTS_DELIM);
        // parameters of nested parfor
        sb.append(serializeStringHashMap(pfpb.getParForParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getFromInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getToInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getIncrementInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pfpb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(pfpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(ipb.getPredicate(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(ipb.getExitInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(ipb.getChildBlocksIfBody(), clsMap));
        sb.append(PARFOR_PBS_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(ipb.getChildBlocksElseBody(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else if (pb instanceof FunctionProgramBlock && !(pb instanceof ExternalFunctionProgramBlock)) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        sb.append(serializeDataIdentifiers(fpb.getInputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeDataIdentifiers(fpb.getOutputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(fpb.getInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
        sb.append(COMPONENTS_DELIM);
    } else if (pb instanceof ExternalFunctionProgramBlock) {
        if (!(pb instanceof ExternalFunctionProgramBlockCP)) {
            throw new DMLRuntimeException(NOT_SUPPORTED_EXTERNALFUNCTION_PB);
        }
        ExternalFunctionProgramBlockCP fpb = (ExternalFunctionProgramBlockCP) pb;
        sb.append(serializeDataIdentifiers(fpb.getInputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeDataIdentifiers(fpb.getOutputParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeStringHashMap(fpb.getOtherParams()));
        sb.append(COMPONENTS_DELIM);
        sb.append(fpb.getBaseDir());
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_INST_BEGIN);
        // create on construction anyway
        sb.append(PARFOR_INST_END);
        sb.append(COMPONENTS_DELIM);
        sb.append(PARFOR_PBS_BEGIN);
        sb.append(rSerializeProgramBlocks(fpb.getChildBlocks(), clsMap));
        sb.append(PARFOR_PBS_END);
    } else // all generic program blocks
    {
        sb.append(PARFOR_INST_BEGIN);
        sb.append(serializeInstructions(pb.getInstructions(), clsMap));
        sb.append(PARFOR_INST_END);
    }
    // handle end
    sb.append(PARFOR_PB_END);
    return sb.toString();
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ExternalFunctionProgramBlockCP(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlockCP) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Example 27 with IfProgramBlock

use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project 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 28 with IfProgramBlock

use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.

the class OptTreeConverter method rCreateAbstractOptNode.

public static OptNode rCreateAbstractOptNode(StatementBlock sb, ProgramBlock pb, LocalVariableMap vars, boolean topLevel, Set<String> memo) {
    OptNode node = null;
    if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) sb;
        IfStatement is = (IfStatement) isb.getStatement(0);
        node = new OptNode(NodeType.IF);
        _hlMap.putProgMapping(sb, pb, node);
        node.setExecType(ExecType.CP);
        node.setLineNumbers(isb.getBeginLine(), isb.getEndLine());
        // handle predicate
        isb.getPredicateHops().resetVisitStatus();
        node.addChilds(rCreateAbstractOptNodes(isb.getPredicateHops(), vars, memo));
        // process if branch
        OptNode ifn = new OptNode(NodeType.GENERIC);
        _hlMap.putProgMapping(sb, pb, ifn);
        ifn.setExecType(ExecType.CP);
        node.addChild(ifn);
        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);
            ifn.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
        }
        // process else branch
        if (ipb.getChildBlocksElseBody() != null) {
            OptNode efn = new OptNode(NodeType.GENERIC);
            _hlMap.putProgMapping(sb, pb, efn);
            efn.setExecType(ExecType.CP);
            node.addChild(efn);
            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);
                efn.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
            }
        }
    } else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        WhileStatement ws = (WhileStatement) wsb.getStatement(0);
        node = new OptNode(NodeType.WHILE);
        _hlMap.putProgMapping(sb, pb, node);
        node.setExecType(ExecType.CP);
        node.setLineNumbers(wsb.getBeginLine(), wsb.getEndLine());
        // handle predicate
        wsb.getPredicateHops().resetVisitStatus();
        node.addChilds(rCreateAbstractOptNodes(wsb.getPredicateHops(), vars, memo));
        // process body
        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);
            node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
        }
    } else if (pb instanceof ForProgramBlock && sb instanceof ForStatementBlock && !(pb instanceof ParForProgramBlock)) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) sb;
        ForStatement fs = (ForStatement) fsb.getStatement(0);
        node = new OptNode(NodeType.FOR);
        _hlMap.putProgMapping(sb, pb, node);
        node.setExecType(ExecType.CP);
        node.setLineNumbers(fsb.getBeginLine(), fsb.getEndLine());
        // determine number of iterations
        long N = OptimizerUtils.getNumIterations(fpb, vars, CostEstimator.FACTOR_NUM_ITERATIONS);
        node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(N));
        // handle predicate
        fsb.getFromHops().resetVisitStatus();
        fsb.getToHops().resetVisitStatus();
        if (fsb.getIncrementHops() != null)
            fsb.getIncrementHops().resetVisitStatus();
        node.addChilds(rCreateAbstractOptNodes(fsb.getFromHops(), vars, memo));
        node.addChilds(rCreateAbstractOptNodes(fsb.getToHops(), vars, memo));
        if (fsb.getIncrementHops() != null)
            node.addChilds(rCreateAbstractOptNodes(fsb.getIncrementHops(), vars, memo));
        // process body
        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);
            node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
        }
    } else if (pb instanceof ParForProgramBlock && sb instanceof ParForStatementBlock) {
        ParForProgramBlock fpb = (ParForProgramBlock) pb;
        ParForStatementBlock fsb = (ParForStatementBlock) sb;
        ParForStatement fs = (ParForStatement) fsb.getStatement(0);
        node = new OptNode(NodeType.PARFOR);
        node.setLineNumbers(fsb.getBeginLine(), fsb.getEndLine());
        _hlMap.putProgMapping(sb, pb, 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;
            case UNSPECIFIED:
                node.setExecType(null);
        }
        if (!topLevel) {
            fsb.getFromHops().resetVisitStatus();
            fsb.getToHops().resetVisitStatus();
            if (fsb.getIncrementHops() != null)
                fsb.getIncrementHops().resetVisitStatus();
            node.addChilds(rCreateAbstractOptNodes(fsb.getFromHops(), vars, memo));
            node.addChilds(rCreateAbstractOptNodes(fsb.getToHops(), vars, memo));
            if (fsb.getIncrementHops() != null)
                node.addChilds(rCreateAbstractOptNodes(fsb.getIncrementHops(), vars, memo));
        }
        // process body
        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);
            node.addChild(rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
        }
        // parameters, add required parameters
        Map<String, String> lparams = fpb.getParForParams();
        node.addParam(ParamType.DATA_PARTITIONER, lparams.get(ParForStatementBlock.DATA_PARTITIONER));
        node.addParam(ParamType.TASK_PARTITIONER, lparams.get(ParForStatementBlock.TASK_PARTITIONER));
        node.addParam(ParamType.RESULT_MERGE, lparams.get(ParForStatementBlock.RESULT_MERGE));
    // TODO task size
    } else // last level program block
    {
        sb = pb.getStatementBlock();
        // process all hops
        node = new OptNode(NodeType.GENERIC);
        _hlMap.putProgMapping(sb, pb, node);
        node.addChilds(createAbstractOptNodes(sb.getHops(), vars, memo));
        node.setExecType(ExecType.CP);
        node.setLineNumbers(sb.getBeginLine(), sb.getEndLine());
        // TODO remove this workaround once this information can be obtained from hops/lops compiler
        if (node.isCPOnly()) {
            boolean isSparkExec = OptimizerUtils.isSparkExecutionMode();
            if (!isSparkExec && containsMRJobInstruction(pb, false, false))
                node.setExecType(ExecType.MR);
            else if (isSparkExec && containsMRJobInstruction(pb, false, true))
                node.setExecType(ExecType.SPARK);
        }
    }
    // final cleanup
    // NOTE: required because this function is also used to create subtrees
    node.checkAndCleanupLeafNodes();
    return node;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ParForStatement(org.apache.sysml.parser.ParForStatement) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileStatement(org.apache.sysml.parser.WhileStatement) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) IfStatement(org.apache.sysml.parser.IfStatement) ParForStatementBlock(org.apache.sysml.parser.ParForStatementBlock) 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) ParForStatement(org.apache.sysml.parser.ParForStatement) ForStatement(org.apache.sysml.parser.ForStatement) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) Map(java.util.Map) 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 29 with IfProgramBlock

use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project 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 30 with IfProgramBlock

use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.

the class OptTreeConverter method replaceProgramBlock.

public static void replaceProgramBlock(OptNode parent, OptNode n, ProgramBlock pbOld, ProgramBlock pbNew, boolean rtMap) {
    ProgramBlock pbParent = null;
    if (rtMap)
        pbParent = (ProgramBlock) _rtMap.getMappedObject(parent.getID());
    else {
        if (parent.getNodeType() == NodeType.FUNCCALL) {
            FunctionOp fop = (FunctionOp) _hlMap.getMappedHop(parent.getID());
            pbParent = ((Program) _hlMap.getRootProgram()[1]).getFunctionProgramBlock(fop.getFunctionNamespace(), fop.getFunctionName());
        } else
            pbParent = (ProgramBlock) _hlMap.getMappedProg(parent.getID())[1];
    }
    if (pbParent instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pbParent;
        replaceProgramBlock(ipb.getChildBlocksIfBody(), pbOld, pbNew);
        replaceProgramBlock(ipb.getChildBlocksElseBody(), pbOld, pbNew);
    } else if (pbParent instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pbParent;
        replaceProgramBlock(wpb.getChildBlocks(), pbOld, pbNew);
    } else if (pbParent instanceof ForProgramBlock || pbParent instanceof ParForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pbParent;
        replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
    } else if (pbParent instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pbParent;
        replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
    } else
        throw new DMLRuntimeException("Optimizer doesn't support " + pbParent.getClass().getName());
    // update repository
    if (rtMap)
        _rtMap.replaceMapping(pbNew, n);
    else
        _hlMap.replaceMapping(pbNew, n);
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) 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) FunctionOp(org.apache.sysml.hops.FunctionOp) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException)

Aggregations

IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)62 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)60 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)60 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)52 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)46 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)34 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)28 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)26 StatementBlock (org.apache.sysml.parser.StatementBlock)26 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)26 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)22 Instruction (org.apache.sysml.runtime.instructions.Instruction)22 ArrayList (java.util.ArrayList)20 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)14 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)12 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)11 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 LocalVariableMap (org.apache.sysml.runtime.controlprogram.LocalVariableMap)9 Hop (org.apache.sysml.hops.Hop)8 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)8