use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction 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.instructions.cp.FunctionCallCPInstruction in project incubator-systemml by apache.
the class ProgramConverter method createDeepCopyInstructionSet.
/**
* Creates a deep copy of an array of instructions and replaces the placeholders of parworker
* IDs with the concrete IDs of this parfor instance. This is a helper method uses for generating
* deep copies of program blocks.
*
* @param instSet list of instructions
* @param pid ?
* @param IDPrefix ?
* @param prog runtime program
* @param fnStack ?
* @param fnCreated ?
* @param plain ?
* @param cpFunctions ?
* @return list of instructions
*/
public static ArrayList<Instruction> createDeepCopyInstructionSet(ArrayList<Instruction> instSet, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean cpFunctions) {
ArrayList<Instruction> tmp = new ArrayList<>();
for (Instruction inst : instSet) {
if (inst instanceof FunctionCallCPInstruction && cpFunctions) {
FunctionCallCPInstruction finst = (FunctionCallCPInstruction) inst;
createDeepCopyFunctionProgramBlock(finst.getNamespace(), finst.getFunctionName(), pid, IDPrefix, prog, fnStack, fnCreated, plain);
}
tmp.add(cloneInstruction(inst, pid, plain, cpFunctions));
}
return tmp;
}
use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction in project incubator-systemml by apache.
the class OptTreePlanChecker method checkFunctionNames.
private static void checkFunctionNames(Program prog, DMLProgram dprog, Hop root, ArrayList<Instruction> inst, Set<String> fnStack) {
// reset visit status of dag
root.resetVisitStatus();
// get all function op in this dag
HashMap<String, FunctionOp> fops = new HashMap<>();
getAllFunctionOps(root, fops);
for (Instruction linst : inst) if (linst instanceof FunctionCallCPInstruction) {
FunctionCallCPInstruction flinst = (FunctionCallCPInstruction) linst;
String fnamespace = flinst.getNamespace();
String fname = flinst.getFunctionName();
String key = DMLProgram.constructFunctionKey(fnamespace, fname);
// check 1: instruction name equal to hop name
if (!fops.containsKey(key))
throw new DMLRuntimeException("Function Check: instruction and hop names differ (" + key + ", " + fops.keySet() + ")");
// check 2: function exists
if (!prog.getFunctionProgramBlocks().containsKey(key))
throw new DMLRuntimeException("Function Check: function does not exits (" + key + ")");
// check 3: recursive program check
FunctionProgramBlock fpb = prog.getFunctionProgramBlock(fnamespace, fname);
FunctionStatementBlock fsb = dprog.getFunctionStatementBlock(fnamespace, fname);
if (!fnStack.contains(key)) {
fnStack.add(key);
checkProgramCorrectness(fpb, fsb, fnStack);
fnStack.remove(key);
}
}
}
use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction in project incubator-systemml by apache.
the class OptimizerRuleBased method rReplaceFunctionNames.
protected void rReplaceFunctionNames(OptNode n, String oldName, String newName) {
if (n.getNodeType() == NodeType.FUNCCALL) {
FunctionOp fop = (FunctionOp) OptTreeConverter.getAbstractPlanMapping().getMappedHop(n.getID());
String[] names = n.getParam(ParamType.OPSTRING).split(Program.KEY_DELIM);
String fnamespace = names[0];
String fname = names[1];
if (// newName if shared hop
fname.equals(oldName) || fname.equals(newName)) {
// set opttree function name
n.addParam(ParamType.OPSTRING, DMLProgram.constructFunctionKey(fnamespace, newName));
// set instruction function name
long parentID = OptTreeConverter.getAbstractPlanMapping().getMappedParentID(n.getID());
ProgramBlock pb = (ProgramBlock) OptTreeConverter.getAbstractPlanMapping().getMappedProg(parentID)[1];
ArrayList<Instruction> instArr = pb.getInstructions();
for (int i = 0; i < instArr.size(); i++) {
Instruction inst = instArr.get(i);
if (inst instanceof FunctionCallCPInstruction) {
FunctionCallCPInstruction fci = (FunctionCallCPInstruction) inst;
if (oldName.equals(fci.getFunctionName()))
instArr.set(i, FunctionCallCPInstruction.parseInstruction(fci.toString().replaceAll(oldName, newName)));
}
}
// set hop name (for recompile)
if (fop.getFunctionName().equals(oldName))
fop.setFunctionName(newName);
}
}
// recursive invocation
if (!n.isLeaf())
for (OptNode c : n.getChilds()) rReplaceFunctionNames(c, oldName, newName);
}
use of org.apache.sysml.runtime.instructions.cp.FunctionCallCPInstruction in project incubator-systemml by apache.
the class ProgramConverter method cloneInstruction.
public static Instruction cloneInstruction(Instruction oInst, long pid, boolean plain, boolean cpFunctions) {
Instruction inst = null;
String tmpString = oInst.toString();
try {
if (oInst instanceof CPInstruction || oInst instanceof SPInstruction || oInst instanceof MRInstruction || oInst instanceof GPUInstruction) {
if (oInst instanceof FunctionCallCPInstruction && cpFunctions) {
FunctionCallCPInstruction tmp = (FunctionCallCPInstruction) oInst;
if (!plain) {
// safe replacement because target variables might include the function name
// note: this is no update-in-place in order to keep the original function name as basis
tmpString = tmp.updateInstStringFunctionName(tmp.getFunctionName(), tmp.getFunctionName() + CP_CHILD_THREAD + pid);
}
// otherwise: preserve function name
}
inst = InstructionParser.parseSingleInstruction(tmpString);
} else if (oInst instanceof MRJobInstruction) {
// clone via copy constructor
inst = new MRJobInstruction((MRJobInstruction) oInst);
} else
throw new DMLRuntimeException("Failed to clone instruction: " + oInst);
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
// save replacement of thread id references in instructions
inst = saveReplaceThreadID(inst, ProgramConverter.CP_ROOT_THREAD_ID, ProgramConverter.CP_CHILD_THREAD + pid);
return inst;
}
Aggregations