use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-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 incubator-systemml by apache.
the class ProgramConverter method rSerializeProgramBlock.
private static String rSerializeProgramBlock(ProgramBlock pb, HashMap<String, byte[]> clsMap) {
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(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(fpb.getIterVar());
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(pfpb.getIterVar());
sb.append(COMPONENTS_DELIM);
sb.append(serializeResultVariables(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(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.IfProgramBlock in project incubator-systemml by apache.
the class ProgramConverter method createDeepCopyIfProgramBlock.
public static IfProgramBlock createDeepCopyIfProgramBlock(IfProgramBlock ipb, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) {
ArrayList<Instruction> predinst = createDeepCopyInstructionSet(ipb.getPredicate(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true);
IfProgramBlock tmpPB = new IfProgramBlock(prog, predinst);
tmpPB.setStatementBlock(createIfStatementBlockCopy((IfStatementBlock) ipb.getStatementBlock(), pid, plain, forceDeepCopy));
tmpPB.setThreadID(pid);
tmpPB.setExitInstructions2(createDeepCopyInstructionSet(ipb.getExitInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
tmpPB.setChildBlocksIfBody(rcreateDeepCopyProgramBlocks(ipb.getChildBlocksIfBody(), pid, IDPrefix, fnStack, fnCreated, plain, forceDeepCopy));
tmpPB.setChildBlocksElseBody(rcreateDeepCopyProgramBlocks(ipb.getChildBlocksElseBody(), pid, IDPrefix, fnStack, fnCreated, plain, forceDeepCopy));
return tmpPB;
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-systemml by apache.
the class OptTreeConverter method rCreateOptNode.
public static OptNode rCreateOptNode(ProgramBlock pb, LocalVariableMap vars, boolean topLevel, boolean storeObjs) {
OptNode node = null;
if (pb instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
node = new OptNode(NodeType.IF);
if (storeObjs)
_rtMap.putMapping(ipb, node);
node.setExecType(ExecType.CP);
// process if condition
OptNode ifn = new OptNode(NodeType.GENERIC);
node.addChilds(createOptNodes(ipb.getPredicate(), vars, storeObjs));
node.addChild(ifn);
for (ProgramBlock lpb : ipb.getChildBlocksIfBody()) ifn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
// process else condition
if (ipb.getChildBlocksElseBody() != null && ipb.getChildBlocksElseBody().size() > 0) {
OptNode efn = new OptNode(NodeType.GENERIC);
node.addChild(efn);
for (ProgramBlock lpb : ipb.getChildBlocksElseBody()) efn.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
}
} else if (pb instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
node = new OptNode(NodeType.WHILE);
if (storeObjs)
_rtMap.putMapping(wpb, node);
node.setExecType(ExecType.CP);
// process predicate instruction
node.addChilds(createOptNodes(wpb.getPredicate(), vars, storeObjs));
// process body
for (ProgramBlock lpb : wpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
} else if (pb instanceof ForProgramBlock && !(pb instanceof ParForProgramBlock)) {
ForProgramBlock fpb = (ForProgramBlock) pb;
node = new OptNode(NodeType.FOR);
if (storeObjs)
_rtMap.putMapping(fpb, node);
node.setExecType(ExecType.CP);
// determine number of iterations
long N = OptimizerUtils.getNumIterations(fpb, vars, CostEstimator.FACTOR_NUM_ITERATIONS);
node.addParam(ParamType.NUM_ITERATIONS, String.valueOf(N));
node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
// process body
for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, topLevel, storeObjs));
} else if (pb instanceof ParForProgramBlock) {
ParForProgramBlock fpb = (ParForProgramBlock) pb;
node = new OptNode(NodeType.PARFOR);
if (storeObjs)
_rtMap.putMapping(fpb, node);
node.setK(fpb.getDegreeOfParallelism());
long N = fpb.getNumIterations();
node.addParam(ParamType.NUM_ITERATIONS, (N != -1) ? String.valueOf(N) : String.valueOf(CostEstimator.FACTOR_NUM_ITERATIONS));
switch(fpb.getExecMode()) {
case LOCAL:
node.setExecType(ExecType.CP);
break;
case REMOTE_MR:
case REMOTE_MR_DP:
node.setExecType(ExecType.MR);
break;
case REMOTE_SPARK:
case REMOTE_SPARK_DP:
node.setExecType(ExecType.SPARK);
break;
default:
node.setExecType(null);
}
if (!topLevel) {
node.addChilds(createOptNodes(fpb.getFromInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getToInstructions(), vars, storeObjs));
node.addChilds(createOptNodes(fpb.getIncrementInstructions(), vars, storeObjs));
}
// process body
for (ProgramBlock lpb : fpb.getChildBlocks()) node.addChild(rCreateOptNode(lpb, vars, false, storeObjs));
// parameters, add required parameters
} else // last level program block
{
node = new OptNode(NodeType.GENERIC);
if (storeObjs)
_rtMap.putMapping(pb, node);
node.addChilds(createOptNodes(pb.getInstructions(), vars, storeObjs));
node.setExecType(ExecType.CP);
}
return node;
}
use of org.apache.sysml.runtime.controlprogram.IfProgramBlock in project incubator-systemml by apache.
the class ProgramRecompiler method rFindAndRecompileIndexingHOP.
/**
* NOTE: if force is set, we set and recompile the respective indexing hops;
* otherwise, we release the forced exec type and recompile again. Hence,
* any changes can be exactly reverted with the same access behavior.
*
* @param sb statement block
* @param pb program block
* @param var variable
* @param ec execution context
* @param force if true, set and recompile the respective indexing hops
*/
public static void rFindAndRecompileIndexingHOP(StatementBlock sb, ProgramBlock pb, String var, ExecutionContext ec, boolean force) {
if (pb instanceof IfProgramBlock && sb instanceof IfStatementBlock) {
IfProgramBlock ipb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement is = (IfStatement) sb.getStatement(0);
// process if condition
if (isb.getPredicateHops() != null)
ipb.setPredicate(rFindAndRecompileIndexingHOP(isb.getPredicateHops(), ipb.getPredicate(), var, ec, force));
// process if branch
int len = is.getIfBody().size();
for (int i = 0; i < ipb.getChildBlocksIfBody().size() && i < len; i++) {
ProgramBlock lpb = ipb.getChildBlocksIfBody().get(i);
StatementBlock lsb = is.getIfBody().get(i);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
// process else branch
if (ipb.getChildBlocksElseBody() != null) {
int len2 = is.getElseBody().size();
for (int i = 0; i < ipb.getChildBlocksElseBody().size() && i < len2; i++) {
ProgramBlock lpb = ipb.getChildBlocksElseBody().get(i);
StatementBlock lsb = is.getElseBody().get(i);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
}
} else if (pb instanceof WhileProgramBlock && sb instanceof WhileStatementBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement ws = (WhileStatement) sb.getStatement(0);
// process while condition
if (wsb.getPredicateHops() != null)
wpb.setPredicate(rFindAndRecompileIndexingHOP(wsb.getPredicateHops(), wpb.getPredicate(), var, ec, force));
// process body
// robustness for potentially added problem blocks
int len = ws.getBody().size();
for (int i = 0; i < wpb.getChildBlocks().size() && i < len; i++) {
ProgramBlock lpb = wpb.getChildBlocks().get(i);
StatementBlock lsb = ws.getBody().get(i);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
} else if (// for or parfor
pb instanceof ForProgramBlock && sb instanceof ForStatementBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fs = (ForStatement) fsb.getStatement(0);
if (fsb.getFromHops() != null)
fpb.setFromInstructions(rFindAndRecompileIndexingHOP(fsb.getFromHops(), fpb.getFromInstructions(), var, ec, force));
if (fsb.getToHops() != null)
fpb.setToInstructions(rFindAndRecompileIndexingHOP(fsb.getToHops(), fpb.getToInstructions(), var, ec, force));
if (fsb.getIncrementHops() != null)
fpb.setIncrementInstructions(rFindAndRecompileIndexingHOP(fsb.getIncrementHops(), fpb.getIncrementInstructions(), var, ec, force));
// process body
// robustness for potentially added problem blocks
int len = fs.getBody().size();
for (int i = 0; i < fpb.getChildBlocks().size() && i < len; i++) {
ProgramBlock lpb = fpb.getChildBlocks().get(i);
StatementBlock lsb = fs.getBody().get(i);
rFindAndRecompileIndexingHOP(lsb, lpb, var, ec, force);
}
} else // last level program block
{
try {
// process actual hops
boolean ret = false;
Hop.resetVisitStatus(sb.getHops());
if (force) {
// set forced execution type
for (Hop h : sb.getHops()) ret |= rFindAndSetCPIndexingHOP(h, var);
} else {
// release forced execution type
for (Hop h : sb.getHops()) ret |= rFindAndReleaseIndexingHOP(h, var);
}
// recompilation on-demand
if (ret) {
// construct new instructions
ArrayList<Instruction> newInst = Recompiler.recompileHopsDag(sb, sb.getHops(), ec.getVariables(), null, true, false, 0);
pb.setInstructions(newInst);
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
}
}
Aggregations