## 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``

However, BLAS may be built also from source as well. 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:

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

Or, if a static library is preferred, compile and archive `libblas.a` with:

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

### Example

The following example program scales a vector by a constant using the BLAS routine `sscal()`. FORTRAN 77 procedures have to be be imported individually using the `external` statement if `implicit none (type, external)` is set. Otherwise, all linked procedures are visible by default.

``````! example.f90
program main
implicit none (type, external)
external :: sscal

integer, parameter :: N = 3

real :: x(N)
real :: a

x = [ 5., 6., 7. ]
a = 5.

print '("a = ", f0.1)' a
print '("X = [ ", 3(f0.1, " "), "]")', x

call sscal(N, a, x, 1)

print '(/, "X = a * X")'
print '("X = [ ", 3(f0.1, " "), "]")', x
end program main``````

Compile and link the example with:

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

If you prefer to link against the static library `libblas.a`, replace `-lblas` with the actual path. The program outputs:

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

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

The compilation can be simplified by writing an appropriate Makefile:

``````.POSIX:

FC      = gfortran12
FFLAGS  = -std=f2018
LDFLAGS = -L/usr/local/lib/
LDLIBS  = -lblas
TARGET  = example

.PHONY: all clean

all:
\$(FC) \$(FFLAGS) \$(LDFLAGS) -o \$(TARGET) example.f90 \$(LDLIBS)

clean:
rm \$(TARGET)``````

We then just have to run `make` to build the executable.