## BLAS

The Basic Linear Algebra Subprograms (BLAS) is a common library including high-quality routines for performing basic operations on vectors and matrices. The first FORTRAN version was released in 1979. As the routines are efficient and portable, they are often used in linear algebra software, like LAPACK. Most implementations are written in C, C++, or FORTRAN 77.

ATLAS
High-performance implementation of the BLAS API, for C and FORTRAN 77 (BSD licence).
BLAS
The official reference implementation by Netlib (C and FORTRAN 77). Also known as refblas (public domain).
IMKL
The Intel Math Kernel Library includes BLAS for Intel processors. Professional and community editions for C, C++, and Fortran are distributed (proprietary).

Please see Wikipedia for a more comprehensive overview of BLAS libraries.

### Operations

The functionality of BLAS is divided into three sets of routines, called levels. Below, A, B, C, and T are matrices, x and y are vectors, and α and β are scalars.

Level 1: Scalar-Vector and Vector-Vector Operations

The level provides low-level operations, like dot product of vectors and vector additions, such as

yαx + y.

Level 2: Matrix-Vector Operations

The level contains basic matrix-vector operations, such as

yαAx + βy.

It also includes a solver for x in the triangular equation

Tx = y,

with triangular matrix T.

Level 3: Matrix-Matrix Operations

The level includes matrix-matrix operations, such as

CαAB + βC,

and routines for solving

BαT−1B,

with triangular matrix T.

### Installation

On FreeBSD, the LAPACK implementation of BLAS is available as a port:

``# pkg install math/blas``

But BLAS can be build also from source. Download version 3.8.0 from Netlib, unpack the archive, and compile the Fortran code manually. In order to create a shared library `libblas.so`, run:

``\$ gfortran9 -O2 -shared -fPIC -o libblas.so *.f``

Or, if the static library `libblas.a` is prefered, run:

``````\$ gfortran9 -O2 -c *.f
\$ ar cr libblas.a *.o``````

### Example

The following example program scales a vector by a constant using BLAS.

``````! example.f90
program main
! Example program that scales a vector by a constant using BLAS.
implicit none
external :: sscal

integer, parameter :: N = 3
integer            :: i
real               :: x(N) = [ 5., 6., 7. ]
real               :: a    = 5.

print '(a, f0.1)', 'a = ', a
print '(a)',       'X ='

do i = 1, N
print '(f0.1)', x(i)
end do

call sscal(N, a, x, 1)

print '(/, a)', 'X = a * X'
print '(a)',    'X ='

do i = 1, N
print '(f0.1)', x(i)
end do
end program main``````

Compile and link the example with:

``\$ gfortran9 -L/usr/local/lib/ -o example example.f90 -lblas``

If you link against the static library `libblas.a`, point the library search path `-L` to the correct location. The program outputs:

``````\$ ./example
a = 5.0
X =
5.0
6.0
7.0

X = a * X
X =
25.0
30.0
35.0``````