use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class RewriteForLoopVectorization method vectorizeElementwiseUnary.
private static StatementBlock vectorizeElementwiseUnary(StatementBlock sb, StatementBlock csb, Hop from, Hop to, Hop increment, String itervar) {
StatementBlock ret = sb;
// check supported increment values
if (!(increment instanceof LiteralOp && ((LiteralOp) increment).getDoubleValue() == 1.0)) {
return ret;
}
// check for applicability
boolean apply = false;
// row or col
boolean rowIx = false;
if (csb.getHops() != null && csb.getHops().size() == 1) {
Hop root = csb.getHops().get(0);
if (root.getDataType() == DataType.MATRIX && root.getInput().get(0) instanceof LeftIndexingOp) {
LeftIndexingOp lix = (LeftIndexingOp) root.getInput().get(0);
Hop lixlhs = lix.getInput().get(0);
Hop lixrhs = lix.getInput().get(1);
if (lixlhs instanceof DataOp && lixrhs instanceof UnaryOp && lixrhs.getInput().get(0) instanceof IndexingOp && lixrhs.getInput().get(0).getInput().get(0) instanceof DataOp) {
boolean[] tmp = checkLeftAndRightIndexing(lix, (IndexingOp) lixrhs.getInput().get(0), itervar);
apply = tmp[0];
rowIx = tmp[1];
}
}
}
// apply rewrite if possible
if (apply) {
Hop root = csb.getHops().get(0);
LeftIndexingOp lix = (LeftIndexingOp) root.getInput().get(0);
UnaryOp uop = (UnaryOp) lix.getInput().get(1);
IndexingOp rix = (IndexingOp) uop.getInput().get(0);
int index1 = rowIx ? 2 : 4;
int index2 = rowIx ? 3 : 5;
// modify left indexing bounds
HopRewriteUtils.replaceChildReference(lix, lix.getInput().get(index1), from, index1);
HopRewriteUtils.replaceChildReference(lix, lix.getInput().get(index2), to, index2);
// modify right indexing
HopRewriteUtils.replaceChildReference(rix, rix.getInput().get(index1 - 1), from, index1 - 1);
HopRewriteUtils.replaceChildReference(rix, rix.getInput().get(index2 - 1), to, index2 - 1);
updateLeftAndRightIndexingSizes(rowIx, lix, rix);
uop.refreshSizeInformation();
// after uop update
lix.refreshSizeInformation();
ret = csb;
LOG.debug("Applied vectorizeElementwiseUnaryForLoop.");
}
return ret;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class RewriteForLoopVectorization method vectorizeScalarAggregate.
private static StatementBlock vectorizeScalarAggregate(StatementBlock sb, StatementBlock csb, Hop from, Hop to, Hop increment, String itervar) {
StatementBlock ret = sb;
// check missing and supported increment values
if (!(increment != null && increment instanceof LiteralOp && ((LiteralOp) increment).getDoubleValue() == 1.0)) {
return ret;
}
// check for applicability
boolean leftScalar = false;
boolean rightScalar = false;
// row or col
boolean rowIx = false;
if (csb.getHops() != null && csb.getHops().size() == 1) {
Hop root = csb.getHops().get(0);
if (root.getDataType() == DataType.SCALAR && root.getInput().get(0) instanceof BinaryOp) {
BinaryOp bop = (BinaryOp) root.getInput().get(0);
Hop left = bop.getInput().get(0);
Hop right = bop.getInput().get(1);
// check for left scalar plus
if (HopRewriteUtils.isValidOp(bop.getOp(), MAP_SCALAR_AGGREGATE_SOURCE_OPS) && left instanceof DataOp && left.getDataType() == DataType.SCALAR && root.getName().equals(left.getName()) && right instanceof UnaryOp && ((UnaryOp) right).getOp() == OpOp1.CAST_AS_SCALAR && right.getInput().get(0) instanceof IndexingOp) {
IndexingOp ix = (IndexingOp) right.getInput().get(0);
if (ix.isRowLowerEqualsUpper() && ix.getInput().get(1) instanceof DataOp && ix.getInput().get(1).getName().equals(itervar)) {
leftScalar = true;
rowIx = true;
} else if (ix.isColLowerEqualsUpper() && ix.getInput().get(3) instanceof DataOp && ix.getInput().get(3).getName().equals(itervar)) {
leftScalar = true;
rowIx = false;
}
} else // check for right scalar plus
if (HopRewriteUtils.isValidOp(bop.getOp(), MAP_SCALAR_AGGREGATE_SOURCE_OPS) && right instanceof DataOp && right.getDataType() == DataType.SCALAR && root.getName().equals(right.getName()) && left instanceof UnaryOp && ((UnaryOp) left).getOp() == OpOp1.CAST_AS_SCALAR && left.getInput().get(0) instanceof IndexingOp) {
IndexingOp ix = (IndexingOp) left.getInput().get(0);
if (ix.isRowLowerEqualsUpper() && ix.getInput().get(1) instanceof DataOp && ix.getInput().get(1).getName().equals(itervar)) {
rightScalar = true;
rowIx = true;
} else if (ix.isColLowerEqualsUpper() && ix.getInput().get(3) instanceof DataOp && ix.getInput().get(3).getName().equals(itervar)) {
rightScalar = true;
rowIx = false;
}
}
}
}
// apply rewrite if possible
if (leftScalar || rightScalar) {
Hop root = csb.getHops().get(0);
BinaryOp bop = (BinaryOp) root.getInput().get(0);
Hop cast = bop.getInput().get(leftScalar ? 1 : 0);
Hop ix = cast.getInput().get(0);
int aggOpPos = HopRewriteUtils.getValidOpPos(bop.getOp(), MAP_SCALAR_AGGREGATE_SOURCE_OPS);
AggOp aggOp = MAP_SCALAR_AGGREGATE_TARGET_OPS[aggOpPos];
// replace cast with sum
AggUnaryOp newSum = HopRewriteUtils.createAggUnaryOp(ix, aggOp, Direction.RowCol);
HopRewriteUtils.removeChildReference(cast, ix);
HopRewriteUtils.removeChildReference(bop, cast);
HopRewriteUtils.addChildReference(bop, newSum, leftScalar ? 1 : 0);
// modify indexing expression according to loop predicate from-to
// NOTE: any redundant index operations are removed via dynamic algebraic simplification rewrites
int index1 = rowIx ? 1 : 3;
int index2 = rowIx ? 2 : 4;
HopRewriteUtils.replaceChildReference(ix, ix.getInput().get(index1), from, index1);
HopRewriteUtils.replaceChildReference(ix, ix.getInput().get(index2), to, index2);
// update indexing size information
if (rowIx)
((IndexingOp) ix).setRowLowerEqualsUpper(false);
else
((IndexingOp) ix).setColLowerEqualsUpper(false);
ix.refreshSizeInformation();
ret = csb;
LOG.debug("Applied vectorizeScalarSumForLoop.");
}
return ret;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class RewriteRemoveUnnecessaryCasts method rule_RemoveUnnecessaryCasts.
@SuppressWarnings("unchecked")
private void rule_RemoveUnnecessaryCasts(Hop hop) {
// check mark processed
if (hop.isVisited())
return;
// recursively process childs
ArrayList<Hop> inputs = hop.getInput();
for (int i = 0; i < inputs.size(); i++) rule_RemoveUnnecessaryCasts(inputs.get(i));
// remove unnecessary value type cast
if (hop instanceof UnaryOp && HopRewriteUtils.isValueTypeCast(((UnaryOp) hop).getOp())) {
Hop in = hop.getInput().get(0);
// type cast input
ValueType vtIn = in.getValueType();
// type cast output
ValueType vtOut = hop.getValueType();
// if input/output types match, no need to cast
if (vtIn == vtOut && vtIn != ValueType.UNKNOWN) {
ArrayList<Hop> parents = hop.getParent();
for (// for all parents
int i = 0; // for all parents
i < parents.size(); // for all parents
i++) {
Hop p = parents.get(i);
ArrayList<Hop> pin = p.getInput();
for (// for all parent childs
int j = 0; // for all parent childs
j < pin.size(); // for all parent childs
j++) {
Hop pinj = pin.get(j);
if (// found parent ref
pinj == hop) {
// rehang cast input as child of cast consumer
// remove cast ref
pin.remove(j);
// add ref to cast input
pin.add(j, in);
// remove cast from cast input parents
in.getParent().remove(hop);
// add parent to cast input parents
in.getParent().add(p);
}
}
}
parents.clear();
}
}
// remove unnecessary data type casts
if (hop instanceof UnaryOp && hop.getInput().get(0) instanceof UnaryOp) {
UnaryOp uop1 = (UnaryOp) hop;
UnaryOp uop2 = (UnaryOp) hop.getInput().get(0);
if ((uop1.getOp() == OpOp1.CAST_AS_MATRIX && uop2.getOp() == OpOp1.CAST_AS_SCALAR) || (uop1.getOp() == OpOp1.CAST_AS_SCALAR && uop2.getOp() == OpOp1.CAST_AS_MATRIX)) {
Hop input = uop2.getInput().get(0);
// rewire parents
ArrayList<Hop> parents = (ArrayList<Hop>) hop.getParent().clone();
for (Hop p : parents) HopRewriteUtils.replaceChildReference(p, hop, input);
}
}
// mark processed
hop.setVisited();
}
use of org.apache.sysml.hops.UnaryOp 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;
}
use of org.apache.sysml.hops.UnaryOp in project incubator-systemml by apache.
the class LiteralReplacement method replaceLiteralValueTypeCastRightIndexing.
private static LiteralOp replaceLiteralValueTypeCastRightIndexing(Hop c, LocalVariableMap vars) {
LiteralOp ret = null;
// as.scalar/right indexing w/ literals/vars and matrix less than 10^6 cells
if (c instanceof UnaryOp && ((UnaryOp) c).getOp() == OpOp1.CAST_AS_SCALAR && c.getInput().get(0) instanceof IndexingOp && c.getInput().get(0).getDataType() == DataType.MATRIX) {
IndexingOp rix = (IndexingOp) c.getInput().get(0);
Hop data = rix.getInput().get(0);
Hop rl = rix.getInput().get(1);
Hop ru = rix.getInput().get(2);
Hop cl = rix.getInput().get(3);
Hop cu = rix.getInput().get(4);
if (rix.dimsKnown() && rix.getDim1() == 1 && rix.getDim2() == 1 && data instanceof DataOp && vars.keySet().contains(data.getName()) && isIntValueDataLiteral(rl, vars) && isIntValueDataLiteral(ru, vars) && isIntValueDataLiteral(cl, vars) && isIntValueDataLiteral(cu, vars)) {
long rlval = getIntValueDataLiteral(rl, vars);
long clval = getIntValueDataLiteral(cl, vars);
MatrixObject mo = (MatrixObject) vars.get(data.getName());
// dimensions might not have been updated during recompile
if (mo.getNumRows() * mo.getNumColumns() < REPLACE_LITERALS_MAX_MATRIX_SIZE) {
MatrixBlock mBlock = mo.acquireRead();
double value = mBlock.getValue((int) rlval - 1, (int) clval - 1);
mo.release();
// literal substitution (always double)
ret = new LiteralOp(value);
}
}
}
return ret;
}
Aggregations