Académique Documents
Professionnel Documents
Culture Documents
Pascal Getreuer
MATLAB C/MEX
1 / 21
What is MEX?
MEX-functions are programs written in C, C++, or Fortran code
that are callable from Matlab. We will focus on C.
MEX provides C functions to manipulate Matlab variables, for
example
C/MEX
Meaning
mxCreateDoubleMatrix Create a 2D double array
mxGetPr
Get pointer to array data
mxGetDimensions
Get array dimensions
mxGetClassID
Determine variables datatype
MATLAB C/MEX
2 / 21
Words of Warning
C
Matlab
C ++
Fortran
MEX
the spooky beast
My advice:
1
Try to optimize your M-code first
2
Consider porting the entire application to C/C++
3
Use MEX to substitute only the bottleneck M-functions
MATLAB C/MEX
3 / 21
Getting Started
Hello, world!
hello.c
#include "mex.h"
MATLAB C/MEX
4 / 21
Getting Started
On the Matlab console, compile hello.c with
>> mex hello.c
MATLAB C/MEX
5 / 21
Notation:
Meaning
Number of output variables
Array of mxArray pointers to the output variables
Number of input variables
Array of mxArray pointers to the input variables
lhs = left-hand side (output variables)
rhs = right-hand side (input variables)
MATLAB C/MEX
6 / 21
M-code equivalent
nlhs
plhs
nrhs
prhs
nargout
varargout
nargin
varargin
Notation:
MATLAB C/MEX
6 / 21
nrhs = 3
prhs[0] points to A
prhs[1] points to B
prhs[2] points to C
MATLAB C/MEX
7 / 21
MATLAB C/MEX
8 / 21
normalizecols
Objective: Given matrix A, construct matrix B according to
Bm,n = Am,n / kAn kp
where kAn kp is the `p norm of the nth column.
M-function implementation
normalizecols.m
function B = normalizecols(A, p)
if nargin < 2
p = 2;
end
[M, N] = size(A);
B = zeros(M, N);
for n = 1:N
% Build matrix B columnbycolumn
B(:,n) = A(:,n) / norm(A(:,n), p);
end
Pascal Getreuer (UCLA)
MATLAB C/MEX
9 / 21
MEX-function implementation
/* Always include this */
MATLAB C/MEX
10 / 21
prhs[0]
prhs[1]
plhs[0]
MATLAB C/MEX
11 / 21
prhs[0]
prhs[1]
plhs[0]
MATLAB C/MEX
11 / 21
normalizecols.c
#include "mex.h"
#define A IN
#define P IN
#define B OUT
prhs[0]
prhs[1]
plhs[0]
MATLAB C/MEX
12 / 21
Meaning
mxIsDouble(A IN)
True for a double array
mxIsComplex(A IN)
True if array is complex
mxIsSparse(A IN)
True if array is sparse
mxGetNumberOfDimensions(A IN) Number of array dimensions
mxGetNumberOfElements(A IN)
Number of array elements
For simplicity, lets require that A is a real 2D full double matrix.
if(mxIsComplex(A IN) | | mxGetNumberOfDimensions(A IN) != 2
| | mxIsSparse(A IN) | | !mxIsDouble(A IN))
mexErrMsgTxt("Sorry! A must be a real 2D full double matrix.");
MATLAB C/MEX
13 / 21
% nrhs == 1
% nrhs == 2
(use p = 2.0)
MATLAB C/MEX
14 / 21
normalizecols.c
#include "mex.h"
#define A IN
#define P IN
#define B OUT
prhs[0]
prhs[1]
plhs[0]
MATLAB C/MEX
15 / 21
MATLAB C/MEX
16 / 21
*/
MATLAB C/MEX
17 / 21
normalizecols.c
#include "mex.h"
#define A IN
#define P IN
#define B OUT
prhs[0]
prhs[1]
plhs[0]
*/
MATLAB C/MEX
18 / 21
normalizecols: DoComputation
All that remains is to code the computation itself.
#include <math.h>
void DoComputation(double *B, double *A, int M, int N, double p)
{
double colnorm;
int m, n;
for(n = 0; n < N; n++)
{
/* Compute the norm of the nth column */
for(m = 0, colnorm = 0.0; m < M; m++)
colnorm += pow(A[m + M*n], p);
colnorm = pow(fabs(colnorm), 1.0/p);
/* Fill the nth column of B */
for(m = 0; m < M; m++)
B[m + M*n] = A[m + M*n]/colnorm;
}
}
MATLAB C/MEX
19 / 21
MATLAB C/MEX
20 / 21
More Information
Please see Writing Matlab C/MEX Code on my webpage
www.math.ucla.edu/getreuer
MATLAB C/MEX
21 / 21