Magma in Matlab mex-files

Open discussion for MAGMA library (Matrix Algebra on GPU and Multicore Architectures)

Magma in Matlab mex-files

Postby jens_g » Fri Jul 22, 2016 11:29 am

Hello,

I'm new to Magma and I'm trying to use it in Matlab via a mex-files, but I ran into some troubles with it.
First of all, as far as I can tell everything is installed correctly (on a 64bit Linux machine) and all the tests and examples that come with Magma run fine. Also, when I put together some simple tests in C++ (i.e. creating random complex matrices and calcuting the eigenvalues with magma_zgeev) everything works. However, if I try to do the same test in a mex-file, Matlab completely crashes with a segmentation violation at the point where the mex-file calls magma_zgeev. Here's the stack trace:
Code: Select all
Stack Trace (from fault):
[  0] 0x00007f7f867b02fb          /opt/intel/mkl/lib/intel64/libmkl_avx2.so+10638075 mkl_lapack_ps_avx2_xzlange+00000187
[  1] 0x00007f7f64eda080        /usr/local/MATLAB/R2014b/bin/glnxa64/mkl.so+03129472 mkl_lapack_ps_xzlange+00000224
[  2] 0x00007f7f64d6280d        /usr/local/MATLAB/R2014b/bin/glnxa64/mkl.so+01591309 mkl_lapack_zlange+00001821
[  3] 0x00007f8035e1a623 /usr/local/MATLAB/R2014b/bin/glnxa64/../../sys/os/glnxa64/libiomp5.so+00558627 __kmp_invoke_microtask+00000147
[  4] 0x00007f8035df2f64 /usr/local/MATLAB/R2014b/bin/glnxa64/../../sys/os/glnxa64/libiomp5.so+00397156
[  5] 0x00007f8035df1e02 /usr/local/MATLAB/R2014b/bin/glnxa64/../../sys/os/glnxa64/libiomp5.so+00392706
[  6] 0x00007f8035e1a8d9 /usr/local/MATLAB/R2014b/bin/glnxa64/../../sys/os/glnxa64/libiomp5.so+00559321
[  7] 0x00007f80492f90a4                             /lib64/libpthread.so.0+00032932
[  8] 0x00007f804902e02d                                   /lib64/libc.so.6+00938029 clone+00000109


From this I gathered that Matlab is switching from it's own copy of MKL to Intel MKL, which I installed separately, and that there is some mismatch happening between the two implementations of MKL. I noticed that Matlab's MKL version is 11.1.1 and the one that is installed separately is version 11.3.3. So, I'm not quite sure what to do next from this point... I guess I could try to update Matlab to possibly get Matlab's MKL to version 11.3.3, but I would assume that that would not be an elegant way since Matlab would still be loading libraries from two different places. On the other hand, I read on the Mathworks forums that there are some ways to force Matlab into using the external MKL by setting some environment variables like LD_LIBRARY_PATH or BLAS_VERSION, but people from Mathworks support generally seem to discourage this method on their forums and I'm also not sure what exactly I should set them to in this case.
So is there some proper way to avoid such conflicts of the libraries when using Magma in Matlab?

Best regards,
Jens
jens_g
 
Posts: 2
Joined: Fri Jul 22, 2016 11:01 am

Re: Magma in Matlab mex-files

Postby mgates3 » Fri Jul 22, 2016 1:39 pm

Is it possible to link with the MKL that's in Matlab, i.e., /usr/local/MATLAB/R2014b/bin/glnxa64/mkl.so ?
I guess just list that specific file in LIB in MAGMA's make.inc.

I believe Matlab uses the ILP64 version of MKL, so you would need to compile MAGMA that way. See the sample make.inc.mkl-icc-ilp64 (or gcc).

NB: I have not tried to use MAGMA from Mex files, so this is just a suggestion to try out.

Searching for "Intel MKL with Matlab MEX" yields some results on both Intel's and MathWorks's sites. They seem a few years dated and OS-specific, but worth a look.

-mark
mgates3
 
Posts: 738
Joined: Fri Jan 06, 2012 2:13 pm

Re: Magma in Matlab mex-files

Postby jens_g » Mon Jul 25, 2016 10:38 am

Thanks mark, for your quick response.

I tried compiling MAGMA using Matlab's version of MKL, but I couldn't get it to work. Most of it compiled, but then it needed the headers mkl_services.h, which I coudln't find in the Matlab directories.

Anyways, I found a different solution, which I think could be interesting for other users as well: I now force Matlab to completely switch to the external MKL. This only needed these 3 environment variables:
Code: Select all
MKLROOT=/opt/intel/mkl/lib/intel64
export MKLROOT
BLAS_VERSION=libmkl_rt.so
export BLAS_VERSION
LAPACK_VERSION=libmkl_rt.so
export LAPACK_VERSION

Now everything seems fine and I can already run the first tests using MAMGMA from a mex-file :)

- Jens
jens_g
 
Posts: 2
Joined: Fri Jul 22, 2016 11:01 am

Re: Magma in Matlab mex-files

Postby mgates3 » Tue Jul 26, 2016 5:17 pm

Note that Matlab uses MAGMA in its parallel computing toolbox. See "help gpu".

However, using Mex files would give access to some additional MAGMA functionality.

-mark
mgates3
 
Posts: 738
Joined: Fri Jan 06, 2012 2:13 pm


Return to User discussion

Who is online

Users browsing this forum: No registered users and 5 guests