From a09904f4e50ad5f15308c5bf515e4dd49f5ba718 Mon Sep 17 00:00:00 2001 From: Tor Myklebust Date: Thu, 17 Apr 2014 23:55:01 -0400 Subject: [PATCH 1/2] Helper function for wrapping Array[Double]'s with DoubleMatrix's. --- .../org/apache/spark/mllib/recommendation/ALS.scala | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala b/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala index 5cc47de8ffdf..87a2d44ed23f 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala @@ -267,7 +267,7 @@ class ALS private ( private def computeYtY(factors: RDD[(Int, Array[Array[Double]])]) = { val n = rank * (rank + 1) / 2 val LYtY = factors.values.aggregate(new DoubleMatrix(n))( seqOp = (L, Y) => { - Y.foreach(y => dspr(1.0, new DoubleMatrix(y), L)) + Y.foreach(y => dspr(1.0, wrapDoubleArray(y), L)) L }, combOp = (L1, L2) => { L1.addi(L2) @@ -302,6 +302,13 @@ class ALS private ( } } + /** + * Wrap a double array in a DoubleMatrix without creating garbage. + */ + private def wrapDoubleArray(v: Array[Double]): DoubleMatrix = { + new DoubleMatrix(v.length, 1, v:_*) + } + /** * Flatten out blocked user or product factors into an RDD of (id, factor vector) pairs */ @@ -455,7 +462,7 @@ class ALS private ( // block for (productBlock <- 0 until numBlocks) { for (p <- 0 until blockFactors(productBlock).length) { - val x = new DoubleMatrix(blockFactors(productBlock)(p)) + val x = wrapDoubleArray(blockFactors(productBlock)(p)) tempXtX.fill(0.0) dspr(1.0, x, tempXtX) val (us, rs) = inLinkBlock.ratingsForBlock(productBlock)(p) From 2784fc597b63ddac78954359d78e3d7a6478eba6 Mon Sep 17 00:00:00 2001 From: Tor Myklebust Date: Fri, 18 Apr 2014 10:15:20 -0400 Subject: [PATCH 2/2] Mention that this is probably fixed as of jblas 1.2.4; repunctuate. --- .../scala/org/apache/spark/mllib/recommendation/ALS.scala | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala b/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala index 87a2d44ed23f..eb1190507dd2 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/recommendation/ALS.scala @@ -304,9 +304,11 @@ class ALS private ( /** * Wrap a double array in a DoubleMatrix without creating garbage. + * This is a temporary fix for jblas 1.2.3; it should be safe to move back to the + * DoubleMatrix(double[]) constructor come jblas 1.2.4. */ private def wrapDoubleArray(v: Array[Double]): DoubleMatrix = { - new DoubleMatrix(v.length, 1, v:_*) + new DoubleMatrix(v.length, 1, v: _*) } /**