use of org.apache.sysml.hops.Hop.OpOp2 in project incubator-systemml by apache.
the class DMLTranslator method processRelationalExpression.
private Hop processRelationalExpression(RelationalExpression source, DataIdentifier target, HashMap<String, Hop> hops) throws ParseException {
Hop left = processExpression(source.getLeft(), null, hops);
Hop right = processExpression(source.getRight(), null, hops);
Hop currBop = null;
if (target == null) {
target = createTarget(source);
if (left.getDataType() == DataType.MATRIX || right.getDataType() == DataType.MATRIX) {
// Added to support matrix relational comparison
// (we support only matrices of value type double)
target.setDataType(DataType.MATRIX);
target.setValueType(ValueType.DOUBLE);
} else {
// Added to support scalar relational comparison
target.setDataType(DataType.SCALAR);
target.setValueType(ValueType.BOOLEAN);
}
}
OpOp2 op = null;
if (source.getOpCode() == Expression.RelationalOp.LESS) {
op = OpOp2.LESS;
} else if (source.getOpCode() == Expression.RelationalOp.LESSEQUAL) {
op = OpOp2.LESSEQUAL;
} else if (source.getOpCode() == Expression.RelationalOp.GREATER) {
op = OpOp2.GREATER;
} else if (source.getOpCode() == Expression.RelationalOp.GREATEREQUAL) {
op = OpOp2.GREATEREQUAL;
} else if (source.getOpCode() == Expression.RelationalOp.EQUAL) {
op = OpOp2.EQUAL;
} else if (source.getOpCode() == Expression.RelationalOp.NOTEQUAL) {
op = OpOp2.NOTEQUAL;
}
currBop = new BinaryOp(target.getName(), target.getDataType(), target.getValueType(), op, left, right);
currBop.setAllPositions(source.getBeginLine(), source.getBeginColumn(), source.getEndLine(), source.getEndColumn());
return currBop;
}
use of org.apache.sysml.hops.Hop.OpOp2 in project incubator-systemml by apache.
the class DMLTranslator method processBooleanExpression.
private Hop processBooleanExpression(BooleanExpression source, DataIdentifier target, HashMap<String, Hop> hops) throws ParseException {
// Boolean Not has a single parameter
boolean constLeft = (source.getLeft().getOutput() instanceof ConstIdentifier);
boolean constRight = false;
if (source.getRight() != null) {
constRight = (source.getRight().getOutput() instanceof ConstIdentifier);
}
if (constLeft || constRight) {
LOG.error(source.printErrorLocation() + "Boolean expression with constant unsupported");
throw new RuntimeException(source.printErrorLocation() + "Boolean expression with constant unsupported");
}
Hop left = processExpression(source.getLeft(), null, hops);
Hop right = null;
if (source.getRight() != null) {
right = processExpression(source.getRight(), null, hops);
}
//(type should not be determined by target (e.g., string for print)
if (target == null) {
target = createTarget(source);
}
target.setValueType(ValueType.BOOLEAN);
if (source.getRight() == null) {
Hop currUop = new UnaryOp(target.getName(), target.getDataType(), target.getValueType(), Hop.OpOp1.NOT, left);
currUop.setAllPositions(source.getBeginLine(), source.getBeginColumn(), source.getEndLine(), source.getEndColumn());
return currUop;
} else {
Hop currBop = null;
OpOp2 op = null;
if (source.getOpCode() == Expression.BooleanOp.LOGICALAND) {
op = OpOp2.AND;
} else if (source.getOpCode() == Expression.BooleanOp.LOGICALOR) {
op = OpOp2.OR;
} else {
LOG.error(source.printErrorLocation() + "Unknown boolean operation " + source.getOpCode());
throw new RuntimeException(source.printErrorLocation() + "Unknown boolean operation " + source.getOpCode());
}
currBop = new BinaryOp(target.getName(), target.getDataType(), target.getValueType(), op, left, right);
currBop.setAllPositions(source.getBeginLine(), source.getBeginColumn(), source.getEndLine(), source.getEndColumn());
// setIdentifierParams(currBop,source.getOutput());
return currBop;
}
}
use of org.apache.sysml.hops.Hop.OpOp2 in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationStatic method simplifyTransposedAppend.
private Hop simplifyTransposedAppend(Hop parent, Hop hi, int pos) {
//e.g., t(cbind(t(A),t(B))) --> rbind(A,B), t(rbind(t(A),t(B))) --> cbind(A,B)
if (//t() rooted
HopRewriteUtils.isTransposeOperation(hi) && hi.getInput().get(0) instanceof BinaryOp && (//append (cbind/rbind)
((BinaryOp) hi.getInput().get(0)).getOp() == OpOp2.CBIND || ((BinaryOp) hi.getInput().get(0)).getOp() == OpOp2.RBIND) && //single consumer of append
hi.getInput().get(0).getParent().size() == 1) {
BinaryOp bop = (BinaryOp) hi.getInput().get(0);
//both inputs transpose ops, where transpose is single consumer
if (HopRewriteUtils.isTransposeOperation(bop.getInput().get(0), 1) && HopRewriteUtils.isTransposeOperation(bop.getInput().get(1), 1)) {
Hop left = bop.getInput().get(0).getInput().get(0);
Hop right = bop.getInput().get(1).getInput().get(0);
//create new subdag (no in-place dag update to prevent anomalies with
//multiple consumers during rewrite process)
OpOp2 binop = (bop.getOp() == OpOp2.CBIND) ? OpOp2.RBIND : OpOp2.CBIND;
BinaryOp bopnew = HopRewriteUtils.createBinary(left, right, binop);
HopRewriteUtils.replaceChildReference(parent, hi, bopnew, pos);
hi = bopnew;
LOG.debug("Applied simplifyTransposedAppend (line " + hi.getBeginLine() + ").");
}
}
return hi;
}
use of org.apache.sysml.hops.Hop.OpOp2 in project incubator-systemml by apache.
the class MatrixBlock method estimateSparsityOnBinary.
private static SparsityEstimate estimateSparsityOnBinary(MatrixBlock m1, MatrixBlock m2, BinaryOperator op) {
SparsityEstimate est = new SparsityEstimate();
//see also, special sparse-safe case for DIV in LibMatrixBincell
if (!op.sparseSafe && !(op.fn instanceof Divide)) {
est.sparse = false;
return est;
}
BinaryAccessType atype = LibMatrixBincell.getBinaryAccessType(m1, m2);
boolean outer = (atype == BinaryAccessType.OUTER_VECTOR_VECTOR);
long m = m1.getNumRows();
long n = outer ? m2.getNumColumns() : m1.getNumColumns();
long nz1 = m1.getNonZeros();
long nz2 = m2.getNonZeros();
//account for matrix vector and vector/vector
long estnnz = 0;
if (atype == BinaryAccessType.OUTER_VECTOR_VECTOR) {
//for outer vector operations the sparsity estimate is exactly known
estnnz = nz1 * nz2;
} else //DEFAULT CASE
{
if (atype == BinaryAccessType.MATRIX_COL_VECTOR)
nz2 = nz2 * n;
else if (atype == BinaryAccessType.MATRIX_ROW_VECTOR)
nz2 = nz2 * m;
//compute output sparsity consistent w/ the hop compiler
OpOp2 bop = op.getBinaryOperatorOpOp2();
double sp1 = OptimizerUtils.getSparsity(m, n, nz1);
double sp2 = OptimizerUtils.getSparsity(m, n, nz2);
double spout = OptimizerUtils.getBinaryOpSparsity(sp1, sp2, bop, true);
estnnz = UtilFunctions.toLong(spout * m * n);
}
est.sparse = evalSparseFormatInMemory(m, n, estnnz);
est.estimatedNonZeros = estnnz;
return est;
}
use of org.apache.sysml.hops.Hop.OpOp2 in project incubator-systemml by apache.
the class RewriteAlgebraicSimplificationStatic method simplifyBushyBinaryOperation.
/**
* (X*(Y*(Z%*%v))) -> (X*Y)*(Z%*%v)
* (X+(Y+(Z%*%v))) -> (X+Y)+(Z%*%v)
*
* Note: Restriction ba() at leaf and root instead of data at leaf to not reorganize too
* eagerly, which would loose additional rewrite potential. This rewrite has two goals
* (1) enable XtwXv, and increase piggybacking potential by creating bushy trees.
*
* @param parent parent high-level operator
* @param hi high-level operator
* @param pos position
* @return high-level operator
*/
private Hop simplifyBushyBinaryOperation(Hop parent, Hop hi, int pos) {
if (hi instanceof BinaryOp && parent instanceof AggBinaryOp) {
BinaryOp bop = (BinaryOp) hi;
Hop left = bop.getInput().get(0);
Hop right = bop.getInput().get(1);
OpOp2 op = bop.getOp();
if (left.getDataType() == DataType.MATRIX && right.getDataType() == DataType.MATRIX && HopRewriteUtils.isValidOp(op, LOOKUP_VALID_ASSOCIATIVE_BINARY)) {
boolean applied = false;
if (right instanceof BinaryOp) {
BinaryOp bop2 = (BinaryOp) right;
Hop left2 = bop2.getInput().get(0);
Hop right2 = bop2.getInput().get(1);
OpOp2 op2 = bop2.getOp();
if (op == op2 && right2.getDataType() == DataType.MATRIX && (right2 instanceof AggBinaryOp)) {
//(X*(Y*op()) -> (X*Y)*op()
BinaryOp bop3 = HopRewriteUtils.createBinary(left, left2, op);
BinaryOp bop4 = HopRewriteUtils.createBinary(bop3, right2, op);
HopRewriteUtils.replaceChildReference(parent, bop, bop4, pos);
HopRewriteUtils.cleanupUnreferenced(bop, bop2);
hi = bop4;
applied = true;
LOG.debug("Applied simplifyBushyBinaryOperation1");
}
}
if (!applied && left instanceof BinaryOp) {
BinaryOp bop2 = (BinaryOp) left;
Hop left2 = bop2.getInput().get(0);
Hop right2 = bop2.getInput().get(1);
OpOp2 op2 = bop2.getOp();
if (op == op2 && left2.getDataType() == DataType.MATRIX && (left2 instanceof AggBinaryOp) && //X not vector, or Y vector
(right2.getDim2() > 1 || right.getDim2() == 1) && //X not vector, or Y vector
(right2.getDim1() > 1 || right.getDim1() == 1)) {
//((op()*X)*Y) -> op()*(X*Y)
BinaryOp bop3 = HopRewriteUtils.createBinary(right2, right, op);
BinaryOp bop4 = HopRewriteUtils.createBinary(left2, bop3, op);
HopRewriteUtils.replaceChildReference(parent, bop, bop4, pos);
HopRewriteUtils.cleanupUnreferenced(bop, bop2);
hi = bop4;
LOG.debug("Applied simplifyBushyBinaryOperation2");
}
}
}
}
return hi;
}
Aggregations