Practical ways to reduce overheads in MATLAB simulations
Abstract
MATLAB is now well-established as an
effective tool for performing numerical experiments and graphical
simulations. Its simple, high-level programming language allows rapid
development of new projects and facilitates debugging. However, a
high-level interpreted language such as MATLAB m-code cannot compete
in speed and memory efficiency with traditional compiled languages
such as Fortran and C. Even on a modern workstation, the capacity to
perform complex two and three-dimensional simulations is limited.
In this presentation we will investigate some of the possibilities for
alleviating bottlenecks in slow or memory intensive MATLAB programs.
We will briefly consider the use of the Profiler in identifying these
bottlenecks, the vectorisation of m-files, and the use of the MATLAB
Compiler. We will then look in detail at the MATLAB Application
Program Interface which allows one to incorporate C and Fortran
routines directly into MATLAB programs. I will describe how to write
and build MEX files using a simple example and will make a
quantitative cost comparison between the different strategies.
Click here to begin the presentation.
![[Made with StarOffice]](matlab/sologo.gif)
Sample MEX files.
Here are two real example MEX applications that demonstrate common
implementation issues. The programs are available in both C and
Fortran versions and are fully commented. All programs have been
tested on the Sun Workshop Compiler Version 4.0.
Example 1: d = find_min_dist(line_x,line_y,x,y). Given a
grid (x,y) and a piecewise continuous line (line_x,line_y), this
function will compute the minimum distance from each grid node to a
point on the line. The underlying algorithm is simple and the
function demonstrates basic error validation and the use of MATLAB
dense mxArrays (matrices). The Fortran version also demonstrates the
use of the %val compiler directive for passing arrays of unspecified
length.
Example 2: L = myluinc(A[,milu[,steps]]). Given a matrix A,
this function will compute the level zero incomplete LU factorisation
of the matrix. Optional arguments turn on modified incomplete
factorisation and specify the number of elimination steps to
perform. The algorithm is based on that presented in Owe Axelsson,
`Iterative Solution Methods', Cambridge 1996. The program
demonstrates how to manipulate sparse matrices, something which can be
challenging when unfamiliar. The program also demonstrates the use of
optional input and output arguments.
N.B. Fortran version does not compile on versions of gcc below
2.9.x, since before then gcc did not support the %val() directive.
|