How to do vector matrix multiplication using BLAS?

BLAS defines a Level-2 GEMV (Matrix-Vector Multiplication) operation. How to use the BLAS library to perform vector matrix multiplication?

This is probably obvious, but I don't see how to use the BLAS operation for this multiplication. I would expect a GEVM operation.

+6
source share
1 answer

Matrix vector multiplication of the matrix (M x N) with the vector (N x 1) will lead to the vector (M x 1). In short, a*A(MxN)*X(Nx1) + b*Y(Mx1) -> Y(Mx1) . Of course, you can use INCX and INCY when your vector is included in the matrix.

To determine the multiplication of a vector matrix, the vector must be transposed. those. a*X(1xM)*A(MxN) + b*Y(1xN) -> Y(1xN) . Basically, you do not have a vector, except for a matrix with one row.

From this moment there are two possibilities.

Use level 3 "GEMM"

 ?gemm(transa, transb, m, n, k, alpha, a, lda, b, ldb, beta, c, ldc) 

using

 ?gemm('N', 'N', 1, N, M, a, X, 1, A, M, b, Y, 1) 

Or do some more math. Given that (X*A)^T = A^T * X^T matrix of rows X transformed into the vector X ^ T (MX1). Also Y transpose the vector Y^T(Nx1) . Of course, the memory X and X^T are stored equally sequentially. This means that you can reuse GEMV with the transpose matrix A

 ?gemv('T', M, N, a, A, M, X, 1, b, Y, 1) 
+7
source

Source: https://habr.com/ru/post/984742/


All Articles