Github user srowen commented on a diff in the pull request: https://github.com/apache/spark/pull/18624#discussion_r127200696 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/recommendation/MatrixFactorizationModel.scala --- @@ -286,40 +288,124 @@ object MatrixFactorizationModel extends Loader[MatrixFactorizationModel] { srcFeatures: RDD[(Int, Array[Double])], dstFeatures: RDD[(Int, Array[Double])], num: Int): RDD[(Int, Array[(Int, Double)])] = { - val srcBlocks = blockify(srcFeatures) - val dstBlocks = blockify(dstFeatures) - val ratings = srcBlocks.cartesian(dstBlocks).flatMap { case (srcIter, dstIter) => - val m = srcIter.size - val n = math.min(dstIter.size, num) - val output = new Array[(Int, (Int, Double))](m * n) - var i = 0 - val pq = new BoundedPriorityQueue[(Int, Double)](n)(Ordering.by(_._2)) - srcIter.foreach { case (srcId, srcFactor) => - dstIter.foreach { case (dstId, dstFactor) => - // We use F2jBLAS which is faster than a call to native BLAS for vector dot product - val score = BLAS.f2jBLAS.ddot(rank, srcFactor, 1, dstFactor, 1) - pq += dstId -> score - } - pq.foreach { case (dstId, score) => - output(i) = (srcId, (dstId, score)) + val srcBlocks = blockify(rank, srcFeatures).zipWithIndex() + val dstBlocks = blockify(rank, dstFeatures) + val ratings = srcBlocks.cartesian(dstBlocks).map { + case (((srcIds, srcFactors), index), (dstIds, dstFactors)) => + val m = srcIds.length + val n = dstIds.length + val dstIdMatrix = new Array[Int](m * num) + val scoreMatrix = Array.fill[Double](m * num)(Double.MinValue) + val pq = new BoundedPriorityQueue[(Int, Double)](num)(Ordering.by(_._2)) + + val ratings = srcFactors.transpose.multiply(dstFactors) + var i = 0 + var j = 0 + while (i < m) { + var k = 0 + while (k < n) { + pq += dstIds(k) -> ratings(i, k) + k += 1 + } + var size = pq.size + while(size > 0) { --- End diff -- You'll need to fix up a few style things like a space after while
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org