You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemds.apache.org by ba...@apache.org on 2021/03/10 11:30:43 UTC

[systemds] branch master updated: [MINOR] CLA Unary contains NaN

This is an automated email from the ASF dual-hosted git repository.

baunsgaard pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/systemds.git


The following commit(s) were added to refs/heads/master by this push:
     new a3693f5  [MINOR] CLA Unary contains NaN
a3693f5 is described below

commit a3693f54de1c791947b3e8f56555f3b0a36056d7
Author: baunsgaard <ba...@tugraz.at>
AuthorDate: Wed Mar 10 12:30:22 2021 +0100

    [MINOR] CLA Unary contains NaN
---
 .../runtime/compress/CompressedMatrixBlock.java    | 51 +++++++++++++++-------
 .../sysds/runtime/compress/colgroup/AColGroup.java |  2 +
 .../runtime/compress/colgroup/ADictionary.java     |  2 +
 .../compress/colgroup/ColGroupUncompressed.java    |  6 ++-
 .../runtime/compress/colgroup/ColGroupValue.java   |  4 ++
 .../runtime/compress/colgroup/Dictionary.java      | 28 ++++++++++--
 .../runtime/compress/colgroup/QDictionary.java     |  7 +++
 .../compress/dictionary/DictionaryTest.java        | 47 ++++++++++++++++++++
 8 files changed, 127 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/apache/sysds/runtime/compress/CompressedMatrixBlock.java b/src/main/java/org/apache/sysds/runtime/compress/CompressedMatrixBlock.java
index 65db4a2..f2958da 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/CompressedMatrixBlock.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/CompressedMatrixBlock.java
@@ -625,22 +625,22 @@ public class CompressedMatrixBlock extends MatrixBlock {
 		op.indexFn.computeDimension(rlen, clen, tempCellIndex);
 
 		// if(op.aggOp.existsCorrection()) {
-		// 	switch(op.aggOp.correction) {
-		// 		case LASTROW:
-		// 			// tempCellIndex.row++;
-		// 			break;
-		// 		case LASTCOLUMN:
-		// 			// tempCellIndex.column++;
-		// 			break;
-		// 		case LASTTWOROWS:
-		// 			tempCellIndex.row += 1;
-		// 			break;
-		// 		case LASTTWOCOLUMNS:
-		// 			tempCellIndex.column += 1;
-		// 			break;
-		// 		default:
-		// 			throw new DMLRuntimeException("unrecognized correctionLocation: " + op.aggOp.correction);
-		// 	}
+		// switch(op.aggOp.correction) {
+		// case LASTROW:
+		// // tempCellIndex.row++;
+		// break;
+		// case LASTCOLUMN:
+		// // tempCellIndex.column++;
+		// break;
+		// case LASTTWOROWS:
+		// tempCellIndex.row += 1;
+		// break;
+		// case LASTTWOCOLUMNS:
+		// tempCellIndex.column += 1;
+		// break;
+		// default:
+		// throw new DMLRuntimeException("unrecognized correctionLocation: " + op.aggOp.correction);
+		// }
 		// }
 
 		// initialize and allocate the result
@@ -854,12 +854,31 @@ public class CompressedMatrixBlock extends MatrixBlock {
 
 	@Override
 	public MatrixBlock unaryOperations(UnaryOperator op, MatrixValue result) {
+
+		// early abort for comparisons w/ special values
+		if(Builtin.isBuiltinCode(op.fn, BuiltinCode.ISNAN, BuiltinCode.ISNA) && !containsValue(op.getPattern()))
+			return new MatrixBlock(getNumRows(), getNumColumns(), 0); // avoid unnecessary allocation
+
 		printDecompressWarning("unaryOperations " + op.fn.toString());
 		MatrixBlock tmp = getUncompressed();
 		return tmp.unaryOperations(op, result);
 	}
 
 	@Override
+	public boolean containsValue(double pattern) {
+		if(isOverlapping()) {
+			throw new NotImplementedException("Not implemented contains value for overlapping matrix");
+		}
+		else {
+			for(AColGroup g : _colGroups) {
+				if(g.containsValue(pattern))
+					return true;
+			}
+			return false;
+		}
+	}
+
+	@Override
 	public double max() {
 		AggregateUnaryOperator op = InstructionUtils.parseBasicAggregateUnaryOperator("uamax", -1);
 		return aggregateUnaryOperations(op, null, 1000, null).getValue(0, 0);
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/AColGroup.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/AColGroup.java
index e431ea9..808d43b 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/AColGroup.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/AColGroup.java
@@ -644,6 +644,8 @@ public abstract class AColGroup implements Serializable {
 
 	public abstract AColGroup copy();
 
+	public abstract boolean containsValue(double pattern);
+
 	@Override
 	public String toString() {
 		StringBuilder sb = new StringBuilder();
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ADictionary.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ADictionary.java
index f5bbc10..8422a3f 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ADictionary.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ADictionary.java
@@ -248,4 +248,6 @@ public abstract class ADictionary {
 	 * @return The re expanded Dictionary.
 	 */
 	public abstract ADictionary reExpandColumns(int max);
+
+	public abstract boolean containsValue(double pattern);
 }
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupUncompressed.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupUncompressed.java
index cbef62b..824ff8c 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupUncompressed.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupUncompressed.java
@@ -532,11 +532,15 @@ public class ColGroupUncompressed extends AColGroup {
 	@Override
 	public void leftMultBySelfDiagonalColGroup(double[] result, int numColumns) {
 		throw new NotImplementedException("Not implemented slice columns");
-
 	}
 
 	@Override
 	public AColGroup copy() {
 		throw new NotImplementedException("Not implemented copy of uncompressed colGroup yet.");
 	}
+
+	@Override
+	public boolean containsValue(double pattern){
+		return _data.containsValue(pattern);
+	}
 }
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupValue.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupValue.java
index dc7652f..d0a6ee9 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupValue.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/ColGroupValue.java
@@ -1026,4 +1026,8 @@ public abstract class ColGroupValue extends AColGroup implements Cloneable {
 		}
 	}
 
+	@Override
+	public boolean containsValue(double pattern){
+		return _dict.containsValue(pattern);
+	}
 }
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/Dictionary.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/Dictionary.java
index ac67d2a..658decb 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/Dictionary.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/Dictionary.java
@@ -92,12 +92,12 @@ public class Dictionary extends ADictionary {
 	}
 
 	@Override
-	public double[] aggregateTuples(Builtin fn, final int nCol){
+	public double[] aggregateTuples(Builtin fn, final int nCol) {
 		if(nCol == 1)
 			return _values;
 		final int nRows = _values.length / nCol;
 		double[] res = new double[nRows];
-		for(int i = 0; i < nRows; i++){
+		for(int i = 0; i < nRows; i++) {
 			final int off = i * nCol;
 			res[i] = _values[off];
 			for(int j = off + 1; j < off + nCol; j++)
@@ -220,7 +220,7 @@ public class Dictionary extends ADictionary {
 
 		// pre-aggregate value tuple
 		final int numVals = getNumberOfValues(nrColumns);
-		double[] ret = ColGroupValue.allocDVector(numVals, false);
+		double[] ret = new double[numVals];
 		for(int k = 0; k < numVals; k++) {
 			ret[k] = sumRow(k, square, nrColumns);
 		}
@@ -365,4 +365,26 @@ public class Dictionary extends ADictionary {
 
 		return new Dictionary(newDictValues);
 	}
+
+	@Override
+	public boolean containsValue(double pattern) {
+
+		if(_values == null)
+			return false;
+
+		boolean NaNpattern = Double.isNaN(pattern);
+
+		if(NaNpattern) {
+			for(double v : _values)
+				if(Double.isNaN(v))
+					return true;
+		}
+		else {
+			for(double v : _values)
+				if(v == pattern)
+					return true;
+		}
+
+		return false;
+	}
 }
diff --git a/src/main/java/org/apache/sysds/runtime/compress/colgroup/QDictionary.java b/src/main/java/org/apache/sysds/runtime/compress/colgroup/QDictionary.java
index 5ce481e..05b1817 100644
--- a/src/main/java/org/apache/sysds/runtime/compress/colgroup/QDictionary.java
+++ b/src/main/java/org/apache/sysds/runtime/compress/colgroup/QDictionary.java
@@ -445,4 +445,11 @@ public class QDictionary extends ADictionary {
 
 		return new QDictionary(newDictValues, 1.0);
 	}
+
+	@Override
+	public boolean containsValue(double pattern){
+		if(Double.isNaN(pattern) || Double.isInfinite(pattern))
+			return false;
+		throw new NotImplementedException("Not contains value on Q Dictionary");
+	}
 }
diff --git a/src/test/java/org/apache/sysds/test/component/compress/dictionary/DictionaryTest.java b/src/test/java/org/apache/sysds/test/component/compress/dictionary/DictionaryTest.java
new file mode 100644
index 0000000..caf2da2
--- /dev/null
+++ b/src/test/java/org/apache/sysds/test/component/compress/dictionary/DictionaryTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+
+package org.apache.sysds.test.component.compress.dictionary;
+
+import static org.junit.Assert.assertTrue;
+
+import org.apache.sysds.runtime.compress.colgroup.Dictionary;
+import org.junit.Test;
+
+public class DictionaryTest {
+
+    @Test
+    public void testContainsValue() {
+        Dictionary d = new Dictionary(new double[] {1, 2, 3});
+        assertTrue(d.containsValue(1));
+        assertTrue(!d.containsValue(-1));
+    }
+
+    @Test
+    public void testContainsValue_nan() {
+        Dictionary d = new Dictionary(new double[] {Double.NaN, 2, 3});
+        assertTrue(d.containsValue(Double.NaN));
+    }
+
+    @Test
+    public void testContainsValue_nan_not() {
+        Dictionary d = new Dictionary(new double[] {1, 2, 3});
+        assertTrue(!d.containsValue(Double.NaN));
+    }
+}