Using Intel® MKL with MATLAB*

1. Overview

This guide is intended to help Intel Math Kernel Library (Intel® MKL) customers use the latest version of Intel® MKL for Windows* with The MathWorks* MATLAB
MATLAB* is an interactive software program that performs mathematical computations and visualization. For more information about MATLAB visit www.mathworks.com/products/matlab.

2. Dependencies

Software:
This note applies to MATLAB R2017b and Intel MKL 2018

Hardware:  
The note should work for all hardware configurations that meet the system requirements for both MATLAB R2017b and Intel MKL 2018. This note has been tested on Intel® Core® i7-6820HQ CPU.

Operating System:
This note should work for Windows* users of MATLAB. Process on Linux is similar.


3. Obtaining the Latest Version of Intel MKL

The latest release of the Intel Math Kernel Library is available at www.intel.com/software/products/mkl.  You can download the latest Intel MKL if you have a valid license.  You can also obtain the latest version by renewing your current license or registering for a free 30-day evaluation or get the community license for free.


4. Building a Custom DLL

By default, MATLAB R2017b uses the Intel MKL 11.3.1 Basic Linear Algebra Subroutines (BLAS) libraries to perform computations.

Version of Matlab R2017b and MKL

To upgrade to the Intel MKL 2018 release, you need to create a DLL using the Intel MKL 2018 or later custom DLL builder located in ${MKL}\tools\builder:

  • Scroll to the bottom of this page and download the attached file matlab_custom_dll.zip.
  • Extract and copy the files from matlab_custom_dll.zip and copy them to ${MKL}\tools\builder. It may be preferable to save the old ones by renaming them or moving them to a temporary folder.
  • Customize the makefile by editing the CB_EXPORT=functions_list. It’s in line 101.
  • The custom DLL is built from the command line via a makefile with command nmake. Set the command environment as appropriate for your compiler.
    To Start the Microsoft® Compiler, from the Start Menu select Programs>>Microsoft Visual Studio 20xx>> Visual Studio Tools>> Visual Studio 20xx Command Prompt.​ For Microsoft Visual Studio 2017, please use x64 Native Tools Command Prompt for VS 2017 for 64bit compilation, and x86 Native Tools Command Prompt for VS 2017 for 32bit compilation
  • Build the custom DLL with the following command nmake libintel64 for a 64-static library. For additional options such as customizing the name of the DLL, please refer to the Intel MKL User Guide.
    After this you will find the resulting file at ${MKL}\tools\builder\mkl_custom.dll.

5. MATLAB Configuration and Running

Modify the system environment so that MATLAB uses the Intel MKL BLAS functions from the custom DLL. Set the BLAS_VERSION environment variable by following the steps on Windows 10:

  • Right click Start, select System.
  • Click the Advanced System Settings.
  • Click the Environment Variables button to open the Environment Variables dialog box.
  • Under the User variables section, click the New button to open the New User Variable dialog box.
  • Set the name of the variable to BLAS_VERSION and set the value of the variable to mkl_custom.dll. I.e. BLAS_VERSION=mkl_custom.dll

Start MATLAB as you normally would and the Intel MKL BLAS library will be called.

Alternatively, Users can use the attached script file matlab_mkl.bat/matlab_mkl.sh to start Matlab with all necessary configurations. Please be sure to modify the script file to edit values according to your own environment, like MKL path, custom MKL dll path and name, etc.

6. Verifying Correctness

From MATLAB, run the command version -blas and check that the output should be “Version information not found” (Since mkl_custom.dll does not include version information).

Alternatively, run the command version -modules and check the output list to verify that the custom DLL is loaded.

7. Troubleshooting

The error "The specified module could not be found." occurs for two cases:

  1. One or more of the required interfaces is missing from the custom DLL
    Solution: Follow the steps in section 4 and rebuild the custom DLL.
  2. The custom DLL is not found.
    Solution: Verify that the path to and the name of the custom DLL is correct (see section 5).
    Alternatively, use Matlab command getenv() and setenv() to add the custom DLL path into environment variable PATH.

    --------------------------code-------------------------------------------------------------------------------

    >>setenv('PATH',[getenv('PATH') ';C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_xxxx.x.xxx\windows\mkl\tools\builder']);

    -------------------------------------------------------------------------------------------------------------


Appendix A - References

AttachmentSize
Package icon matlab_custom_dll.zip1.11 KB
Package icon matlab_mkl.zip724 bytes
For more complete information about compiler optimizations, see our Optimization Notice.

7 comments

Top

Hello,

Previous versions of this article and forum discussions suggest static linking as a possible solution. Is this still the case?

I have some algorithms written with Eigen that I'm trying to compile into a MEX file (Eigen 3.3, MATLAB R2018b). For performance reasons, I ask Eigen to link against the MKL with -DEIGEN_USE_MKL_ALL. While this works without problems on Windows (64bit, MSVC), on Linux (64bit, GCC 6.5) I can successfully compile the MEX file but MATLAB consistently crashes with memory violation/corruption errors during the execution of the MEX file.

MEX targets are compiled from CMAKE using matlab_add_mex(). My link lines are:

target_link_libraries(${target_name} PRIVATE mkl_intel_lp64.lib mkl_sequential.lib mkl_core.lib)  #Windows

target_link_libraries(${target_name} -Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_intel_lp64.a ${MKLROOT}/lib/intel64/libmkl_sequential.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl) #Linux

The build and link commands generated by CMAKE on Linux are:

g++  -DDLL_EXPORT_SYM="__attribute__ ((visibility (\"default\")))" -DEIGEN_DONT_PARALLELIZE -DEIGEN_USE_MKL_ALL -I/<...>/MATLAB_R2018b/extern/include  -I/<...>/externals/eigen -I/<...>/boost_1_60_0/include -I/<...>/gurobi/gurobi810/linux64/include -I/<...>/intel_mkl/2019_3/mkl/include  -march=ivybridge -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -mno-sse4a -mno-fma -mno-bmi2 -mno-avx2 -mno-xop -mno-fma4 -mno-avx512f -mno-avx512vl -mno-avx512pf -mno-avx512er -mno-avx512cd -mno-avx512dq -mno-avx512bw -mno-avx512ifma -mno-avx512vbmi -O2 -g -DNDEBUG -fPIC -fvisibility=hidden -fvisibility-inlines-hidden   -pthread -o CMakeFiles/<...>/mex.cpp.o -c /<...>/mex.cpp

g++ -fPIC  -march=ivybridge -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mavx -mno-sse4a -mno-fma -mno-bmi2 -mno-avx2 -mno-xop -mno-fma4 -mno-avx512f -mno-avx512vl -mno-avx512pf -mno-avx512er -mno-avx512cd -mno-avx512dq -mno-avx512bw -mno-avx512ifma -mno-avx512vbmi -O2 -g -DNDEBUG  -shared -Wl,-soname,sample_td_space_mex.mexa64 -o sample_td_space_mex.mexa64 CMakeFiles/<...>/mex.cpp.o  -L/<...>/MATLAB_R2018b/bin/glnxa64  -L/<...>/MATLAB_R2018b/extern/bin/glnxa64  -L/<...>/gurobi/gurobi810/linux64/lib -Wl,-rpath,/<...>/MATLAB_R2018b/bin/glnxa64:/<...>/MATLAB_R2018b/extern/bin/glnxa64:/<...>/gurobi/gurobi810/linux64/lib: -lmx -lMatlabEngine -lMatlabDataArray -lmex ../externals/gurobi/libgurobi.a -Wl,--start-group /<...>/intel_mkl/2019_3/mkl/lib/intel64/libmkl_intel_lp64.a /<...>/intel_mkl/2019_3/mkl/lib/intel64/libmkl_sequential.a /<...>/intel_mkl/2019_3/mkl/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl -lgurobi81

Any ideas how can I solve this issue?

The only possible problem I noticed is that Eigen requires the LP64 interface. Can this possibly conflict with MATLAB?

 

Best,

Mattia

Is there the same chance to do this in Linux?

Here's an updated method that worked for me with MKL 2018.3.210 and Matlab 2018a:

  • Open makefile and locate the following segment:
!if "$(export)"==""
CB_EXPORT=user_example_list
!else
CB_EXPORT="$(export)"
!endif
  • Replace user_example_list with functions_list.
  • Continue per instruction in the post.

Note: Setting ONLY the name of environment variable BLAS_VERSION to mkl_cusom didn't work. Using the batch file (after updating with correct paths) DID work with the added benefit of being able to launch Matlab with 2018 and 2017 MKL versions.

 

 matlab_custom_dll.rar where is supposed to be? lul

I do not see any download link for a .dll, .rar, or a .zip at the bottom of the page, contrary to what the article suggests.

Can you, please, expand this article by adding instructions for models the canal.
Thank you.

Can you, please, expand this article by adding instructions for Linux.
Thank you.

Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.