What is BLAS?

Notes on Basic Linear Algebra Subprograms

Author

Eunki Chung

Published

March 30, 2023

Basic Linear Algebra Subprograms(BLAS) is a set of low-level routines for linear algebra operations. Most of the numerical libraries like NumPy and R do their linear algebra computations based on these standard operations. BLAS is often optimized for specific hardware for better performance; thus, BLAS has numerous different implementations depending on the vendor or type of processing unit(CPU/GPU).

Operations in BLAS are categorized into three levels based on their complexity.

Level 1: Vector operations

O(n) operations on O(n) operands

ex)  axpy, “a x plus y” yαx+y

where x,yRn and αR. x,y are n-dimensional vectors each, hence we have 2n numbers. α is a scalar, hence we have 2n+1 numbers in total. yi=α×xi+yifor i=1,...,n We have two floating point operations(1 multiply and 1 add) for each i and we have to repeat it for n times, thus we have 2n floating point operations in total.

In summary,
2n+1 numbers O(n) operands
2n floating point operations O(n) operations

Level 2: Matrix-vector operations

O(n2) operations on O(n2) operands

ex) gemv, “generalized matrix-vector multiplication” yαAx+βy where ARn×n, x,yRn and α,βR.

yi=α×jAij×xj+β×yifor i=1,...,n

Here, A has n2 numbers, x,y has n numbers each, thus we have n2+2n+2 numbers.
For each i, we have n+2 multiplies and (n1)+1 adds, hence n(2n+2)=2n2+2n FP_OPs.

In summary,
n2+2n+2 numbers O(n2) operands
2n2+2n floating point operations O(n2) operations

Level 3 Matrix-matrix operations

O(n3) operations on O(n2) operands

ex) gemm, “generalized matrix multiplication” CαAB+βC

where A,B,CRn×n, and α,βR.

Cij=α×kAik×Bkj+β×Cijfor i,j=1,...,n Here, A,B,C has n2 numbers each, thus we have 3n2+2 numbers.
For each i and j, we have n+2 multiplies and (n1)+1 adds, hence n2(2n+2)=2n3+2n2 FP_OPs.

In summary,
3n2+2 numbers O(n2) operands
2n3+2n2 floating point operations O(n3) operations