Difference between revisions of "The BLAS and LAPACK libraries"
| Line 52: | Line 52: | ||
</pre> | </pre> | ||
at compile time. Note the ordering: as some LAPACK routines rely on BLAS routines one must specify the LAPACK library first | at compile time. Note the ordering: as some LAPACK routines rely on BLAS routines one must specify the LAPACK library first | ||
| + | |||
| + | == A simple example to test your Lapack installation == | ||
| + | If you want to double check that all is well, you can for instance test with this small Fortran program: | ||
| + | <syntaxhighlight lang="fortran"> | ||
| + | program test_lapack_dgesv | ||
| + | |||
| + | ! test solving linear system with Lapack's DGESV | ||
| + | ! compile with: | ||
| + | ! gfortran test_lapack_dgesv.F90 -o test_lapack_dgesv -llapack | ||
| + | implicit none | ||
| + | |||
| + | ! Solve system A*X=B | ||
| + | integer,parameter :: ssize=2 | ||
| + | double precision :: a(ssize,ssize) | ||
| + | double precision :: b(ssize) | ||
| + | double precision :: pivot(ssize) | ||
| + | integer :: rc ! Return code. | ||
| + | |||
| + | ! build system | ||
| + | a = reshape([ 2., 3., 1., 1. ], [ 2, 2 ]) | ||
| + | b = [ 5., 6. ] | ||
| + | |||
| + | write(*,*) "a(1,1)=",a(1,1)," a(1,2)=",a(1,2) | ||
| + | write(*,*) "a(2,1)=",a(2,1)," a(2,2)=",a(2,2) | ||
| + | write(*,*) "" | ||
| + | write(*,*) "b(1)=",b(1)," b(2)=",b(2) | ||
| + | |||
| + | call dgesv(ssize, 1, a, ssize, pivot, b, ssize, rc) | ||
| + | ! nb: b is overwritten with solution | ||
| + | if (rc==0) then | ||
| + | write(*,*) "call to dgesv successfull!" | ||
| + | endif | ||
| + | write(*,*) "x(1)=",b(1)," x(2)=",b(2) | ||
| + | |||
| + | end program test_lapack_dgesv | ||
| + | </syntaxhighlight> | ||
| + | which, when run, should give the following output: | ||
| + | <pre> | ||
| + | a(1,1)= 2.0000000000000000 a(1,2)= 1.0000000000000000 | ||
| + | a(2,1)= 3.0000000000000000 a(2,2)= 1.0000000000000000 | ||
| + | |||
| + | b(1)= 5.0000000000000000 b(2)= 6.0000000000000000 | ||
| + | call to dgesv successfull! | ||
| + | x(1)= 1.0000000000000002 x(2)= 2.9999999999999996 | ||
| + | </pre> | ||
[[Category:FAQ]] | [[Category:FAQ]] | ||
Latest revision as of 09:59, 6 October 2026
The BLAS (Basic Linaer Algebra) and LAPACK (Linear Algebra PACKage) libraries are very common libraries useful for doing linear algebra operations involving matrices and vectors, .e.g. solving linear equation systems, doing SVD factorization, etc. In many case they are already installed and available Linux systems, but not always... and then you will have to download and install them yourself.
Contents
Using package managers etc.
In many cases, obtaining and installing the BLAS and LAPACK libraries can be done via the system's package manager; e.g. under Ubuntu-Linux with:
sudo apt-get install libblas-dev liblapack-dev
If you don't have root-like (sudo) privileges, or your system manager doesn't help, then you can always install the libraries from scratch as detailed below.
Obtaining the source code of the BLAS and LAPACK libraries
The Fortran source codes are available on Netlib.org : http://www.netlib.org/blas/ and http://www.netlib.org/lapack/
Compiling the libraries
Prerequisites
All you need are a Fortran compiler and the gmake utility
Compiling the BLAS library
Assuming you dowloaded the package in a "BLAS" directory, e.g.:
wget http://www.netlib.org/blas/blas-3.11.0.tgz
tar xvzf blas-3.11.0.tgz
cd BLAS-3.11.0/
Edit the make.inc to fit your settings (e.g. compiler name and options), if needed and the simply run
make
which should produce the static library blas_LINUX.a. It is good practice to usually use a different naming convention and call the library libblas.a so you can make a symbolic link to have both available:
ln -s blas_LINUX.a libblas.a
Compiling the LAPACK library
Assuming you dowloaded the package in a "LAPACK" directory, e.g.:
wget https://github.com/Reference-LAPACK/lapack/archive/refs/tags/v3.11.0.tar.gz
tar xvzf v3.11.0.tar.gz
cd lapack-3.11.0
copy the provided make.inc.example as make.inc and adapt it to fit your settings (e.g. compiler name and options), if needed, and then run:
make
which should produce the static library liblapack.a. Note that recent versions of LAPACK actually incorporate the BLAS library but with a different name: librefblas.a.
Linking to the libraries
One simply needs to specify options
-Lpath/to/the/LAPACK/library -llapack -Lpath/to/the/BLAS/library -lblas
at compile time. Note the ordering: as some LAPACK routines rely on BLAS routines one must specify the LAPACK library first
A simple example to test your Lapack installation
If you want to double check that all is well, you can for instance test with this small Fortran program:
program test_lapack_dgesv
! test solving linear system with Lapack's DGESV
! compile with:
! gfortran test_lapack_dgesv.F90 -o test_lapack_dgesv -llapack
implicit none
! Solve system A*X=B
integer,parameter :: ssize=2
double precision :: a(ssize,ssize)
double precision :: b(ssize)
double precision :: pivot(ssize)
integer :: rc ! Return code.
! build system
a = reshape([ 2., 3., 1., 1. ], [ 2, 2 ])
b = [ 5., 6. ]
write(*,*) "a(1,1)=",a(1,1)," a(1,2)=",a(1,2)
write(*,*) "a(2,1)=",a(2,1)," a(2,2)=",a(2,2)
write(*,*) ""
write(*,*) "b(1)=",b(1)," b(2)=",b(2)
call dgesv(ssize, 1, a, ssize, pivot, b, ssize, rc)
! nb: b is overwritten with solution
if (rc==0) then
write(*,*) "call to dgesv successfull!"
endif
write(*,*) "x(1)=",b(1)," x(2)=",b(2)
end program test_lapack_dgesv
which, when run, should give the following output:
a(1,1)= 2.0000000000000000 a(1,2)= 1.0000000000000000 a(2,1)= 3.0000000000000000 a(2,2)= 1.0000000000000000 b(1)= 5.0000000000000000 b(2)= 6.0000000000000000 call to dgesv successfull! x(1)= 1.0000000000000002 x(2)= 2.9999999999999996