use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class LiteralReplacement method replaceLiteralValueTypeCastScalarRead.
private static LiteralOp replaceLiteralValueTypeCastScalarRead(Hop c, LocalVariableMap vars) {
LiteralOp ret = null;
// as.double/as.integer/as.boolean over scalar read - literal replacement
if (c instanceof UnaryOp && (((UnaryOp) c).getOp() == OpOp1.CAST_AS_DOUBLE || ((UnaryOp) c).getOp() == OpOp1.CAST_AS_INT || ((UnaryOp) c).getOp() == OpOp1.CAST_AS_BOOLEAN) && c.getInput().get(0) instanceof DataOp && c.getDataType() == DataType.SCALAR) {
Data dat = vars.get(c.getInput().get(0).getName());
if (// required for selective constant propagation
dat != null) {
ScalarObject sdat = (ScalarObject) dat;
UnaryOp cast = (UnaryOp) c;
switch(cast.getOp()) {
case CAST_AS_INT:
ret = new LiteralOp(sdat.getLongValue());
break;
case CAST_AS_DOUBLE:
ret = new LiteralOp(sdat.getDoubleValue());
break;
case CAST_AS_BOOLEAN:
ret = new LiteralOp(sdat.getBooleanValue());
break;
default:
}
}
}
return ret;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class LiteralReplacement method replaceLiteralDataTypeCastMatrixRead.
private static LiteralOp replaceLiteralDataTypeCastMatrixRead(Hop c, LocalVariableMap vars) {
LiteralOp ret = null;
// as.scalar/matrix read - literal replacement
if (c instanceof UnaryOp && ((UnaryOp) c).getOp() == OpOp1.CAST_AS_SCALAR && c.getInput().get(0) instanceof DataOp && c.getInput().get(0).getDataType() == DataType.MATRIX) {
Data dat = vars.get(c.getInput().get(0).getName());
if (// required for selective constant propagation
dat != null) {
// cast as scalar (see VariableCPInstruction)
MatrixObject mo = (MatrixObject) dat;
MatrixBlock mBlock = mo.acquireRead();
if (mBlock.getNumRows() != 1 || mBlock.getNumColumns() != 1)
throw new DMLRuntimeException("Dimension mismatch - unable to cast matrix of dimension (" + mBlock.getNumRows() + " x " + mBlock.getNumColumns() + ") to scalar.");
double value = mBlock.getValue(0, 0);
mo.release();
// literal substitution (always double)
ret = new LiteralOp(value);
}
}
return ret;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class HopRewriteUtils method createDataGenOp.
public static Hop createDataGenOp(Hop rowInput, boolean tRowInput, Hop colInput, boolean tColInput, double value) {
long nrow = tRowInput ? rowInput.getDim2() : rowInput.getDim1();
long ncol = tColInput ? colInput.getDim1() : rowInput.getDim2();
Hop rows = (nrow >= 0) ? new LiteralOp(nrow) : new UnaryOp("tmprows", DataType.SCALAR, ValueType.INT, tRowInput ? OpOp1.NCOL : OpOp1.NROW, rowInput);
Hop cols = (ncol >= 0) ? new LiteralOp(ncol) : new UnaryOp("tmpcols", DataType.SCALAR, ValueType.INT, tColInput ? OpOp1.NROW : OpOp1.NCOL, colInput);
Hop val = new LiteralOp(value);
HashMap<String, Hop> params = new HashMap<>();
params.put(DataExpression.RAND_ROWS, rows);
params.put(DataExpression.RAND_COLS, cols);
params.put(DataExpression.RAND_MIN, val);
params.put(DataExpression.RAND_MAX, val);
params.put(DataExpression.RAND_PDF, new LiteralOp(DataExpression.RAND_PDF_UNIFORM));
params.put(DataExpression.RAND_LAMBDA, new LiteralOp(-1.0));
params.put(DataExpression.RAND_SPARSITY, new LiteralOp(1.0));
params.put(DataExpression.RAND_SEED, new LiteralOp(DataGenOp.UNSPECIFIED_SEED));
// note internal refresh size information
Hop datagen = new DataGenOp(DataGenMethod.RAND, new DataIdentifier("tmp"), params);
datagen.setOutputBlocksizes(rowInput.getRowsInBlock(), colInput.getColsInBlock());
copyLineNumbers(rowInput, datagen);
if (value == 0)
datagen.setNnz(0);
return datagen;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class HopRewriteUtils method rContainsRead.
public static boolean rContainsRead(Hop root, String var, boolean includeMetaOp) {
if (root.isVisited())
return false;
boolean ret = false;
// handle leaf node for variable
if (root instanceof DataOp && ((DataOp) root).isRead() && root.getName().equals(var)) {
boolean onlyMetaOp = true;
if (!includeMetaOp) {
for (Hop p : root.getParent()) {
onlyMetaOp &= (p instanceof UnaryOp && (((UnaryOp) p).getOp() == OpOp1.NROW || ((UnaryOp) p).getOp() == OpOp1.NCOL));
}
ret = !onlyMetaOp;
} else
ret = true;
}
// recursively process childs
for (Hop c : root.getInput()) ret |= rContainsRead(c, var, includeMetaOp);
root.setVisited();
return ret;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class HopRewriteUtils method createUnary.
public static UnaryOp createUnary(Hop input, OpOp1 type) {
DataType dt = (type == OpOp1.CAST_AS_SCALAR) ? DataType.SCALAR : (type == OpOp1.CAST_AS_MATRIX) ? DataType.MATRIX : input.getDataType();
ValueType vt = (type == OpOp1.CAST_AS_MATRIX) ? ValueType.DOUBLE : input.getValueType();
UnaryOp unary = new UnaryOp(input.getName(), dt, vt, type, input);
unary.setOutputBlocksizes(input.getRowsInBlock(), input.getColsInBlock());
if (type == OpOp1.CAST_AS_SCALAR || type == OpOp1.CAST_AS_MATRIX) {
int dim = (type == OpOp1.CAST_AS_SCALAR) ? 0 : 1;
int blksz = (type == OpOp1.CAST_AS_SCALAR) ? 0 : ConfigurationManager.getBlocksize();
setOutputParameters(unary, dim, dim, blksz, blksz, -1);
}
copyLineNumbers(input, unary);
unary.refreshSizeInformation();
return unary;
}
Aggregations