How to perform Vector-Matrix Multiplication with BLAS ?

眉间皱痕 提交于 2019-12-04 12:21:17

问题


BLAS defines the GEMV (Matrix-Vector Multiplication) level-2 operation. How to use a BLAS Library to perform Vector-Matrix Multiplication ?

It's probably obvious, but I don't see how to use BLAS operation for this multiplication. I would have expected a GEVM operation.


回答1:


The Matrix-Vector multiplication of a (M x N) Matrix with a (N x 1) Vector will result an (M x 1) Vector. 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 a matrix.

In order to define a Vector-Matrix multiplication The Vector should be transposed. i.e. a*X(1xM)*A(MxN) + b*Y(1xN) -> Y(1xN). Basically you do not have a vector but a single row matrix.

Starting from this point there are two possibilities.

Either 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. Considering that (X*A)^T = A^T * X^T the row matrix X is converted to vector X^T(MX1). Also Y transpose is vector Y^T(Nx1). Of course memory-wise both X and X^T are store with the same way, sequentially. This means you can use again GEMV using transpose matrix A

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


来源:https://stackoverflow.com/questions/29472362/how-to-perform-vector-matrix-multiplication-with-blas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!