Basic Linear Algebra Subprograms


Basic Linear Algebra Subprograms is a specification that prescribes a set of low-level routines for performing common linear algebra operations such as vector addition, scalar multiplication, dot products, linear combinations, and matrix multiplication. They are the de facto standard low-level routines for linear algebra libraries; the routines have bindings for both C and Fortran. Although the BLAS specification is general, BLAS implementations are often optimized for speed on a particular machine, so using them can bring substantial performance benefits. 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 Forum, whose latest BLAS report can be found on the netlib website. This Fortran library is known as the reference implementation and is not optimized for speed but is in the public domain.
Most libraries that offer linear algebra routines conform to the BLAS interface, allowing library users to develop programs that are agnostic of the BLAS library being used. Examples of BLAS libraries include: AMD Core Math Library, Arm Performance Libraries, ATLAS, Intel Math Kernel Library, and OpenBLAS. ACML is no longer supported by its producer. ATLAS is a portable library that automatically optimizes itself for an arbitrary architecture. MKL is a freeware and proprietary vendor library optimized for x86 and x86-64 with a performance emphasis on Intel processors. OpenBLAS is an open-source library that is hand-optimized for many of the popular architectures. The LINPACK benchmarks rely heavily on the BLAS routine gemm for its performance measurements.
Many numerical software applications use BLAS-compatible libraries to do linear algebra computations, including Armadillo, LAPACK, LINPACK, GNU Octave, Mathematica, MATLAB, NumPy, R, and Julia.

Background

With the advent of numerical programming, sophisticated subroutine libraries became useful. These libraries would contain subroutines for common high-level mathematical operations such as root finding, matrix inversion, and solving systems of equations. The language of choice was FORTRAN. The most prominent numerical programming library was IBM's Scientific Subroutine Package. These subroutine libraries allowed programmers to concentrate on their specific problems and avoid re-implementing well-known algorithms. The library routines would also be better than average implementations; matrix algorithms, for example, might use full pivoting to get better numerical accuracy. The library routines would also have more efficient routines. For example, a library may include a program to solve a matrix that is upper triangular. The libraries would include single-precision and double-precision versions of some algorithms.
Initially, these subroutines used hard-coded loops for their low-level operations. For example, if a subroutine need to perform a matrix multiplication, then the subroutine would have three nested loops. Linear algebra programs have many common low-level operations. Between 1973 and 1977, several of these kernel operations were identified. These kernel operations became defined subroutines that math libraries could call. The kernel calls had advantages over hard-coded loops: the library routine would be more readable, there were fewer chances for bugs, and the kernel implementation could be optimized for speed. A specification for these kernel operations using scalars and vectors, the level-1 Basic Linear Algebra Subroutines, was published in 1979. BLAS was used to implement the linear algebra subroutine library LINPACK.
The BLAS abstraction allows customization for high performance. For example, LINPACK is a general purpose library that can be used on many different machines without modification. LINPACK could use a generic version of BLAS. To gain performance, different machines might use tailored versions of BLAS. As computer architectures became more sophisticated, vector machines appeared. BLAS for a vector machine could use the machine's fast vector operations.
Other machine features became available and could also be exploited. Consequently, BLAS was augmented from 1984 to 1986 with level-2 kernel operations that concerned vector-matrix operations. Memory hierarchy was also recognized as something to exploit. Many computers have cache memory that is much faster than main memory; keeping matrix manipulations localized allows better usage of the cache. In 1987 and 1988, the level 3 BLAS were identified to do matrix-matrix operations. The level 3 BLAS encouraged block-partitioned algorithms. The LAPACK library uses level 3 BLAS.
The original BLAS concerned only densely stored vectors and matrices. Further extensions to BLAS, such as for sparse matrices, have been addressed.

ATLAS

attempts to make a BLAS implementation with higher performance. ATLAS defines many BLAS operations in terms of some core routines and then tries to automatically tailor the core routines to have good performance. A search is performed to choose good block sizes. The block sizes may depend on the computer's cache size and architecture. Tests are also made to see if copying arrays and vectors improves performance. For example, it may be advantageous to copy arguments so that they are cache-line aligned so user-supplied routines can use SIMD instructions.

Functionality

BLAS functionality is categorized into three sets of routines called "levels", which correspond to both the chronological order of definition and publication, as well as the degree of the polynomial in the complexities of algorithms; Level 1 BLAS operations typically take linear time,, Level 2 operations quadratic time and Level 3 operations cubic time. Modern BLAS implementations typically provide all three levels.

Level 1

This level consists of all the routines described in the original presentation of BLAS, which defined only vector operations on strided arrays: dot products, vector norms, a generalized vector addition of the form
and several other operations.

Level 2

This level contains matrix-vector operations including, among other things, a generalized matrix-vector multiplication :
as well as a solver for in the linear equation
with being triangular. Design of the Level 2 BLAS started in 1984, with results published in 1988. The Level 2 subroutines are especially intended to improve performance of programs using BLAS on vector processors, where Level 1 BLAS are suboptimal "because they hide the matrix-vector nature of the operations from the compiler."

Level 3

This level, formally published in 1990, contains matrix-matrix operations, including a "general matrix multiplication", of the form
where and can optionally be transposed or hermitian-conjugated inside the routine and all three matrices may be strided. The ordinary matrix multiplication can be performed by setting to one and to an all-zeros matrix of the appropriate size.
Also included in Level 3 are routines for solving
where is a triangular matrix, among other functionality.
Due to the ubiquity of matrix multiplications in many scientific applications, including for the implementation of the rest of Level 3 BLAS, and because faster algorithms exist beyond the obvious repetition of matrix-vector multiplication, gemm is a prime target of optimization for BLAS implementers. E.g., by decomposing one or both of, into block matrices, gemm can be implemented recursively. This is one of the motivations for including the parameter, so the results of previous blocks can be accumulated. Note that this decomposition requires the special case which many implementations optimize for, thereby eliminating one multiplication for each value of. This decomposition allows for better locality of reference both in space and time of the data used in the product. This, in turn, takes advantage of the cache on the system. For systems with more than one level of cache, the blocking can be applied a second time to the order in which the blocks are used in the computation. Both of these levels of optimization are used in implementations such as ATLAS. More recently, implementations by Kazushige Goto have shown that blocking only for the L2 cache, combined with careful amortizing of copying to contiguous memory to reduce TLB misses, is superior to ATLAS. A highly tuned implementation based on these ideas is part of the GotoBLAS, OpenBLAS and BLIS.

Implementations

; Accelerate: Apple's framework for macOS and iOS, which includes tuned versions of BLAS and LAPACK.
; ACML: The AMD Core Math Library, supporting the AMD Athlon and Opteron CPUs under Linux and Windows.
; C++ AMP BLAS: The C++ AMP BLAS Library is an open source implementation of BLAS for Microsoft's AMP language extension for Visual C++.
; ATLAS: Automatically Tuned Linear Algebra Software, an open source implementation of BLAS APIs for C and Fortran 77.
; BLIS: BLAS-like Library Instantiation Software framework for rapid instantiation.
; cuBLAS:Optimized BLAS for NVIDIA based GPU cards, requiring few additional library calls.
; NVBLAS:Optimized BLAS for NVIDIA based GPU cards, providing only Level 3 functions, but as direct drop-in replacement for other BLAS libraries.
; clBLAS: An OpenCL implementation of BLAS.
; clBLAST: A tuned OpenCL implementation of BLAS.
; Eigen BLAS: A Fortran 77 and C BLAS library implemented on top of the MPL-licensed Eigen library, supporting x86, x86 64, ARM, and PowerPC architectures.
; ESSL: IBM's Engineering and Scientific Subroutine Library, supporting the PowerPC architecture under AIX and Linux.
; GotoBLAS: Kazushige Goto's BSD-licensed implementation of BLAS, tuned in particular for Intel Nehalem/Atom, VIA Nanoprocessor, AMD Opteron.
; HP MLIB: HP's Math library supporting IA-64, PA-RISC, x86 and Opteron architecture under HPUX and Linux.
; Intel MKL: The Intel Math Kernel Library, supporting x86 32-bits and 64-bits, available free from Intel. Includes optimizations for Intel Pentium, Core and Intel Xeon CPUs and Intel Xeon Phi; support for Linux, Windows and macOS.
; MathKeisan: NEC's math library, supporting NEC SX architecture under SUPER-UX, and Itanium under Linux
; Netlib BLAS: The official reference implementation on Netlib, written in Fortran 77.
; Netlib CBLAS: Reference C interface to the BLAS. It is also possible to call the Fortran BLAS from C.
; OpenBLAS: Optimized BLAS based on GotoBLAS, supporting x86, x86-64, MIPS and ARM processors.
; PDLIB/SX: NEC's Public Domain Mathematical Library for the NEC SX-4 system.
; SCSL: SGI's Scientific Computing Software Library contains BLAS and LAPACK implementations for SGI's Irix workstations.
; Sun Performance Library: Optimized BLAS and LAPACK for SPARC, Core and AMD64 architectures under Solaris 8, 9, and 10 as well as Linux.

Similar libraries but not compatible with BLAS

; Armadillo: Armadillo is a C++ linear algebra library aiming towards a good balance between speed and ease of use. It employs template classes, and has optional links to BLAS/ATLAS and LAPACK. It is sponsored by NICTA and is licensed under a free license.
; ACL: AMD Compute Libraries
; CUDA SDK: The NVIDIA CUDA SDK includes BLAS functionality for writing C programs that runs on GeForce 8 Series or newer graphics cards. The library cuBLAS has been designed with the purpose of implementing the BLAS capabilities using the CUDA SDK.
; Eigen: The Eigen template library provides an easy to use highly generic C++98 template interface to matrix/vector operations and related algorithms like solving algorithms, decompositions etc. It uses vector capabilities and is optimized for both fixed size and dynamic sized and sparse matrices.
; Elemental: Elemental is an open source software for distributed-memory dense and sparse-direct linear algebra and optimization.
; GSL: The GNU Scientific Library Contains a multi-platform implementation in C which is distributed under the GNU General Public License.
; HASEM: is a C++ template library, being able to solve linear equations and to compute eigenvalues. It is licensed under BSD License.
; LAMA: The Library for Accelerated Math Applications is a C++ template library for writing numerical solvers targeting various kinds of hardware on distributed memory systems, hiding the hardware specific programming from the program developer
; Libflame: FLAME project implementation of dense linear algebra library
; MAGMA: Matrix Algebra on GPU and Multicore Architectures project develops a dense linear algebra library similar to LAPACK but for heterogeneous and hybrid architectures including multicore systems accelerated with general-purpose computing on graphics processing units.
; Mir
; MTL4: The Matrix Template Library version 4 is a generic C++ template library providing sparse and dense BLAS functionality. MTL4 establishes an intuitive interface and broad applicability thanks to generic programming.
; PLASMA: The Parallel Linear Algebra for Scalable Multi-core Architectures project is a modern replacement of LAPACK for multi-core architectures. PLASMA is a software framework for development of asynchronous operations and features out of order scheduling with a runtime scheduler called QUARK that may be used for any code that expresses its dependencies with a directed acyclic graph.
; uBLAS: A generic C++ template class library providing BLAS functionality. Part of the Boost library. It provides bindings to many hardware-accelerated libraries in a unifying notation. Moreover, uBLAS focuses on correctness of the algorithms using advanced C++ features.

Sparse BLAS

Several extensions to BLAS for handling sparse matrices have been suggested over the course of the library's history; a small set of sparse matrix kernel routines were finally standardized in 2002.