use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.
the class ForProgramBlock method executePredicateInstructions.
protected IntObject executePredicateInstructions(int pos, ArrayList<Instruction> instructions, ExecutionContext ec) {
ScalarObject tmp = null;
IntObject ret = null;
try {
if (_sb != null) {
if (// set program block specific remote memory
DMLScript.isActiveAM())
DMLAppMasterUtils.setupProgramBlockRemoteMaxMemory(this);
ForStatementBlock fsb = (ForStatementBlock) _sb;
Hop predHops = null;
boolean recompile = false;
if (pos == 1) {
predHops = fsb.getFromHops();
recompile = fsb.requiresFromRecompilation();
} else if (pos == 2) {
predHops = fsb.getToHops();
recompile = fsb.requiresToRecompilation();
} else if (pos == 3) {
predHops = fsb.getIncrementHops();
recompile = fsb.requiresIncrementRecompilation();
}
tmp = (IntObject) executePredicate(instructions, predHops, recompile, ValueType.INT, ec);
} else
tmp = (IntObject) executePredicate(instructions, null, false, ValueType.INT, ec);
} catch (Exception ex) {
String predNameStr = null;
if (pos == 1)
predNameStr = "from";
else if (pos == 2)
predNameStr = "to";
else if (pos == 3)
predNameStr = "increment";
throw new DMLRuntimeException(this.printBlockErrorLocation() + "Error evaluating '" + predNameStr + "' predicate", ex);
}
// final check of resulting int object (guaranteed to be non-null, see executePredicate)
if (tmp instanceof IntObject)
ret = (IntObject) tmp;
else
// downcast to int if necessary
ret = new IntObject(tmp.getLongValue());
return ret;
}
use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.
the class ProgramBlock method executePredicateInstructions.
protected ScalarObject executePredicateInstructions(ArrayList<Instruction> inst, ValueType retType, ExecutionContext ec) {
// execute all instructions (indexed access required due to debug mode)
int pos = 0;
for (Instruction currInst : inst) {
ec.updateDebugState(pos++);
executeSingleInstruction(currInst, ec);
}
// get scalar return
ScalarObject ret = (ScalarObject) ec.getScalarInput(PRED_VAR, retType, false);
// check and correct scalar ret type (incl save double to int)
if (ret.getValueType() != retType)
switch(retType) {
case BOOLEAN:
ret = new BooleanObject(ret.getBooleanValue());
break;
case INT:
ret = new IntObject(ret.getLongValue());
break;
case DOUBLE:
ret = new DoubleObject(ret.getDoubleValue());
break;
case STRING:
ret = new StringObject(ret.getStringValue());
break;
default:
}
// remove predicate variable
ec.removeVariable(PRED_VAR);
return ret;
}
use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project 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.cp.ScalarObject in project systemml by apache.
the class InterProceduralAnalysis method populateLocalVariableMapForFunctionCall.
private static void populateLocalVariableMapForFunctionCall(FunctionStatement fstmt, FunctionOp fop, LocalVariableMap callvars, LocalVariableMap vars, FunctionCallSizeInfo fcallSizes) {
ArrayList<DataIdentifier> inputVars = fstmt.getInputParams();
ArrayList<Hop> inputOps = fop.getInput();
String fkey = fop.getFunctionKey();
for (int i = 0; i < inputVars.size(); i++) {
// create mapping between input hops and vars
DataIdentifier dat = inputVars.get(i);
Hop input = inputOps.get(i);
if (input.getDataType() == DataType.MATRIX) {
// propagate matrix characteristics
MatrixObject mo = new MatrixObject(ValueType.DOUBLE, null);
MatrixCharacteristics mc = new MatrixCharacteristics(input.getDim1(), input.getDim2(), ConfigurationManager.getBlocksize(), ConfigurationManager.getBlocksize(), fcallSizes.isSafeNnz(fkey, i) ? input.getNnz() : -1);
MetaDataFormat meta = new MetaDataFormat(mc, null, null);
mo.setMetaData(meta);
vars.put(dat.getName(), mo);
} else if (input.getDataType() == DataType.SCALAR) {
// (for multiple calls, literal equivalence already checked)
if (input instanceof LiteralOp) {
vars.put(dat.getName(), ScalarObjectFactory.createScalarObject(input.getValueType(), (LiteralOp) input));
} else // and input scalar is existing variable in symbol table
if (PROPAGATE_SCALAR_VARS_INTO_FUN && fcallSizes.getFunctionCallCount(fkey) == 1 && input instanceof DataOp) {
Data scalar = callvars.get(input.getName());
if (scalar != null && scalar instanceof ScalarObject) {
vars.put(dat.getName(), scalar);
}
}
}
}
}
use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project systemml by apache.
the class LiteralReplacement method getIntValueDataLiteral.
private static long getIntValueDataLiteral(Hop hop, LocalVariableMap vars) {
long value = -1;
try {
if (hop instanceof LiteralOp) {
value = HopRewriteUtils.getIntValue((LiteralOp) hop);
} else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NROW) {
// get the dimension information from the matrix object because the hop
// dimensions might not have been updated during recompile
MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
value = mo.getNumRows();
} else if (hop instanceof UnaryOp && ((UnaryOp) hop).getOp() == OpOp1.NCOL) {
// get the dimension information from the matrix object because the hop
// dimensions might not have been updated during recompile
MatrixObject mo = (MatrixObject) vars.get(hop.getInput().get(0).getName());
value = mo.getNumColumns();
} else {
ScalarObject sdat = (ScalarObject) vars.get(hop.getName());
value = sdat.getLongValue();
}
} catch (HopsException ex) {
throw new DMLRuntimeException("Failed to get int value for literal replacement", ex);
}
return value;
}
Aggregations