Child pages
  • Question 20 - BLAS, LAPACK libraries
Skip to end of metadata
Go to start of metadata

(Answers summarized from and

The BLAS (Basic Linear Algebra Subprograms) are high quality routines that provide standard building blocks for performing basic vector and matrix operations. The BLAS are commonly used to develop linear algebra software like LAPACK and LINPACK. The original reference implementation in Netlib is written in Fortran 77, but hardware vendors have produced highly optimized versions. A user can also download ATLAS to automatically generate an optimized BLAS library for his architecture.

Level 1 BLAS do vector-vector operations, Level 2 BLAS do matrix-vector operations, and Level 3 BLAS do matrix-matrix operations.
It is possible to call these libraries from C as well as Fortran programs.

To call the BLAS from a C program, use the header:

#include <cblas.h>

To compile a program that uses the BLAS, use the command:

cc fileblas.c -o outblas -lblas -lm

Calling a Complex BLAS Level 1 Function from C

The complex dot product is returned in the structure c.

#define N 5
void main()
  int n, inca = 1, incb = 1, i;
  typedef struct{ double re; double im; } complex16;
  complex16 a[N], b[N], c;
  void zdotc();
  n = N;
  for( i = 0; i < n; i++ ){
    a[i].re = (double)i; a[i].im = (double)i * 2.0;
    b[i].re = (double)(n - i); b[i].im = (double)i * 2.0;
  zdotc( &c, &n, a, &inca, b, &incb );
  printf( "The complex dot product is: ( %6.2f, %6.2f )\n",, );
Using BLAS Level 3 Routine on a Fortran program

The following example illustrates a call to the BLAS Level 3 routine ssymm. This routine performs a matrix-matrix operation

c := alpha*a*b' + beta*c

program symm_main
real a(3,3), b(3,2), c(3,3), alpha, beta
integer m, n, lda, ldb, ldc, i, j
character uplo, side
uplo = 'u'
side = 'l'
m = 3
n = 2
lda = 3
ldb = 3
ldc = 3
alpha = 0.5
beta = 2.0
do i = 1, m
  do j = 1, m
    a(i,j) = 1.0
  end do
end do
do i = 1, m
  do j = 1, n
    c(i,j) = 1.0
    b(i,j) = 2.0
  end do
end do
call ssymm (side, uplo, m, n, alpha,
a, lda, b, ldb, beta, c, ldc)
print*, `Matrix C: `
do i = 1, m
  print*, (c(i,j), j = 1, n)
end do
  • No labels