Search in sources :

Example 6 with WhileProgramBlock

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

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

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

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

the class ProgramConverter method rSerializeProgramBlock.

private static String rSerializeProgramBlock(ProgramBlock pb, HashMap<String, byte[]> clsMap) throws DMLRuntimeException {
    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(wpb.getPredicateResultVar());
        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(serializeStringArray(fpb.getIterablePredicateVars()));
        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(serializeStringArray(pfpb.getIterablePredicateVars()));
        sb.append(COMPONENTS_DELIM);
        sb.append(serializeStringArrayList(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(ipb.getPredicateResultVar());
        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 10 with WhileProgramBlock

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

the class ProgramConverter method createDeepCopyWhileProgramBlock.

public static WhileProgramBlock createDeepCopyWhileProgramBlock(WhileProgramBlock wpb, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) throws DMLRuntimeException {
    ArrayList<Instruction> predinst = createDeepCopyInstructionSet(wpb.getPredicate(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true);
    WhileProgramBlock tmpPB = new WhileProgramBlock(prog, predinst);
    tmpPB.setPredicateResultVar(wpb.getPredicateResultVar());
    tmpPB.setStatementBlock(createWhileStatementBlockCopy((WhileStatementBlock) wpb.getStatementBlock(), pid, plain, forceDeepCopy));
    tmpPB.setThreadID(pid);
    tmpPB.setExitInstructions2(createDeepCopyInstructionSet(wpb.getExitInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
    tmpPB.setChildBlocks(rcreateDeepCopyProgramBlocks(wpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, forceDeepCopy));
    return tmpPB;
}
Also used : WhileProgramBlock(org.apache.sysml.runtime.controlprogram.WhileProgramBlock) GPUInstruction(org.apache.sysml.runtime.instructions.gpu.GPUInstruction) MRJobInstruction(org.apache.sysml.runtime.instructions.MRJobInstruction) CPInstruction(org.apache.sysml.runtime.instructions.cp.CPInstruction) ExternalFunctionInvocationInstruction(org.apache.sysml.udf.ExternalFunctionInvocationInstruction) SpoofCPInstruction(org.apache.sysml.runtime.instructions.cp.SpoofCPInstruction) Instruction(org.apache.sysml.runtime.instructions.Instruction) SPInstruction(org.apache.sysml.runtime.instructions.spark.SPInstruction) VariableCPInstruction(org.apache.sysml.runtime.instructions.cp.VariableCPInstruction) FunctionCallCPInstruction(org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction) MRInstruction(org.apache.sysml.runtime.instructions.mr.MRInstruction) WhileStatementBlock(org.apache.sysml.parser.WhileStatementBlock)

Aggregations

WhileProgramBlock (org.apache.sysml.runtime.controlprogram.WhileProgramBlock)34 ForProgramBlock (org.apache.sysml.runtime.controlprogram.ForProgramBlock)33 IfProgramBlock (org.apache.sysml.runtime.controlprogram.IfProgramBlock)33 FunctionProgramBlock (org.apache.sysml.runtime.controlprogram.FunctionProgramBlock)29 ProgramBlock (org.apache.sysml.runtime.controlprogram.ProgramBlock)26 ParForProgramBlock (org.apache.sysml.runtime.controlprogram.ParForProgramBlock)19 WhileStatementBlock (org.apache.sysml.parser.WhileStatementBlock)14 ForStatementBlock (org.apache.sysml.parser.ForStatementBlock)13 IfStatementBlock (org.apache.sysml.parser.IfStatementBlock)13 StatementBlock (org.apache.sysml.parser.StatementBlock)13 ArrayList (java.util.ArrayList)12 ExternalFunctionProgramBlock (org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock)11 Instruction (org.apache.sysml.runtime.instructions.Instruction)11 Hop (org.apache.sysml.hops.Hop)7 MRJobInstruction (org.apache.sysml.runtime.instructions.MRJobInstruction)7 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)6 VariableCPInstruction (org.apache.sysml.runtime.instructions.cp.VariableCPInstruction)6 FunctionCallCPInstruction (org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction)5 MultiThreadedHop (org.apache.sysml.hops.Hop.MultiThreadedHop)4 FunctionStatementBlock (org.apache.sysml.parser.FunctionStatementBlock)4