How to revert MATLAB back to it's default BLAS and LAPACK libraries after setting environment variables in Linux?

14 views (last 30 days)
Ubuntu 15.04 64bit
I have a C++ mex file that I have written using the armadillo C++ library with openBLAS that performs well in the terminal, but leads to segmentation faults in matlab. Mex links with the correct openBLAS library when compiled, but during runtime, it tries to use matlab's proprietary mkl.so library, which leads to hard crashes without any helpful stack trace. I have ran and debugged the mex file using gdb to confirm that it is trying to use mkl.so instead of libopenblas.so like I want it to.
(gdb) where
#0 0x00007fffb35c3030 in mkl_blas_avx2_xdgemv ()
from /usr/local/MATLAB/R2015a/bin/glnxa64/mkl.so
#1 0x00007fffb2637755 in dgemv_ ()
from /usr/local/MATLAB/R2015a/bin/glnxa64/mkl.so
#2 0x00007fffcc93f638 in gemv<double> (incy=0x7fffdbff4884, y=0x7fffdbff6790,
beta=0x7fffdbff4890, incx=0x7fffdbff4884, x=0x7fffdbff4cc0,
ldA=0x7fffdbff487c, A=<optimized out>, alpha=0x7fffdbff4888,
n=0x7fffdbff4880, m=0x7fffdbff487c, transA=0x7fffdbff487b "N\006")
at /usr/local/include/armadillo_bits/blas_wrapper.hpp:34
I have kind of fixed this using a workaround I have found online that uses environment variables in linux to force the mexfile to using the libraries I want at runtime.
setenv('BLAS_VERSION','/usr/lib/libopenblas.so');
setenv('LAPACK_VERSION','/usr/lib/liblapack.so');
Y = mymex(X,F,G,H);
setenv('BLAS_VERSION','/usr/local/MATLAB/R2015a/bin/glnxa64/mkl.so')
setenv('LAPACK_VERSION','/usr/local/MATLAB/R2015a/bin/glnxa64/mkl.so')
This allows me to run my mex file like I want, but the last two lines where I try to set matlab back to using its default libraries doesn't seem to be working as intended. Doing certain operations involving '\' after running my mex file will result in matlab crashing.
Stack Trace (from fault):
[ 0] 0x00007f7646582690 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmathlinalg.so+01369744
[ 1] 0x00007f76465802ac /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmathlinalg.so+01360556
[ 2] 0x00007f764658a9ad /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmathlinalg.so+01403309
[ 3] 0x00007f764658abbb /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmathlinalg.so+01403835
[ 4] 0x00007f767e9fe305 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00557829
[ 5] 0x00007f767e9e5744 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00456516 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+00000244
[ 6] 0x00007f764658d040 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmathlinalg.so+01413184
[ 7] 0x00007f767e9fe305 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00557829
[ 8] 0x00007f767e9e5744 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00456516 _ZN13Mfh_MATLAB_fn11dispatch_fhEiPP11mxArray_tagiS2_+00000244
[ 9] 0x00007f767dcacd20 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04189472
[ 10] 0x00007f767dc5c432 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03859506
[ 11] 0x00007f767dc5e612 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03868178
[ 12] 0x00007f767dc64597 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03892631
[ 13] 0x00007f767dc5fcff /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03874047
[ 14] 0x00007f767dc60934 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03877172
[ 15] 0x00007f767dcd62ce /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04358862
[ 16] 0x00007f767ea3caea /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00813802 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000762
[ 17] 0x00007f767ea3cfb0 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00815024 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000032
[ 18] 0x00007f767dcacd20 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04189472
[ 19] 0x00007f767dc5c432 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03859506
[ 20] 0x00007f767dc5e612 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03868178
[ 21] 0x00007f767dc64597 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03892631
[ 22] 0x00007f767dc5fcff /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03874047
[ 23] 0x00007f767dc60934 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03877172
[ 24] 0x00007f767dcd62ce /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04358862
[ 25] 0x00007f767ea3caea /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00813802 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00000762
[ 26] 0x00007f767ea3cfb0 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00815024 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000032
[ 27] 0x00007f767dcacd20 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04189472
[ 28] 0x00007f767dc2d4b3 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03667123
[ 29] 0x00007f767dc5d6ee /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03864302
[ 30] 0x00007f767dc64597 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03892631
[ 31] 0x00007f767dc5fcff /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03874047
[ 32] 0x00007f767dc60934 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03877172
[ 33] 0x00007f767dcd62ce /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04358862
[ 34] 0x00007f767ea3cc39 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00814137 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00001097
[ 35] 0x00007f767ea3cfb0 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00815024 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000032
[ 36] 0x00007f767dcacd20 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04189472
[ 37] 0x00007f767dc2d4b3 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03667123
[ 38] 0x00007f767dc5d6ee /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03864302
[ 39] 0x00007f767dc64597 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03892631
[ 40] 0x00007f767dc5fcff /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03874047
[ 41] 0x00007f767dc60934 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03877172
[ 42] 0x00007f767dcd62ce /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04358862
[ 43] 0x00007f767ea3cc39 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00814137 _ZN8Mfh_file16dispatch_fh_implEMS_FviPP11mxArray_tagiS2_EiS2_iS2_+00001097
[ 44] 0x00007f767ea3cfb0 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_dispatcher.so+00815024 _ZN8Mfh_file11dispatch_fhEiPP11mxArray_tagiS2_+00000032
[ 45] 0x00007f767dc95495 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+04093077
[ 46] 0x00007f767dc55fb9 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03833785
[ 47] 0x00007f767dc523f5 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03818485
[ 48] 0x00007f767dc52ad3 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwm_interpreter.so+03820243
[ 49] 0x00007f767f948b6c /usr/local/MATLAB/R2015a/bin/glnxa64/libmwbridge.so+00228204
[ 50] 0x00007f767f949751 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwbridge.so+00231249 _Z8mnParserv+00000737
[ 51] 0x00007f768ad4f3af /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00799663 _ZN11mcrInstance30mnParser_on_interpreter_threadEv+00000031
[ 52] 0x00007f768ad2f133 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00667955
[ 53] 0x00007f768ad30fd9 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00675801 _ZN5boost6detail11task_objectIvNS_3_bi6bind_tIvPFvRKNS_8functionIFvvEEEENS2_5list1INS2_5valueIS6_EEEEEEE6do_runEv+00000025
[ 54] 0x00007f768ad319a7 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00678311 _ZN5boost6detail9task_baseIvE3runEv+00000071
[ 55] 0x00007f768ad31a07 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00678407
[ 56] 0x00007f768ad2cd8a /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00658826
[ 57] 0x00007f767cfe8a56 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwuix.so+00334422
[ 58] 0x00007f767cfd03a2 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwuix.so+00234402
[ 59] 0x00007f768b51df1f /usr/local/MATLAB/R2015a/bin/glnxa64/libmwservices.so+02621215
[ 60] 0x00007f768b51e08c /usr/local/MATLAB/R2015a/bin/glnxa64/libmwservices.so+02621580
[ 61] 0x00007f768b51fbdf /usr/local/MATLAB/R2015a/bin/glnxa64/libmwservices.so+02628575
[ 62] 0x00007f768b52061c /usr/local/MATLAB/R2015a/bin/glnxa64/libmwservices.so+02631196 _Z25svWS_ProcessPendingEventsiib+00000092
[ 63] 0x00007f768ad2d408 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00660488
[ 64] 0x00007f768ad2d724 /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00661284
[ 65] 0x00007f768ad19b0d /usr/local/MATLAB/R2015a/bin/glnxa64/libmwmcr.so+00580365
[ 66] 0x00007f7689eb66aa /lib/x86_64-linux-gnu/libpthread.so.0+00030378
[ 67] 0x00007f7689bebeed /lib/x86_64-linux-gnu/libc.so.6+01076973 clone+00000109
[ 68] 0x0000000000000000 <unknown-module>+00000000
My code does run using this crude workaround, but I would still appreciate if anyone has a better solution to force matlab to use the BLAS/LAPACK library of my choice for my mexfile, then go back to normal matlab operation afterwords without having to exit matlab.
  2 Comments
Andrew
Andrew on 11 Jun 2015
OK So I ended up being able to answer this problem with myself and another coworker after much hairpulling, but I feel that it is best to put the answer out there so that no one else need go through this hassle of getting armadillo to work with matlab mex.
The solution to this problem was: do NOT use dynamic libraries for BLAS or LAPACK (i.e. openBLAS) in a MATLAB mex file. MATLAB just can't handle that. The solution to this problem is static linking of your libraries.
mex -v CXXFLAGS="\$CXXFLAGS -g -std=c++11 -largeArrayDims" -I/home/YOU/armadillo-x.xxx.x/include yoursourcfiles.cpp -lopenblas
I copy and pasted the libopenblas.a (it will not be named exactly this, but there is a pointer to it that is) static library from the folder where I built the library from source to the matlab folder with my source code. I also changed the
#include <armadillo>
to
#include "armadillo"
in the armaMex.hpp header since I am no longer linking to the dynamic library libarmadillo.so, but am including all of the armadillo header files with the -I/home/YOU/armadillo-.... argument.
The downside of this is that it makes the binary for my mex file slightly larger, but the upside is that it no longer crashes and/or breaks matlab. If this helps anyone else with this problem, cheers!

Sign in to comment.

Answers (0)

Categories

Find more on Troubleshooting in MATLAB Compiler SDK 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!