Ubuntu – Octave 4.2.1 and Intel MKL

inteloctave

I have compiled Octave 4.2.1 from sources with no error. Is there any way to make it use Intel Math Kernel Library?

I have installed it in \opt\intel.
When I add it to blas and lapack alternatives with:

sudo update-alternatives --install /usr/lib/libblas.so.3 \
                                 libblas.so.3 \
                                 /opt/intel/compilers_and_libraries/linux/mkl/lib/intel64/libmkl_rt.so \
                                 50

sudo update-alternatives --install /usr/lib/liblapack.so.3 \
                                 liblapack.so.3 \
                                 /opt/intel/compilers_and_libraries/linux/mkl/lib/intel64/libmkl_rt.so \
                                 50

Octave doesn't start with the following error:

Intel MKL FATAL ERROR: Cannot load libmkl_core.so.

I have also tried to compile Octave using MKL.

export JAVA_HOME="/usr/lib/jvm/default-java"
export CFLAGS="-O2 -fPIC -DMKL_LP64 -DM_PI=3.1415926535897932384"
export FFLAGS="-O2 -fPIC"
export CPPFLAGS="-I/opt/intel/mkl/include/ -I/opt/intel/mkl/include/fftw/"
export LDFLAGS="-L/opt/intel/mkl/lib/intel64 -L/opt/intel/lib/intel64"
export LD_LIBRARY_PATH="/usr/lib/gcc:/usr/lib/gcc/x86_64-linux-gnu/:/opt/intel/lib/intel64:/opt/intel/mkl/lib/intel64:$LD_LIBRARY_PATH"

./configure --prefix=/opt/octave4_2_mkl --with-blas="-lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -liomp5 -lpthread" --with-lapack="-lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -liomp5 -lpthread" --with-fftw3="-lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -liomp5 -lpthread -lm" --with-fftw3f="-lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -liomp5 -lpthread -lm"

Unfortunately although ./configure does not spit out any errors, when I run 'make' and 'make check' afterwards, the test gets crazy and exits with Signal 6.

Best Answer

  • Maybe you have figured it out already, but I have gotten it to work (also Octave 4.2.1) FWIW here are the steps:

    Install the MKL library, then set up update-alternatives:

    sudo update-alternatives --install /usr/lib/libblas.so libblas.so  /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
    
    sudo update-alternatives --install /usr/lib/libblas.so.3 libblas.so.3 /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
    
    sudo update-alternatives --install /usr/lib/liblapack.so  liblapack.so  /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
    
    sudo update-alternatives --install /usr/lib/liblapack.so.3 liblapack.so.3 /opt/intel/mkl/lib/intel64/libmkl_rt.so 50
    

    Now create a file mkl.conf in /etc/ld.so.conf.d with the paths as entries:

    /opt/intel/lib/intel64
    
    /opt/intel/mkl/lib/intel64
    

    Then run in the terminal

    sudo ldconfig
    

    Now use update-alternatives configure to use mkl as your default blas

    To check if octave is using mkl, start octave, open a terminal and type

    ps aux | grep octave
    

    My output shows 3 processes, the second one looks like

    my_user_name  6032  6.4  1.7 1454636 103952 ?      Ssl  08:05   0:01 /usr/local/libexec/octave/4.2.1/exec/x86_64-pc-linux-gnu/octave-gui --force-gui
    

    note the PID (6032 here) then in the terminal run

    lsof -p 6032 | grep 'mkl' (change 6032 to your process's PID)
    

    My output is

    octave-gu 6032  my_user_name  mem  REG  8,2  5461454 4594702 /opt/intel/compilers_and_libraries_2017.2.174/linux/mkl/lib/intel64_lin/libmkl_rt.so
    

    If it is not using mkl it wouldn't show anything.

    You don't need to compile octave with MKL, Octave uses whatever blas library linked to /usr/lib/libblas and /usr/lib/libbapack dynamically.You can switch with update-alternatives and octave will use the one you choose. I compiled octave 4.2.1 before I had MKL, I was using openblas as my blas when I compiled.

    In my machine MKL is actually not faster than openblas if the latter is built to optimised for the hardware (rather than installed straight from the repo).


    USE WITH CAUTION, This seems to give wrong results for large matrices. See this bug report


    As mentioned in the comments, you will have to run LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_rt.so octave to use Intel MKL with Octave. You can edit the desktop file to make this default.

    If you regularly use octave-cli, you can also make a new launcher /usr/bin/octave-mkl (with contents LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_rt.so octave-cli) to launch octave-cli with MKL from Terminal.

    Test: This is my code, which will produce real part of the eigenvalue having the largest real part of the 500x500 matrix c whose elements are c(i,j) = sin(i + j^2) .

    for a = 1:500
        for b = 1:500
            c(a,b) = sin(a + b^2);
        endfor
    endfor
    
    g = eig(c);
    max(real(g))
    

    Without MKL the output is ans = 16.915 With MKL the output is ans = 1.2196e+05, and the answer keeps oscillating with every run, but it is always in order of 10^5-10^6,which is wrong.

  • Related Question