use of org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock in project incubator-systemml by apache.
the class CostEstimator method rGetTimeEstimate.
private double rGetTimeEstimate(ProgramBlock pb, HashMap<String, VarStats> stats, HashSet<String> memoFunc, boolean recursive) throws DMLRuntimeException {
double ret = 0;
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock tmp = (WhileProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
ret *= DEFAULT_NUMITER;
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock tmp = (IfProgramBlock) pb;
if (recursive) {
for (ProgramBlock pb2 : tmp.getChildBlocksIfBody()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
if (tmp.getChildBlocksElseBody() != null)
for (ProgramBlock pb2 : tmp.getChildBlocksElseBody()) {
ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
//weighted sum
ret /= 2;
}
}
} else if (//includes ParFORProgramBlock
pb instanceof ForProgramBlock) {
ForProgramBlock tmp = (ForProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
ret *= getNumIterations(stats, tmp.getIterablePredicateVars());
} else if (pb instanceof FunctionProgramBlock && //see generic
!(pb instanceof ExternalFunctionProgramBlock)) {
FunctionProgramBlock tmp = (FunctionProgramBlock) pb;
if (recursive)
for (ProgramBlock pb2 : tmp.getChildBlocks()) ret += rGetTimeEstimate(pb2, stats, memoFunc, recursive);
} else {
ArrayList<Instruction> tmp = pb.getInstructions();
for (Instruction inst : tmp) {
if (//CP
inst instanceof CPInstruction) {
//obtain stats from createvar, cpvar, rmvar, rand
maintainCPInstVariableStatistics((CPInstruction) inst, stats);
//extract statistics (instruction-specific)
Object[] o = extractCPInstStatistics(inst, stats);
VarStats[] vs = (VarStats[]) o[0];
String[] attr = (String[]) o[1];
//if(LOG.isDebugEnabled())
// LOG.debug(inst);
//call time estimation for inst
ret += getCPInstTimeEstimate(inst, vs, attr);
if (//functions
inst instanceof FunctionCallCPInstruction) {
FunctionCallCPInstruction finst = (FunctionCallCPInstruction) inst;
String fkey = DMLProgram.constructFunctionKey(finst.getNamespace(), finst.getFunctionName());
//awareness of recursive functions, missing program
if (!memoFunc.contains(fkey) && pb.getProgram() != null) {
if (LOG.isDebugEnabled())
LOG.debug("Begin Function " + fkey);
memoFunc.add(fkey);
Program prog = pb.getProgram();
FunctionProgramBlock fpb = prog.getFunctionProgramBlock(finst.getNamespace(), finst.getFunctionName());
ret += rGetTimeEstimate(fpb, stats, memoFunc, recursive);
memoFunc.remove(fkey);
if (LOG.isDebugEnabled())
LOG.debug("End Function " + fkey);
}
}
} else if (//MR
inst instanceof MRJobInstruction) {
//obtain stats for job
maintainMRJobInstVariableStatistics(inst, stats);
//extract input statistics
Object[] o = extractMRJobInstStatistics(inst, stats);
VarStats[] vs = (VarStats[]) o[0];
if (LOG.isDebugEnabled())
LOG.debug("Begin MRJob type=" + ((MRJobInstruction) inst).getJobType());
//call time estimation for complex MR inst
ret += getMRJobInstTimeEstimate(inst, vs, null);
if (LOG.isDebugEnabled())
LOG.debug("End MRJob");
//cleanup stats for job
cleanupMRJobVariableStatistics(inst, stats);
}
}
}
return ret;
}
use of org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock 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();
}
use of org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock 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;
}
use of org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock 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();
}
use of org.apache.sysml.runtime.controlprogram.ExternalFunctionProgramBlock in project incubator-systemml by apache.
the class ProgramConverter method rParseExternalFunctionProgramBlock.
private static ExternalFunctionProgramBlock rParseExternalFunctionProgramBlock(String in, Program prog, int id) throws DMLRuntimeException {
String lin = in.substring(PARFOR_PB_EFC.length(), in.length() - PARFOR_PB_END.length());
HierarchyAwareStringTokenizer st = new HierarchyAwareStringTokenizer(lin, COMPONENTS_DELIM);
//LocalVariableMap vars = parseVariables(st.nextToken());
//inputs, outputs and params
ArrayList<DataIdentifier> dat1 = parseDataIdentifiers(st.nextToken());
ArrayList<DataIdentifier> dat2 = parseDataIdentifiers(st.nextToken());
HashMap<String, String> dat3 = parseStringHashMap(st.nextToken());
//basedir
String basedir = st.nextToken();
//instructions (required for removing INST BEGIN, END)
parseInstructions(st.nextToken(), id);
//program blocks
ArrayList<ProgramBlock> pbs = rParseProgramBlocks(st.nextToken(), prog, id);
ArrayList<DataIdentifier> tmp1 = new ArrayList<DataIdentifier>(dat1);
ArrayList<DataIdentifier> tmp2 = new ArrayList<DataIdentifier>(dat2);
//only CP external functions, because no nested MR jobs for reblocks
ExternalFunctionProgramBlockCP efpb = new ExternalFunctionProgramBlockCP(prog, tmp1, tmp2, dat3, basedir);
//efpb.setInstructions(inst);
efpb.setChildBlocks(pbs);
return efpb;
}
Aggregations