segmentation violation using armadillo pinv() mex file MATLAB 2013a

1 view (last 30 days)
Hi everybody, I am having issues using armadillo's pinv(arma::mat m) function in my mex file (created using the legacy code tool). Here is an example of what I'am trying to do
arma::mat unit(2,2);
arma::mat p;
unit.randu();
unit.print();
p = pinv(unit);
p.print();
When I perform the peudo inverse and the assignment I get a segmentation violation error causing matlab to crash. I cannot understand why this is appening. Even because the following code, actually works
arma::mat unit(2,2);
arma::mat p;
unit.randu();
unit.print();
pinv(unit);
But obviously I cannot access to the pseudoinverse. Can anyone help me? Is very important and I cannot find any information on the matter. Thank you very much Andrea
  1 Comment
Andrea
Andrea on 17 Oct 2013
Edited: Andrea on 17 Oct 2013
There is an update. The problem is caused by the file /usr/local/MATLAB/R2013a/bin/glnxa64/mkl.so Here are the firt rows of the stacktrace
[ 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
It is clear now that the problem is matlab. Apparently armadillo does not use the system lapack libraries but it uses the "mkl_lapack_dlange" loaded by matlab. I'm trying to solve the problem. The options are
  1. compiling statically the lapack and blas libraries in armadillo
  2. rename the referred libary and add a symbolic link to the lapack library.
If anyone has got a better way of solving this problem please let me know.
thank you
Andrea

Sign in to comment.

Accepted Answer

Andrea
Andrea on 17 Oct 2013
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

More Answers (1)

Matthias
Matthias on 4 Jan 2014
I have to mention that it is actually not necessary to use the systems "blas+lapack" libraries. For exactly your problem Armadillo offers the define flags "ARMA_BLAS_LONG" and "ARMA_BLAS_LONG_LONG". You can use these flags to define the correct array element sizes that serve as parameters for blas and lapack routines. In my case (also Ubuntu 12.04 64bit) I compile the mex files with "-DARMA_BLAS_LONG" and link them with "-lmwlapack -lmwblas". Afterwards everything works fine.
You may have a look at the "config.hpp" and "typedef_blas_int.hpp" headers of the armadillo library.

Categories

Find more on Software Development Tools in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!