armadillo matlab mexfile segmentation fault

Hello everybody,
I am having issues writing a mex file for simulink using c++ and armadillo.
I'm using the Legacy Code Tool to compile a classical c++ file and use it as a block function in simulink.
Everything works fine except when I try to use the pinv() function provided by the armadillo library. I am trying to execute the following code:
1
2
3
4
5
6
    arma::mat unit(2,2);
    arma::mat p;
    unit.randu();
    unit.print();    
    p = pinv(unit);
    p.print();

But I obtain an error message by matlab saying that there has been a segmentation violation and then it asks me to end the program. The strange thing is if I compile the program in a file called test.cpp and compile it
g++ test.cpp -larmadillo I do not have any problem. Could you please help me to solve this problem?
Thanks
Andrea
This may be way off base, but I thought I'd mention it anyway.

If you are on a UNIX/Linux machine, be aware that there is almost always a program called "test" that is used in the shell. Make sure when you run your program that you specify ./test instead of just test.
Actually if I compile as said in my previous post that command does work. The problem is when I execute the simulink program with my S-function in it. Here the first rows of the stack trace provided by matlab
1
2
3
4
5
6
7
8
[  0] 0x00007ff96f8c46cb        /usr/local/MATLAB/R2013a/bin/glnxa64/mkl.so+06420171 mkl_lapack_dlange+00000187
[  1] 0x00007ff96f90c626        /usr/local/MATLAB/R2013a/bin/glnxa64/mkl.so+06714918 mkl_lapack_dgesvd+00006006
[  2] 0x00007ff96f3853e6        /usr/local/MATLAB/R2013a/bin/glnxa64/mkl.so+00918502 dgesvd+00000150
[  3] 0x00007ff966895292                   /usr/local/lib/libarmadillo.so.3+00017042 wrapper_dgesvd_+00000135
[  4] 0x00007ff966aa8277 /home/mago/Development/Repository/MIPWips/Heterogeneous/matlab/hetCon.mexa64+00062071
[  5] 0x00007ff966aae737 /home/mago/Development/Repository/MIPWips/Heterogeneous/matlab/hetCon.mexa64+00087863
[  6] 0x00007ff966aa29b3 /home/mago/Development/Repository/MIPWips/Heterogeneous/matlab/hetCon.mexa64+00039347
[  7] 0x00007ff966a9af11 /home/mago/Development/Repository/MIPWips/Heterogeneous/matlab/hetCon.mexa64+00007953

The impression I have is that armadillo, instead of using the system lapack library, uses the one loaded on matlab (mkl_lapack_dgesvd). I'm trying to overcome the problem. One solution I'm thinking about is to build armadillo again with lapack a blas statically compiled in it. But I cannot find the proper way of doing it (armadillo is not well documented at all, in my opinion). Can anybody tell me how to do it? Another solution is appreciated anyway :)
Thank you!


Here I am with a work around. The problem is caused by the fact that armadillo invokes lapack and blas with 32bit integer pointerd. Matlab instead uses the mkl version of the above libraries and invokes them with a 64bit pointer. This causes the segmentation fault. To avoid this problem it is necessary to export to environment variable that force matlab to use the system libraries. This can be done prompting the two following commands in the same terminal from which matlab is launched

export BLAS_VERSION="/usr/lib/libblas.so"
export LAPACK_VERSION="/usr/lib/liblapack.so"

this is the case of my machine (running ubuntu 12.04). Use the locate command to find the libraries on your system. However, this solution reduces the efficiency of matlab, therefore is just a workaround for the people that are forced to write S-Functions using armadillo (just like me). Hence, do no include this exports in your .bashrc file!

Thanks to everyone

Andrea
Sorry, other than the ubiquitous Southern animal, I don't know anything about armadillo. ;)

From the stack trace, it is showing the full paths to the libraries this process was using. Is there a way in Matlab to specify where to look elsewhere for the libraries?
I already posted the "solution". Thank you anyway for you attention. I would like also to let you know that I contacted the armadillo team to let them know about the bug.
Thank you
Andrea
Topic archived. No new replies allowed.