External Interfaces/API    

Using LAPACK and BLAS Functions

LAPACK is a large, multiauthor Fortran subroutine library that MATLAB uses for numerical linear algebra. BLAS, which stands for Basic Linear Algebra Subroutines, is used by MATLAB to speed up matrix multiplication and the LAPACK routines themselves. The functions provided by LAPACK and BLAS can also be called directly from within your C MEX-files.

This section explains how to write and build MEX-files that call LAPACK and BLAS functions. It provides information on

It also provides a symmetric indefinite factorization example that uses functions from LAPACK.

Specifying the Function Name

When calling an LAPACK or BLAS function, some platforms require an underscore character following the function name in the call statement.

On the PC, IBM_RS, and HP platforms, use the function name alone, with no trailing underscore. For example, to call the LAPACK dgemm function, use

On the SGI, LINUX, Solaris, and Alpha platforms, add the underscore after the function name. For example, to call dgemm on any of these platforms, use

Passing Arguments to Fortran from C

Since the LAPACK and BLAS functions are written in Fortran, arguments passed to and from these functions must be passed by reference. The following example calls dgemm, passing all arguments by reference. An ampersand (&) precedes each argument unless that argument is already a reference.

Handling Complex Numbers

MATLAB stores complex numbers differently than FORTRAN. MATLAB stores the real and imaginary parts of a complex number in separate, equal length vectors, pr and pi. FORTRAN stores the same number in one location with the real and imaginary parts interleaved.

As a result, complex variables exchanged between MATLAB and the FORTRAN functions in LAPACK and BLAS are incompatible. MATLAB provides conversion routines that change the storage format of complex numbers to address this incompatibility.

Input Arguments.   For all complex variables passed as input arguments to a FORTRAN function, you need to convert the storage of the MATLAB variable to be compatible with the FORTRAN function. Use the mat2fort function for this. See the example that follows.

Output Arguments.   For all complex variables passed as output arguments to a FORTRAN function, you need to do the following.

  1. When allocating storage for the complex variable, allocate a real variable with twice as much space as you would for a MATLAB variable of the same size. You need to do this because the returned variable uses the FORTRAN format, which takes twice the space. See the allocation of zout in the example that follows.
  2. Once the variable is returned to MATLAB, convert its storage so that it is compatible with MATLAB. Use the fort2mat function for this.

Example - Passing Complex Variables.   The example below shows how to call an LAPACK function from MATLAB, passing complex prhs[0] as input and receiving complex plhs[0] as output. Temporary variables zin and zout are used to hold prhs[0] and plhs[0] in FORTRAN format.

Building the MEX-File

The examples in this section show how to compile and link a C MEX file, myCmexFile.c, on the platforms supported by MATLAB. In each example, the term <matlab> stands for the MATLAB root directory.

If you build your C MEX-file on a PC or IBM_RS platform, you need to explicitly specify a library file to link with.

On the PC, use either one of the following.

For IBM_RS, use the following syntax.

On all other platforms, you can build your MEX-file as you would any other C MEX-file. For example,

Example - Symmetric Indefinite Factorization Using LAPACK

You will find an example C MEX-file that calls two LAPACK functions in the directory <matlab>/extern/examples/refbook, where <matlab> stands for the MATLAB root directory. There are two versions of this file.

The output files can be found in the same directory and have the filename xc_utdu_slv with the usual MEX-file extensions.

An M-file wrapper, utdusolve.m, provides help and allows you to call


 Memory Management Debugging C Language MEX-Files