Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BLAS: gemm vs. gemv

Tags:

Why does BLAS have a gemm function for matrix-matrix multiplication and a separate gemv function for matrix-vector multiplication? Isn't matrix-vector multiplication just a special case of matrix-matrix multiplication where one matrix only has one row/column?

like image 357
dsimcha Avatar asked Aug 15 '11 16:08

dsimcha


People also ask

What is GEMM in BLAS?

Background: Matrix-Matrix Multiplication GEMM is defined as the operation C = αAB+βC , with A and B as matrix inputs, α and β as scalar inputs, and C as a pre-existing matrix which is overwritten by the output.

What is GEMV?

Computes a matrix-vector product using a general matrix. Description. The gemv routines compute a scalar-matrix-vector product and add the result to a scalar-vector product, with a general matrix.

Is BLAS written in Fortran?

BLAS implementations will take advantage of special floating point hardware such as vector registers or SIMD instructions. It originated as a Fortran library in 1979 and its interface was standardized by the BLAS Technical (BLAST) Forum, whose latest BLAS report can be found on the netlib website.

What is GEMM in machine learning?

GEMM is a general procedure ubiquitously used in linear al- gebra, machine learning, statistics, and many other areas and is implemented in the BLAS (Basic Linear Algebra Subpro- grams) library (BLA, 2002). It multiplies two input matrices to produce an output matrix.


2 Answers

Mathematically, matrix-vector multiplication is a special case of matrix-matrix multiplication, but that's not necessarily true of them as realized in a software library.

They support different options. For example, gemv supports strided access to the vectors on which it is operating, whereas gemm does not support strided matrix layouts. In the C language bindings, gemm requires that you specify the storage ordering of all three matrices, whereas that is unnecessary in gemv for the vector arguments because it would be meaningless.

Besides supporting different options, there are families of optimizations that might be performed on gemm that are not applicable to gemv. If you know that you are doing a matrix-vector product, you don't want the library to waste time figuring out that's the case before switching into a code path that is optimized for that case; you'd rather call it directly instead.

like image 98
Stephen Canon Avatar answered Oct 01 '22 08:10

Stephen Canon


When you optimize gemv and gemm different techniques apply:

  • For the matrix-matrix operation you are using blocked algorithms. Block sizes depend on cache sizes.
  • For optimising the matrix-vector product you use so called fused Level 1 operations (e.g. fused dot-products or fused axpy).

Let me know if you want more details.

like image 30
Michael Lehn Avatar answered Oct 01 '22 07:10

Michael Lehn