Search in sources :

Example 6 with ParForProgramBlock

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

the class OptimizerRuleBased method rIsInLoop.

/* 	
	 * This will check if candidate LeftIndexingOp are in loop (while, for or parfor).
	 * 
	 * @param pn:				OpNode of parfor loop
	 * @param uipCandHopHM:		Hashmap of UIPCandidateHop with name as a key.		
	 * @throws DMLRuntimeException
	 */
private void rIsInLoop(OptNode pn, HashMap<String, ArrayList<UIPCandidateHop>> uipCandHopHM, boolean bInLoop) throws DMLRuntimeException {
    if (!pn.isLeaf()) {
        ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
        VariableSet varUpdated = pb.getStatementBlock().variablesUpdated();
        boolean bUIPCandHopUpdated = false;
        for (Entry<String, ArrayList<UIPCandidateHop>> entry : uipCandHopHM.entrySet()) {
            String uipCandHopID = entry.getKey();
            if (varUpdated.containsVariable(uipCandHopID)) {
                bUIPCandHopUpdated = true;
                break;
            }
        }
        // As none of the UIP candidates updated in this DAG, no need for further processing within this DAG
        if (!bUIPCandHopUpdated)
            return;
        boolean bLoop = false;
        if (bInLoop || pb instanceof WhileProgramBlock || (pb instanceof ParForProgramBlock && ((ParForProgramBlock) pb).getDegreeOfParallelism() == 1) || (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)))
            bLoop = true;
        for (OptNode optNode : pn.getChilds()) {
            rIsInLoop(optNode, uipCandHopHM, bLoop);
        }
    } else {
        Hop hop = (Hop) OptTreeConverter.getAbstractPlanMapping().getMappedHop(pn.getID());
        for (Entry<String, ArrayList<UIPCandidateHop>> entry : uipCandHopHM.entrySet()) {
            ArrayList<UIPCandidateHop> uipCandHopList = entry.getValue();
            if (uipCandHopList != null) {
                for (UIPCandidateHop uipCandHop : uipCandHopList) {
                    //Identify where intermediate object has been defined.
                    if (hop instanceof DataGenOp && hop.getName().equals(uipCandHop.getLixHop().getName())) {
                        uipCandHop.setHop(hop);
                        uipCandHop.setLocation(hop.getBeginLine());
                        uipCandHop.setIntermediate(true);
                    }
                    //Update if candiate hop defined outside this loop, and leftindexing is within this loop.
                    if ((bInLoop) && (uipCandHop.getLocation() <= hop.getBeginLine() && uipCandHop.getLixHop().getBeginLine() <= hop.getEndLine()))
                        uipCandHop.setIsLoopApplicable(true);
                }
            }
        }
    }
}
Also used : ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ArrayList(java.util.ArrayList) Hop(org.apache.sysml.hops.Hop) MultiThreadedHop(org.apache.sysml.hops.Hop.MultiThreadedHop) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) VariableSet(org.apache.sysml.parser.VariableSet) DataGenOp(org.apache.sysml.hops.DataGenOp) 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)

Example 7 with ParForProgramBlock

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

the class OptTreeConverter method rCreateOptNode.

public static OptNode rCreateOptNode(ProgramBlock pb, LocalVariableMap vars, boolean topLevel, boolean storeObjs) throws DMLRuntimeException {
    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);
        //TODO use constant value if known
        node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
        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(CostEstimatorRuntime.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 8 with ParForProgramBlock

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

the class Explain method explainProgramBlock.

//////////////
// internal explain RUNTIME
private static String explainProgramBlock(ProgramBlock pb, int level) {
    StringBuilder sb = new StringBuilder();
    String offset = createOffset(level);
    if (pb instanceof FunctionProgramBlock) {
        FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
        for (ProgramBlock pbc : fpb.getChildBlocks()) sb.append(explainProgramBlock(pbc, level + 1));
    } else if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        StatementBlock wsb = pb.getStatementBlock();
        sb.append(offset);
        if (wsb != null && !wsb.getUpdateInPlaceVars().isEmpty())
            sb.append("WHILE (lines " + wpb.getBeginLine() + "-" + wpb.getEndLine() + ") [in-place=" + wsb.getUpdateInPlaceVars().toString() + "]\n");
        else
            sb.append("WHILE (lines " + wpb.getBeginLine() + "-" + wpb.getEndLine() + ")\n");
        sb.append(explainInstructions(wpb.getPredicate(), level + 1));
        for (ProgramBlock pbc : wpb.getChildBlocks()) sb.append(explainProgramBlock(pbc, level + 1));
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        sb.append(offset);
        sb.append("IF (lines " + ipb.getBeginLine() + "-" + ipb.getEndLine() + ")\n");
        sb.append(explainInstructions(ipb.getPredicate(), level + 1));
        for (ProgramBlock pbc : ipb.getChildBlocksIfBody()) sb.append(explainProgramBlock(pbc, level + 1));
        if (!ipb.getChildBlocksElseBody().isEmpty()) {
            sb.append(offset);
            sb.append("ELSE\n");
            for (ProgramBlock pbc : ipb.getChildBlocksElseBody()) sb.append(explainProgramBlock(pbc, level + 1));
        }
    } else if (//incl parfor
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        StatementBlock fsb = pb.getStatementBlock();
        sb.append(offset);
        if (pb instanceof ParForProgramBlock)
            sb.append("PARFOR (lines " + fpb.getBeginLine() + "-" + fpb.getEndLine() + ")\n");
        else {
            if (fsb != null && !fsb.getUpdateInPlaceVars().isEmpty())
                sb.append("FOR (lines " + fpb.getBeginLine() + "-" + fpb.getEndLine() + ") [in-place=" + fsb.getUpdateInPlaceVars().toString() + "]\n");
            else
                sb.append("FOR (lines " + fpb.getBeginLine() + "-" + fpb.getEndLine() + ")\n");
        }
        sb.append(explainInstructions(fpb.getFromInstructions(), level + 1));
        sb.append(explainInstructions(fpb.getToInstructions(), level + 1));
        sb.append(explainInstructions(fpb.getIncrementInstructions(), level + 1));
        for (ProgramBlock pbc : fpb.getChildBlocks()) sb.append(explainProgramBlock(pbc, level + 1));
    } else {
        sb.append(offset);
        if (pb.getStatementBlock() != null)
            sb.append("GENERIC (lines " + pb.getBeginLine() + "-" + pb.getEndLine() + ") [recompile=" + pb.getStatementBlock().requiresRecompilation() + "]\n");
        else
            sb.append("GENERIC (lines " + pb.getBeginLine() + "-" + pb.getEndLine() + ") \n");
        sb.append(explainInstructions(pb.getInstructions(), level + 1));
    }
    return sb.toString();
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) 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) 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) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock)

Example 9 with ParForProgramBlock

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

the class DMLProgram method createRuntimeProgramBlock.

public ProgramBlock createRuntimeProgramBlock(Program prog, StatementBlock sb, DMLConfig config) throws IOException, LopsException, DMLRuntimeException {
    Dag<Lop> dag = null;
    Dag<Lop> pred_dag = null;
    ArrayList<Instruction> instruct;
    ArrayList<Instruction> pred_instruct = null;
    ProgramBlock retPB = null;
    // process While Statement - add runtime program blocks to program
    if (sb instanceof WhileStatementBlock) {
        // create DAG for loop predicates
        pred_dag = new Dag<Lop>();
        ((WhileStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
        // create instructions for loop predicates
        pred_instruct = new ArrayList<Instruction>();
        ArrayList<Instruction> pInst = pred_dag.getJobs(null, config);
        for (Instruction i : pInst) {
            pred_instruct.add(i);
        }
        // create while program block
        WhileProgramBlock rtpb = new WhileProgramBlock(prog, pred_instruct);
        if (rtpb.getPredicateResultVar() == null) {
            // e.g case : WHILE(continue)
            if (((WhileStatementBlock) sb).get_predicateLops().getExecLocation() == LopProperties.ExecLocation.Data) {
                String resultVar = ((WhileStatementBlock) sb).get_predicateLops().getOutputParameters().getLabel();
                rtpb.setPredicateResultVar(resultVar);
            } else {
                LOG.error(sb.printBlockErrorLocation() + "Error in translating the WHILE predicate.");
                throw new LopsException(sb.printBlockErrorLocation() + "Error in translating the WHILE predicate.");
            }
        }
        //// process the body of the while statement block ////
        WhileStatementBlock wsb = (WhileStatementBlock) sb;
        if (wsb.getNumStatements() > 1) {
            LOG.error(wsb.printBlockErrorLocation() + "WhileStatementBlock should only have 1 statement");
            throw new LopsException(wsb.printBlockErrorLocation() + "WhileStatementBlock should only have 1 statement");
        }
        WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
        for (StatementBlock sblock : wstmt.getBody()) {
            // process the body
            ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
            rtpb.addProgramBlock(childBlock);
        }
        // check there are actually Lops in to process (loop stmt body will not have any)
        if (wsb.getLops() != null && !wsb.getLops().isEmpty()) {
            LOG.error(wsb.printBlockErrorLocation() + "WhileStatementBlock should have no Lops");
            throw new LopsException(wsb.printBlockErrorLocation() + "WhileStatementBlock should have no Lops");
        }
        retPB = rtpb;
        //post processing for generating missing instructions
        //retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
        // add statement block
        retPB.setStatementBlock(sb);
        // add location information
        retPB.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
    } else // process If Statement - add runtime program blocks to program
    if (sb instanceof IfStatementBlock) {
        // create DAG for loop predicates
        pred_dag = new Dag<Lop>();
        ((IfStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
        // create instructions for loop predicates
        pred_instruct = new ArrayList<Instruction>();
        ArrayList<Instruction> pInst = pred_dag.getJobs(null, config);
        for (Instruction i : pInst) {
            pred_instruct.add(i);
        }
        // create if program block
        IfProgramBlock rtpb = new IfProgramBlock(prog, pred_instruct);
        if (rtpb.getPredicateResultVar() == null) {
            // e.g case : If(continue)
            if (((IfStatementBlock) sb).get_predicateLops().getExecLocation() == LopProperties.ExecLocation.Data) {
                String resultVar = ((IfStatementBlock) sb).get_predicateLops().getOutputParameters().getLabel();
                rtpb.setPredicateResultVar(resultVar);
            } else {
                LOG.error(sb.printBlockErrorLocation() + "Error in translating the IF predicate.");
                throw new LopsException(sb.printBlockErrorLocation() + "Error in translating the IF predicate.");
            }
        }
        // process the body of the if statement block
        IfStatementBlock isb = (IfStatementBlock) sb;
        if (isb.getNumStatements() > 1) {
            LOG.error(isb.printBlockErrorLocation() + "IfStatementBlock should have only 1 statement");
            throw new LopsException(isb.printBlockErrorLocation() + "IfStatementBlock should have only 1 statement");
        }
        IfStatement istmt = (IfStatement) isb.getStatement(0);
        // process the if body
        for (StatementBlock sblock : istmt.getIfBody()) {
            ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
            rtpb.addProgramBlockIfBody(childBlock);
        }
        // process the else body
        for (StatementBlock sblock : istmt.getElseBody()) {
            ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
            rtpb.addProgramBlockElseBody(childBlock);
        }
        // check there are actually Lops in to process (loop stmt body will not have any)
        if (isb.getLops() != null && !isb.getLops().isEmpty()) {
            LOG.error(isb.printBlockErrorLocation() + "IfStatementBlock should have no Lops");
            throw new LopsException(isb.printBlockErrorLocation() + "IfStatementBlock should have no Lops");
        }
        retPB = rtpb;
        //post processing for generating missing instructions
        //retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
        // add statement block
        retPB.setStatementBlock(sb);
        // add location information
        retPB.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
    } else // NOTE: applies to ForStatementBlock and ParForStatementBlock
    if (sb instanceof ForStatementBlock) {
        ForStatementBlock fsb = (ForStatementBlock) sb;
        // create DAGs for loop predicates 
        Dag<Lop> fromDag = new Dag<Lop>();
        Dag<Lop> toDag = new Dag<Lop>();
        Dag<Lop> incrementDag = new Dag<Lop>();
        if (fsb.getFromHops() != null)
            fsb.getFromLops().addToDag(fromDag);
        if (fsb.getToHops() != null)
            fsb.getToLops().addToDag(toDag);
        if (fsb.getIncrementHops() != null)
            fsb.getIncrementLops().addToDag(incrementDag);
        // create instructions for loop predicates			
        ArrayList<Instruction> fromInstructions = fromDag.getJobs(null, config);
        ArrayList<Instruction> toInstructions = toDag.getJobs(null, config);
        ArrayList<Instruction> incrementInstructions = incrementDag.getJobs(null, config);
        // create for program block
        String sbName = null;
        ForProgramBlock rtpb = null;
        IterablePredicate iterPred = fsb.getIterPredicate();
        String[] iterPredData = IterablePredicate.createIterablePredicateVariables(iterPred.getIterVar().getName(), fsb.getFromLops(), fsb.getToLops(), fsb.getIncrementLops());
        if (sb instanceof ParForStatementBlock) {
            sbName = "ParForStatementBlock";
            rtpb = new ParForProgramBlock(prog, iterPredData, iterPred.getParForParams());
            ParForProgramBlock pfrtpb = (ParForProgramBlock) rtpb;
            pfrtpb.setResultVariables(((ParForStatementBlock) sb).getResultVariables());
            //used for optimization and creating unscoped variables
            pfrtpb.setStatementBlock((ParForStatementBlock) sb);
        } else //ForStatementBlock
        {
            sbName = "ForStatementBlock";
            rtpb = new ForProgramBlock(prog, iterPredData);
        }
        rtpb.setFromInstructions(fromInstructions);
        rtpb.setToInstructions(toInstructions);
        rtpb.setIncrementInstructions(incrementInstructions);
        rtpb.setIterablePredicateVars(iterPredData);
        // process the body of the for statement block
        if (fsb.getNumStatements() > 1) {
            LOG.error(fsb.printBlockErrorLocation() + " " + sbName + " should have 1 statement");
            throw new LopsException(fsb.printBlockErrorLocation() + " " + sbName + " should have 1 statement");
        }
        ForStatement fs = (ForStatement) fsb.getStatement(0);
        for (StatementBlock sblock : fs.getBody()) {
            ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
            rtpb.addProgramBlock(childBlock);
        }
        // check there are actually Lops in to process (loop stmt body will not have any)
        if (fsb.getLops() != null && !fsb.getLops().isEmpty()) {
            LOG.error(fsb.printBlockErrorLocation() + sbName + " should have no Lops");
            throw new LopsException(fsb.printBlockErrorLocation() + sbName + " should have no Lops");
        }
        retPB = rtpb;
        //post processing for generating missing instructions
        //retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
        // add statement block
        retPB.setStatementBlock(sb);
        // add location information
        retPB.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
    } else // process function statement block - add runtime program blocks to program
    if (sb instanceof FunctionStatementBlock) {
        FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
        if (fsb.getNumStatements() > 1) {
            LOG.error(fsb.printBlockErrorLocation() + "FunctionStatementBlock should only have 1 statement");
            throw new LopsException(fsb.printBlockErrorLocation() + "FunctionStatementBlock should only have 1 statement");
        }
        FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
        FunctionProgramBlock rtpb = null;
        if (fstmt instanceof ExternalFunctionStatement) {
            // create external function program block
            String execType = ((ExternalFunctionStatement) fstmt).getOtherParams().get(ExternalFunctionStatement.EXEC_TYPE);
            boolean isCP = (execType.equals(ExternalFunctionStatement.IN_MEMORY)) ? true : false;
            String scratchSpaceLoc = null;
            try {
                scratchSpaceLoc = config.getTextValue(DMLConfig.SCRATCH_SPACE);
            } catch (Exception e) {
                LOG.error(fsb.printBlockErrorLocation() + "could not retrieve parameter " + DMLConfig.SCRATCH_SPACE + " from DMLConfig");
            }
            StringBuilder buff = new StringBuilder();
            buff.append(scratchSpaceLoc);
            buff.append(Lop.FILE_SEPARATOR);
            buff.append(Lop.PROCESS_PREFIX);
            buff.append(DMLScript.getUUID());
            buff.append(Lop.FILE_SEPARATOR);
            buff.append(ProgramConverter.CP_ROOT_THREAD_ID);
            buff.append(Lop.FILE_SEPARATOR);
            buff.append("PackageSupport");
            buff.append(Lop.FILE_SEPARATOR);
            String basedir = buff.toString();
            if (isCP) {
                rtpb = new ExternalFunctionProgramBlockCP(prog, fstmt.getInputParams(), fstmt.getOutputParams(), ((ExternalFunctionStatement) fstmt).getOtherParams(), basedir);
            } else {
                rtpb = new ExternalFunctionProgramBlock(prog, fstmt.getInputParams(), fstmt.getOutputParams(), ((ExternalFunctionStatement) fstmt).getOtherParams(), basedir);
            }
            if (!fstmt.getBody().isEmpty()) {
                LOG.error(fstmt.printErrorLocation() + "ExternalFunctionStatementBlock should have no statement blocks in body");
                throw new LopsException(fstmt.printErrorLocation() + "ExternalFunctionStatementBlock should have no statement blocks in body");
            }
        } else {
            // create function program block
            rtpb = new FunctionProgramBlock(prog, fstmt.getInputParams(), fstmt.getOutputParams());
            // process the function statement body
            for (StatementBlock sblock : fstmt.getBody()) {
                // process the body
                ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
                rtpb.addProgramBlock(childBlock);
            }
        }
        // check there are actually Lops in to process (loop stmt body will not have any)
        if (fsb.getLops() != null && !fsb.getLops().isEmpty()) {
            LOG.error(fsb.printBlockErrorLocation() + "FunctionStatementBlock should have no Lops");
            throw new LopsException(fsb.printBlockErrorLocation() + "FunctionStatementBlock should have no Lops");
        }
        retPB = rtpb;
        // add location information
        retPB.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
    } else {
        // handle general case
        ProgramBlock rtpb = new ProgramBlock(prog);
        // DAGs for Lops
        dag = new Dag<Lop>();
        // check there are actually Lops in to process (loop stmt body will not have any)
        if (sb.getLops() != null && !sb.getLops().isEmpty()) {
            for (Lop l : sb.getLops()) {
                l.addToDag(dag);
            }
            // Instructions for Lobs DAGs
            instruct = dag.getJobs(sb, config);
            rtpb.addInstructions(instruct);
        }
        /*// TODO: check with Doug
			// add instruction for a function call
			if (sb.getFunctionCallInst() != null){
				rtpb.addInstruction(sb.getFunctionCallInst());
			}*/
        retPB = rtpb;
        //post processing for generating missing instructions
        //retPB = verifyAndCorrectProgramBlock(sb.liveIn(), sb.liveOut(), sb._kill, retPB);
        // add statement block
        retPB.setStatementBlock(sb);
        // add location information
        retPB.setAllPositions(sb.getBeginLine(), sb.getBeginColumn(), sb.getEndLine(), sb.getEndColumn());
    }
    return retPB;
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ArrayList(java.util.ArrayList) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) Instruction(org.apache.sysml.runtime.instructions.Instruction) ExternalFunctionProgramBlockCP(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlockCP) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) 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) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) 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) Dag(org.apache.sysml.lops.compile.Dag) Lop(org.apache.sysml.lops.Lop) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LopsException(org.apache.sysml.lops.LopsException) IOException(java.io.IOException) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) LopsException(org.apache.sysml.lops.LopsException)

Example 10 with ParForProgramBlock

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

the class OptimizerRuleBased method rewriteSetOperationsExecType.

///////
//REWRITE set operations exec type
///
protected void rewriteSetOperationsExecType(OptNode pn, boolean recompile) throws DMLRuntimeException {
    //set exec type in internal opt tree
    int count = setOperationExecType(pn, ExecType.CP);
    //recompile program (actual programblock modification)
    if (recompile && count <= 0)
        LOG.warn("OPT: Forced set operations exec type 'CP', but no operation requires recompile.");
    ParForProgramBlock pfpb = (ParForProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(pn.getID())[1];
    HashSet<String> fnStack = new HashSet<String>();
    Recompiler.recompileProgramBlockHierarchy2Forced(pfpb.getChildBlocks(), 0, fnStack, LopProperties.ExecType.CP);
    //debug output
    LOG.debug(getOptMode() + " OPT: rewrite 'set operation exec type CP' - result=" + count);
}
Also used : ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) HashSet(java.util.HashSet)

Aggregations

ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)53 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)22 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)20 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)20 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)20 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)19 MatrixObject (org.apache.sysml.runtime.controlprogram.caching.MatrixObject)17 ArrayList (java.util.ArrayList)13 ParForStatementBlock (org.apache.sysml.parser.ParForStatementBlock)10 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)9 RDDObject (org.apache.sysml.runtime.instructions.spark.data.RDDObject)9 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 LopsException (org.apache.sysml.lops.LopsException)7 HopsException (org.apache.sysml.hops.HopsException)6 LanguageException (org.apache.sysml.parser.LanguageException)6 StatementBlock (org.apache.sysml.parser.StatementBlock)5 Hop (org.apache.sysml.hops.Hop)4 DMLProgram (org.apache.sysml.parser.DMLProgram)4