Skip to content

Instantly share code, notes, and snippets.

@sheljohn
Last active November 30, 2019 11:01
Show Gist options
  • Save sheljohn/47d0137eba74db99d25f to your computer and use it in GitHub Desktop.
Save sheljohn/47d0137eba74db99d25f to your computer and use it in GitHub Desktop.
Building Armadillo + OpenBLAS [on Intel + Ubuntu]

Introduction

I actually found very few indications on the net to install Armadillo with OpenBLAS on Ubuntu, and I ran into problems when I tried to install the Ubuntu pre-build packages (using apt-get). So I decided to share my experience building the whole thing from scratch, hopefully this can serve as a step-by-step through the install.

Platform

As of writing this, I'm using Ubuntu desktop 14.04 with an Intel i7 4820k Ivy-Bridge E CPU. Of course your config will be different, but I think it should be fairly similar for any Intel + Ubuntu desktop platform.

Standalone install

I'm not going to install Armadillo on my entire system, although it shouldn't make a whole lot of difference. I'm using this inside another project, which is currently synchronized between three machines (Ubuntu desk, OSX Yosemite desk, laptop OSX Snow Leopard). I'll update the gist when I setup the whole thing.

You should create a local directory in which we'll build the standalone install. I'll simply call it myproject from now on.

Requirements

You should of course have a decent C/C++ compiler (ie up-to-date gcc or clang suite). You should install cmake, gfortran, LAPACK, BLAS, ATLAS and ARPACK:

sudo apt-get install cmake cmake-gui gfortran 
sudo apt-get install liblapack-dev libblas-dev libatlas-base-dev libarpack2-dev libarpack++2-dev

Note that because we're going to use OpenBLAS, it is not necessary to install BLAS or ATLAS. If you want to install them anyway (you might not always want to use OpenBLAS for some reason):

sudo apt-get install libblas-dev libatlas-base-dev

Installing OpenBLAS

I assume you're in the myproject directory.

Directories

Create the directories for OpenBLAS:

mkdir -p openblas/src
mkdir -p openblas/build/mymachine

src will contain the full downloaded sources, build/mymachine will contain the build for the machine you're on.

Download

Download the latest tarball from here, extract it in openblas/src, rename the directory if you want.

Options

Go to the folder you extracted, the key files are:

  • TargetList.txt: contains the list of supported CPU microarchitecture. This is useful to enable/disable certain optimizations inside the code that are specific to your CPU. If you're not sure what target to choose, you should check you processor name in Ubuntu "System Settings > Details", then look it up online, and check against the list of Intel microarchitectures. For example, in my case, Ivy Bridge E is a subcategory of Sandy Bridge.
  • GotoBLAS_02QuickInstall.txt: contains basic step-by-step instructions to compile OpenBLAS on your machine.
  • Makefile.rule: contains all the flags you can set for the build, and explanations on how to use them.

Open all three files with your favourite text editor. Go to the build folder openblas/build and create a file make_commands where you'll put one by one the relevant options for your build. For example in my case:

make CC=gcc FC=gfortran BINARY=64 USE_THREADS=1 NUM_THREADS=8 TARGET=SANDYBRIDGE COMMON_OPT=" -O2 "

You can create several commands with different options and different build prefixes. For example, for a build intended to be deployed on several 64 bits machines, we could choose the DYNAMIC_ARCH option:

make BINARY=64 DYNAMIC_ARCH=1

Or another example eg on my Mac Core2 duo laptop:

make CC=gcc FC=gfortran BINARY=64 USE_THREADS=1 NUM_THREADS=2 TARGET=CORE2 COMMON_OPT=" -O2 "

Build & Install

Go back to the source directory openblas/src/<extracted version>. For each make command you just created in the build directory, you should run make clean first and then the command. Each build can take a while, so sit back and check for errors.

If any error occurs, and you can't fix it or don't understand it, you should ask a question on StackOverflow, and if needed create a new issue on the Github repository.

If everything goes well though, you should get a message like this one:

 OpenBLAS build complete. (BLAS CBLAS LAPACK LAPACKE)

  OS               ... Linux             
  Architecture     ... x86_64               
  BINARY           ... 64bit                 
  C compiler       ... GCC  (command line : gcc)
  Fortran compiler ... GFORTRAN  (command line : gfortran)
  Library Name     ... libopenblas_sandybridgep-r0.2.13.a (Multi threaded; Max num-threads is 8)

To install the library, you can run "make PREFIX=/path/to/your/installation install".

after which you should, as indicated, run a command like (eg, in my case) make PREFIX=../../build/mymachine install, and continue with the next build.

Installing Armadillo

I assume you're in the myproject directory.

Directories

Create the directories for Armadillo:

mkdir -p arma/src
mkdir -p arma/build/mymachine

src will contain the full downloaded sources, build/mymachine will contain the build for the machine you're on.

Download

Download the latest tarball from here, extract it in openblas/src, rename the directory if you want, and throw the tarball away.

Configure, build and install

Go to the source directory arma/src/<version extracted>, and edit the file include/armadillo_bits/config.hpp. The following flags should be defined: ARMA_USE_LAPACK, ARMA_USE_BLAS and ARMA_USE_CXX11. The following flag should be commented: ARMA_USE_WRAPPER.

Save the config file, return to the source directory, and run:

./configure
cmake-gui .

A window will open:

  • Browse Source and Browse Build should indicate the path of your source directory.
  • You should see a bunch of options highlighted in red.
  • In the field openblas_LIBRARY, select the previously created library openblas/build/mymachine/lib/libopenblas.so.
  • In the field CMAKE_INSTALL_PREFIX, select the build directory arma/build/mymachine.
  • Run Configure, the options should become white, and you should see messages about "OpenBLAS found" in the output.
  • Run Generate and close the window.

Finally, in the terminal run:

make
make install

and if you want to clean after yourself

make clean
rm -f CMakeCache.txt

Compiling examples

Go to the examples directory, and compile the first example:

clang++ -std=c++0x -I/path/to/arma/build/mymachine/include -I/path/to/openblas/build/mymachine/include -o example1 example1.cpp -L/path/to/openblas/build/mymachine/lib -lopenblas -L/path/to/arma/build/mymachine/lib -larmadillo

Under construction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment