Calling LAPACK in MEX files
Mostrar comentarios más antiguos
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!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!