Skip to content

Commit f27dbf3

Browse files
committed
move dspr to BLAS
1 parent ae74c3f commit f27dbf3

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/BLAS.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,41 @@ private[spark] object BLAS extends Serializable with Logging {
168168
sum
169169
}
170170

171+
/**
172+
* Adds alpha * x * x.t to a matrix in-place. This is the same as BLAS's DSPR.
173+
*
174+
* @param U the upper triangular part of the matrix packed in an array (column major)
175+
*/
176+
def dspr(alpha: Double, v: Vector, U: Array[Double]): Unit = {
177+
val n = v.size
178+
v match {
179+
case DenseVector(values) =>
180+
NativeBLAS.dspr("U", n, alpha, values, 1, U)
181+
case SparseVector(size, indices, values) =>
182+
val nnz = indices.length
183+
var colStartIdx = 0
184+
var prevCol = 0
185+
var col = 0
186+
var j = 0
187+
var i = 0
188+
var av = 0.0
189+
while (j < nnz) {
190+
col = indices(j)
191+
// Skip empty columns.
192+
colStartIdx += (col - prevCol) * (col + prevCol + 1) / 2
193+
col = indices(j)
194+
av = alpha * values(j)
195+
i = 0
196+
while (i <= j) {
197+
U(colStartIdx + indices(i)) += av * values(i)
198+
i += 1
199+
}
200+
j += 1
201+
prevCol = col
202+
}
203+
}
204+
}
205+
171206
/**
172207
* y = x
173208
*/

mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import scala.collection.mutable.ListBuffer
2424
import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV, SparseVector => BSV, axpy => brzAxpy,
2525
svd => brzSvd, MatrixSingularException, inv}
2626
import breeze.numerics.{sqrt => brzSqrt}
27-
import com.github.fommil.netlib.BLAS.{getInstance => blas}
2827

2928
import org.apache.spark.Logging
3029
import org.apache.spark.SparkContext._
@@ -123,7 +122,7 @@ class RowMatrix @Since("1.0.0") (
123122
// Compute the upper triangular part of the gram matrix.
124123
val GU = rows.treeAggregate(new BDV[Double](new Array[Double](nt)))(
125124
seqOp = (U, v) => {
126-
RowMatrix.dspr(1.0, v, U.data)
125+
BLAS.dspr(1.0, v, U.data)
127126
U
128127
}, combOp = (U1, U2) => U1 += U2)
129128

@@ -673,42 +672,6 @@ class RowMatrix @Since("1.0.0") (
673672
@Experimental
674673
object RowMatrix {
675674

676-
/**
677-
* Adds alpha * x * x.t to a matrix in-place. This is the same as BLAS's DSPR.
678-
*
679-
* @param U the upper triangular part of the matrix packed in an array (column major)
680-
*/
681-
private def dspr(alpha: Double, v: Vector, U: Array[Double]): Unit = {
682-
// TODO: Find a better home (breeze?) for this method.
683-
val n = v.size
684-
v match {
685-
case DenseVector(values) =>
686-
blas.dspr("U", n, alpha, values, 1, U)
687-
case SparseVector(size, indices, values) =>
688-
val nnz = indices.length
689-
var colStartIdx = 0
690-
var prevCol = 0
691-
var col = 0
692-
var j = 0
693-
var i = 0
694-
var av = 0.0
695-
while (j < nnz) {
696-
col = indices(j)
697-
// Skip empty columns.
698-
colStartIdx += (col - prevCol) * (col + prevCol + 1) / 2
699-
col = indices(j)
700-
av = alpha * values(j)
701-
i = 0
702-
while (i <= j) {
703-
U(colStartIdx + indices(i)) += av * values(i)
704-
i += 1
705-
}
706-
j += 1
707-
prevCol = col
708-
}
709-
}
710-
}
711-
712675
/**
713676
* Fills a full square matrix from its upper triangular part.
714677
*/

0 commit comments

Comments
 (0)