use of org.apache.sysml.runtime.controlprogram.ForProgramBlock in project incubator-systemml by apache.
the class SpoofCompiler method generateCodeFromProgramBlock.
public static void generateCodeFromProgramBlock(ProgramBlock current) {
if (current instanceof FunctionProgramBlock) {
FunctionProgramBlock fsb = (FunctionProgramBlock) current;
for (ProgramBlock pb : fsb.getChildBlocks()) generateCodeFromProgramBlock(pb);
} else if (current instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) current;
WhileStatementBlock wsb = (WhileStatementBlock) wpb.getStatementBlock();
if (wsb != null && wsb.getPredicateHops() != null)
wpb.setPredicate(generateCodeFromHopDAGsToInst(wsb.getPredicateHops()));
for (ProgramBlock sb : wpb.getChildBlocks()) generateCodeFromProgramBlock(sb);
} else if (current instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) current;
IfStatementBlock isb = (IfStatementBlock) ipb.getStatementBlock();
if (isb != null && isb.getPredicateHops() != null)
ipb.setPredicate(generateCodeFromHopDAGsToInst(isb.getPredicateHops()));
for (ProgramBlock pb : ipb.getChildBlocksIfBody()) generateCodeFromProgramBlock(pb);
for (ProgramBlock pb : ipb.getChildBlocksElseBody()) generateCodeFromProgramBlock(pb);
} else if (// incl parfor
current instanceof ForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) current;
ForStatementBlock fsb = (ForStatementBlock) fpb.getStatementBlock();
if (fsb != null && fsb.getFromHops() != null)
fpb.setFromInstructions(generateCodeFromHopDAGsToInst(fsb.getFromHops()));
if (fsb != null && fsb.getToHops() != null)
fpb.setToInstructions(generateCodeFromHopDAGsToInst(fsb.getToHops()));
if (fsb != null && fsb.getIncrementHops() != null)
fpb.setIncrementInstructions(generateCodeFromHopDAGsToInst(fsb.getIncrementHops()));
for (ProgramBlock pb : fpb.getChildBlocks()) generateCodeFromProgramBlock(pb);
} else // generic (last-level)
{
StatementBlock sb = current.getStatementBlock();
current.setInstructions(generateCodeFromHopDAGsToInst(sb, sb.getHops()));
}
}
use of org.apache.sysml.runtime.controlprogram.ForProgramBlock 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.ForProgramBlock 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.ForProgramBlock 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.ForProgramBlock in project incubator-systemml by apache.
the class OptimizerRuleBased method removeRecursiveParFor.
protected int removeRecursiveParFor(OptNode n, HashSet<ParForProgramBlock> recPBs) {
int count = 0;
if (!n.isLeaf()) {
for (OptNode sub : n.getChilds()) {
if (sub.getNodeType() == NodeType.PARFOR) {
long id = sub.getID();
Object[] progobj = OptTreeConverter.getAbstractPlanMapping().getMappedProg(id);
ParForStatementBlock pfsb = (ParForStatementBlock) progobj[0];
ParForProgramBlock pfpb = (ParForProgramBlock) progobj[1];
if (recPBs.contains(pfpb)) {
// create for pb as replacement
Program prog = pfpb.getProgram();
ForProgramBlock fpb = ProgramConverter.createShallowCopyForProgramBlock(pfpb, prog);
// replace parfor with for, and update objectmapping
OptTreeConverter.replaceProgramBlock(n, sub, pfpb, fpb, false);
// update link to statement block
fpb.setStatementBlock(pfsb);
// update node
sub.setNodeType(NodeType.FOR);
sub.setK(1);
count++;
}
}
count += removeRecursiveParFor(sub, recPBs);
}
}
return count;
}
Aggregations