3

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.

3 Answers3

2

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.

  • I do not know how to compile Openblas myself, but I noticed that when I use python with MKL it is a little bit faster and it uses less resources than Python+Openblas for the same computations (I tested it with inverting matrices only) – Kamil Mróz May 09 '17 at 13:20
  • My bad, I missed something. It turned out that octave does not automatically use /usr/bin/liblas created by update-alternatives. I tried to compile octave explicitly using mkl and got fortran error. But the answer turns out to be simpler

    Install octave in any way (say from repo), then start it with

    LD_PRELOAD=/opt/intel/mkl/lib/intel64/libmkl_rt.so octave

    Now it actually uses mkl and turns out to be faster than openblas.

    – Icannotlogin May 09 '17 at 20:51
  • Or if you want to benchmark different versions of blas, set up update-alternatives as above then start octave with

    LD_PRELOAD= /usr/lib/libblas.so octave

    You can edit octave's .Desktop file so you can just click the launcher.

    – Icannotlogin May 09 '17 at 21:01
1

I'm able to compile GNU Octave 9.3.0 from source directly with Intel MKL 2025.0.1 and make check without any segfault, all without using the LD_PRELOAD trick. Computation is now at MKL speed, without the issues reported in this bug report. My system is x86_64 Ubuntu 22.04.3. Here're my steps.

  1. Install Intel MKL. Choose the online installer so you only have to download/install the product (MKL) you need.

  2. Properly set up your Intel OneAPI environment. For me, running . /opt/intel/oneapi/setvars.sh intel64 in a terminal would do.

  3. Go to your Octave source folder. Edit the file configure to hard-code both FFTW3_LIBS and FFTW3F_LIBS as follows.

Paste this near line 93994: FFTW3_LIBS="-m64 -L$MKLROOT/lib -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl"

Paste this near line 94155: FFTW3F_LIBS="-m64 -L$MKLROOT/lib -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl"

  1. ./configure --with-fftw3-includedir=$MKLROOT/include/fftw --with-fftw3-libdir=$MKLROOT/lib/intel64 --with-fftw3f-includedir=$MKLROOT/include/fftw --with-fftw3f-libdir=$MKLROOT/lib/intel64 --with-blas="-m64 -L$MKLROOT/lib -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl" --with-lapack="-m64 -L$MKLROOT/lib -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl"

  2. Make sure 4. doesn't give out any suspicious warning/error messages. Then make; make check; sudo make install as usual.

  3. My make check didn't segfault and resulted in only four failures, three of which are benign, and one is probably OK. Here're more details.

make check Failures:

Look at your test/fntests.log and search for test failed for more info. Mine is as follows.

  • subspace.m: Benign. I can live with an error of 7.7716e-16

Abs err 7.7716e-16 exceeds tol 6.6613e-16 by 1e-16

  • svd.cc-tst: Benign. The two errors are due to negating the unitary matrix in SVD. The sign of the unitary matrix is arbitrary and this result is still mathematically correct.

Observed | Expected
0.70711 | -0.70711
0.70711 | -0.70711
0.70711 | -0.70711
-0.70711 | 0.70711

  • fftw.cc-tst: Probably OK. I'm not sure if Intel MKL even supports wisdom. See here.

could not get current FFTW wisdom

Performance Test:

From a terminal run MKL_NUM_THREADS=4 octave and test the (faster MKL) speed of matrix multiplication. On my i7-4770K quadcore, multiplying two double matrices of sizes 6400 x 10000 and 10000 x 6400 using all MKL_NUM_THREADS=4 physical cores took 3.9 seconds, which I know is a reasonable MKL speed.

0

Not sure if this page did not exist back when this question was asked, but Intel provides official instructions on how to install octave with MKL support

https://software.intel.com/content/www/us/en/develop/articles/using-intel-mkl-in-gnu-octave.html