Using Intel® MKL in MATLAB Executable (MEX) Files

Note: This document applies to Intel® MKL 2017 and MATLAB* R2015b running on Windows*.

HHow can I configure MATLAB to run the Intel MKL 2017 at start-up?
By default, MATLAB R2015b uses Intel MKL 11.1 BLAS. To upgrade to the Intel MKL 2017 BLAS, follow the instructions in the document Using Intel MKL with MATLAB.

How do I link to Intel MKL with my MATLAB executable (mex) file?To avoid conflicts with Intel MKL 11.1 BLAS used by MATLAB R2015b, please follow the instructions in the document Using Intel MKL with MATLAB when calling Intel MKL 2017 BLAS functions from the mex file. Then link the mex file to the custom DLL.

The instructions below refer to an example C-language mex file, m_dgetr.c, which calls the Intel MKL the Linear Algebra PACKage (LAPACK) routines dgetrf and dgetri functions as a way of computing invert matrix. Intel MKL is installed to the default location.

Choosing a Compiler
MATLAB provides a command which searches for supported compilers installed on your system.

>> mex -setup

The above command also allows you to select the compiler you wish to use. Click here to view a list of MATLAB supported compilers. For more information, visit the MATLAB support web site.

Static Linking

Use the following command to statically link the mex file to the Intel MKL static libraries:


>> mex -v mex_dgetr.c mkl_core.lib mkl_intel_thread.lib mkl_intel_lp64.lib libiomp5md.lib

Alternatively, mkl_rt.lib can be choose as a default dynamic link:

>> mex -v mex_dgetr.c mkl_rt.lib

If you started the Matlab from Intel® Compiler Command Line Windows (Programs>> Intel®   Software Development Tools >> Intel® Compiler [version] >> Build Environment for...), you may link the static libraries directly. Otherwise, you have to add the MKL path (Usually at C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_version\windows\mkl) to the Matlab or system path, or simply type them before the *.lib files.

Refer to Intel MKL User's Guide for detailed instructions on statically/dynamically thread/sequential linking to Intel MKL.

Note: It is important to always dynamically link to the Intel MKL threading library, libiomp5md.lib, to the mex file.

Custom Library Builder

For applications which use only a few Intel MKL functions or are limited by code size, we provide a tool to create a smaller, customized dynamic library for distribution. Follow the instructions contained in the Intel MKL User's Guide, Building Custom Dynamic-link Libraries to create a new dynamic library, containing the functions you selected, with the Custom Library Builder. Use the following command line to link the mex file to your custom dynamic library:

>> mex -v mex_dgetr.c ${MKL}\tools\builder\mkl_custom.lib

MATLAB error message: "??? Error using ==> mex at 207 Unable to complete successfully."
When trying to build a mex file with Intel MKL, the MATLAB error message may be given:

??? Error using ==> mex at 207
Unable to complete successfully.

This error message occurs when MATLAB cannot find the Intel MKL libraries during the link step. Full paths to the location of the Intel MKL installation on the build system are required for the compiler to find the libraries and link to them. The default location for the latest version of Intel MKL was bundled with Intel Parallel Studio XE: C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_version\windows\mkl\lib

MATLAB error message: "??? Invalid MEX-file 'filename.mexw64': The specified module could not be found."
When trying to run a mex file linked with an Intel MKL Custom Dynamic Library, the MATLAB error message is given:

??? Invalid MEX-file 'filename.mexw32': The specified module could not be found.

This error occurs when the Intel MKL runtime libraries are not found by MATLAB. To resolve the error, either copy your custom dynamic library to the same directory as the mex file or make sure the location of your custom dynamic library is listed in the system path.


The following example is an *.m file which calls the MKL dgetrf and dgetri functions to realize two ways of matrix inverse computation.

This example is based on Intel MKL 2017 and MATLAB R2015b and the compiler used in Matlab* is Visual Studio* 2015.
The following screen shows the computation time of using Matlab built-in ‘inv’ function and MKL dgetrf and dgetri functions.
Para obtener información más completa sobre las optimizaciones del compilador, consulte nuestro Aviso de optimización.