xuyang1706 commented on a change in pull request #9732: [FLINK-14153][ml] Add 
to BLAS a method that performs DenseMatrix and SparseVector multiplication.
URL: https://github.com/apache/flink/pull/9732#discussion_r336441369
 
 

 ##########
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/linalg/BLAS.java
 ##########
 @@ -146,4 +148,38 @@ public static void gemv(double alpha, DenseMatrix matA, 
boolean transA,
                final String ta = transA ? "T" : "N";
                NATIVE_BLAS.dgemv(ta, m, n, alpha, matA.getData(), lda, 
x.getData(), 1, beta, y.getData(), 1);
        }
+
+       /**
+        * y := alpha * A * x + beta * y .
+        */
+       public static void gemv(double alpha, DenseMatrix matA, boolean transA,
+                                                       SparseVector x, double 
beta, DenseVector y) {
+               if (transA) {
+                       Preconditions.checkArgument(matA.numCols() == y.size() 
&& matA.numRows() == x.size(),
+                               "Matrix and vector size mismatched.");
+               } else {
+                       Preconditions.checkArgument(matA.numRows() == y.size() 
&& matA.numCols() == x.size(),
+                               "Matrix and vector size mismatched.");
+               }
+               final int m = matA.numRows();
+               final int n = matA.numCols();
+               if (transA) {
+                       int start = 0;
+                       for (int i = 0; i < n; i++) {
+                               double s = 0.;
+                               for (int j = 0; j < x.indices.length; j++) {
+                                       s += x.values[j] * matA.data[start + 
x.indices[j]];
+                               }
+                               y.data[i] = beta * y.data[i] + alpha * s;
+                               start += m;
+                       }
+               } else {
+                       scal(beta, y);
+                       for (int i = 0; i < x.indices.length; i++) {
+                               int index = x.indices[i];
+                               double value = alpha * x.values[i];
+                               F2J_BLAS.daxpy(m, value, matA.data, index * m, 
1, y.data, 0, 1);
 
 Review comment:
   There are two reasons I use BLAS here. 
   1. BLAS is a mature linear algebra libarary, which pays a lot of attension 
to data locality, thus it is more cache friendly than naive implementation. 
Usually we gain a lot of permformace improvement on level-2/level-3 BLAS 
routine through calling native (JNI) BLAS, while F2J BLAS is better in level-1 
BLAS routines.
   2. In the case here, y is first scaled by b, then each columns of Ax is 
added to y. It is inevitable that y would be visited more than once.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to