[SYSTEMML-2139] Fix codegen with boolean literal inputs (e.g., ifelse)

This patch fixes the code generator to compile valid source code for
boolean literal inputs as used in ternary ifelse of binary lgoical
operations. Furthermore, this also includes the related tests.

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

Branch: refs/heads/master
Commit: 5983e96ee213030b854800016aecfc5018ad45a1
Parents: e0b66b3
Author: Matthias Boehm <[email protected]>
Authored: Thu Feb 8 19:06:05 2018 -0800
Committer: Matthias Boehm <[email protected]>
Committed: Thu Feb 8 22:04:10 2018 -0800

----------------------------------------------------------------------
 .../sysml/hops/codegen/cplan/CNodeData.java     |  4 ++-
 .../hops/codegen/template/TemplateUtils.java    |  4 ++-
 .../functions/codegen/CellwiseTmplTest.java     | 30 +++++++++++++++-----
 .../scripts/functions/codegen/cellwisetmpl19.R  | 30 ++++++++++++++++++++
 .../functions/codegen/cellwisetmpl19.dml        | 28 ++++++++++++++++++
 5 files changed, 87 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/systemml/blob/5983e96e/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeData.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeData.java 
b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeData.java
index 653b50b..11c5868 100644
--- a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeData.java
+++ b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeData.java
@@ -51,7 +51,7 @@ public class CNodeData extends CNode
                _cols = node.getNumCols();
                _dataType = node.getDataType();
        }
-               
+       
        @Override
        public String getVarname() {
                if( "NaN".equals(_name) )
@@ -60,6 +60,8 @@ public class CNodeData extends CNode
                        return "Double.POSITIVE_INFINITY";
                else if( "-Infinity".equals(_name) )
                        return "Double.NEGATIVE_INFINITY";
+               else if( "true".equals(_name) || "false".equals(_name) )
+                       return "true".equals(_name) ? "1d" : "0d";
                else
                        return _name;
        }

http://git-wip-us.apache.org/repos/asf/systemml/blob/5983e96e/src/main/java/org/apache/sysml/hops/codegen/template/TemplateUtils.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/sysml/hops/codegen/template/TemplateUtils.java 
b/src/main/java/org/apache/sysml/hops/codegen/template/TemplateUtils.java
index 8ac2d54..1f8151e 100644
--- a/src/main/java/org/apache/sysml/hops/codegen/template/TemplateUtils.java
+++ b/src/main/java/org/apache/sysml/hops/codegen/template/TemplateUtils.java
@@ -317,7 +317,9 @@ public class TemplateUtils
                                && !TemplateUtils.isUnary(output, 
                                        UnaryType.EXP, UnaryType.LOG, 
UnaryType.ROW_COUNTNNZS)) 
                        || (output instanceof CNodeBinary
-                               && !TemplateUtils.isBinary(output, 
BinType.VECT_OUTERMULT_ADD))) 
+                               && !TemplateUtils.isBinary(output, 
BinType.VECT_OUTERMULT_ADD))
+                       || output instanceof CNodeTernary 
+                               && ((CNodeTernary)output).getType() == 
TernaryType.IFELSE)
                        && hasOnlyDataNodeOrLookupInputs(output);
        }
        

http://git-wip-us.apache.org/repos/asf/systemml/blob/5983e96e/src/test/java/org/apache/sysml/test/integration/functions/codegen/CellwiseTmplTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/sysml/test/integration/functions/codegen/CellwiseTmplTest.java
 
b/src/test/java/org/apache/sysml/test/integration/functions/codegen/CellwiseTmplTest.java
index 2f44f61..b32f06d 100644
--- 
a/src/test/java/org/apache/sysml/test/integration/functions/codegen/CellwiseTmplTest.java
+++ 
b/src/test/java/org/apache/sysml/test/integration/functions/codegen/CellwiseTmplTest.java
@@ -34,7 +34,7 @@ import org.apache.sysml.test.integration.TestConfiguration;
 import org.apache.sysml.test.utils.TestUtils;
 
 public class CellwiseTmplTest extends AutomatedTestBase 
-{      
+{
        private static final String TEST_NAME = "cellwisetmpl";
        private static final String TEST_NAME1 = TEST_NAME+1;
        private static final String TEST_NAME2 = TEST_NAME+2;
@@ -54,7 +54,7 @@ public class CellwiseTmplTest extends AutomatedTestBase
        private static final String TEST_NAME16 = TEST_NAME+16; 
//colSums(2*log(X));
        private static final String TEST_NAME17 = TEST_NAME+17; //xor operation
        private static final String TEST_NAME18 = TEST_NAME+18; 
//sum(ifelse(X,Y,Z))
-       
+       private static final String TEST_NAME19 = TEST_NAME+19; 
//sum(ifelse(true,Y,Z))+sum(ifelse(false,Y,Z))
        
        private static final String TEST_DIR = "functions/codegen/";
        private static final String TEST_CLASS_DIR = TEST_DIR + 
CellwiseTmplTest.class.getSimpleName() + "/";
@@ -67,7 +67,7 @@ public class CellwiseTmplTest extends AutomatedTestBase
        @Override
        public void setUp() {
                TestUtils.clearAssertionInformation();
-               for( int i=1; i<=18; i++ ) {
+               for( int i=1; i<=19; i++ ) {
                        addTestConfiguration( TEST_NAME+i, new 
TestConfiguration(
                                        TEST_CLASS_DIR, TEST_NAME+i, new 
String[] {String.valueOf(i)}) );
                }
@@ -320,9 +320,24 @@ public class CellwiseTmplTest extends AutomatedTestBase
                testCodegenIntegration( TEST_NAME18, true, ExecType.SPARK );
        }
        
+       @Test
+       public void testCodegenCellwiseRewrite19() {
+               testCodegenIntegration( TEST_NAME19, true, ExecType.CP );
+       }
+
+       @Test
+       public void testCodegenCellwise19() {
+               testCodegenIntegration( TEST_NAME19, false, ExecType.CP );
+       }
+
+       @Test
+       public void testCodegenCellwiseRewrite19_sp() {
+               testCodegenIntegration( TEST_NAME19, true, ExecType.SPARK );
+       }
+       
        
        private void testCodegenIntegration( String testname, boolean rewrites, 
ExecType instType )
-       {                       
+       {
                boolean oldRewrites = 
OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION;
                String oldTestConf = TEST_CONF;
                RUNTIME_PLATFORM platformOld = rtplatform;
@@ -350,7 +365,7 @@ public class CellwiseTmplTest extends AutomatedTestBase
                        programArgs = new String[]{"-explain", "hops", 
"-stats", "-args", output("S") };
                        
                        fullRScriptName = HOME + testname + ".R";
-                       rCmd = getRCmd(inputDir(), expectedDir());              
        
+                       rCmd = getRCmd(inputDir(), expectedDir());
 
                        OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION = 
rewrites;
 
@@ -367,11 +382,12 @@ public class CellwiseTmplTest extends AutomatedTestBase
                        else {
                                //compare matrices 
                                HashMap<CellIndex, Double> dmlfile = 
readDMLMatrixFromHDFS("S");
-                               HashMap<CellIndex, Double> rfile  = 
readRMatrixFromFS("S");     
+                               HashMap<CellIndex, Double> rfile  = 
readRMatrixFromFS("S");
                                TestUtils.compareMatrices(dmlfile, rfile, eps, 
"Stat-DML", "Stat-R");
                        }
                        
-                       if( !(rewrites && testname.equals(TEST_NAME2)) ) 
//sigmoid
+                       if( !(rewrites && (testname.equals(TEST_NAME2)
+                               || testname.equals(TEST_NAME19))) ) //sigmoid
                                Assert.assertTrue(heavyHittersContainsSubString(
                                                "spoofCell", "sp_spoofCell", 
"spoofMA", "sp_spoofMA"));
                        if( testname.equals(TEST_NAME7) ) //ensure matrix mult 
is fused

http://git-wip-us.apache.org/repos/asf/systemml/blob/5983e96e/src/test/scripts/functions/codegen/cellwisetmpl19.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/cellwisetmpl19.R 
b/src/test/scripts/functions/codegen/cellwisetmpl19.R
new file mode 100644
index 0000000..fb09020
--- /dev/null
+++ b/src/test/scripts/functions/codegen/cellwisetmpl19.R
@@ -0,0 +1,30 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+args<-commandArgs(TRUE)
+options(digits=22)
+library("Matrix")
+
+Y = as.numeric(matrix(seq(0, 199999), 1000, 200, byrow=TRUE));
+Z = as.numeric(matrix(seq(1000, 200999), 1000, 200, byrow=TRUE));
+R = as.matrix(sum(Y) + sum(Z));
+
+writeMM(as(R,"CsparseMatrix"), paste(args[2], "S", sep=""));

http://git-wip-us.apache.org/repos/asf/systemml/blob/5983e96e/src/test/scripts/functions/codegen/cellwisetmpl19.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/cellwisetmpl19.dml 
b/src/test/scripts/functions/codegen/cellwisetmpl19.dml
new file mode 100644
index 0000000..c7733f8
--- /dev/null
+++ b/src/test/scripts/functions/codegen/cellwisetmpl19.dml
@@ -0,0 +1,28 @@
+#-------------------------------------------------------------
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+#-------------------------------------------------------------
+
+Y = matrix(seq(0, 199999), 1000, 200);
+Z = matrix(seq(1000, 200999), 1000, 200);
+
+while(FALSE){}
+
+R = as.matrix(sum(ifelse(TRUE,Y,Z)) + sum(ifelse(FALSE,Y,Z)));
+write(R, $1)

Reply via email to