1

The question in the header summarizes what I aim to achieve, which is more precisely detailed as below.

The goal is to compile C++ based mex files that rely on Intel MKL function calls (e.g. matrix inverse calculation).

In order to do so, I would like to ensure that I use the exact same Intel MKL libraries which MATLAB is shipped with, so as to avoid any compatibility issues. In this particular case, this is:

>> version('-blas')

ans =

    'Intel(R) Math Kernel Library Version 2018.0.3 Product Build 20180406 for Intel(R) 64 architecture applications, CNR branch AVX
     '

>> version('-lapack')

ans =

    'Intel(R) Math Kernel Library Version 2018.0.3 Product Build 20180406 for Intel(R) 64 architecture applications, CNR branch AVX
     Linear Algebra PACKage Version 3.7.0
     '

Warning: the above Intel MKL BLAS & LAPACK are not the same as the ones that are available for download from Intel’s official website. The latter ones I would prefer not to use for the above-mentioned potential compatibility reasons.

In which MATLAB folder(s) are the above reference static/dynamic Intel MKL libraries are located?

I have extensively searched after them in the many MATLAB folders, but I unfortunately could not find them. It seems that they are ‘buried’ somewhere deep in MATLAB.

How is it possible to do this at all?

My setup: Windows 10, MATLAB R2091b, Intel MKL.

I am very grateful for any help. Thank you in advance.

rawpointer
  • 105
  • 5
  • This old thread provides some answers to my question. https://stackoverflow.com/questions/20623308/where-to-locate-the-intel-mkl-in-matlab However, I am still wondering if there are updates on this topic and potential best practices since that time. – rawpointer Jan 20 '21 at 10:20
  • 1
    Do you know how to use mingw to compile and link against a dll ? It is sufficient the proper dll be on the system path. Please read [this](https://dev.my-gate.net/2018/06/02/generate-a-def-file-from-a-dll/). – rahnema1 Jan 20 '21 at 10:48
  • @rahnema1 thanks for your quick answer. No, I haven't done it before, but according to your link it should be possible. Please correct me if I am wrong, but do I understand you correctly, your suggestion is to use mingw64 for turning (i.e. reverse engineering) the mkl.dll located in [matlabroot]\bin\ARCH path to a static .lib file? – rawpointer Jan 20 '21 at 10:58
  • 1
    That is not actually a static library. It is named _import library_ that is used to dynamically link against a .dll. You need to compile some examples to learn working with mingw and gcc. – rahnema1 Jan 20 '21 at 11:31
  • @rahnema1 Yes, your are absolutely right! _Import library_ is the correct name. Sorry for me being not that punctual with the use of terminology. – rawpointer Jan 20 '21 at 11:37
  • @rahnema1, you seem to be very knowledgeable in this domain. Please let me ask you a few related questions. Do you think if it is safe to use a higher version of mingw64 that is shipped with the gendef.exe? This would be ‘just’ an intermediate step for generating the .def file. Or would that lead to compatibility problems later? – rawpointer Jan 20 '21 at 12:58
  • @rahnema1 Reason for asking is that I've been trying to use the gendef.exe, and to compile and link against the mkl.dll (as described in the link you provided), however the mingw64 edition (x86_64-6.3.0-posix-seh-rt_v5-rev2) that this version of MATLAB supports, does not contain the gendef.exe in its bin folder. [Link](https://www.mathworks.com/matlabcentral/answers/575380-failed-to-download-mingw-6-3-0-for-2020a-matlab) – rawpointer Jan 20 '21 at 12:58
  • 1
    It is safe to use higher version of gendef.exe to generate .def files. – rahnema1 Jan 20 '21 at 13:11
  • @rahnema1 - thank you! I appreciate your answer. – rawpointer Jan 20 '21 at 13:13
  • 1
    Note that in the case of MKL the mingw compiler may not work and you may need to use other compilers. – rahnema1 Jan 20 '21 at 15:36
  • @rahnema1 thanks again for the reply. Yes, that's a valid point. Fortunately, it worked with the mingw64 compiler, so I could generate the .def file without any problem. Then I also found another great description how to create a dll using MSVC Command Prompt. [Link.](https://asawicki.info/news_1420_generating_lib_file_for_dll_library) In the third step, I wanted to compile the mexw64 file with the previously built .lib + corresponding .dll files using MATLAB's mex command which unfortunately led to MKL and Armadillo related error messages. So, this is the point where I got stuck now. – rawpointer Jan 20 '21 at 17:06
  • 1
    A better approach may be to compile a standalone armadillo+mkl application and if it works make it as a mex. – rahnema1 Jan 20 '21 at 17:58
  • @rahnema1 I like your pedagogical approach! as you comment by comment decrement the amount of spoon-feeding to hints! :-) But don't let me alone in the dark yet! To be more serious. Do you mean creating a compiled armadillo+mkl static lib or dynamic + import libs then link them to MATLAB by invoking the mex command? Or do you rather mean abandoning MATLAB's mex command way of building mex files. I think of something similar how it is written here. [link](https://www.mathworks.com/matlabcentral/answers/377799-compiling-mex-files-without-the-mex-command) – rawpointer Jan 20 '21 at 18:15
  • 1
    I mean first don't use mex. Create a simple armadillo+mkl applicaton .exe file. If it works continue to create more complicated mex. – rahnema1 Jan 20 '21 at 18:26
  • @rahnema1 Thanks for the hint. I will try it asap. One side-effect is that the C++ code is not standalone because it is a bit involved for-loop integrated in a legacy MATLAB code base. So as to create an .exe I will have to setup everything, but it should work but takes time. – rawpointer Jan 20 '21 at 19:20

1 Answers1

1

On my Win64 machine I find them here

[matlabroot '/extern/lib/win64/microsoft']

and here

[matlabroot '/extern/lib/win64/mingw64']

The BLAS library is named libmwblas.lib, and the LAPACK library is named libmwlapack.lib

For reference, note that in R2007a and earlier, The Mathworks shipped the BLAS and LAPACK libraries as a single combined library. They weren't shipped as two separate libraries until R2007b and later.

James Tursa
  • 2,242
  • 8
  • 9
  • Thanks @James Tursa for the answer. How could you compile mex files with them, please? Maybe I miss another necessary compiler flag or something else. I have also found them since posting my original question; however, I could not link with them somehow. Furthermore, I was not even sure if they were the proper ones for Intel MKL. There is also another file called mkl.dll in the below directory. [matlabroot '/bin/win64'] In my opinion, this is a runtime library for MATLAB itself, right? – rawpointer Jan 28 '21 at 07:13