Tuesday, June 2, 2015

Building caffe with OpenBlas on Ubuntu 12.04

After suffering the same installation issue several times, I decided to write a note about how to build caffe with OpenBlas (which makes caffe a lot faster comparing to using the default AtlasBlas) on Ubuntu 12.04.

1. Compiling OpenBlas

$ git clone git://github.com/xianyi/OpenBLAS
$ cd OpenBLAS && make FC=gfortran
$ sudo make PREFIX=/opt/OpenBLAS install
$ sudo ldconfig

2. Compiling numpy and scipy with OpenBlas

We need to re-compile numpy from the source to ensure numpy uses OpenBlas

$ git clone https://github.com/numpy/numpy
$ cd numpy
$ cp site.cfg.example site.cfg
$ vim site.cfg
Look for [openblas] in the site.cfg and uncomment these lines

....
[openblas]
libraries = openblas
library_dirs = /opt/OpenBLAS/lib
include_dirs = /opt/OpenBLAS/include
....
Then setup the configuration file and build numpy

$ python setup.py config 
$ python setup.py build && python setup.py install

Repeat the same thing (2) for scipy

3. Compiling caffe using OpenBlasbuild and install numpy

Copy libopenblas.a to /usr/lib and /usr/local/lib (otherwise caffe gives compile error: usr/lib/ld: cannot find -lopenblas)
$ cp /opt/OpenBlas/lib/libopenblas.a /usr/lib /usr/local/lib/
$ cp /opt/OpenBlas/lib/libopenblas.so* /usr/lib /usr/local/lib
In Makefile.config set
$ BLAS := open
Then, you should be able to build caffe!

Under the caffe directory
$ make all
$ make pycaffe

No comments:

Post a Comment