Search in sources :

Example 1 with ProgramBlock

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

the class Recompiler method rRecompileProgramBlock.

//////////////////////////////
// private helper functions //
//////////////////////////////
private static void rRecompileProgramBlock(ProgramBlock pb, LocalVariableMap vars, RecompileStatus status, long tid, boolean resetRecompile) throws HopsException, DMLRuntimeException, LopsException, IOException {
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
        //recompile predicate
        recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
        //remove updated scalars because in loop
        removeUpdatedScalars(vars, wsb);
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, wsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, wsb)) {
            //second pass with unknowns if required
            recompileWhilePredicate(wpb, wsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : wpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, wsb);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
        //recompile predicate
        recompileIfPredicate(ipb, isb, vars, status, tid, resetRecompile);
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        LocalVariableMap varsElse = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        RecompileStatus statusElse = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : ipb.getChildBlocksIfBody()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        for (ProgramBlock pb2 : ipb.getChildBlocksElseBody()) rRecompileProgramBlock(pb2, varsElse, statusElse, tid, resetRecompile);
        reconcileUpdatedCallVarsIf(oldVars, vars, varsElse, isb);
        reconcileUpdatedCallVarsIf(oldStatus, status, statusElse, isb);
        removeUpdatedScalars(vars, ipb.getStatementBlock());
    } else if (//includes ParFORProgramBlock
    pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
        //recompile predicates
        recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
        //remove updated scalars because in loop
        removeUpdatedScalars(vars, fpb.getStatementBlock());
        //copy vars for later compare
        LocalVariableMap oldVars = (LocalVariableMap) vars.clone();
        RecompileStatus oldStatus = (RecompileStatus) status.clone();
        for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        if (reconcileUpdatedCallVarsLoops(oldVars, vars, fsb) | reconcileUpdatedCallVarsLoops(oldStatus, status, fsb)) {
            //second pass with unknowns if required
            recompileForPredicates(fpb, fsb, vars, status, tid, resetRecompile);
            for (ProgramBlock pb2 : fpb.getChildBlocks()) rRecompileProgramBlock(pb2, vars, status, tid, resetRecompile);
        }
        removeUpdatedScalars(vars, fpb.getStatementBlock());
    } else if (//includes ExternalFunctionProgramBlock and ExternalFunctionProgramBlockCP
    pb instanceof FunctionProgramBlock) {
    //do nothing
    } else {
        StatementBlock sb = pb.getStatementBlock();
        ArrayList<Instruction> tmp = pb.getInstructions();
        if (//recompile all for stats propagation and recompile flags
        sb != null) //&& Recompiler.requiresRecompilation( sb.get_hops() ) 
        /*&& !Recompiler.containsNonRecompileInstructions(tmp)*/
        {
            tmp = Recompiler.recompileHopsDag(sb, sb.get_hops(), vars, status, true, false, tid);
            pb.setInstructions(tmp);
            //propagate stats across hops (should be executed on clone of vars)
            Recompiler.extractDAGOutputStatistics(sb.get_hops(), vars);
            //reset recompilation flags (w/ special handling functions)
            if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && !containsRootFunctionOp(sb.get_hops()) && resetRecompile) {
                Hop.resetRecompilationFlag(sb.get_hops(), ExecType.CP);
                sb.updateRecompilationFlag();
            }
        }
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForStatementBlock(org.apache.sysml.parser.ForStatementBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) LocalVariableMap(org.apache.sysml.runtime.controlprogram.LocalVariableMap) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ParForProgramBlock(org.apache.sysml.runtime.controlprogram.ParForProgramBlock) ArrayList(java.util.ArrayList) 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) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) 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)

Example 2 with ProgramBlock

use of org.apache.sysml.runtime.controlprogram.ProgramBlock 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 3 with ProgramBlock

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

the class Explain method explain.

public static String explain(Program rtprog) throws HopsException {
    //counts number of instructions
    boolean sparkExec = OptimizerUtils.isSparkExecutionMode();
    ExplainCounts counts = new ExplainCounts();
    countCompiledInstructions(rtprog, counts, !sparkExec, true, sparkExec);
    StringBuilder sb = new StringBuilder();
    //create header
    sb.append("\nPROGRAM ( size CP/" + (sparkExec ? "SP" : "MR") + " = ");
    sb.append(counts.numCPInst);
    sb.append("/");
    sb.append(counts.numJobs);
    sb.append(" )\n");
    //explain functions (if exists)
    Map<String, FunctionProgramBlock> funcMap = rtprog.getFunctionProgramBlocks();
    if (funcMap != null && !funcMap.isEmpty()) {
        sb.append("--FUNCTIONS\n");
        //show function call graph
        if (!rtprog.getProgramBlocks().isEmpty() && rtprog.getProgramBlocks().get(0).getStatementBlock() != null) {
            sb.append("----FUNCTION CALL GRAPH\n");
            sb.append("------MAIN PROGRAM\n");
            DMLProgram prog = rtprog.getProgramBlocks().get(0).getStatementBlock().getDMLProg();
            FunctionCallGraph fgraph = new FunctionCallGraph(prog);
            sb.append(explainFunctionCallGraph(fgraph, new HashSet<String>(), null, 3));
        }
        //show individual functions
        for (Entry<String, FunctionProgramBlock> e : funcMap.entrySet()) {
            String fkey = e.getKey();
            FunctionProgramBlock fpb = e.getValue();
            if (fpb instanceof ExternalFunctionProgramBlock)
                sb.append("----EXTERNAL FUNCTION " + fkey + "\n");
            else {
                sb.append("----FUNCTION " + fkey + " [recompile=" + fpb.isRecompileOnce() + "]\n");
                for (ProgramBlock pb : fpb.getChildBlocks()) sb.append(explainProgramBlock(pb, 3));
            }
        }
    }
    //explain main program
    sb.append("--MAIN PROGRAM\n");
    for (ProgramBlock pb : rtprog.getProgramBlocks()) sb.append(explainProgramBlock(pb, 2));
    return sb.toString();
}
Also used : FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) ExternalFunctionProgramBlock(org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock) DMLProgram(org.apache.sysml.parser.DMLProgram) FunctionCallGraph(org.apache.sysml.hops.ipa.FunctionCallGraph) 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) HashSet(java.util.HashSet)

Example 4 with ProgramBlock

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

the class JMLCUtils method rCleanupRuntimeProgram.

/**
	 * Cleanup program blocks (called recursively).
	 * 
	 * @param pb program block
	 * @param outputs registered output variables
	 */
public static void rCleanupRuntimeProgram(ProgramBlock pb, String[] outputs) {
    if (pb instanceof WhileProgramBlock) {
        WhileProgramBlock wpb = (WhileProgramBlock) pb;
        for (ProgramBlock pbc : wpb.getChildBlocks()) rCleanupRuntimeProgram(pbc, outputs);
    } else if (pb instanceof IfProgramBlock) {
        IfProgramBlock ipb = (IfProgramBlock) pb;
        for (ProgramBlock pbc : ipb.getChildBlocksIfBody()) rCleanupRuntimeProgram(pbc, outputs);
        for (ProgramBlock pbc : ipb.getChildBlocksElseBody()) rCleanupRuntimeProgram(pbc, outputs);
    } else if (pb instanceof ForProgramBlock) {
        ForProgramBlock fpb = (ForProgramBlock) pb;
        for (ProgramBlock pbc : fpb.getChildBlocks()) rCleanupRuntimeProgram(pbc, outputs);
    } else {
        ArrayList<Instruction> tmp = pb.getInstructions();
        cleanupRuntimeInstructions(tmp, outputs);
    }
}
Also used : IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) ForProgramBlock(org.apache.sysml.runtime.controlprogram.ForProgramBlock) FunctionProgramBlock(org.apache.sysml.runtime.controlprogram.FunctionProgramBlock) ProgramBlock(org.apache.sysml.runtime.controlprogram.ProgramBlock) IfProgramBlock(org.apache.sysml.runtime.controlprogram.IfProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction)

Example 5 with ProgramBlock

use of org.apache.sysml.runtime.controlprogram.ProgramBlock 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)

Aggregations

ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)114 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)103 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)99 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)95 WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)95 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)66 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)48 Instruction (org.apache.sysml.runtime.instructions.Instruction)33 ArrayList (java.util.ArrayList)30 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)25 StatementBlock (org.apache.sysml.parser.StatementBlock)24 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)20 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)20 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)20 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)19 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)18 Program (org.apache.sysml.runtime.controlprogram.Program)16 CPInstruction (org.apache.sysml.runtime.instructions.cp.CPInstruction)16 MRInstruction (org.apache.sysml.runtime.instructions.mr.MRInstruction)16 DMLProgram (org.apache.sysml.parser.DMLProgram)15