1

Intel Math Kernel Library is a BLAS library, which is available in Ubuntu repositories from Ubuntu 19.10 onwards, and is reported to give much faster results.

How to make GNU Octave use Intel MKL installed from the repositories?

Archisman Panigrahi
  • 25,210
  • 17
  • 90
  • 185
  • Note: This is not a duplicate of https://askubuntu.com/questions/891189/octave-4-2-1-and-intel-mkl, because that was for an older version of Ubuntu where Intel MKL had to be manually installed (because it was not available in the repositories), and this method had **serious bugs** (as mentioned in its answer). I am interested in knowing how to configure Octave with the Intel MKL Library for the repositories. – Archisman Panigrahi Aug 09 '20 at 16:18
  • I have not upgraded to Focal yet, I want to know whether Octave can be successfully used with the MKL from repositories before I upgrade. – Archisman Panigrahi Aug 09 '20 at 16:21

1 Answers1

3

You have to install Octave with

sudo apt-get install octave

and then install the full Intel MKL development package with

sudo apt-get install libmkl-full-dev

during installation you have to select Intel MKL as default mathematical libraries provider.

Important:
To avoid bug between Octave and MKL one should add the following variable declaration to the ~/.bashrc or ~/.profile:

export MKL_THREADING_LAYER=gnu

and then launch Octave.

As noted in the recent comments in the bug, Octave closes with segmentation fault when __run_test_suite__ is run, even after applying this workaround. At the moment it is not recommended to use the version of Octave from the Ubuntu repositories alongside MKL.


To confirm that MKL is used one can see htop libraries listing below (F4, octave, select octave-gui, L, F4, mkl):

mkl libraries are listed


Benchmark by using code below

c = sin((1:500)' + (1:500).^2);

tic;
g = eig(c);
toc
m = max(real(g))
assert (m, 16.914886, 1e-6)

Using Intel i7-3537U:

  • default - 0.46 s;
  • MKL - 0.14 s.
Archisman Panigrahi
  • 25,210
  • 17
  • 90
  • 185
N0rbert
  • 97,162
  • 34
  • 239
  • 423
  • To enhance the answer, could you provide an example of the speedup with and without (does it use OpenBLAS by default?) MKL? E.g. could you measure the time to diagonalize a random 2000x2000 matrix? Octave Code: `a = randn(2000); tic; [b,c]=eig(a);toc;` – Archisman Panigrahi Aug 09 '20 at 19:15
  • Also, could you clarify what I am supposed to do with `htop`? I thought it is a CLI task manager – Archisman Panigrahi Aug 09 '20 at 19:18
  • The `htop` is a console-based process manager. I have checked load of dynamic MKL libraries by using it to be sure that they were really loaded. Will do the benchmark. – N0rbert Aug 09 '20 at 19:22
  • Benchmark done, on fresh 3rd generation of i7 have two-fold speedup. On core2duo t9550 got slowdown. – N0rbert Aug 09 '20 at 20:44
  • I am running a live Xubuntu 20.04, and it looks like the [same bug](https://askubuntu.com/a/913029/124466) (at the end of that answer) is present when I install `intel-mkl` package. It is much faster (`i3`), but the results are all wrong for large matrices. Could you check whether the code at the end of that link gives wrong answer (which changes every time!!) with MKL? Then I would open a bug report. – Archisman Panigrahi Aug 10 '20 at 05:04
  • For a quick reference, this is the code, which produces wrong (and variable) results with MKL. https://pastebin.pl/view/611d6fe3 – Archisman Panigrahi Aug 10 '20 at 05:29
  • 1
    I can confirm that Octave gives wrong values with MKL. But the `scilab-cli` gives correct results with MKL and it is 2.5 times faster (to compare with default libraries) for the same task. The test script is located [on GitHub](https://gist.github.com/N0rbert/cfda101b8f0aa326df1edb6beee0076d). So you have to create bug report about using MKL with Octave. – N0rbert Aug 10 '20 at 10:24
  • I have created a bug report https://savannah.gnu.org/bugs/index.php?58926 – Archisman Panigrahi Aug 11 '20 at 05:24
  • @ArchismanPanigrahi great. I can confirm fix by using environment variable, so I edited the answer to include this important part. – N0rbert Aug 11 '20 at 07:49
  • I tried to compile OpenBLAS and use it with Octave, but there is some issue (some users reported that it gives speed comparable to MKL, and it will not have the wrong results bug). Do you happen to know the answer to this? https://askubuntu.com/questions/1267070/how-to-use-octave-from-repositories-with-openblas-compiled-on-my-computer – Archisman Panigrahi Aug 14 '20 at 06:11