0

I am trying to make a MEX file, so i can call LAPACK subroutine dgesv() to solve linear equations Ax=b.

If i comment the line when dgesv_ is called, i got results (for instance if i pass the values of nb to x, when i call the MEX file, i got the values to returned value in my matlab code)

The problem is when i call dgesv_ and i can't point out what creates the segmentation fault.

Here is the code (the important things for my question):

#include "mex.h"

/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
             int nrhs, const mxArray *prhs[])
{
    /* Variable Declaration */

    double *A;  /* input Array */
    double *b;  /* input Vector */

    double *nA;
    double *nb;
    double *x;  /* Output Vector */


    int M, N, NRHS, INFO, *IPIV, LDA, LDB, i;

    /* Check input arguments */
    if(nrhs!=2) 
    {
        mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs",
                      "Two inputs required.");
    }


    /* create a pointer to the real data in the input matrix  */
    A = mxGetPr(prhs[0]);

    /* create a pointer to the real data in the input vector  */
    b = mxGetPr(prhs[1]);

    /* Get Dims */
    M = mxGetM(prhs[0]);
    N = mxGetN(prhs[0]);

    NRHS=mxGetN(prhs[1]);

    LDA=M;
    LDB=M;

    /* Allocate mem for new arrays */
    nA = (double *)mxCalloc(M*N, sizeof(double));
    nb = (double *)mxCalloc(M*NRHS, sizeof(double));
    IPIV = (int* )mxCalloc(M,sizeof(int));


    /* Copy values new arrays */
    for (i=0; i<M*N; i++)
        nA[i]=A[i];

    for (i=0; i<M*NRHS; i++)
        nb[i]=b[i];


    /* Programm is tested on OS X Mavericks so dgesv_ instead of dgesv */
    dgesv_(&M, &NRHS, nA, &LDA, IPIV, nb, &LDB, &INFO);


    /* create the output vector */
    plhs[0] = mxCreateDoubleMatrix(M,NRHS,mxREAL);

    /* get a pointer to the real data in the output matrix */
    x = mxGetPr(plhs[0]);

    /* If INFO == 0 then we got a solve on nb, so pass it to x */
    if (INFO == 0)
        for(i=0;i<M*NRHS;i++) 
            x[i] = nb[i];
    else if (INFO < 0)
         /*Print something */

    else
         /* Print something */


    mxFree(nA);
    mxFree(nb);
    mxFree(IPIV);
}

Also i tried to call the dgesv_ without making new arrays (like this)

dgesv_(&M, &NRHS, A, &LDA, IPIV, b, &LDB, &INFO);

but i have the same problem.

I compile and i call the MEX file on MATLAB like this:

mex -v -largeArrayDims MEXfile.c -lmwlapack

A=rand(3);
b=rand(3,1);
x=MEXfile(A,b);

and i got segmentation fault.

oeoeo
  • 1
  • 1
  • Use [these instructions](http://stackoverflow.com/questions/23714141/preventing-a-mex-file-from-crashing-in-matlab/23714301#23714301) to set up debugging MEX files in MATLAB. Next, edit your post that pinpoints where exactly the seg fault is happening. The instructions I provided will allow you to step through your MEX code and it will tell you the exact line where the program crashes. In hindsight, I can't see anything wrong with your code. I also looked up the prototype for `dgesv` and it seems to conform with what you have. – rayryeng Dec 26 '14 at 21:27
  • Hard to help without knowing which version of OS X and Matlab you're using. What is full build command that `mex` uses (in your mexopts.h file and also printed out in verbose mode). Are you using the `std=C99` flag? I was not able to get your code to compile as is under R2014b and OS X 10.10.1. Does a segmentation fault occur every time? – horchler Dec 26 '14 at 23:44
  • @rayryeng i tried it before but i couldn't get xCode to debug it (i am not good at debugging with xcode). It seems that the problem is from *IPIV and its mxcalloc. If i change it to an array IPIV(M) then i get OUT OF MEMORY. – oeoeo Dec 27 '14 at 14:28
  • @horchler i am using OS X 10.9.3, matlab R2014a, compiler Xcode with Clang (i want to set gcc as compiler but i cant find it on matlab). I believe that may is a compilers fault, because i found an example with dgesv on the internet and i also get segmentation fault. I have segmentation fault every time, but as i said, if i change the IPIV declaration and malloc with int IPIV(M) i get error OUT OF MEMORY on matlab. – oeoeo Dec 27 '14 at 14:33
  • I don't know why you'd want to switch to an inferior compiler like gcc, especially on OS X. I asked about your full build command to see what other flags are being used. I'm guessing that this in being compiled with C99 (the examples you may find were probably written before then). Another thought: I never link just using generic `int` types when a function prototype is defined in terms of it's own custom typedefs. This could be part of the issue. Also, why don't you define any header in your mex file? And why is there an underscore after the function name – is that CLAPACK style? – horchler Dec 31 '14 at 18:19

0 Answers0