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

mkl with matlab mex interface

numerix1
Beginner
3,831 Views

Dear users:

I am having trouble interfacing my intel mkl code with Matlab using the mex interface. I have a code that does a certain factorization based on the pivoted QR decomposition. It works fine when I compile the code by itself. However, I would like to make a mex code for other users. I have done this before and have been able to compile simple mex files with icc and mkl. However, now I have a problem. Perhaps someone here has ideas for me to try. I am trying to use the pivoted QR decomposition function and this is where my code fails:

m = nrows; n = ncols;

data = (double*)mxCalloc(nrows*ncols,sizeof(double));

Iarr = (lapack_int*)mxCalloc(n,sizeof(lapack_int));
tauarr = (double*)mxCalloc(min(m,n),sizeof(double));


LAPACKE_dgeqp3(LAPACK_COL_MAJOR, (lapack_int)nrows, (lapack_int)ncols, data, (lapack_int)nrows, Iarr, tauarr);

sometimes it simply segfaults (when mex file is run inside matlab). Other times it complains that parameter 8 to dgeqp3 is incorrect.

Intel MKL ERROR: Parameter 8 was incorrect on entry to DGEQP3.

Parameter 8 is not present in the lapacke function, it is the info parameter in the fortran routines. Notice that I initialize space using mxCalloc vs regular C calloc..

https://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-4326D19C-246A-4A7B-9728-03D371FB39AD.htm

Here is how I compile my mex file:

#!/bin/bash

icc -c -DMX_COMPAT_32   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/apps/matlab2014a/extern/include" -I"/apps/matlab2014a/simulink/include" -fexceptions -fPIC -fno-omit-frame-pointer -openmp -shared -O -DNDEBUG mex_code1.c  -o mex_code1.o

icc -openmp -shared -L"/opt/intel/mkl/lib/intel64/" -I"/opt/intel/mkl/include/" /opt/intel/mkl/lib/intel64/libmkl_intel_lp64.a  /opt/intel/mkl/lib/intel64/libmkl_intel_thread.a /opt/intel/mkl/lib/intel64/libmkl_core.a /opt/intel/mkl/lib/intel64/libmkl_blas95_lp64.a  /opt/intel/mkl/lib/intel64/libmkl_lapack95_lp64.a -lmkl_rt -lmkl_core -lmkl_gnu_thread -lgomp -lpthread  -Wl,--no-undefined -Wl,-rpath-link,apps/matlab2014a/bin/glnxa64 -O -Wl,--version-script,"apps/matlab2014a/extern/lib/glnxa64/mexFunction.map" mex_code1.o   -L"apps/matlab2014a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o mex_code1.mexa64

rm -f mex_code1.o

I suspect the issue maybe in the compilation step (which compiles fine). As i mentioned, the code runs fine outside of the mex interface, when I replace back

all the mxCalloc calls by regular calloc.

thanks for your help

 

0 Kudos
45 Replies
mecej4
Honored Contributor III
397 Views

Numerix1, have you reached any resolution of the problem discussed in this thread?

I recently gained access to a PC with Matlab 2014a - X64 installed (Windows 8.1 X64 OS), and ran your example in #37. 

The output from Matlab:

the matrix is of size 3 x 5
call QR
first call to QR..
After first call to QR, info = 0 and lwork = -1
lwork from work array = 16
After Second call to QR, info = 0
printing result:

 4      1.64038       -4.3247      -2.1266       1.8133       0.4651      -0.9858
 3      1.22685       -0.1903      -2.9305       1.3075       1.3496      -0.0427
 1      0.00000        0.4278       0.7938      -1.9383      -0.7192      -0.2596

 

0 Kudos
numerix1
Beginner
397 Views

I never got it to work properly with Linux based installations (I tried a few different Linux boxes I had access to with different Matlab versions up to 2014b and a couple of different icc versions). At the end I wrote some code to pass data back and forth through Linux based sockets within a mex file (i.e. to avoid passing data through disk). This way I can call inside mex files external library functions (such as from mkl) but do not need to link mkl when compiling the mex file. The mkl code then runs completely separately from the mex code and passes results back to mex via a simple socket server when finished (so no issues with openmp, etc). You can find my code @MatlabMexSockets http://tinyurl.com/nk34t83

This is what I currently use in my project to interface mkl code and Matlab. It does obviously have some overhead for data i/o but that can be fairly fast if executing on local machine and if socket transfer settings are set properly. I plan to write another version later using another IPC approach instead of sockets.

Thanks for your help with this.

0 Kudos
Vladimir_E_Intel
Employee
397 Views

Hi  Numerix1,

I checked what happens if create mex-file with MKL LAPACKE routine.

During Matlab execution LAPACKE routine is invoked from MKL library but base routine (dgeqp3_ for LAPACKE_dgeqp3) is invoked from Matlab library libmwlapack.so. libmwlapack.so supports 8-byte integers. So call of LAPACKE routine with LP64 interface leads to crash.

There are 3 options:

1) Use ILP64 libraries when compile mex file. Please be careful and ensure that:

·         integer variables passed to MKL routine is declared as lapack_int or MKL_INT

·         compile with –DMKL_ILP64

·         link with ILP64 libraries

Link line for this case:

icc -g -DMKL_ILP64 -fpic -shared -I${MKLROOT}/include -I "${matlab_root}/extern/include/" test_mex5.c -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_intel_thread -lmkl_core -L"${matlab_root}/bin/glnxa64/" -lmex -lmx -o test_mex5.mexa64

 

2) Say matlab to use MKL libraries

export BLAS_VERSION=${MKLROOT}/lib/intel64/libmkl_rt.so

export LAPACK_VERSION=${MKLROOT}/lib/intel64/libmkl_rt.so

In this case all LAPACK routines will be invoked from MKL with specified interface (default is LP64,  export MKL_INTERFACE_LAYER=ILP64 to use ILP64 interface).

 

3) Also there is option to invoke LAPACK routine from libmwlapack.so. Great if there is no standalone MKL.

icc -fpic -shared -I "${matlab_root}/extern/include/" test_mex5.c -L"${matlab_root}/bin/glnxa64/" -lmwlapack -lmex -lmx -o test_mex5.mexa64

In this case you call dgeqp3_ from matlab library and need to pass 8-byte integers to routine.

Best Regards,

        Vladimir

 

0 Kudos
Vladimir_E_Intel
Employee
397 Views

I would add that I made these checks with 2013b Matlab version.

3.1) corrected version of option 3)

icc -DMKL_ILP64 -fpic -shared -I "${matlab_root}/extern/include/" test_mex5.c "${matlab_root}/bin/gln
xa64/"mkl.so -L"${matlab_root}/bin/glnxa64/" -lmex -lmx -o test_mex5.mexa64

0 Kudos
Reply