It seems that the calculation speed changes depending on the BLAS used for Numpy [Python]

2019/6/4

In "numpy", the numerical operation library called BLAS is responsible for the actual processing.

The BLAS used depends on whether you install numpy with "conda install" or "pip install", and depending on which BLAS you use, the processing speed will be affected not only by numpy but also by the libraries that depend on it.

This time, I will introduce the BLAS and the actual speed difference, and how to check which BLAS you are using.

BLAS and their types

What is BLAS

Regarding BLAS, the explanation on the following site was very easy to understand, so I will quote it.

BLAS stands for Basic Linear Algebra Subprograms.In a nutshell, it's a library of basic linear operations.For example, matrix multiplication, vector norm calculation, etc.The point is to stop implementing these calculations (redeveloping the wheel) and use libraries to code efficiently and efficiently.

Easy to use BLAS

Since BLAS is also used behind numpy, it is said that calculation time will differ depending on which BLAS is called when numpy is executed.

BLAS type

Various types of BLAS have been developed.Let's take a look at some of the typical ones.

Open BLAS

An open source implementation of BLAS.In numpy installed with pip, OpenBLAS is called internally to perform operations.
It is compatible with various CPUs, and it is said that it can achieve the same speed as Intel MKL for Intel's Sandy Bridge CPU.It is well known for its multithreading function, and it seems that the number of cores and speedup are well proportional.

Intel MKL (Intel Math Kernel Library)

BLAS developed by Intel. Only Intel CPUs are supported and cannot be used with CPUs made by other companies. Since it is optimized for Intel CPUs such as Xeon, Core i series, and Atom, it is faster to use intel MKL as an arithmetic library on a computer that uses these CPUs.Intel MKL is used for numpy installed by "conda install" with Anaconda or Miniconda.

ATLAS (Automatically Tuned Linear Algebra Software)

One of the free BLAS.As the name suggests, tuning sets the optimum parameters for the hardware to be installed and realizes high-speed calculation.

Difference in calculation time due to difference in BLAS

The fact that BLAS used in the back of numpy affects the processing speed means that it is related to the execution speed of the library that depends on numpy.In other words, it also affects scipy, pandas, scikit-learn, and tensorflow.

A lot of techies are comparing benchmarks for BLAS differences (conda install vs. pip install), but let's take the one published on the Intel® Developer Zone.
Matrix calculation by scipy shows the time required for multiplication of complicated matrices as it goes to the right, and intel MKL can process faster than ATLAS.

Comparison of matrix calculation by scipy in intel MKL and ATLAS
Comparison of matrix calculation by scipy in intel MKL and ATLAS
Quote source:https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl/

The following is from Anaconda official. We compared the number of trained images per second for each deep learning model with tensorflow installed by either conda install or pip install, and the difference is up to 1 times.

Difference between pip (OpenBLAS) and conda (intel MKL) in each deep learning model
Difference between pip (OpenBLAS) and conda (intel MKL) in each deep learning model
(Cpu is Intel® Xeon® Gold 6130)
Quote source:https://www.anaconda.com/tensorflow-in-anaconda/

Other comparisons include pandas and scikit learn (The performance of Intel vs. Anaconda vs. vanilla Python – my personal benchmark). Since conda has its own library installation and management, it seems to be a favorable result in many cases.

How to check BLAS in Python

__config__.show()Just run.

import numpy as np np .__ config __. show ()

>>
mkl_info: libraries = ['mkl_rt'] library_dirs = ['C: / Users /'] define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)] include_dirs = ['C: \\ Program Files ('C: \\ Program Files (' x86) \\ IntelSWTools \\ compilers_and_libraries_2019.0.117 \\ windows \\ mkl','C: \\ Program Files (x86) \\ IntelSWTools \\ compilers_and_libraries_2019.0.117 \\ windows \\ mkl \\ include',' C: \\ Program Files (x86) \\ IntelSWTools \\ compilers_and_libraries_2019.0.117 \\ windows \\ mkl \\ lib','C: / Users / programming / Miniconda3 / envs / ML \\ Library \\ include']

If mkl_info is displayed, intel MKL is used.