use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.
the class ProgramConverter method createDeepCopyWhileProgramBlock.
public static WhileProgramBlock createDeepCopyWhileProgramBlock(WhileProgramBlock wpb, long pid, int IDPrefix, Program prog, HashSet<String> fnStack, HashSet<String> fnCreated, boolean plain, boolean forceDeepCopy) throws DMLRuntimeException {
ArrayList<Instruction> predinst = createDeepCopyInstructionSet(wpb.getPredicate(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true);
WhileProgramBlock tmpPB = new WhileProgramBlock(prog, predinst);
tmpPB.setPredicateResultVar(wpb.getPredicateResultVar());
tmpPB.setStatementBlock(createWhileStatementBlockCopy((WhileStatementBlock) wpb.getStatementBlock(), pid, plain, forceDeepCopy));
tmpPB.setThreadID(pid);
tmpPB.setExitInstructions2(createDeepCopyInstructionSet(wpb.getExitInstructions(), pid, IDPrefix, prog, fnStack, fnCreated, plain, true));
tmpPB.setChildBlocks(rcreateDeepCopyProgramBlocks(wpb.getChildBlocks(), pid, IDPrefix, fnStack, fnCreated, plain, forceDeepCopy));
return tmpPB;
}
use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.
the class ProgramRewriter method rewriteStatementBlockHopDAGs.
public void rewriteStatementBlockHopDAGs(StatementBlock current, ProgramRewriteStatus state) throws LanguageException, HopsException {
//ensure robustness for calls from outside
if (state == null)
state = new ProgramRewriteStatus();
if (current instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) current;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
for (StatementBlock sb : fstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
} else if (current instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) current;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wsb.setPredicateHops(rewriteHopDAG(wsb.getPredicateHops(), state));
for (StatementBlock sb : wstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
} else if (current instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) current;
IfStatement istmt = (IfStatement) isb.getStatement(0);
isb.setPredicateHops(rewriteHopDAG(isb.getPredicateHops(), state));
for (StatementBlock sb : istmt.getIfBody()) rewriteStatementBlockHopDAGs(sb, state);
for (StatementBlock sb : istmt.getElseBody()) rewriteStatementBlockHopDAGs(sb, state);
} else if (//incl parfor
current instanceof ForStatementBlock) {
ForStatementBlock fsb = (ForStatementBlock) current;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
fsb.setFromHops(rewriteHopDAG(fsb.getFromHops(), state));
fsb.setToHops(rewriteHopDAG(fsb.getToHops(), state));
fsb.setIncrementHops(rewriteHopDAG(fsb.getIncrementHops(), state));
for (StatementBlock sb : fstmt.getBody()) rewriteStatementBlockHopDAGs(sb, state);
} else //generic (last-level)
{
current.set_hops(rewriteHopDAGs(current.get_hops(), state));
}
}
use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.
the class ProgramRewriter method rewriteStatementBlock.
private ArrayList<StatementBlock> rewriteStatementBlock(StatementBlock sb, ProgramRewriteStatus status) throws HopsException {
ArrayList<StatementBlock> ret = new ArrayList<StatementBlock>();
ret.add(sb);
//recursive invocation
if (sb instanceof FunctionStatementBlock) {
FunctionStatementBlock fsb = (FunctionStatementBlock) sb;
FunctionStatement fstmt = (FunctionStatement) fsb.getStatement(0);
fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
} else if (sb instanceof WhileStatementBlock) {
WhileStatementBlock wsb = (WhileStatementBlock) sb;
WhileStatement wstmt = (WhileStatement) wsb.getStatement(0);
wstmt.setBody(rewriteStatementBlocks(wstmt.getBody(), status));
} else if (sb instanceof IfStatementBlock) {
IfStatementBlock isb = (IfStatementBlock) sb;
IfStatement istmt = (IfStatement) isb.getStatement(0);
istmt.setIfBody(rewriteStatementBlocks(istmt.getIfBody(), status));
istmt.setElseBody(rewriteStatementBlocks(istmt.getElseBody(), status));
} else if (//incl parfor
sb instanceof ForStatementBlock) {
//maintain parfor context information (e.g., for checkpointing)
boolean prestatus = status.isInParforContext();
if (sb instanceof ParForStatementBlock)
status.setInParforContext(true);
ForStatementBlock fsb = (ForStatementBlock) sb;
ForStatement fstmt = (ForStatement) fsb.getStatement(0);
fstmt.setBody(rewriteStatementBlocks(fstmt.getBody(), status));
status.setInParforContext(prestatus);
}
//apply rewrite rules
for (StatementBlockRewriteRule r : _sbRuleSet) {
ArrayList<StatementBlock> tmp = new ArrayList<StatementBlock>();
for (StatementBlock sbc : ret) tmp.addAll(r.rewriteStatementBlock(sbc, status));
//take over set of rewritten sbs
ret.clear();
ret.addAll(tmp);
}
return ret;
}
use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.
the class WhileProgramBlock method executePredicate.
private BooleanObject executePredicate(ExecutionContext ec) throws DMLRuntimeException {
BooleanObject result = null;
try {
if (_predicate != null && !_predicate.isEmpty()) {
if (_sb != null) {
if (//set program block specific remote memory
DMLScript.isActiveAM())
DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
WhileStatementBlock wsb = (WhileStatementBlock) _sb;
Hop predicateOp = wsb.getPredicateHops();
boolean recompile = wsb.requiresPredicateRecompilation();
result = (BooleanObject) executePredicate(_predicate, predicateOp, recompile, ValueType.BOOLEAN, ec);
} else
result = (BooleanObject) executePredicate(_predicate, null, false, ValueType.BOOLEAN, ec);
} else {
//get result var
ScalarObject scalarResult = null;
Data resultData = ec.getVariable(_predicateResultVar);
if (resultData == null) {
//note: resultvar is a literal (can it be of any value type other than String, hence no literal/varname conflict)
scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, true);
} else {
scalarResult = ec.getScalarInput(_predicateResultVar, ValueType.BOOLEAN, false);
}
//check for invalid type String
if (scalarResult instanceof StringObject)
throw new DMLRuntimeException(this.printBlockErrorLocation() + "\nWhile predicate variable " + _predicateResultVar + " evaluated to string " + scalarResult + " which is not allowed for predicates in DML");
//process result
if (scalarResult instanceof BooleanObject)
result = (BooleanObject) scalarResult;
else
//auto casting
result = new BooleanObject(scalarResult.getBooleanValue());
}
} catch (Exception ex) {
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Failed to evaluate the while predicate.", ex);
}
//(guaranteed to be non-null, see executePredicate/getScalarInput)
return result;
}
use of org.apache.sysml.parser.WhileStatementBlock in project incubator-systemml by apache.
the class ResourceConfig method addProgramBlock.
private void addProgramBlock(ProgramBlock pb, long init) throws HopsException {
if (pb instanceof FunctionProgramBlock) {
FunctionProgramBlock fpb = (FunctionProgramBlock) pb;
addProgramBlocks(fpb.getChildBlocks(), init);
} else if (pb instanceof WhileProgramBlock) {
WhileProgramBlock fpb = (WhileProgramBlock) pb;
WhileStatementBlock wsb = (WhileStatementBlock) pb.getStatementBlock();
if (ResourceOptimizer.INCLUDE_PREDICATES && wsb != null && wsb.getPredicateHops() != null)
_mrres.add(init);
addProgramBlocks(fpb.getChildBlocks(), init);
} else if (pb instanceof IfProgramBlock) {
IfProgramBlock fpb = (IfProgramBlock) pb;
IfStatementBlock isb = (IfStatementBlock) pb.getStatementBlock();
if (ResourceOptimizer.INCLUDE_PREDICATES && isb != null && isb.getPredicateHops() != null)
_mrres.add(init);
addProgramBlocks(fpb.getChildBlocksIfBody(), init);
addProgramBlocks(fpb.getChildBlocksElseBody(), init);
} else if (//incl parfor
pb instanceof ForProgramBlock) {
ForProgramBlock fpb = (ForProgramBlock) pb;
ForStatementBlock fsb = (ForStatementBlock) pb.getStatementBlock();
if (ResourceOptimizer.INCLUDE_PREDICATES && fsb != null)
_mrres.add(init);
addProgramBlocks(fpb.getChildBlocks(), init);
} else {
//for objects hash is unique because memory location used
_mrres.add(init);
}
}
Aggregations