BLAS Update

It appears GotoBLAS2 has been deprecated (i.e. orphaned).

The new standard is OpenBLAS.

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.

OpenBLAS is an open source project supported by Lab of Parallel Software and Computational Science, Institute of Software Chinese Academy of Sciences (ISCAS).


-Tom Irvine


MinGW should be installed first.



GotoBLAS2 is an implementation of Basic Linear Algebra Subprograms (BLAS).


GotoBLAS2 has configurations for a variety of hardware platforms. It builds a library that is optimized for the given PC’s CPU.

Make a folder called: c:\LAPACK

Download GotoBLAS2. Filename = GotoBLAS2-1.13_bsd.tar.gz

Save File to: c:\LAPACK

Then open and extract with jZip or some other utility.

jZip is available for free at:

The extracted files should appear in:



wget – is a utility that retrieves files using HTTP, HTTPS and FTP
It will be used in the “make” process to download the latest LAPACK files.

It is given in file: wget

Copy wget into c:\MinGW

Then rename it as: wget.exe
(This approach is needed to circumvent Email firewalls.)

Next… Open the Unix-like shell

Programs > MinGW > MinGW Shell

$ cd /c/LAPACK
$ mkdir include (make include sub-folder)

Install the files from: includes.tgz     (This is for CBLAS).


$ cd /c/LAPACK/GotoBLAS2 (The $ sign is the prompt)

$ make
(this is the step to build the libraries for LAPACK & GotoBLAS2)

$ ls *.lib (directory)

You should then see a file such as:




The string in the middle depends on your CPU type.

Then type

$ nm libgoto2*.lib | grep dsptrd (file listing)

You should several lines such as
U _dsptrd_

From within the MinGW shell:

Copy includes.tgz into /c/LAPACK/include

and run: tar -zxvf includes.tgz

Also check for


This dll can be used for compiling programs which use LAPACK subroutines in a pure Windows environment.

More later . . .

* * *

Tom Irvine

Matrix Inversion in LAPACK

Here is a Fortran program which performs matrix inversion using the LU decomposition method:  INVERSE_MATRIX.F

It is compiled via:


It compiles & runs under both Ubuntu & Cygwin.

See also:

* * *

The INVERSE_MATRIX.F program uses the subroutines: DGETRF & DGETRI

DGETRF computes an LU factorization of a general M-by-N matrix A using partial pivoting with row interchanges.

DGETRI computes the inverse of a matrix using the LU factorization computed by DGETRF.

This method inverts U and then computes inv(A) by solving the system inv(A)*L = inv(U) for inv(A).

* * *

Here is a similar C++ program:  matrix_inverse.cpp 

It is compiled as:

gcc -o matrix_inverse matrix_inverse.cpp -llapack -lstdc++

* * *

See also:  Python Matrix Inversion

* * *

Tom Irvine

Mixed C++ Fortran Programming

This is another matrix multiplication project using the BLAS function dgemm. It is complied and run in an Ubuntu system.

The set consists of two programs:  (right mouse click.  save target or link as)


The set calculates: C = A*B

The C++ program matrix_mult_cf reads in two matrices, A & B.

The input matrices may each have an arbitrary number of rows and columns up to a certain cell limit. Furthermore, the number of A columns must be equal to the number of B rows.

The matrix_mult_cf program then calls a subroutine from fort_matmul_main to do the matrix multiplication.

The fort_matmul_main subroutine uses dgemm to perform the actual multiplication.

An important feature of this program set is that the C++ and Fortran codes pass the matrices back and forth using 1D arrays. This approach requires some extra steps, but it avoids the complexity of passing 2D arrays, especially if any of the matrices is non-square.

The set is compiled using:

gfortran matrix_mult_cf.cpp fort_matmul_main.f -o mmult_cf -lstdc++ -lblas

The resulting executable code is run as


* * *

Tom Irvine


Cygwin is a collection of tools which provide a Linux look and feel environment for Windows.

It is also a DLL (cygwin1.dll) which acts as a Linux Application Programming Interface (API) layer providing substantial Linux API functionality.

Cygwin is not a way to run native Linux apps on Windows. You must rebuild your application from source if you want it to run on Windows.

Further information and installation instructions are given at:

* * *


The following command can be used to determine the version of Cygwin on your PC.

$ uname -a

Determine GNU Compiler Collection (GCC) version

$ gcc –version

The Cygwin file directory can be accessed by the following path or similar:


The directory contents can be displayed via:

$ ls -lt

* * *

XEmacs is text editor for Cygwin.  It may be downloaded from:

* * *

Blas & Lapack are available packages in Cygwin.

The library files are:   libblas.a & liblapack.a

Further informat on Cygwin packages is given at:

* * *

Tom Irvine

Install CBLAS in Ubuntu & Cygwin

CBLAS is C interface to the BLAS library.

* * *

Ubuntu Installation instructions:

BLAS must be installed first.

The instructions for BLAS are given at:
Generalized Eigenvalue Problem & BLAS



tar zxf cblas.tgz

Go to CBLAS folder.

Open in a text editor.

Modify this line in

BLLIB = (specify path)/BLAS/blas_LINUX.a

make all

* * *

The Cygwin installation is similar except that the following is needed in

BLLIB = C:/cygwin/lib/libblas.a

* * *

Tom Irvine

Matrix Multiplication in BLAS & CBLAS

Here is a program that uses dgemm to multiply two matrices:  matrix_mult.cpp

The dgemm function is implemented using gsl blas.

It can be compiled under Cygwin as:

gcc -o matrix_mult_alt matrix_mult_alt.cpp -lgsl -lstdc++

And under Ubuntu as:

gcc -o matrix_mult_alt matrix_mult_alt.cpp -lgsl -lgslcblas -lstdc++

* * *

For benchmark comparison, here is a matrix multiplication program which uses pure pointer access without BLAS.    matrix_11.cpp

* * *

A Fortran program which uses the intrinsic function MATMUL is given at:  MATRIX_MULT.F

A Fortran program which uses DGEMM is:  MATRIX_MULT_D.F

The program was compiled using:

gfortran -o MATRIX_MULT_D MATRIX_MULT_D.F -lblas -llapack

As an aside, the following was used to determine memory leaks during the debugging process:

gfortran -g -o MATRIX_MULT_D MATRIX_MULT_D.F -lblas -llapack -Wall -fbounds-check -fmax-errors=1 -Werror

* * *

Here is a program  matrix_mult_cblas.cpp  which uses cblas_dgemm.

It is compiled in Cygwin via:

gcc -o matrix_mult_cblas matrix_mult_cblas.cpp c:/cygwin/lib/libblas.a c:/cygwin/lib/libcygwin.a c:/cygwin/home/tirvine/CBLAS/lib/cblas_LINUX.a -lstdc++

The paths should be modified according to your library installation.

Note that CBLAS can be installed in Cygwin using the instructions at:

* * *

Tom Irvine

Lapack in Ubunutu

Blas and Lapack may be installed on an Ubuntu system using the instructions at:

Generalized Eigenvalue Problem

* * *

C++ programs that use Lapack can be compiled in Terminal or Bash shell mode via:

g++ file_in -o file_out -lgfortran -llapack -lm


file_in is the source code

file_out is the output executable file

In addition, linker option -lblas may be needed by the source code.

Verify that the output file exists:

ls -lt

Then run the program:


* * *

The source code can also be built in the Code::Blocks IDE.

The following lines must be added to the linker options:

-lblas     (if needed by source code)


1. Suggest running Ubuntu with Gnome shell rather than Unity in order to avoid Code::Blocks missing menu bar problem.

2. Failure to include the linker options will result in errors such as:

ilaenv.f   undefined reference to ‘_gfortran_compare_string’

* * *
Tom Irvine

Generalized Eigenvalue Problem

The goal of this project is to solve a “very large” generalized eigenvalue problem in a “reasonable” amount of time using library functions. The mass and stiffness matrices may be either dense or sparse. Optimum routines are desired for each case.

Here is description of the libraries and packages

gcc – GNU Compiler Collection for C/C++

BLAS – Basic Linear Algebra Subprograms

CBLAS – C interface to the BLAS

LAPACK – Linear Algebra PACKage. LAPACK is a software library for numerical linear algebra including the generalized eigenvalue problem. It uses BLAS.

PETSc – Portable, Extensible Toolkit for Scientific Computation. PETSc is a suite of data structures and routines for the scalable (parallel) solution of scientific applications modeled by partial differential equations. It includes a large suite of parallel linear and nonlinear equation solvers that are easily used in application codes written in C, C++, Fortran and now Python.

SLEPc – a software library for the parallel computation of eigenvalues and eigenvectors of large, sparse matrices. It can be seen as a module of PETSc that provides solvers for different types of eigenproblems, including linear (standard and generalized) and quadratic, as well as the SVD.

Note that PETSc/SLEPc requires BLAS/LAPACK.

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

Installation Steps:

I have a PC with Ubuntu 11.10 running with the gnome-classic shell.

I have installed the following:

Code::Blocks with the gcc compiler from the Ubuntu Software Center

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *


Step 1: download

$ wget

Step 2: extraction

$ tar zxf blas.tgz    this will create a directory BLAS

Step 3: compilation

$ cd BLAS

$ make all

If everything was correct in the previous step, your library is in the BLAS directory, called blas_LINUX.a

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *


Installation instructions:

BLAS must be installed first.



tar zxf cblas.tgz

Go to CBLAS folder.

Open in a text editor.

Modify this line in

BLLIB = (specify path)/BLAS/blas_LINUX.a

make all

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *


Step 1: download

$ wget

Step 2: extraction

$ tar zxf lapack.tgz        this will create a directory lapack.

Step 3: compilation

$ cd lapack-3.3.1

The configuration can be done in the file:

modify:  BLASLIB      = /directory/where/to/find/BLAS/blas$(PLAT).a

The LaPack library uses the BLAS library, so you need to tell where to find it. The result is a library lapack_LINUX.a: this can be copied in a place of your choice.

Save file as:

$ sudo apt-get install cmake      (if not installed already)

$ cmake

$ make

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


The following step may also be needed:
$ sudo apt-get install libblas-dev liblapack-dev

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

The C++ source code for the generalized eigenvalue program is given at:
gen_eigen.cpp    (right mouse click & save target or link)

The header files are:   dsygv.h     dsyev.h     cblas.h

The source code uses BLAS, CBLAS & LAPACK.

The following linker options are required:


* * * * * * * * *

Here is another C++ version: gen_eig.cpp

The program can be compiled via:

gcc -o gen_eig gen_eig.cpp -lblas -llapack -lstdc++

*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *

Here is a pure Fortran program for the generalized eigenvalue problem:  geigen.f

The program can be compiled via:

$ gfortran -o geigen geigen.f -lblas -llapack

* * *

Matlab scripts are given at:

Matlab Linear Algebra Page

* * *

A future generalized eigenvalue problem code will use PETSC & SLEPc.  These packages can be downloaded via:

sudo apt-get install petsc-dev

sudo apt-get install slepc3.1-dev

* * *

Python is well-suited for the generalized eigenvalue problem.  It has functions derived from LAPACK.  It also has functions for sparse systems using ARPACK.  Sample scripts are posted at:

Vibrationdata Python  Generalized Eigenvalue

* * *

Tom Irvine