Compiling numpy with OpenBLAS integration
I just compiled numpy
inside a virtualenv
with OpenBLAS
integration, and it seems to be working OK.
This was my process:
Compile
OpenBLAS
:$ git clone https://github.com/xianyi/OpenBLAS
$ cd OpenBLAS && make FC=gfortran
$ sudo make PREFIX=/opt/OpenBLAS installIf you don't have admin rights you could set
PREFIX=
to a directory where you have write privileges (just modify the corresponding steps below accordingly).Make sure that the directory containing
libopenblas.so
is in your shared library search path.To do this locally, you could edit your
~/.bashrc
file to contain the lineexport LD_LIBRARY_PATH=/opt/OpenBLAS/lib:$LD_LIBRARY_PATH
The
LD_LIBRARY_PATH
environment variable will be updated when you start a new terminal session (use$ source ~/.bashrc
to force an update within the same session).Another option that will work for multiple users is to create a
.conf
file in/etc/ld.so.conf.d/
containing the line/opt/OpenBLAS/lib
, e.g.:$ sudo sh -c "echo '/opt/OpenBLAS/lib' > /etc/ld.so.conf.d/openblas.conf"
Once you are done with either option, run
$ sudo ldconfig
Grab the
numpy
source code:$ git clone https://github.com/numpy/numpy
$ cd numpyCopy
site.cfg.example
tosite.cfg
and edit the copy:$ cp site.cfg.example site.cfg
$ nano site.cfgUncomment these lines:
....
[openblas]
libraries = openblas
library_dirs = /opt/OpenBLAS/lib
include_dirs = /opt/OpenBLAS/include
....Check configuration, build, install (optionally inside a
virtualenv
)$ python setup.py config
The output should look something like this:
...
openblas_info:
FOUND:
libraries = ['openblas', 'openblas']
library_dirs = ['/opt/OpenBLAS/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
FOUND:
libraries = ['openblas', 'openblas']
library_dirs = ['/opt/OpenBLAS/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
...Installing with
pip
is preferable to usingpython setup.py install
, sincepip
will keep track of the package metadata and allow you to easily uninstall or upgrade numpy in the future.$ pip install .
Optional: you can use this script to test performance for different thread counts.
$ OMP_NUM_THREADS=1 python build/test_numpy.py
version: 1.10.0.dev0+8e026a2
maxint: 9223372036854775807
BLAS info:
* libraries ['openblas', 'openblas']
* library_dirs ['/opt/OpenBLAS/lib']
* define_macros [('HAVE_CBLAS', None)]
* language c
dot: 0.099796795845 sec
$ OMP_NUM_THREADS=8 python build/test_numpy.py
version: 1.10.0.dev0+8e026a2
maxint: 9223372036854775807
BLAS info:
* libraries ['openblas', 'openblas']
* library_dirs ['/opt/OpenBLAS/lib']
* define_macros [('HAVE_CBLAS', None)]
* language c
dot: 0.0439578056335 sec
There seems to be a noticeable improvement in performance for higher thread counts. However, I haven't tested this very systematically, and it's likely that for smaller matrices the additional overhead would outweigh the performance benefit from a higher thread count.
No _dotblas.so after installing OpenBLAS and Numpy
For those who are also struggling with building NumPy with OpenBLAS, the _dotblas
module is no longer available since NumPy 1.10.0, according to the Release Notes. Found from this post.
Can't get Python3 numpy to see BLAS/LAPACK
If anyone's interested in the answer, I managed to finally get OpenBLAS recognized in numpy, and received a decent speed boost.
To do it you must first uninstall python3-numpy and any numpy installed via pip3. Then manually compile OpenBLAS and numpy as explained in Compiling numpy with OpenBLAS integration.
Installing the default packages via apt-get or pip apparently doesn't seem to link in any BLAS library by default, at least not on the TinkerBoard Linaro OS...
Link ATLAS/MKL to an installed Numpy
Assuming you're running some flavour of linux, here's one way you could do it:
Find out what BLAS library numpy is currently linked against using
ldd
.For versions of numpy older than v1.10:
$ ldd /<path_to_site-packages>/numpy/core/_dotblas.so
For example, if I install numpy via
apt-get
, it links to...
libblas.so.3 => /usr/lib/libblas.so.3 (0x00007fed81de8000)
...If
_dotblas.so
doesn't exist, this probably means that numpy failed to detect any BLAS libraries when it was originally installed, in which case it simply doesn't build any of the BLAS-dependent components. This often happens if you install numpy usingpip
without manually specifying a BLAS library (see below). I'm afraid you'll have no option but to rebuild numpy if you want to link against an external BLAS library.
For numpy v1.10 and newer:
_dotblas.so
has been removed from recent versions of numpy, but you should be able to check the dependencies ofmultiarray.so
instead:$ ldd /<path_to_site-packages>/numpy/core/multiarray.so
Install ATLAS/MKL/OpenBLAS if you haven't already. By the way, I would definitely recommend OpenBLAS over ATLAS - take a look at this answer (although the benchmarking data is now probably a bit out of date).
Use
update-alternatives
to create a symlink to the new BLAS library of your choice. For example, if you installedlibopenblas.so
into/opt/OpenBLAS/lib
, you would do:$ sudo update-alternatives --install /usr/lib/libblas.so.3 \
libblas.so.3 \
/opt/OpenBLAS/lib/libopenblas.so \
50You can have multiple symlinks configured for a single target library, allowing you to manually switch between multiple installed BLAS libraries.
For example, when I call
$ sudo update-alternatives --config libblas.so.3
, I can choose between one of 3 libraries:Selection Path Priority Status
------------------------------------------------------------
0 /opt/OpenBLAS/lib/libopenblas.so 40 auto mode
1 /opt/OpenBLAS/lib/libopenblas.so 40 manual mode
2 /usr/lib/atlas-base/atlas/libblas.so.3 35 manual mode
* 3 /usr/lib/libblas/libblas.so.3 10 manual mode
If you really want the "newest" version of numpy, you could also take a look at my answer on compiling numpy from source with OpenBLAS integration.
Installing numpy with BLAS support using pip
As @tndoan mentioned in the comments, it's possible to make pip
respect a particular configuration for numpy by placing a config file in ~/.numpy-site.cfg
- see this answer for more details.
My personal preference is to configure and build numpy by hand. It's not particularly difficult, and it gives you better control over numpy's configuration.
Related Topics
Placing Custom Images in a Plot Window--As Custom Data Markers or to Annotate Those Markers
Why Are Scripting Languages (E.G. Perl, Python, and Ruby) Not Suitable as Shell Languages
Parallel Processing from a Command Queue on Linux (Bash, Python, Ruby... Whatever)
Name This Python/Ruby Language Construct (Using Array Values to Satisfy Function Parameters)
Numpy Selecting Specific Column Index Per Row by Using a List of Indexes
How to Do Parallel Programming in Python
How to Make an Immutable Object in Python
How to "Test" Nonetype in Python
Error: Command 'Gcc' Failed with Exit Status 1 While Installing Eventlet
Why Is a List Comprehension So Much Faster Than Appending to a List
Show Default Value for Editing on Python Input Possible
How to Prevent Numbers Being Changed to Exponential Form in Python Matplotlib Figure
Combine Two Pandas Data Frames (Join on a Common Column)
Sort List of Lists Ascending and Then Descending
Too Many Values to Unpack', Iterating Over a Dict. Key=>String, Value=>List