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

    https://github.com/apache/spark/pull/2451#discussion_r17765167
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/BLAS.scala ---
    @@ -197,4 +201,452 @@ private[mllib] object BLAS extends Serializable {
             throw new IllegalArgumentException(s"scal doesn't support vector 
type ${x.getClass}.")
         }
       }
    +
    +  // For level-3 routines, we use the native BLAS.
    +  private def nativeBLAS: NetlibBLAS = {
    +    if (_nativeBLAS == null) {
    +      _nativeBLAS = NativeBLAS
    +    }
    +    _nativeBLAS
    +  }
    +
    +  /**
    +   * C := alpha * A * B + beta * C
    +   * @param transA whether to use the transpose of matrix A (true), or A 
itself (false).
    +   * @param transB whether to use the transpose of matrix B (true), or B 
itself (false).
    +   * @param alpha a scalar to scale the multiplication A * B.
    +   * @param A the matrix A that will be left multiplied to B. Size of m x 
k.
    +   * @param B the matrix B that will be left multiplied by A. Size of k x 
n.
    +   * @param beta a scalar that can be used to scale matrix C.
    +   * @param C the resulting matrix C. Size of m x n.
    +   */
    +  def gemm(
    +      transA: Boolean,
    +      transB: Boolean,
    +      alpha: Double,
    +      A: Matrix,
    +      B: Matrix,
    +      beta: Double,
    +      C: DenseMatrix): Unit = {
    +    if (alpha == 0.0) {
    +      logDebug("gemm: alpha is equal to 0. Returning C.")
    +    } else {
    +      A match {
    +        case sparse: SparseMatrix =>
    +          B match {
    +            case dB: DenseMatrix => gemm(transA, transB, alpha, sparse, 
dB, beta, C)
    +            case sB: SparseMatrix =>
    +              throw new IllegalArgumentException(s"gemm doesn't support 
sparse-sparse matrix " +
    +                s"multiplication")
    +            case _ =>
    +              throw new IllegalArgumentException(s"gemm doesn't support 
matrix type ${B.getClass}.")
    +          }
    +        case dense: DenseMatrix =>
    +          B match {
    +            case dB: DenseMatrix => gemm(transA, transB, alpha, dense, dB, 
beta, C)
    +            case sB: SparseMatrix => gemm(transA, transB, alpha, dense, 
sB, beta, C)
    +            case _ =>
    +              throw new IllegalArgumentException(s"gemm doesn't support 
matrix type ${B.getClass}.")
    +          }
    +        case _ =>
    +          throw new IllegalArgumentException(s"gemm doesn't support matrix 
type ${A.getClass}.")
    +      }
    +    }
    +  }
    +
    +  /**
    +   * C := alpha * A * B + beta * C
    +   *
    +   * @param alpha a scalar to scale the multiplication A * B.
    +   * @param A the matrix A that will be left multiplied to B. Size of m x 
k.
    +   * @param B the matrix B that will be left multiplied by A. Size of k x 
n.
    +   * @param beta a scalar that can be used to scale matrix C.
    +   * @param C the resulting matrix C. Size of m x n.
    +   */
    +  def gemm(
    +      alpha: Double,
    +      A: Matrix,
    +      B: Matrix,
    +      beta: Double,
    +      C: DenseMatrix): Unit = {
    +    gemm(false, false, alpha, A, B, beta, C)
    +  }
    +
    +  /**
    +   * C := alpha * A * B + beta * C
    +   * For `DenseMatrix` A.
    +   */
    +  private def gemm(
    +      transA: Boolean,
    +      transB: Boolean,
    +      alpha: Double,
    +      A: DenseMatrix,
    +      B: DenseMatrix,
    +      beta: Double,
    +      C: DenseMatrix): Unit = {
    +    val mA: Int = if (!transA) A.numRows else A.numCols
    +    val nB: Int = if (!transB) B.numCols else B.numRows
    +    val kA: Int = if (!transA) A.numCols else A.numRows
    +    val kB: Int = if (!transB) B.numRows else B.numCols
    +    val tAstr = if (!transA) "N" else "T"
    +    val tBstr = if (!transB) "N" else "T"
    +
    +    require(kA == kB, s"The columns of A don't match the rows of B. A: 
$kA, B: $kB")
    +    require(mA == C.numRows, s"The rows of C don't match the rows of A. C: 
${C.numRows}, A: $mA")
    +    require(nB == C.numCols,
    +      s"The columns of C don't match the columns of B. C: ${C.numCols}, A: 
$nB")
    +
    +    nativeBLAS.dgemm(tAstr, tBstr, mA, nB, kA, alpha, A.values, A.numRows, 
B.values, B.numRows,
    +      beta, C.values, C.numRows)
    +  }
    +
    +  /**
    +   * C := alpha * A * B + beta * C
    +   * For `SparseMatrix` A.
    +   */
    +  private def gemm(
    +      transA: Boolean,
    +      transB: Boolean,
    +      alpha: Double,
    +      A: SparseMatrix,
    +      B: DenseMatrix,
    +      beta: Double,
    +      C: DenseMatrix): Unit = {
    +    val mA: Int = if (!transA) A.numRows else A.numCols
    +    val nB: Int = if (!transB) B.numCols else B.numRows
    +    val kA: Int = if (!transA) A.numCols else A.numRows
    +    val kB: Int = if (!transB) B.numRows else B.numCols
    +
    +    require(kA == kB, s"The columns of A don't match the rows of B. A: 
$kA, B: $kB")
    +    require(mA == C.numRows, s"The rows of C don't match the rows of A. C: 
${C.numRows}, A: $mA")
    +    require(nB == C.numCols,
    +      s"The columns of C don't match the columns of B. C: ${C.numCols}, A: 
$nB")
    +
    +    val Avals = A.values
    +    val Arows = if (!transA) A.rowIndices else A.colPtrs
    +    val Acols = if (!transA) A.colPtrs else A.rowIndices
    +
    +    // Slicing is easy in this case. This is the optimal multiplication 
setting for sparse matrices
    --- End diff --
    
    Comment belongs inside "if (transA)"


---
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

Reply via email to