Calling LAPACK in MEX files
9 views (last 30 days)
Show older comments
Hi,
I am trying to write mex files with LAPACK calls, but I keep getting mysterious runtime issues. (MATLAB compiles the mex file, but then while running just crashes with no message.) I looked over my code compared against examples of dgemm and dgesv and I think I'm doing everything right (but there is no dsyev to compare against as far as I'm aware.)
Here is my mex code, which is in projections.c
#if !defined(_WIN32)
#define dsyev dsyev_
#define dgemm dgemm_
#endif
#include <stdlib.h>
#include<stdio.h>
#include "mex.h"
#include "lapack.h"
#include "blas.h"
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]);
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char *vectors = "N";
char *uplo = "U";
double *inMatrix;
double *outMatrix;
double *eigenvalues;
mwSize m,n;
int info, lwork;
int i,j;
double * work;
double wkopt;
inMatrix = mxGetPr(prhs[0]);
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
eigenvalues = (double*)malloc(n*sizeof(double));
plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
outMatrix = mxGetPr(plhs[0]);
memcpy(outMatrix, inMatrix, m*n*sizeof(double));
lwork = -1;
dsyev(vectors, uplo, &n, outMatrix, &n, eigenvalues, &wkopt, &lwork, &info);
}
and here is my matlab call
lapacklib = fullfile(matlabroot,'extern','lib',computer('arch'),'microsoft',...
'libmwlapack.lib');
mex('-largeArrayDims', '../c/projections.c',lapacklib) %so far so good
n = 10
X = randn(n); X = X + X';
Y = projections(X) %here MATLAB crashes
Any idea what I'm doing wrong? I checked to make sure I'm not running an in place operation on the input pointer, but since the matrixdivide.m example does the inplace on the output pointer, I figured that would be ok. Any tips would be appreciated!
0 Comments
Accepted Answer
James Tursa
on 4 Nov 2015
Some observations:
1) You are using mwSize for the type of integer to use for the LAPACK arguments. Don't do this since this can result in a mismatch between what mwSize is and what the LAPACK/BLAS library functions expect. You should use mwSignedIndex or ptrdiff_t instead. E.g.,
ptrdiff_t m, n;
ptrdiff_t info, lwork;
2) As written, you have a memory leak for eigenvalues since you use malloc instead of mxMalloc.
3) You are using lwork = -1, which means only a "workspace query". So no eigenvalues will be calculated, only the size of an optimal work array. I assume this was your intent and the only thing you are trying to get working right now is getting that optimal value calculated.
4) I guess I am a little surprised that you don't have to link with libmwblas.lib also. What version of MATLAB are you using? 32-bit or 64-bit?
5) Why do you have a prototype for mexFunction in your code?
6) You use the memcpy function but do not include its header to get the prototype. You should have this in your code:
#include <string.h>
I assume what you have posted is only for the purpose of getting the optimal lwork value, and that you intend to add more code (i.e. another dsyev call) to calculate the eigenvectors/eigenvalues themselves and return them to the workspace.
More Answers (0)
See Also
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!