use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.
the class ResourceOptimizer method pruneHasOnlyUnknownMR.
private static boolean pruneHasOnlyUnknownMR(ProgramBlock pb) {
if (pb instanceof WhileProgramBlock) {
WhileStatementBlock sb = (WhileStatementBlock) pb.getStatementBlock();
sb.getPredicateHops().resetVisitStatus();
return pruneHasOnlyUnknownMR(sb.getPredicateHops());
} else if (pb instanceof IfProgramBlock) {
IfStatementBlock sb = (IfStatementBlock) pb.getStatementBlock();
sb.getPredicateHops().resetVisitStatus();
return pruneHasOnlyUnknownMR(sb.getPredicateHops());
} else if (// incl parfor
pb instanceof ForProgramBlock) {
ForStatementBlock sb = (ForStatementBlock) pb.getStatementBlock();
sb.getFromHops().resetVisitStatus();
sb.getToHops().resetVisitStatus();
sb.getIncrementHops().resetVisitStatus();
return pruneHasOnlyUnknownMR(sb.getFromHops()) && pruneHasOnlyUnknownMR(sb.getToHops()) && pruneHasOnlyUnknownMR(sb.getIncrementHops());
} else // last-level program blocks
{
StatementBlock sb = pb.getStatementBlock();
return pruneHasOnlyUnknownMR(sb.getHops());
}
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.
the class DMLTranslator method createRuntimeProgramBlock.
public ProgramBlock createRuntimeProgramBlock(Program prog, StatementBlock sb, DMLConfig config) {
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<>();
((WhileStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<>();
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);
// // process the body of the while statement block ////
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
for (StatementBlock sblock : wstmt.getBody()) {
// process the body
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlock(childBlock);
}
retPB = rtpb;
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
} else // process If Statement - add runtime program blocks to program
if (sb instanceof IfStatementBlock) {
// create DAG for loop predicates
pred_dag = new Dag<>();
((IfStatementBlock) sb).get_predicateLops().addToDag(pred_dag);
// create instructions for loop predicates
pred_instruct = new ArrayList<>();
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);
// process the body of the if statement block
IfStatementBlock isb = (IfStatementBlock) sb;
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);
}
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.setParseInfo(sb);
} 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<>();
Dag<Lop> toDag = new Dag<>();
Dag<Lop> incrementDag = new Dag<>();
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
ForProgramBlock rtpb = null;
IterablePredicate iterPred = fsb.getIterPredicate();
if (sb instanceof ParForStatementBlock && ConfigurationManager.isParallelParFor()) {
rtpb = new ParForProgramBlock(prog, iterPred.getIterVar().getName(), iterPred.getParForParams(), ((ParForStatementBlock) sb).getResultVariables());
ParForProgramBlock pfrtpb = (ParForProgramBlock) rtpb;
// used for optimization and creating unscoped variables
pfrtpb.setStatementBlock((ParForStatementBlock) sb);
} else {
// ForStatementBlock
rtpb = new ForProgramBlock(prog, iterPred.getIterVar().getName());
}
rtpb.setFromInstructions(fromInstructions);
rtpb.setToInstructions(toInstructions);
rtpb.setIncrementInstructions(incrementInstructions);
// process the body of the for statement block
ForStatement fs = (ForStatement) fsb.getStatement(0);
for (StatementBlock sblock : fs.getBody()) {
ProgramBlock childBlock = createRuntimeProgramBlock(prog, sblock, config);
rtpb.addProgramBlock(childBlock);
}
retPB = rtpb;
// add statement block
retPB.setStatementBlock(sb);
// add location information
retPB.setParseInfo(sb);
} else // process function statement block - add runtime program blocks to program
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
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;
StringBuilder buff = new StringBuilder();
buff.append(config.getTextValue(DMLConfig.SCRATCH_SPACE));
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.setParseInfo(sb);
} else {
// handle general case
ProgramBlock rtpb = new ProgramBlock(prog);
// DAGs for Lops
dag = new Dag<>();
// 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 Lops DAGs
instruct = dag.getJobs(sb, config);
rtpb.addInstructions(instruct);
}
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.setParseInfo(sb);
}
return retPB;
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.
the class ProgramConverter method rParseIfProgramBlock.
private static IfProgramBlock rParseIfProgramBlock(String in, Program prog, int id) {
String lin = in.substring(PARFOR_PB_IF.length(), in.length() - PARFOR_PB_END.length());
HierarchyAwareStringTokenizer st = new HierarchyAwareStringTokenizer(lin, COMPONENTS_DELIM);
// predicate instructions
ArrayList<Instruction> inst = parseInstructions(st.nextToken(), id);
// exit instructions
ArrayList<Instruction> exit = parseInstructions(st.nextToken(), id);
// program blocks: if and else
ArrayList<ProgramBlock> pbs1 = rParseProgramBlocks(st.nextToken(), prog, id);
ArrayList<ProgramBlock> pbs2 = rParseProgramBlocks(st.nextToken(), prog, id);
IfProgramBlock ipb = new IfProgramBlock(prog, inst);
ipb.setExitInstructions2(exit);
ipb.setChildBlocksIfBody(pbs1);
ipb.setChildBlocksElseBody(pbs2);
return ipb;
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.
the class ProgramConverter method rFindSerializationCandidates.
private static void rFindSerializationCandidates(ArrayList<ProgramBlock> pbs, HashSet<String> cand) {
for (ProgramBlock pb : pbs) {
if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
rFindSerializationCandidates(wpb.getChildBlocks(), cand);
} else if (pb instanceof ForProgramBlock || pb instanceof ParForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
rFindSerializationCandidates(fpb.getChildBlocks(), cand);
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
rFindSerializationCandidates(ipb.getChildBlocksIfBody(), cand);
if (ipb.getChildBlocksElseBody() != null)
rFindSerializationCandidates(ipb.getChildBlocksElseBody(), cand);
} else // all generic program blocks
{
for (Instruction inst : pb.getInstructions()) if (inst instanceof FunctionCallCPInstruction) {
FunctionCallCPInstruction fci = (FunctionCallCPInstruction) inst;
String fkey = DMLProgram.constructFunctionKey(fci.getNamespace(), fci.getFunctionName());
if (// memoization for multiple calls, recursion
!cand.contains(fkey)) {
// add to candidates
cand.add(fkey);
// investigate chains of function calls
FunctionProgramBlock fpb = pb.getProgram().getFunctionProgramBlock(fci.getNamespace(), fci.getFunctionName());
rFindSerializationCandidates(fpb.getChildBlocks(), cand);
}
}
}
}
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project systemml by apache.
the class ProgramConverter method rcreateDeepCopyProgramBlocks.
/**
* This recursively creates a deep copy of program blocks and transparently replaces filenames according to the
* specified parallel worker in order to avoid conflicts between parworkers. This happens recursively in order
* to support arbitrary control-flow constructs within a parfor.
*
* @param childBlocks child program blocks
* @param pid ?
* @param IDPrefix ?
* @param fnStack ?
* @param fnCreated ?
* @param plain if true, full deep copy without id replacement
* @param forceDeepCopy if true, force deep copy
* @return list of program blocks
*/
public static ArrayList<ProgramBlock> rcreateDeepCopyProgramBlocks(ArrayList<ProgramBlock> childBlocks, long pid, int IDPrefix, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) {
ArrayList<ProgramBlock> tmp = new ArrayList<>();
for (ProgramBlock pb : childBlocks) {
Program prog = pb.getProgram();
ProgramBlock tmpPB = null;
if (pb instanceof WhileProgramBlock) {
tmpPB = createDeepCopyWhileProgramBlock((WhileProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
tmpPB = createDeepCopyForProgramBlock((ForProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof ParForProgramBlock) {
ParForProgramBlock pfpb = (ParForProgramBlock) pb;
if (ParForProgramBlock.ALLOW_NESTED_PARALLELISM)
tmpPB = createDeepCopyParForProgramBlock(pfpb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
else
tmpPB = createDeepCopyForProgramBlock((ForProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else if (pb instanceof IfProgramBlock) {
tmpPB = createDeepCopyIfProgramBlock((IfProgramBlock) pb, pid, IDPrefix, prog, fnStack, fnCreated, plain, forceDeepCopy);
} else // last-level program block
{
// general case use for most PBs
tmpPB = new ProgramBlock(prog);
// for recompile in the master node JVM
tmpPB.setStatementBlock(createStatementBlockCopy(pb.getStatementBlock(), pid, plain, forceDeepCopy));
// tmpPB.setStatementBlock(pb.getStatementBlock());
tmpPB.setThreadID(pid);
}
// copy instructions
tmpPB.setInstructions(createDeepCopyInstructionSet(pb.getInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
// copy symbol table
// tmpPB.setVariables( pb.getVariables() ); //implicit cloning
tmp.add(tmpPB);
}
return tmp;
}
Aggregations