Github user mengxr commented on a diff in the pull request:

    https://github.com/apache/spark/pull/4256#discussion_r23739796
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/CoordinateMatrix.scala
 ---
    @@ -98,6 +97,59 @@ class CoordinateMatrix(
         toIndexedRowMatrix().toRowMatrix()
       }
     
    +  /** Converts to BlockMatrix. Creates blocks of size 1024 x 1024. */
    +  def toBlockMatrix(): BlockMatrix = {
    +    toBlockMatrix(1024, 1024)
    +  }
    +
    +  /**
    +   * Converts to BlockMatrix.
    +   * @param rowsPerBlock The number of rows of each block. The blocks at 
the bottom edge may have
    +   *                     a smaller value. Must be an integer value greater 
than 0.
    +   * @param colsPerBlock The number of columns of each block. The blocks 
at the right edge may have
    +   *                     a smaller value. Must be an integer value greater 
than 0.
    +   * @return a `BlockMatrix`
    +   */
    +  def toBlockMatrix(rowsPerBlock: Int, colsPerBlock: Int): BlockMatrix = {
    +    require(rowsPerBlock > 0,
    +      s"rowsPerBlock needs to be greater than 0. rowsPerBlock: 
$rowsPerBlock")
    +    require(colsPerBlock > 0,
    +      s"colsPerBlock needs to be greater than 0. colsPerBlock: 
$colsPerBlock")
    +    val m = numRows()
    +    val n = numCols()
    +    val numRowBlocks = math.ceil(m.toDouble / rowsPerBlock).toInt
    +    val numColBlocks = math.ceil(n.toDouble / colsPerBlock).toInt
    +    val partitioner = GridPartitioner(numRowBlocks, numColBlocks, 
entries.partitions.length)
    +
    +    val blocks: RDD[((Int, Int), Matrix)] = entries.map { entry =>
    +      val blockRowIndex = (entry.i / rowsPerBlock).toInt
    +      val blockColIndex = (entry.j / colsPerBlock).toInt
    +
    +      val rowId = entry.i % rowsPerBlock
    +      val colId = entry.j % colsPerBlock
    +
    +      ((blockRowIndex, blockColIndex), (rowId.toInt, colId.toInt, 
entry.value))
    +    }.groupByKey(partitioner).map { case ((blockRowIndex, blockColIndex), 
entry) =>
    +      val effRows =
    +        if (blockRowIndex == numRowBlocks - 1) {
    --- End diff --
    
    `math.min(m - blockRowIndex * rowsPerBlock, rowsPerBlock)`


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to