Note: The following applies to R2021a and newer releases of MATLAB Coder and MKL-DNN v1.4 as described here. For older releases, see the next answer below.
Note: Intel recently renamed the library from MKL-DNN to oneDNN, so we use MKL-DNN and oneDNN interchangeably.
Background
To generate and run C++ code for Deep Learning, you must have the Intel Math Kernel Library for Deep Neural Networks (Intel MKL-DNN). Do not use a prebuilt library because some required files are missing. Instead, build the library from source code.
The following describes build instructions for MKL-DNN on Windows and Linux platforms.
To build the Intel MKL-DNN library from source code, you must have:
- Operating system with Intel 64 architecture support
- C++ compiler with C++11 standard support
- CMake 2.8.11 or later version
Windows MKL-DNN build instructions
These MKL-DNN build steps have been validated by using Visual Studio 2017 version 15.0 on 64-bit Windows platform. If you want to use a newer version of Visual Studio to build a working MKL-DNN library, use it at your own discretion.
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.zip) and extract the source code. This action creates the folder oneDNN-1.4.
2. At the Windows command line, change the current directory to oneDNN-1.4. Generate a Microsoft Visual Studio solution “Intel(R) MKL-DNN.sln” by running these commands:
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
3. Change the current directory to oneDNN-1.4\build. Run this command:
cmake --build . --config Release
This action creates these libraries in the folder oneDNN-1.4\build\src\Release:
- dnnl.lib
- dnnl.dll
Inside the folder C:\Program Files, create a folder named mkl-dnn. Then, inside the folder C:\Program Files\mkl-dnn, create a folder named lib. Copy the generated libraries to the folder C:\Program Files\mkl-dnn\lib.
On Windows operating systems, special characters and spaces are allowed in the path only if 8.3 file names are enabled. If they are not enabled, replace C:\Program Files\mkl-dnn\lib by a path that does not include spaces. For more information on 8.3 file names, refer to the Windows documentation.
Copy the include files from oneDNN-1.4\include and oneDNN-1.4\build\include to C:\Program Files\mkl-dnn\include.
4. Set the MATLAB environment variable INTEL_MKLDNN to C:\Program Files\mkl-dnn. At the MATLAB command prompt, enter:
setenv('INTEL_MKLDNN', 'C:\Program Files\mkl-dnn')
Add C:\Program Files\mkl-dnn\lib to the PATH variable by running:
setenv('PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('PATH')])
5. If you have not done so already, you must set the environment variable for Windows. At the Windows command line, enter:
set PATH=%PATH%; C:\Program Files\mkl-dnn\lib
Also, create and set the INTEL_MKLDNN environment variable in Windows that points to C:\Program Files\mkl-dnn.
Linux MKL-DNN build instructions
C++ Compiler requirements:
- Install any one of the GNU compiler versions from GNU Compiler Collection. The versions include 4.8, 5.4, 6.1, 7.2, and 8.1.
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.tar.gz) and extract the source code by running this command at the Linux terminal:
tar -xvzf oneDNN-1.4.tar.gz
This action creates the folder oneDNN-1.4.
2. To generate the makefile for compilation, run these commands at the terminal from the oneDNN-1.4 folder:
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
3. To build the library, run this command at the terminal from the oneDNN-1.4/build folder:
make -j
These commands creates these libraries in the folder oneDNN-1.4/build/src:
- libdnnl.so
- libdnnl.so.1
- libdnnl.so.1.4
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from oneDNN-1.4/include and oneDNN-1.4/build/include to /usr/local/mkl-dnn/include.
4. Set the MATLAB environment variable INTEL_MKLDNN to /usr/local/mkl-dnn. At the MATLAB command line, enter:
setenv('INTEL_MKLDNN', ‘/usr/local/mkl-dnn')
5. Add /usr/local/mkl-dnn/lib to the PATH variable:
setenv('LD_LIBRARY_PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('LD_LIBRARY_PATH')]);
If you have not done so already, you must set the environment variables for Linux. Use Linux syntax at the Linux terminal to set the variable LD_LIBRARY_PATH to /usr/local/mkl-dnn/lib, and the environment variable INTEL_MKLDNN to /usr/local/mkl-dnn.
macOS MKL-DNN build instructions
C++ Compiler requirements:
- Install supported Xcode versions described in https://www.mathworks.com/support/requirements/supported-compilers.html.
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.tar.gz) and extract this source code by running this command at the terminal:
tar -xvzf oneDNN-1.4.tar
This action creates the folder oneDNN-1.4.
2. The macOS clang compiler does not ship with OpenMP. To get an OpenMP enabled mkldnn library, you must install brew and libomp:
a. Install home brew on host mac PC by running this command at the bash terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
b. Install OpenMP by using brew with command:
$(BREW_INSTALL_PATH)/brew install libomp
3. To generate the makefile for compilation, run these commands at the terminal from the oneDNN-1.4 folder:
mkdir -p build
cd build
$(CMAKE_INSTALL_PATH)cmake .. -DOpenMP_CXX_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_C_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_CXX_LIB_NAMES=libomp -DOpenMP_C_LIB_NAMES=libomp -DOpenMP_libomp_LIBRARY=$(OMP_INSTALL_PATH)/lib/libomp.dylib -DCMAKE_SHARED_LINKER_FLAGS="-L$(OMP_INSTALL_PATH)/lib -lomp"
4. To build the library, run this command at the terminal from the oneDNN-1.4/build folder:
make -j
These commands create these libraries in the folder oneDNN-1.4/build/src:
- libdnnl.dylib
- libdnnl.1.dylib
- libdnnl.1.4.dylib
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from oneDNN-1.4/include to /usr/local/mkl-dnn/include.
5. Copy the OpenMP dependencies required by MKL-DNN:
- OpenMP libraries from $(OMP_INSTALL_PATH)/lib to /usr/local/mkl-dnn/lib
- OpenMP include files from $(OMP_INSTALL_PATH)/include to /usr/local/mkl-dnn/include
6. The OpenMP library path is hard bound to mkldnn library. To list this hard binding, run the below command:
otool -l libdnnl.dylib
7. This hard binding of the paths must be changed for portability to other mac machines. To do this, run:
install_name_tool -change /usr/local/opt/libomp/lib/libomp.dylib @rpath/libomp.dylib libdnnl.1.dylib
install_name_tool -id "@rpath/libomp.dylib" libomp.dylib
Changing to rpath enables portability of the mkldnn/OpenMP libraries to other mac machines. This avoids any runtime linking issues.
8. Set the MATLAB environment variable INTEL_MKLDNN to /usr/local/mkl-dnn. At the MATLAB command line, enter:
setenv('INTEL_MKLDNN', ‘/usr/local/mkl-dnn')