[SYSTEMML-694] Improved binary-unary rewrites (added max(0,X)->selp(X))

Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/461184aa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/461184aa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/461184aa

Branch: refs/heads/master
Commit: 461184aa5576bef29b7561d8dfc1d6de37bcff83
Parents: 382df65
Author: Matthias Boehm <[email protected]>
Authored: Sat Jul 30 16:23:01 2016 -0700
Committer: Matthias Boehm <[email protected]>
Committed: Sat Jul 30 18:56:46 2016 -0700

----------------------------------------------------------------------
 .../RewriteAlgebraicSimplificationStatic.java   | 21 +++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/461184aa/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
 
b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
index ae9c073..f23686c 100644
--- 
a/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
+++ 
b/src/main/java/org/apache/sysml/hops/rewrite/RewriteAlgebraicSimplificationStatic.java
@@ -1184,6 +1184,7 @@ public class RewriteAlgebraicSimplificationStatic extends 
HopRewriteRule
                                //by definition, either left or right or none 
applies. 
                                //note: if there are multiple consumers on the 
intermediate tmp=(X>0), it's still beneficial
                                //to replace the X*tmp with selp(X) due to 
lower memory requirements and simply sparsity propagation 
+                               boolean applied = false;
                                
                                if( left instanceof BinaryOp ) //(X>0)*X
                                {
@@ -1206,11 +1207,12 @@ public class RewriteAlgebraicSimplificationStatic 
extends HopRewriteRule
                                                        
HopRewriteUtils.removeAllChildReferences(left);
                                                
                                                hi = unary;
+                                               applied = true;
                                                
                                                LOG.debug("Applied 
fuseBinarySubDAGToUnaryOperation-selp1");
                                        }
                                }                               
-                               if( right instanceof BinaryOp ) //X*(X>0)
+                               if( !applied && right instanceof BinaryOp ) 
//X*(X>0)
                                {
                                        BinaryOp bright = (BinaryOp)right;
                                        Hop right1 = bright.getInput().get(0);
@@ -1231,6 +1233,7 @@ public class RewriteAlgebraicSimplificationStatic extends 
HopRewriteRule
                                                        
HopRewriteUtils.removeAllChildReferences(right);
                                                
                                                hi = unary;
+                                               applied= true;
                                                
                                                LOG.debug("Applied 
fuseBinarySubDAGToUnaryOperation-selp2");
                                        }
@@ -1252,6 +1255,22 @@ public class RewriteAlgebraicSimplificationStatic 
extends HopRewriteRule
                                
                                LOG.debug("Applied 
fuseBinarySubDAGToUnaryOperation-selp3");
                        }
+                       
+                       //select positive (selp) operator; pattern: max(0,X) -> 
selp+
+                       if( bop.getOp() == OpOp2.MAX && 
right.getDataType()==DataType.MATRIX 
+                                       && left instanceof LiteralOp && 
HopRewriteUtils.getDoubleValue((LiteralOp)left)==0 )
+                       {
+                               UnaryOp unary = 
HopRewriteUtils.createUnary(right, OpOp1.SELP);
+                               
HopRewriteUtils.removeChildReferenceByPos(parent, bop, pos);
+                               HopRewriteUtils.addChildReference(parent, 
unary, pos);
+                               
+                               //cleanup if only consumer of intermediate
+                               if( bop.getParent().isEmpty() )
+                                       
HopRewriteUtils.removeAllChildReferences(bop);                                  
+                               hi = unary;
+                               
+                               LOG.debug("Applied 
fuseBinarySubDAGToUnaryOperation-selp4");
+                       }
                }
                
                return hi;

Reply via email to