use of org.apache.sysml.hops.FunctionOp in project incubator-systemml by apache.
the class OptTreeConverter method replaceProgramBlock.
public static void replaceProgramBlock(OptNode parent, OptNode n, ProgramBlock pbOld, ProgramBlock pbNew, boolean rtMap) {
ProgramBlock pbParent = null;
if (rtMap)
pbParent = (ProgramBlock) _rtMap.getMappedObject(parent.getID());
else {
if (parent.getNodeType() == NodeType.FUNCCALL) {
FunctionOp fop = (FunctionOp) _hlMap.getMappedHop(parent.getID());
pbParent = ((Program) _hlMap.getRootProgram()[1]).getFunctionProgramBlock(fop.getFunctionNamespace(), fop.getFunctionName());
} else
pbParent = (ProgramBlock) _hlMap.getMappedProg(parent.getID())[1];
}
if (pbParent instanceof IfProgramBlock) {
IfProgramBlock ipb = (IfProgramBlock) pbParent;
replaceProgramBlock(ipb.getChildBlocksIfBody(), pbOld, pbNew);
replaceProgramBlock(ipb.getChildBlocksElseBody(), pbOld, pbNew);
} else if (pbParent instanceof WhileProgramBlock) {
WhileProgramBlock wpb = (WhileProgramBlock) pbParent;
replaceProgramBlock(wpb.getChildBlocks(), pbOld, pbNew);
} else if (pbParent instanceof ForProgramBlock || pbParent instanceof ParForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) pbParent;
replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
} else if (pbParent instanceof FunctionProgramBlock) {
FunctionProgramBlock fpb = (FunctionProgramBlock) pbParent;
replaceProgramBlock(fpb.getChildBlocks(), pbOld, pbNew);
} else
throw new DMLRuntimeException("Optimizer doesn't support " + pbParent.getClass().getName());
// update repository
if (rtMap)
_rtMap.replaceMapping(pbNew, n);
else
_hlMap.replaceMapping(pbNew, n);
}
use of org.apache.sysml.hops.FunctionOp in project incubator-systemml by apache.
the class OptTreePlanChecker method getAllFunctionOps.
private static void getAllFunctionOps(Hop hop, HashMap<String, FunctionOp> memo) {
if (hop.isVisited())
return;
// process functionop
if (hop instanceof FunctionOp) {
FunctionOp fop = (FunctionOp) hop;
memo.put(fop.getFunctionKey(), fop);
}
// process children
for (Hop in : hop.getInput()) getAllFunctionOps(in, memo);
hop.setVisited();
}
use of org.apache.sysml.hops.FunctionOp in project incubator-systemml by apache.
the class OptimizerRuleBased method rFindAndUnfoldRecursiveFunction.
protected void rFindAndUnfoldRecursiveFunction(OptNode n, ParForProgramBlock parfor, HashSet<ParForProgramBlock> recPBs, LocalVariableMap vars) {
// unfold if found
if (n.getNodeType() == NodeType.FUNCCALL && n.isRecursive()) {
boolean exists = rContainsNode(n, parfor);
if (exists) {
String fnameKey = n.getParam(ParamType.OPSTRING);
String[] names = fnameKey.split(Program.KEY_DELIM);
String fnamespace = names[0];
String fname = names[1];
String fnameNew = FUNCTION_UNFOLD_NAMEPREFIX + fname;
// unfold function
FunctionOp fop = (FunctionOp) OptTreeConverter.getAbstractPlanMapping().getMappedHop(n.getID());
Program prog = parfor.getProgram();
DMLProgram dmlprog = parfor.getStatementBlock().getDMLProg();
FunctionProgramBlock fpb = prog.getFunctionProgramBlock(fnamespace, fname);
FunctionProgramBlock copyfpb = ProgramConverter.createDeepCopyFunctionProgramBlock(fpb, new HashSet<String>(), new HashSet<String>());
prog.addFunctionProgramBlock(fnamespace, fnameNew, copyfpb);
dmlprog.addFunctionStatementBlock(fnamespace, fnameNew, (FunctionStatementBlock) copyfpb.getStatementBlock());
// replace function names in old subtree (link to new function)
rReplaceFunctionNames(n, fname, fnameNew);
// recreate sub opttree
String fnameNewKey = fnamespace + Program.KEY_DELIM + fnameNew;
OptNode nNew = new OptNode(NodeType.FUNCCALL);
OptTreeConverter.getAbstractPlanMapping().putHopMapping(fop, nNew);
nNew.setExecType(ExecType.CP);
nNew.addParam(ParamType.OPSTRING, fnameNewKey);
long parentID = OptTreeConverter.getAbstractPlanMapping().getMappedParentID(n.getID());
OptTreeConverter.getAbstractPlanMapping().getOptNode(parentID).exchangeChild(n, nNew);
HashSet<String> memo = new HashSet<>();
// required if functionop not shared (because not replaced yet)
memo.add(fnameKey);
// requied if functionop shared (indirectly replaced)
memo.add(fnameNewKey);
for (int i = 0; i < copyfpb.getChildBlocks().size(); /*&& i<len*/
i++) {
ProgramBlock lpb = copyfpb.getChildBlocks().get(i);
StatementBlock lsb = lpb.getStatementBlock();
nNew.addChild(OptTreeConverter.rCreateAbstractOptNode(lsb, lpb, vars, false, memo));
}
// compute delta for recPB set (use for removing parfor)
recPBs.removeAll(rGetAllParForPBs(n, new HashSet<ParForProgramBlock>()));
recPBs.addAll(rGetAllParForPBs(nNew, new HashSet<ParForProgramBlock>()));
// replace function names in new subtree (recursive link to new function)
rReplaceFunctionNames(nNew, fname, fnameNew);
}
return;
}
// recursive invocation (only for non-recursive functions)
if (!n.isLeaf())
for (OptNode c : n.getChilds()) rFindAndUnfoldRecursiveFunction(c, parfor, recPBs, vars);
}
use of org.apache.sysml.hops.FunctionOp in project incubator-systemml by apache.
the class OptimizerRuleBased method rGetUIPConsumerList.
private void rGetUIPConsumerList(Hop hop, HashMap<String, ArrayList<UIPCandidateHop>> uipCandHopHM) throws DMLRuntimeException {
if (hop.isVisited())
return;
if ((!(!hop.getParent().isEmpty() && hop.getParent().get(0) instanceof LeftIndexingOp)) && ((hop instanceof DataOp && ((DataOp) hop).getDataOpType() == DataOpTypes.TRANSIENTREAD) || (hop instanceof ReorgOp && (((ReorgOp) hop).getOp() == ReOrgOp.RESHAPE || ((ReorgOp) hop).getOp() == ReOrgOp.TRANSPOSE)) || (hop instanceof FunctionOp))) {
// If candidate's name is same as input hop.
String uipCandiateID = hop.getName();
ArrayList<UIPCandidateHop> uipCandHopList = uipCandHopHM.get(uipCandiateID);
if (uipCandHopList != null) {
for (UIPCandidateHop uipCandHop : uipCandHopList) {
// Add consumers for candidate hop.
ArrayList<Hop> consumerHops = uipCandHop.getConsumerHops();
if (uipCandHop.getConsumerHops() == null)
consumerHops = new ArrayList<Hop>();
consumerHops.add(getRootHop(hop));
uipCandHop.setConsumerHops(consumerHops);
}
}
}
for (Hop hopIn : hop.getInput()) rGetUIPConsumerList(hopIn, uipCandHopHM);
hop.setVisited();
}
use of org.apache.sysml.hops.FunctionOp in project incubator-systemml by apache.
the class InterProceduralAnalysis method determineFunctionCandidatesNNZPropagation.
/////////////////////////////
// DETERMINE NNZ PROPAGATE SAFENESS
//////
/**
* Populates fcandSafeNNZ with all <functionKey,hopID> pairs where it is safe to
* propagate nnz into the function.
*
* @param fcandHops function candidate HOPs
* @param fcandSafeNNZ function candidate safe non-zeros
*/
private void determineFunctionCandidatesNNZPropagation(Map<String, FunctionOp> fcandHops, Map<String, Set<Long>> fcandSafeNNZ) {
//for all function candidates
for (Entry<String, FunctionOp> e : fcandHops.entrySet()) {
String fKey = e.getKey();
FunctionOp fop = e.getValue();
HashSet<Long> tmp = new HashSet<Long>();
//for all inputs of this function call
for (Hop input : fop.getInput()) {
//we checked of equivalence and hence all calls have the same nnz
if (input.getNnz() >= 0)
tmp.add(input.getHopID());
}
fcandSafeNNZ.put(fKey, tmp);
}
}
Aggregations