Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Intel MKL's call from mex-files are crashing MATLAB

Cutter_S_
Beginner
794 Views

Hello,

this is an issue already discussed but I have not been able to find a solution. I'm using Intel MKL 10.2 and MATLAB R2011b. My C function, which has many MKL BLAS and MKL Lapack calls, runs well when called from a C program.

When called from a mex file, MATLAB crashs. If I comment all the calls to mkl, it works (but output array are all zeros, as I don't perform calculations), so it is the call to Intel mkl that crashs matlab. I downlaoded yesterday the lastest version of Intel MKL but it did'nt solve the problem.

From what I can read here https://software.intel.com/en-us/articles/intel-math-kernel-library-intel-mkl-for-windows-using-intel-mkl-in-matlab-executable-mex-files

there are 2 possible causes:

1) The mex file is linked to the static threading library

or

2) The mex file is linked to the Intel MKL 10.3.x (or later) dynamic libraries. When the mex file is invoked, MATLAB crashes.. This occurs when the Intel MKL dynamic libraries used by the mex file are conflicting with those used by MATLAB. To resolve the problem, use static linking or link to a custom dynamic library and link to the dynamic threading library

I don't use the threading library, as I plan to use the matlabpool function later so I'm guessing the problem is 2). However, I use static linking:

mex mx_func_test.c -I/.../mkl/include -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lmkl_lapack -lrt -lm (I removed -lmkl_lapack when used MKL 11 because I doesn't exist anymore).

In MATLAB version -blas and -lapack return : Intel MKL 10.3.2.

So my questions are:

- is it possible to link my MKL call (in my C code) to the Intel MKL lib included in MATLAB (-> 10.3.2) ?

- or, is it possible to change the linking in MATLAB so it uses 10.2 or 11 ? It seems possible from what I read here but the procedure is not clear :http://www.mathworks.com/matlabcentral/newsreader/view_thread/305856

I hope I won't have to change my code to use ATLAS instead :(

 

0 Kudos
6 Replies
mecej4
Honored Contributor III
795 Views

One cause of crashes that you can take steps to avoid is that the MKL shared libraries called by Matlab are not the same as the ones used when the MEX file was built. Among others, there are two ways to proceed: (i) build the MEX using the MKL shared libraries that came with Matlab, and (ii) build the MEX using a different and independently obtained version of MKL. To do the latter, you may have to do the build from a shell window instead of using the mex script.

In either case, you have to make sure that the value of $LD_LIBRARY_PATH is such that the shared library used to build the MEX file is also the first one to be encountered by searching along the paths in LD_LIBRARY_PATH.

0 Kudos
Cutter_S_
Beginner
795 Views

mecej4 wrote:

One cause of crashes that you can take steps to avoid is that the MKL shared libraries called by Matlab are not the same as the ones used when the MEX file was built. Among others, there are two ways to proceed: (i) build the MEX using the MKL shared libraries that came with Matlab, and (ii) build the MEX using a different and independently obtained version of MKL. To do the latter, you may have to do the build from a shell window instead of using the mex script.

In either case, you have to make sure that the value of $LD_LIBRARY_PATH is such that the shared library used to build the MEX file is also the first one to be encountered by searching along the paths in LD_LIBRARY_PATH.

Thanks. For solution (i), where do I find the the MKL shared libraries that came with Matlab ??

0 Kudos
mecej4
Honored Contributor III
795 Views

Thanks. For solution (i), where do I find the the MKL shared libraries that came with Matlab

They are in the main Matlab bin/ directory. For example, on openSuse x64 and Matlab 2007b, I find .../ML2007b/bin/glnxa64/mklcompat.so and .../ML2007b/bin/glnxa64/mkl.so

0 Kudos
Cutter_S_
Beginner
794 Views

mecej4 wrote:

Quote:

Thanks. For solution (i), where do I find the the MKL shared libraries that came with Matlab

They are in the main Matlab bin/ directory. For example, on openSuse x64 and Matlab 2007b, I find .../ML2007b/bin/glnxa64/mklcompat.so and .../ML2007b/bin/glnxa64/mkl.so

Alright, I have these two .so files. Can you give me an exemple of how your link mex to those files when compiling ?

Thanks

EDIT: I made a mistake on my post, as written in the compilation line, when compiling with mex I link with dynamic MKL libraries because I have not been able to link with static libraries. The mex script doesn't recognize the -Wl, --start-group .... command.

0 Kudos
mecej4
Honored Contributor III
795 Views

You can see an example in this month-old post: https://software.intel.com/en-us/forums/topic/518312#comment-1794382. In short, the compiler options for x64 are:

    -mkl -fpic -shared -I <path to Matlab include subdirectory> <source files> -L <path to Matlab bin directory> lmex -lmx -o <mexname>.mexa64

0 Kudos
Cutter_S_
Beginner
794 Views

mecej4 wrote:

You can see an example in this month-old post: https://software.intel.com/en-us/forums/topic/518312#comment-1794382. In short, the compiler options for x64 are:

    -mkl -fpic -shared -I <path to Matlab include subdirectory> <source files> -L <path to Matlab bin directory> lmex -lmx -o <mexname>.mexa64

 

Thanks, I'll try this. In the meantime, I managed to make it work by compiling with static libraries and to avoid the -Wl, --start-group that is not recognized by mex, I set all the combinaisons of lib_mklcore.a, lib_intel_lp64.a and lib_mkl_sequential.a in the command line. It looks pretty ugly but it works!

0 Kudos
Reply