Skip to content

Commit

Permalink
Helper function for wrapping Array[Double]'s with DoubleMatrix's.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmyklebu committed Apr 18, 2014
1 parent 7863ecc commit a09904f
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a09904f

Please sign in to comment.