[PYTHON] Use multithreaded BLAS / LAPACK with numpy / scipy

In Article I wrote earlier, I described how to install python, numpy, and scipy from source without taking root.

However, the above method has the following problems.

The reason is that BLAS and LAPACK are not multi-threaded. I don't have enough money to buy Intel MKL, so I decided to use OpenBLAS.

The following two reference articles.

OpenBLAS

Use SurviveGotoBLAS. First, build the static library.

# wget http://prs.ism.ac.jp/~nakama/SurviveGotoBLAS2/SurviveGotoBLAS2_3.14.tar.gz
# tar xzf SurviveGotoBLAS2_3.14.tar.gz
# cd survivegotoblas2_3.14
# vim Makefile.rule
TARGET=X86_64
BINARY=64
USE_THREAD=1
USE_OPENMP=1
NUM_THREADS=12    #Adjust according to the number of CPUs
NO_CBLAS=1    #CBLAS will be built later
NO_LAPACK=1    #LAPACK will be built later
# make all -j 4
# cp libgoto2.a ~/lib    #A place to leave permanently (here~/copy to lib)

Some packages, such as Theano, require a dynamic link library, so create .so as well.

# mkdir tmp
# cd tmp
# cp ../libgoto2.a .
# ar -x libgoto2.a    # .a.Disassembled into o
# gfortran -shared -lpthread -lgomp  -o libgoto2.so *.o
# cp libgoto2.so ~/lib    #Copy to a place to leave permanently
# export BLAS=~/lib/libgoto2.so    #May not be necessary

CBLAS

You can use the one included in SurviveGotoBLAS, but here you build it yourself. Don't forget to specify the BLAS you built earlier when building CBLAS. Be careful as it is easy to forget when making .so (second line from the bottom).

# wget http://www.netlib.org/blas/blast-forum/cblas.tgz
# tar xzf cblas.tgz
# cd CBLAS
# cp Makefile.LINUX Makefile.in
# vim Makefile.in
BLLIB = ~/lib/libgoto2.a    #See BLAS here
CBLIB = ../lib/libcblas.a    #CBLAS output destination
LOADER = $(FC) -lpthread
CFLAGS = -O3 -m64 -fPIC -DADD_
FFLAGS = -O3 -m64 -fPIC
# make all -j 4
# cp lib/libcblas.a ~/lib    #Copy to a place to leave permanently
# gfortran -L${HOME}/lib -lgoto2 -shared -o libcblas.so *.o
# cp libcblas.so ~/lib    #Copy to a place to leave permanently

LAPACK

Build this yourself as well. Specify the libraries created so far when building LAPACK.

# wget http://www.netlib.org/lapack/lapack.tgz
# tar xzf lapack.tgz
# cd lapack
# cp INSTALL/make.inc.gfortran make.inc
# vi make.inc
OPTS =  -O3 -m64 -fPIC
NOOPT = -O0 -m64 -fPIC
LOADOPTS = -L${HOME}/lib -lgoto2 -lcblas
# make lapacklib -j 4
# cp liblapack.a ~/lib    #Copy to a place to leave permanently
# mkdir tmp
# cd tmp
# cp ../liblapack.a .
# ar -x liblapack.a    # .a.Disassembled into o
# gfortran -L${HOME}/lib -lgoto2 -lcblas -shared -o liblapack.so *.o
# cp liblapack.so ~/lib
# export LAPACK=~/lib/liblapack.so    #May not be necessary

Numpy

The point is to edit site.cfg before building and refer to BLAS and LAPACK there.

# wget http://sourceforge.net/projects/numpy/files/NumPy/1.9.0/numpy-1.9.0.tar.gz/download --no-check-certificate
# tar xzf numpy-1.9.0.tar.gz
# cd numpy-1.9.0
# cp site.cfg.example site.cfg
# vim site.cfg
[DEFAULT]
library_dirs = ${HOME}/lib
[atlas]
atlas_libs = goto2,cblas
# python setup.py build
# python setup.py install

Tested to see if it works. Maybe there should be no error here.

# python -c "import numpy; numpy.test(verbose=2)"

You may want to run the test script at here to check the speed.

# python test_numpy.py

Scipy

# wget http://sourceforge.net/projects/scipy/files/scipy/0.14.0/scipy-0.14.0.tar.gz/download
# tar xzvf scipy-0.14.0.tar.gz
# cd scipy-0.14.0
# vim site.cfg
[DEFAULT]
library_dirs = ${HOME}/lib
[atlas]
atlas_libs = lapack,goto2,cblas

Here, rewrite scipy / lib / lapack / \ _ \ _ init \ _ \ _. Py as follows.

(Old)

__init_old__.py


from scipy.linalg import flapack
from scipy.linalg import clapack
_use_force_clapack = 1
if hasattr(clapack,'empty_module'):
    clapack = flapack
    _use_force_clapack = 0
elif hasattr(flapack,'empty_module'):
    flapack = clapack''

(new)

__init__.py


from scipy.linalg import flapack
clapack = flapack
_use_force_clapack = 0

Finally build and install and finish.

# python setup.py build
# python setup.py install

Let's test this as well. Although it may fail occasionally,

# python -c "import numpy; numpy.test(verbose=2)"

Recommended Posts

Use multithreaded BLAS / LAPACK with numpy / scipy
Use OpenBLAS with numpy, scipy
Use Numpy
Use Numpy, Scipy, scikit-learn on Amazon Linux
LPC with Scipy
Using Intel MKL with NumPy / SciPy (November 2019 version)
ICA with Scipy
I want to use mkl with numpy and scipy under pyenv + poetry environment
CORDIC with Scipy
numpy, scipy If you feel a little slow, check if blas, lapack, atlas are included.
FFT processing with numpy and scipy and low pass filter
Use mecab-ipadic-neologd with igo-python
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use indicator with pd.merge
Use Gentelella with django
Moving average with numpy
Use mecab with Python3
Use DynamoDB with Python
Use pip with MSYS2
Use pyright with Spacemacs
Use python with docker
Use TypeScript with django-compressor
Create filter with scipy
How to use numpy
Use LESS with Django
Use MySQL with Django
Learn with Cheminformatics NumPy
Matrix concatenation with Numpy
Hamming code with numpy
Normarize data with Scipy
Regression analysis with NumPy
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Extend NumPy with Rust
Use nim with Jupyter
See the power of speeding up with NumPy and SciPy
[python] A note when trying to use numpy with Cython