use of org.apache.sysml.runtime.instructions.Instruction 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.Instruction 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);
}
}
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class ProgramRecompiler method rFindAndRecompileIndexingHOP.
private static ArrayList<Instruction> rFindAndRecompileIndexingHOP(Hop hop, ArrayList<Instruction> in, String var, ExecutionContext ec, boolean force) {
ArrayList<Instruction> tmp = in;
try {
boolean ret = false;
hop.resetVisitStatus();
if (// set forced execution type
force)
ret = rFindAndSetCPIndexingHOP(hop, var);
else
// release forced execution type
ret = rFindAndReleaseIndexingHOP(hop, var);
// recompilation on-demand
if (ret) {
// construct new instructions
tmp = Recompiler.recompileHopsDag(hop, ec.getVariables(), null, true, false, 0);
}
} catch (Exception ex) {
throw new DMLRuntimeException(ex);
}
return tmp;
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-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.instructions.Instruction 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