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

 ##########
 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:
   that makes sense. I am thinking that the assumption is for example BLAS can 
do some sort of SIMD/MIMD optimization based on the data locality so that it 
can save register/cache loadings and invalidations. 
   It is good to call out the rationale here by having some sort of inline 
comment:
   ```
   // relying on the native implementation and cache locality from BLAS
   ```
   
   If there's any performance issue later, we can always avoid duplicate 
register loading by doing the multiplication the addition and variable 
assignment at the same line similar to 
   ```
   y.data[i] = beta * y.data[i] + alpha * s;
   ```

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to