# Linear algebra libraries (BLAS and LAPACK)¶

We provide a number of linear algebra library packages on Apocrita. This page documents libraries implementing the BLAS and LAPACK specifications, primarily looking at their use in C and Fortran development.

BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra PACKage) are two specifications of linear algebra libraries. The BLAS package routines address low-level scalar, vector and matrix manipulations (such as matrix--vector and matrix--matrix multiplication) while the LAPACK package builds on the BLAS routines to, among other things, factorize matrices, solve simultaneous linear equations and eigenvalue problems.

On Apocrita we provide several implementations of these specifications, accessible through the module system. Which of the implementations you should use depends on what requirements you have.

The implementations we provide, described below, are chiefly used by C and Fortran programs directly. In most cases Fortran modules are not provided and less modern coding practices are required. Languages such as MATLAB and R which use linear algebra libraries typically bundle their own versions and do not use the implementations we provide. Higher level libraries such as Python's Numpy and C++'s Armadillo provide alternative ways to access BLAS and LAPACK and we do not cover those here.

In this documentation page we describe how to compile programs which require a low-level BLAS or LAPACK implementation. For some uses simply loading a suitable module would be sufficient, but in others it may be necessary to set compiler options. To run a compiled program the same implementation and module that was used when compiling should be loaded.

We do not show here how to write programs using BLAS or LAPACK routines. For a description of the specifications of these packages see the external documentation linked below.

## Netlib reference implementation (BLAS and LAPACK)¶

On Apocrita, the reference implementations of BLAS and LAPACK can be found
in modules `lapack/`

. Not all modules named `lapack/`

use the reference
implementation but those that do state this in the module description:

```
$ module whatis lapack/3.7.0
lapack/3.7.0: adds lapack 3.7.0 to your PATH environment variable
lapack/3.7.0: this is the Netlib reference implementation and is not optimized
```

Avoid using the Netlib reference implementations in code where performance is important. The Netlib documentation itself says "A Fortran 77 reference implementation of the BLAS is available from netlib; however, its use is discouraged as it will not perform as well as a specifically tuned implementation." Other implementations on Apocrita given below are specifically tuned.

Once such a module has been loaded the `pkg-config`

command can be used
to see the linker flags that are required to use the implementations. For
example, with `lapack/3.7.0`

:

```
$ module load lapack/3.7.0
$ pkg-config --libs blas
-L<libdir> -lblas
$ pkg-config --libs lapack
-L<libdir> -llapack -lblas
$ pkg-config --libs lapacke
-L<libdir> -llapacke -llapack -lblas
```

For clarity here, the installation directory on Apocrita containing the BLAS and
LAPACK libraries is represented by `<libdir>`

.

The packages `blas`

and `lapack`

refer to the Fortran interfaces to BLAS
and LAPACK respectively, while the `lapacke`

package provides a native C
interface to LAPACK. Each of these may be used by C or Fortran code or by
languages which are interoperable with C or Fortran.

These modules do not provide LAPACK90 Fortran interfaces.

## Netlib reference implementation (LAPACK, using Intel MKL BLAS)¶

We provide the Netlib LAPACK interface building on the optimized Intel MKL BLAS implementation, exposing a similar BLAS interface to the Netlib reference implementation. An optimized implementation states this in the module description:

```
$ module whatis lapack/3.9.0
lapack/3.9.0: adds lapack 3.9.0 (Intel MKL 2018) to your environment
```

If using the Netlib reference implementation of LAPACK we recommend, for performance, the use of a module with an Intel MKL BLAS basis.

Once such a module has been loaded the `pkg-config`

command can be used
to see the linker flags that are required to use the implementations. For
example, with `lapack/3.9.0`

:

```
$ module load lapack/3.9.0
$ pkg-config --libs blas
-L<libdir> -lmkl_gf_lp64
$ pkg-config --libs lapack
-L<libdir> -llapack
$ pkg-config --libs lapacke
-L<libdir> -llapacke
```

For clarity here, the installation directory on Apocrita containing the MKL
BLAS and LAPACK libraries is represented by `<libdir>`

.

The packages `blas`

and `lapack`

refer to the Fortran interfaces to BLAS
and LAPACK respectively, while the `lapacke`

package provides a native C
interface to LAPACK. Each of these may be used by C or Fortran code or by
languages which are interoperable with C or Fortran.

These modules do not provide LAPACK90 Fortran interfaces.

Note that if you are using BLAS routines in your program as well as
LAPACK routines it may be necessary to specify this on the `pkg-config`

line:

```
$ pkg-config --libs blas lapack
-L<path1> -L<path2> -lmkl_gf_lp64 -llapack
```

Similarly, if using LAPACKE headers in a C program but also BLAS and LAPACK directly, then it may be necessary to specify all packages:

```
$ pkg-config --libs blas lapack lapacke
-L<path1> -L<path2> -lmkl_gf_lp64 -llapack -llapacke
```

As can be seen from the example output here, the location and name of the
underlying Intel MKL BLAS library is not as simple as in the case of using
the Netlib reference BLAS implementation. For this reason we strongly
recommend that you always use `pkg-config`

to simplify your command line
or build description files.

## Intel MKL (BLAS and LAPACK)¶

Intel MKL implements the BLAS and LAPACK specifications. See our documentation for Intel MKL for details of using these on Apocrita. Intel MKL is most easily used with the Intel compilers but it may also be used with other compilers available on Apocrita.

If you wish to use an implementation of LAPACK you may find that using Intel
MKL provides higher performance than does the reference Netlib implementation
found in the `lapack/`

modules.

Intel MKL provides the LAPACK90 interface to LAPACK.

## Further BLAS implementations¶

There are other BLAS implementations available which are not installed system-wide on Apocrita. An implementation like OpenBLAS can be installed by users within user space or lab shares. Contact us for help with using these personal installations if required.

## Using GNU Autotools and CMake with BLAS and LAPACK¶

A consistent interface to the numerous BLAS and LAPACK implementations is
given by the build tools CMake and GNU Autotools. For CMake, the modules
`FindBLAS`

and
`FindLAPACK`

can detect suitable build options; for GNU Autotools, after loading the
Autoconf Archive module we have the corresponding
macros `AX_BLAS`

and `AX_LAPACK`

.

## Higher level implementations¶

In addition to the `numpy.ndarray`

type of NumPy, which provides a higher
level linear algebra package, BLAS and LAPACK routines may be accessed
directly in Python using the
SciPy wrappers.