use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class RewriteConstantFolding method evalScalarOperation.
/**
* In order to (1) prevent unexpected side effects from constant folding and
* (2) for simplicity with regard to arbitrary value type combinations,
* we use the same compilation and runtime for constant folding as we would
* use for actual instruction execution.
*
* @param bop high-level operator
* @return literal op
*/
private LiteralOp evalScalarOperation(Hop bop) {
// Timing time = new Timing( true );
DataOp tmpWrite = new DataOp(TMP_VARNAME, bop.getDataType(), bop.getValueType(), bop, DataOpTypes.TRANSIENTWRITE, TMP_VARNAME);
// generate runtime instruction
Dag<Lop> dag = new Dag<>();
// prevent lops reuse
Recompiler.rClearLops(tmpWrite);
// reconstruct lops
Lop lops = tmpWrite.constructLops();
lops.addToDag(dag);
ArrayList<Instruction> inst = dag.getJobs(null, ConfigurationManager.getDMLConfig());
// execute instructions
ExecutionContext ec = getExecutionContext();
ProgramBlock pb = getProgramBlock();
pb.setInstructions(inst);
pb.execute(ec);
// get scalar result (check before invocation) and create literal according
// to observed scalar output type (not hop type) for runtime consistency
ScalarObject so = (ScalarObject) ec.getVariable(TMP_VARNAME);
LiteralOp literal = null;
switch(so.getValueType()) {
case DOUBLE:
literal = new LiteralOp(so.getDoubleValue());
break;
case INT:
literal = new LiteralOp(so.getLongValue());
break;
case BOOLEAN:
literal = new LiteralOp(so.getBooleanValue());
break;
case STRING:
literal = new LiteralOp(so.getStringValue());
break;
default:
throw new HopsException("Unsupported literal value type: " + bop.getValueType());
}
// cleanup
tmpWrite.getInput().clear();
bop.getParent().remove(tmpWrite);
pb.setInstructions(null);
ec.getVariables().removeAll();
// set literal properties (scalar)
HopRewriteUtils.setOutputParametersForScalar(literal);
return literal;
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class Recompiler method recompileIfPredicate.
// helper functions for predicate recompile
private static void recompileIfPredicate(IfProgramBlock ipb, IfStatementBlock isb, LocalVariableMap vars, RecompileStatus status, long tid, ResetType resetRecompile) {
if (isb == null)
return;
Hop hops = isb.getPredicateHops();
if (hops != null) {
ArrayList<Instruction> tmp = recompileHopsDag(hops, vars, status, true, false, tid);
ipb.setPredicate(tmp);
if (ParForProgramBlock.RESET_RECOMPILATION_FLAGs && resetRecompile.isReset()) {
Hop.resetRecompilationFlag(hops, ExecType.CP, resetRecompile);
isb.updatePredicateRecompilationFlag();
}
}
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class Recompiler method recompile.
/**
* Core internal primitive for the dynamic recompilation of any DAGs/predicate,
* including all variants with slightly different configurations.
*
* @param sb statement block of DAG, null for predicates
* @param hops list of DAG root nodes
* @param vars symbol table
* @param status recompilation status
* @param inplace modify DAG in place, otherwise deep copy
* @param replaceLit replace literals (only applicable on deep copy)
* @param updateStats update statistics, rewrites, and memory estimates
* @param forceEt force a given execution type, null for reset
* @param pred recompile for predicate DAG
* @param et given execution type
* @param tid thread id, 0 for main or before worker creation
* @return modified list of instructions
*/
private static ArrayList<Instruction> recompile(StatementBlock sb, ArrayList<Hop> hops, LocalVariableMap vars, RecompileStatus status, boolean inplace, boolean replaceLit, boolean updateStats, boolean forceEt, boolean pred, ExecType et, long tid) {
// prepare hops dag for recompile
if (!inplace) {
// deep copy hop dag (for non-reversable rewrites)
hops = deepCopyHopsDag(hops);
} else {
// clear existing lops
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) rClearLops(hopRoot);
}
// replace scalar reads with literals
if (!inplace && replaceLit) {
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) rReplaceLiterals(hopRoot, vars, false);
}
// force exec type (et=null for reset)
if (forceEt) {
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) rSetExecType(hopRoot, et);
Hop.resetVisitStatus(hops);
}
// update statistics, rewrites, and mem estimates
if (updateStats) {
// refresh matrix characteristics (update stats)
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) rUpdateStatistics(hopRoot, vars);
// dynamic hop rewrites
if (!inplace) {
_rewriter.get().rewriteHopDAG(hops, null);
// update stats after rewrites
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) rUpdateStatistics(hopRoot, vars);
}
// refresh memory estimates (based on updated stats,
// before: init memo table with propagated worst-case estimates,
// after: extract worst-case estimates from memo table
Hop.resetVisitStatus(hops);
MemoTable memo = new MemoTable();
memo.init(hops, status);
Hop.resetVisitStatus(hops);
for (Hop hopRoot : hops) hopRoot.refreshMemEstimates(memo);
memo.extract(hops, status);
}
// codegen if enabled
if (ConfigurationManager.isCodegenEnabled() && // not on reset
!(forceEt && et == null) && SpoofCompiler.RECOMPILE_CODEGEN) {
// create deep copy for in-place
if (inplace)
hops = deepCopyHopsDag(hops);
Hop.resetVisitStatus(hops);
hops = SpoofCompiler.optimize(hops, (status == null || !status.isInitialCodegen()));
}
// construct lops
Dag<Lop> dag = new Dag<>();
for (Hop hopRoot : hops) {
Lop lops = hopRoot.constructLops();
lops.addToDag(dag);
}
// generate runtime instructions (incl piggybacking)
ArrayList<Instruction> newInst = dag.getJobs(sb, ConfigurationManager.getDMLConfig());
// defer the explain of instructions after additional modifications
if (DMLScript.EXPLAIN == ExplainType.RECOMPILE_HOPS) {
if (pred)
logExplainPred(hops.get(0), newInst);
else
logExplainDAG(sb, hops, newInst);
}
return newInst;
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class ProgramConverter method rParseForProgramBlock.
private static ForProgramBlock rParseForProgramBlock(String in, Program prog, int id) {
String lin = in.substring(PARFOR_PB_FOR.length(), in.length() - PARFOR_PB_END.length());
HierarchyAwareStringTokenizer st = new HierarchyAwareStringTokenizer(lin, COMPONENTS_DELIM);
// inputs
String iterVar = st.nextToken();
// instructions
ArrayList<Instruction> from = parseInstructions(st.nextToken(), id);
ArrayList<Instruction> to = parseInstructions(st.nextToken(), id);
ArrayList<Instruction> incr = parseInstructions(st.nextToken(), id);
// exit instructions
ArrayList<Instruction> exit = parseInstructions(st.nextToken(), id);
// program blocks
ArrayList<ProgramBlock> pbs = rParseProgramBlocks(st.nextToken(), prog, id);
ForProgramBlock fpb = new ForProgramBlock(prog, iterVar);
fpb.setFromInstructions(from);
fpb.setToInstructions(to);
fpb.setIncrementInstructions(incr);
fpb.setExitInstructions(exit);
fpb.setChildBlocks(pbs);
return fpb;
}
use of org.apache.sysml.runtime.instructions.Instruction in project incubator-systemml by apache.
the class ProgramConverter method serializeInstructions.
@SuppressWarnings("all")
private static String serializeInstructions(ArrayList<Instruction> inst, HashMap<String, byte[]> clsMap) {
StringBuilder sb = new StringBuilder();
int count = 0;
for (Instruction linst : inst) {
// check that only cp instruction are transmitted
if (!(linst instanceof CPInstruction || linst instanceof ExternalFunctionInvocationInstruction))
throw new DMLRuntimeException(NOT_SUPPORTED_MR_INSTRUCTION + " " + linst.getClass().getName() + "\n" + linst);
// obtain serialized version of generated classes
if (linst instanceof SpoofCPInstruction) {
Class<?> cla = ((SpoofCPInstruction) linst).getOperatorClass();
clsMap.put(cla.getName(), CodegenUtils.getClassData(cla.getName()));
}
if (count > 0)
sb.append(ELEMENT_DELIM);
sb.append(checkAndReplaceLiterals(linst.toString()));
count++;
}
return sb.toString();
}
Aggregations