use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.
the class ExternalFunctionInvocationInstruction method getInputObjects.
@SuppressWarnings("incomplete-switch")
private ArrayList<FunctionParameter> getInputObjects(CPOperand[] inputs, LocalVariableMap vars) {
ArrayList<FunctionParameter> ret = new ArrayList<>();
for (CPOperand input : inputs) {
switch(input.getDataType()) {
case MATRIX:
MatrixObject mobj = (MatrixObject) vars.get(input.getName());
ret.add(new Matrix(mobj, getMatrixValueType(input.getValueType())));
break;
case SCALAR:
ScalarObject so = (ScalarObject) vars.get(input.getName());
ret.add(new Scalar(getScalarValueType(input.getValueType()), so.getStringValue()));
break;
case OBJECT:
ret.add(new BinaryObject(vars.get(input.getName())));
break;
}
}
return ret;
}
use of org.apache.sysml.runtime.instructions.cp.ScalarObject in project incubator-systemml by apache.
the class JMLCInputOutputTest method testScalarOutputScalarObject.
@Test
public void testScalarOutputScalarObject() throws DMLException {
Connection conn = new Connection();
String str = "outDouble = 1.23;\nwrite(outDouble, './tmp/outDouble');";
PreparedScript script = conn.prepareScript(str, new String[] {}, new String[] { "outDouble" }, false);
ScalarObject so = script.executeScript().getScalarObject("outDouble");
double result = so.getDoubleValue();
Assert.assertEquals(1.23, result, 0);
conn.close();
}
use of org.apache.sysml.runtime.instructions.cp.ScalarObject 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.cp.ScalarObject in project incubator-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 incubator-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