Ubuntu – How to use Octave from repositories with OpenBLAS compiled on the computer

18.04octaveshared libraryupdate-alternatives

It has been suggested that using Octave with OpenBLAS compiled in my own computer (rather than precompiled OpenBLAS from repositories) will make the computations faster. I installed GNU Octave (4.2.2) from repositories using apt, and compiled the latest release of OpenBLAS from https://www.openblas.net/.

How to make Octave use this OpenBLAS?

What I have tried so far.

After compiling OpenBLAS, I installed it in opt/openblas.

Then I entered the commands

sudo update-alternatives --install /usr/lib/libblas.so libblas.so /opt/openblas/lib/libopenblas.so 50

sudo update-alternatives --install /usr/lib/libblas.so.3 libblas.so.3 /opt/openblas/lib/libopenblas.so 50

sudo update-alternatives --install /usr/lib/liblapack.so liblapack.so /opt/openblas/lib/libopenblas.so 50

sudo update-alternatives --install /usr/lib/liblapack.so.3 liblapack.so.3 /opt/openblas/lib/libopenblas.so 50

Then I configured to use the new OpenBLAS with

update-alternatives --config libblas.so 
update-alternatives --config libblas.so.3 
update-alternatives --config liblapack.so 
update-alternatives --config liblapack.so.3 

Finally I ran Octave with the new OpenBLAS, but got this error.

$ LD_PRELOAD=/opt/openblas/lib/libopenblas.so octave-cli
octave-cli: symbol lookup error: /usr/lib/x86_64-linux-gnu/libblas.so.3: undefined symbol: gotoblas

Best Answer

I would recommend to start with all the stuff from Ubuntu repository. First thing to try is to install libopenblas-base package and allow it to provide BLAS and LAPACK alternative, as simple as:

sudo apt-get install octave
sudo apt-get install libopenblas-base

Using our test script we have the following timings for Octave on my i7-3537u:

  • 0.42 s - with simple default libblas3, liblapack3
  • 0.26 s - with libopenblas-base from repository

If you want to optimize it even more - compile the OpenBLAS locally by:

sudo apt-get build-dep libopenblas-dev

cd /tmp
apt-get source libopenblas-base
cd openblas-0.2.20+ds
DEB_CFLAGS_SET="-march=native -mtune=native" DEB_CFLAGS_SET="-march=native -mtune=native" dpkg-buildpackage  -uc -us -j8

sudo apt-get install ../libopenblas-base_0.2.20+ds-4_amd64.deb ../libopenblas-dev_0.2.20+ds-4_amd64.deb

And this will change benchmark results to:

  • 0.25 s - with libopenblas-base self-compiled

So it is not useful to compile the optimized library with additional manual optimization.


Conclusion: I'll recommend to optimize your Octave code first by avoiding for-loops and using vectorized functions as much as possible. This will increase performance tremendously.

Related Question