You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by mb...@apache.org on 2017/04/03 21:38:41 UTC

incubator-systemml git commit: [SYSTEMML-1456] Fix inconsistencies generated code vs built-in functions

Repository: incubator-systemml
Updated Branches:
  refs/heads/master b18908ad7 -> 933824ea9


[SYSTEMML-1456] Fix inconsistencies generated code vs built-in functions

This patch fixes various unary and binary codegen templates to use
exactly the same as existing builtin functions. Note that this fixes
result correctness issues of integer divide and modulus.


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

Branch: refs/heads/master
Commit: 933824ea9389193ab45851f2b5d1ae3e76f760b1
Parents: b18908a
Author: Matthias Boehm <mb...@gmail.com>
Authored: Mon Apr 3 14:28:25 2017 -0700
Committer: Matthias Boehm <mb...@gmail.com>
Committed: Mon Apr 3 14:40:07 2017 -0700

----------------------------------------------------------------------
 .../sysml/hops/codegen/cplan/CNodeBinary.java   | 10 +++---
 .../sysml/hops/codegen/cplan/CNodeUnary.java    | 16 +++++-----
 .../runtime/codegen/LibSpoofPrimitives.java     | 17 +++++++++++
 .../functions/codegen/CellwiseTmplTest.java     | 21 +++++++++++--
 .../scripts/functions/codegen/cellwisetmpl12.R  | 32 ++++++++++++++++++++
 .../functions/codegen/cellwisetmpl12.dml        | 28 +++++++++++++++++
 6 files changed, 108 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeBinary.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeBinary.java b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeBinary.java
index 41f99c9..5ec7231 100644
--- a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeBinary.java
+++ b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeBinary.java
@@ -94,9 +94,9 @@ public class CNodeBinary extends CNode
 				case MINUS:
 					return "    double %TMP% = %IN1% - %IN2%;\n" ;
 				case MODULUS:
-					return "    double %TMP% = %IN1% % %IN2%;\n" ;
+					return "    double %TMP% = LibSpoofPrimitives.mod(%IN1%, %IN2%);\n" ;
 				case INTDIV: 
-					return "    double %TMP% = (int) %IN1% / %IN2%;\n" ;
+					return "    double %TMP% = LibSpoofPrimitives.intDiv(%IN1%, %IN2%);\n" ;
 				case LESS:
 					return "    double %TMP% = (%IN1% < %IN2%) ? 1 : 0;\n" ;
 				case LESSEQUAL:
@@ -111,11 +111,11 @@ public class CNodeBinary extends CNode
 					return "    double %TMP% = (%IN1% != %IN2%) ? 1 : 0;\n" ;
 				
 				case MIN:
-					return "    double %TMP% = Math.min(%IN1%, %IN2%);\n" ;
+					return "    double %TMP% = (%IN1% <= %IN2%) ? %IN1% : %IN2%;\n" ;
 				case MAX:
-					return "    double %TMP% = Math.max(%IN1%, %IN2%);\n" ;
+					return "    double %TMP% = (%IN1% >= %IN2%) ? %IN1% : %IN2%;\n" ;
 				case LOG:
-					return "    double %TMP% = Math.log(%IN1%)/Math.log(%IN2%);\n" ;
+					return "    double %TMP% = FastMath.log(%IN1%)/FastMath.log(%IN2%);\n" ;
 				case POW:
 					return "    double %TMP% = Math.pow(%IN1%, %IN2%);\n" ;
 				case MINUS1_MULT:

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
index 4cf25cd..75b2630 100644
--- a/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
+++ b/src/main/java/org/apache/sysml/hops/codegen/cplan/CNodeUnary.java
@@ -60,19 +60,19 @@ public class CNodeUnary extends CNode
 				case ABS:
 					return "    double %TMP% = Math.abs(%IN1%);\n";
 				case SIN:
-					return "    double %TMP% = Math.sin(%IN1%);\n";
+					return "    double %TMP% = FastMath.sin(%IN1%);\n";
 				case COS: 
-					return "    double %TMP% = Math.cos(%IN1%);\n";
+					return "    double %TMP% = FastMath.cos(%IN1%);\n";
 				case TAN:
-					return "    double %TMP% = Math.tan(%IN1%);\n";
+					return "    double %TMP% = FastMath.tan(%IN1%);\n";
 				case ASIN:
-					return "    double %TMP% = Math.asin(%IN1%);\n";
+					return "    double %TMP% = FastMath.asin(%IN1%);\n";
 				case ACOS:
-					return "    double %TMP% = Math.acos(%IN1%);\n";
+					return "    double %TMP% = FastMath.acos(%IN1%);\n";
 				case ATAN:
 					return "    double %TMP% = Math.atan(%IN1%);\n";
 				case SIGN:
-					return "    double %TMP% = Math.signum(%IN1%);\n";
+					return "    double %TMP% = FastMath.signum(%IN1%);\n";
 				case SQRT:
 					return "    double %TMP% = Math.sqrt(%IN1%);\n";
 				case LOG:
@@ -80,9 +80,9 @@ public class CNodeUnary extends CNode
 				case ROUND: 
 					return "    double %TMP% = Math.round(%IN1%);\n";
 				case CEIL:
-					return "    double %TMP% = Math.ceil(%IN1%);\n";
+					return "    double %TMP% = FastMath.ceil(%IN1%);\n";
 				case FLOOR:
-					return "    double %TMP% = Math.floor(%IN1%);\n";
+					return "    double %TMP% = FastMath.floor(%IN1%);\n";
 				case SELP:
 					return "    double %TMP% = (%IN1%>0) ? %IN1% : 0;\n";
 				case SPROP:

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java b/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
index 52e9893..dfe3244 100644
--- a/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
+++ b/src/main/java/org/apache/sysml/runtime/codegen/LibSpoofPrimitives.java
@@ -22,6 +22,8 @@ package org.apache.sysml.runtime.codegen;
 import java.util.Arrays;
 import java.util.LinkedList;
 
+import org.apache.sysml.runtime.functionobjects.IntegerDivide;
+import org.apache.sysml.runtime.functionobjects.Modulus;
 import org.apache.sysml.runtime.matrix.data.LibMatrixMult;
 
 /**
@@ -33,6 +35,9 @@ import org.apache.sysml.runtime.matrix.data.LibMatrixMult;
  */
 public class LibSpoofPrimitives 
 {
+	private static IntegerDivide intDiv = IntegerDivide.getFnObject();
+	private static Modulus mod = Modulus.getFnObject();
+	
 	//global pool of reusable vectors, individual operations set up their own thread-local
 	//ring buffers of reusable vectors with specific number of vectors and vector sizes 
 	private static ThreadLocal<LinkedList<double[]>> memPool = new ThreadLocal<LinkedList<double[]>>() {
@@ -312,6 +317,18 @@ public class LibSpoofPrimitives
 		return c;
 	}
 	
+	//complex builtin functions that are not directly generated
+	//(included here in order to reduce the number of imports)
+	
+	public static double intDiv(double in1, double in2) {
+		return intDiv.execute(in1, in2);
+	}
+	
+	public static double mod(double in1, double in2) {
+		return mod.execute(in1, in2);
+	}
+	
+	
 	//dynamic memory management
 	
 	public static void setupThreadLocalMemory(int numVectors, int len) {

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/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 79cd2d1..521ea58 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
@@ -46,8 +46,8 @@ public class CellwiseTmplTest extends AutomatedTestBase
 	private static final String TEST_NAME8 = TEST_NAME+8;
 	private static final String TEST_NAME9 = TEST_NAME+9;   //sum((X + 7 * Y)^2)
 	private static final String TEST_NAME10 = TEST_NAME+10; //min/max(X + 7 * Y)
-	private static final String TEST_NAME11 = TEST_NAME+11; //replace((0 / (X - 500))+1, 0/0, 7);
-	
+	private static final String TEST_NAME11 = TEST_NAME+11; //replace((0 / (X - 500))+1, 0/0, 7)
+	private static final String TEST_NAME12 = TEST_NAME+12; //((X/3) %% 0.6) + ((X/3) %/% 0.6)
 
 	private static final String TEST_DIR = "functions/codegen/";
 	private static final String TEST_CLASS_DIR = TEST_DIR + CellwiseTmplTest.class.getSimpleName() + "/";
@@ -60,7 +60,7 @@ public class CellwiseTmplTest extends AutomatedTestBase
 	@Override
 	public void setUp() {
 		TestUtils.clearAssertionInformation();
-		for( int i=1; i<=11; i++ ) {
+		for( int i=1; i<=12; i++ ) {
 			addTestConfiguration( TEST_NAME+i, new TestConfiguration(
 					TEST_CLASS_DIR, TEST_NAME+i, new String[] {String.valueOf(i)}) );
 		}
@@ -121,6 +121,11 @@ public class CellwiseTmplTest extends AutomatedTestBase
 	public void testCodegenCellwiseRewrite11() {
 		testCodegenIntegration( TEST_NAME11, true, ExecType.CP  );
 	}
+	
+	@Test
+	public void testCodegenCellwiseRewrite12() {
+		testCodegenIntegration( TEST_NAME12, true, ExecType.CP  );
+	}
 
 	@Test
 	public void testCodegenCellwise1() {
@@ -177,6 +182,11 @@ public class CellwiseTmplTest extends AutomatedTestBase
 	public void testCodegenCellwise11() {
 		testCodegenIntegration( TEST_NAME11, false, ExecType.CP  );
 	}
+	
+	@Test
+	public void testCodegenCellwise12() {
+		testCodegenIntegration( TEST_NAME12, false, ExecType.CP  );
+	}
 
 	@Test
 	public void testCodegenCellwiseRewrite1_sp() {
@@ -208,6 +218,11 @@ public class CellwiseTmplTest extends AutomatedTestBase
 		testCodegenIntegration( TEST_NAME11, true, ExecType.SPARK );
 	}
 	
+	@Test
+	public void testCodegenCellwiseRewrite12_sp() {
+		testCodegenIntegration( TEST_NAME12, true, ExecType.SPARK );
+	}
+	
 	private void testCodegenIntegration( String testname, boolean rewrites, ExecType instType )
 	{			
 		boolean oldRewrites = OptimizerUtils.ALLOW_ALGEBRAIC_SIMPLIFICATION;

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/src/test/scripts/functions/codegen/cellwisetmpl12.R
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/cellwisetmpl12.R b/src/test/scripts/functions/codegen/cellwisetmpl12.R
new file mode 100644
index 0000000..404b349
--- /dev/null
+++ b/src/test/scripts/functions/codegen/cellwisetmpl12.R
@@ -0,0 +1,32 @@
+#-------------------------------------------------------------
+#
+# 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")
+
+X = matrix(seq(7, 1006), 500, 2, byrow=TRUE);
+
+R1 = (X/3) %% 0.6;
+R2 = (X/3) %/% 0.6;
+R = R1 + R2;
+
+writeMM(as(R,"CsparseMatrix"), paste(args[2], "S", sep=""));

http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/933824ea/src/test/scripts/functions/codegen/cellwisetmpl12.dml
----------------------------------------------------------------------
diff --git a/src/test/scripts/functions/codegen/cellwisetmpl12.dml b/src/test/scripts/functions/codegen/cellwisetmpl12.dml
new file mode 100644
index 0000000..e3f8777
--- /dev/null
+++ b/src/test/scripts/functions/codegen/cellwisetmpl12.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.
+#
+#-------------------------------------------------------------
+
+X = matrix(seq(7, 1006), 500, 2);
+
+R1 = (X/3) %% 0.6;
+R2 = (X/3) %/% 0.6;
+R = R1 + R2;
+
+write(R, $1)