Installing Intel MKL to speed up Torch for Intel CPU

for cpu translate mode, use Intel® MKL can expect at least a 2x speedup when using MKL with multiple threads.

#download mkl from https://software.intel.com/en-us/intel-mkl/

tar -zxf l_mkl_2017.0.098.tgz
sudo ./install.sh

#enter serial number of mkl.
#default install path at: /opt/intel

export CMAKE_INCLUDE_PATH=$CMAKE_INCLUDE_PATH:/opt/intel/mkl/include
export CMAKE_LIBRARY_PATH=$CMAKE_INCLUDE_PATH:/opt/intel/mkl/lib/intel64:/opt/intel/mkl/compiler/lib/intel64
export LD_LIBRARY_PATH=$CMAKE_LIBRARY_PATH:$LD_LIBRARY_PATH
#Also, you need to source the mklvar.sh file like this:
source /opt/intel/mkl/bin/mklvars.sh intel64

###Install Intel version torch

git clone https://github.com/intel/torch.git ./torch

cd torch; 
bash install-deps;
TORCH_LUA_VERSION=LUA52 ./install.sh

###You can test that all libraries are installed properly by running:

cd torch
ldd ./install/lib/lua/5.2/libTHNN.so
    linux-vdso.so.1 =>  (0x00007fff613ff000)
    libTH.so.0 => /yfsys/work/torch/install/lib/libTH.so.0 (0x00007f81b1667000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f81b1353000)
    libgomp.so.1 => /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f81b1125000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f81b0f08000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81b0b49000)
    libmkl_gf_lp64.so => /opt/intel/mkl/lib/intel64/libmkl_gf_lp64.so (0x00007f81b0034000)
    libmkl_gnu_thread.so => /opt/intel/mkl/lib/intel64/libmkl_gnu_thread.so (0x00007f81aee68000)
    libmkl_core.so => /opt/intel/mkl/lib/intel64/libmkl_core.so (0x00007f81ad378000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f81ad170000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f81b1ea2000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f81acf6b000)

you should use OMP_NUM_THREADS environment variable to control the number of threads.

more detail:
https://github.com/intel/torch/

2 Likes

Hi, I tried it, following what you write, but now I come up with a funny problem, in last step,

ldd ./install/lib/lua/5.2/libTHNN.so

my output is
linux-vdso.so.1 => (0x00007ffc39ad5000)
libTH.so.0 => /home/huqiu/torch/install/lib/libTH.so.0 (0x00007f12abf13000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f12abbea000)
libgomp.so.1 => /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f12ab9c8000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f12ab7aa000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f12ab3e1000)
libmkl_gf_lp64.so => /opt/intel/mkl/lib/intel64/libmkl_gf_lp64.so (0x00007f12aa9aa000)
libmkl_gnu_thread.so => /opt/intel/mkl/lib/intel64/libmkl_gnu_thread.so (0x00007f12a9788000)
libmkl_core.so => /opt/intel/mkl/lib/intel64/libmkl_core.so (0x00007f12a7c95000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f12a7a8d000)
/lib64/ld-linux-x86-64.so.2 (0x000055c50b3ac000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f12a7888000)

that is to say, there is no "libmkl_rt.so => /opt/intel/mkl/lib/intel64/libmkl_rt.so (0x00007f5914566000)"
but I have “librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f12a7a8d000)”,

I wonder why ? and how I can do to tackle it, can you give me some insights or help me ? Thank you in advance.:slight_smile:

I think that your installation is correct, in the last version of MKL, my system ld is same as yours:

ldd ./install/lib/lua/5.2/libTHNN.so
linux-vdso.so.1 => (0x00007fff613ff000)
libTH.so.0 => /yfsys/work/torch/install/lib/libTH.so.0 (0x00007f81b1667000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f81b1353000)
libgomp.so.1 => /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f81b1125000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f81b0f08000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f81b0b49000)
libmkl_gf_lp64.so => /opt/intel/mkl/lib/intel64/libmkl_gf_lp64.so (0x00007f81b0034000)
libmkl_gnu_thread.so => /opt/intel/mkl/lib/intel64/libmkl_gnu_thread.so (0x00007f81aee68000)
libmkl_core.so => /opt/intel/mkl/lib/intel64/libmkl_core.so (0x00007f81ad378000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f81ad170000)
/lib64/ld-linux-x86-64.so.2 (0x00007f81b1ea2000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f81acf6b000)

you mean, this info indicates that our torch now is running on MKL instead of OpenBLAS ?

if yes, why a same lua program cost more time than running on OpenBLAS ? it is normal ?

please see more detail:
https://github.com/intel/torch/

I do confirm that my tests are also slower with the MKL+intel torch version vs the standard config.

Surprising but even with multithread = 8 it’s slow.