MEX-files
 
In the Matlab-lab on sparse matrices, it was stated that we would fix the slow matrix-vector multiply with banded matrices. We will do that by linking Matlab with a compiled Fortran-routine from Netlib.


Fetch dgbmv.f from Netlib/BLAS (or use Goto-Blas). Write a MEX-file in C (you may use a wrapper m-file as well, if you like). You do not have to implement all the capabilities of dgbmv.f, it is sufficient that the MEX-file can handle y = B * x, where x and y are vectors and B is a square band matrix. It should be possible for the upper and lower bandwidths to be different.
Call your routine from Matlab and test it on some simple examples so you can see that everything works correctly.
Run your routine from Matlab and check the speedup compared to the sparse version. You must probably use GotoBLAS to get any speedup (in that case you need to use long integers (and not int) in your mex-file).

If you change the C-code and recompile, you (may) have to clear the mex-function to see the changes. In help clear it says that     clear functions   removes all compiled M- and MEX-functions.


Update 2011-04-14: I have tested this on my own 32-bit machine and on the math 64-bit compute server with no problems. Of course there is a problem on the student machines :-(
You can read pages 126-133, in the handouts, but do not read pages 134-136 (they are not wrong, but they are not relevant on the student machines).
Page 132 has to be done in a slightly different way, this is how you do it (the Handouts have been updated). Suppose that the C-routine is called bandsolve.c (as before) and that you have access to Fortran source files for the Lapack-routines. It does not work with the Lapack library (as on page 132), Matlab crashes hard.

cp /chalmers/sw/sup/matlab-2009b/bin/mexopts.sh .

Edit mexopts.sh and search for glnxa64

change
  CFLAGS='-ansi -D_GNU_SOURCE'
to
  CFLAGS='-Wall -std=c99 -D_GNU_SOURCE'

You can choose between using g95 (default) and gfortran. If you use g95, continue editing and change

    CLIBS="$CLIBS -lstdc++"
  to
    CLIBS="$CLIBS -L. -lstdc++"

Then save the file and give the Linux-command:

  ln -s /usr/lib64/libstdc++.so.6.0.8 libstdc++.so

If you want to use gfortran instead, change FC to

  FC='gfortran'

none of the libstdc++ above is necessary in this case.

Now try to compile your C-file and Fortran files.

mex -f ./mexopts.sh bandsolve.c *.f

provided *.f would be the necessary Fortran-files.

Back