You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sm...@apache.org on 2016/03/09 03:46:23 UTC

mahout git commit: MAHOUT-1801:FastUtil to improve speed of Sparse Matrix Operations, closes apache/mahout#186

Repository: mahout
Updated Branches:
  refs/heads/master b21381b92 -> 5926df8f1


MAHOUT-1801:FastUtil to improve speed of Sparse Matrix Operations, closes apache/mahout#186


Project: http://git-wip-us.apache.org/repos/asf/mahout/repo
Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/5926df8f
Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/5926df8f
Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/5926df8f

Branch: refs/heads/master
Commit: 5926df8f1130106ff6ff525a3d670f6fff567f0b
Parents: b21381b
Author: smarthi <sm...@apache.org>
Authored: Tue Mar 8 21:45:11 2016 -0500
Committer: smarthi <sm...@apache.org>
Committed: Tue Mar 8 21:45:11 2016 -0500

----------------------------------------------------------------------
 .../org/apache/mahout/math/SparseMatrix.java    | 58 ++++++++++----------
 1 file changed, 29 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mahout/blob/5926df8f/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
----------------------------------------------------------------------
diff --git a/math/src/main/java/org/apache/mahout/math/SparseMatrix.java b/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
index 520778c..414a910 100644
--- a/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
+++ b/math/src/main/java/org/apache/mahout/math/SparseMatrix.java
@@ -17,29 +17,31 @@
 
 package org.apache.mahout.math;
 
-import com.google.common.collect.AbstractIterator;
+import it.unimi.dsi.fastutil.ints.Int2ObjectMap.Entry;
+import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
+import it.unimi.dsi.fastutil.objects.ObjectIterator;
+
+import java.util.Iterator;
+import java.util.Map;
+
 import org.apache.mahout.math.flavor.MatrixFlavor;
 import org.apache.mahout.math.function.DoubleDoubleFunction;
 import org.apache.mahout.math.function.Functions;
-import org.apache.mahout.math.function.IntObjectProcedure;
 import org.apache.mahout.math.list.IntArrayList;
-import org.apache.mahout.math.map.OpenIntObjectHashMap;
 
-import java.util.Iterator;
-import java.util.Map;
+import com.google.common.collect.AbstractIterator;
 
 /** Doubly sparse matrix. Implemented as a Map of RandomAccessSparseVector rows */
 public class SparseMatrix extends AbstractMatrix {
 
-  private OpenIntObjectHashMap<Vector> rowVectors;
+  private Int2ObjectOpenHashMap<Vector> rowVectors;
   
   /**
    * Construct a matrix of the given cardinality with the given row map
    *
-   * @param rows
-   *          a Map<Integer, RandomAccessSparseVector> of rows
-   * @param columns
-   * @param rowVectors
+   * @param rows no of rows
+   * @param columns no of columns
+   * @param rowVectors a Map<Integer, RandomAccessSparseVector> of rows
    */
   public SparseMatrix(int rows, int columns, Map<Integer, Vector> rowVectors) {
     this(rows, columns, rowVectors, false);
@@ -49,14 +51,14 @@ public class SparseMatrix extends AbstractMatrix {
 
     // Why this is passing in a map? iterating it is pretty inefficient as opposed to simple lists...
     super(rows, columns);
-    this.rowVectors = new OpenIntObjectHashMap<Vector>();
+    this.rowVectors = new Int2ObjectOpenHashMap<>();
     if (shallow) {
       for (Map.Entry<Integer, Vector> entry : rowVectors.entrySet()) {
-        this.rowVectors.put(entry.getKey(), entry.getValue());
+        this.rowVectors.put(entry.getKey().intValue(), entry.getValue());
       }
     } else {
       for (Map.Entry<Integer, Vector> entry : rowVectors.entrySet()) {
-        this.rowVectors.put(entry.getKey(), entry.getValue().clone());
+        this.rowVectors.put(entry.getKey().intValue(), entry.getValue().clone());
       }
     }
   }
@@ -66,7 +68,7 @@ public class SparseMatrix extends AbstractMatrix {
    */
   public SparseMatrix(int rows, int columns) {
     super(rows, columns);
-    this.rowVectors = new OpenIntObjectHashMap<Vector>();
+    this.rowVectors = new Int2ObjectOpenHashMap<>();
   }
 
   @Override
@@ -84,8 +86,7 @@ public class SparseMatrix extends AbstractMatrix {
   }
 
   public Iterator<MatrixSlice> iterateNonEmpty() {
-    final IntArrayList keys = new IntArrayList(rowVectors.size());
-    rowVectors.keys(keys);
+    final int[] keys = rowVectors.keySet().toIntArray();
     return new AbstractIterator<MatrixSlice>() {
       private int slice;
       @Override
@@ -93,7 +94,7 @@ public class SparseMatrix extends AbstractMatrix {
         if (slice >= rowVectors.size()) {
           return endOfData();
         }
-        int i = keys.get(slice);
+        int i = keys[slice];
         Vector row = rowVectors.get(i);
         slice++;
         return new MatrixSlice(row, i);
@@ -168,18 +169,17 @@ public class SparseMatrix extends AbstractMatrix {
       }
 
       SparseMatrix otherSparse = (SparseMatrix) other;
-      otherSparse.rowVectors.forEachPair(new IntObjectProcedure<Vector>() {
-        @Override
-        public boolean apply(int rowIndex, Vector otherRow) {
-          Vector row = rowVectors.get(rowIndex);
-          if (row == null) {
-            rowVectors.put(rowIndex, otherRow.clone());
-          } else {
-            row.assign(otherRow, Functions.PLUS);
-          }
-          return true;
+      for(ObjectIterator<Entry<Vector>> fastIterator = otherSparse.rowVectors.int2ObjectEntrySet().fastIterator();
+              fastIterator.hasNext();) {
+        final Entry<Vector> entry = fastIterator.next();
+        final int rowIndex = entry.getIntKey();
+        Vector row = rowVectors.get(rowIndex);
+        if (row == null) {
+          rowVectors.put(rowIndex, entry.getValue().clone());
+        } else {
+          row.assign(entry.getValue(), Functions.PLUS);
         }
-      });
+      }
       return this;
     } else {
       return super.assign(other, function);
@@ -235,7 +235,7 @@ public class SparseMatrix extends AbstractMatrix {
 
   /** special method necessary for efficient serialization */
   public IntArrayList nonZeroRowIndices() {
-    return rowVectors.keys();
+    return new IntArrayList(rowVectors.keySet().toIntArray());
   }
 
   @Override