Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I link numpy to use MKL as backend?

I have a numpy install and it shows no BLAS backend available:

(pyrepoux) bash-4.2$ python
Python 3.7.3 | packaged by conda-forge | (default, Dec  6 2019, 08:54:18) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.show_config()
blas_mkl_info:
  NOT AVAILABLE
blis_info:
  NOT AVAILABLE
openblas_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
  NOT AVAILABLE
openblas_lapack_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
    libraries = ['openblas', 'openblas']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None)]

I can do pip install mkl but still the same output as above. How can I link numpy to use MKL as BLAS / LAPACK backend?

like image 316
SkyWalker Avatar asked Oct 19 '25 07:10

SkyWalker


2 Answers

You can try using in intel python. Create an environment with intel python and required packages like intel-mkl, intel-numpy etc.

conda create -n <env-name> intelpython3_full python=3.7.3
conda activate <env_name>
pip install mkl
pip install intel-numpy

and try importing numpy and running np.show_config()

Refer : https://pypi.org/project/mkl/ https://pypi.org/project/intel-numpy/

The better way is to install the Intel base Toolkit and source the variables. Intel mkl and intel python is available with the kit. You just need to source the environment variables

source <basekit-installation-directory>/setvars.sh

Installation guide : https://software.intel.com/content/www/us/en/develop/documentation/installation-guide-for-intel-oneapi-toolkits-linux/top.html

You can also try the suggestion provided by Jerome Richard – try setting the LD_LIBRARY_PATH and LD_PRELOAD path to the mkl library .so file. Refer : https://software.intel.com/content/www/us/en/develop/articles/optimizing-without-breaking-a-sweat.html

like image 194
Raeesa - Intel Avatar answered Oct 20 '25 20:10

Raeesa - Intel


NumPy has to be compiled against mkl, so you can't just switch BLAS/LAPACK libraries at runtime by installing mkl. Instead you will need to install a NumPy version that has been compiled against the MKL libraries.

Since the other answer is somewhat outdated (Intel Python no longer has current NumPy versions), let me provide a list of current options here:

Conda-based options

Here I'm using pixi as package manager, but similar results can be obtained using conda or mamba.

  • Install from anaconda (linked against mkl by default):
    pixi init -c anaconda
    pixi add numpy
    
  • Install from conda-forge (requires libblas=*=*mkl and "liblapack=*=*mkl")
    pixi init -c conda-forge
    pixi add numpy "libblas=*=*mkl" "liblapack=*=*mkl"
    

Pip-based options

  • Manually download wheels from https://github.com/cgohlke/numpy-mkl-wheels (only works on Windows), then run
    pip install /path/to/numpy_wheel.whl
    
  • Install from https://github.com/urob/numpy-mkl
    pip install numpy --extra-index-url https://urob.github.io/numpy-mkl
    
like image 28
Bob Avatar answered Oct 20 '25 22:10

Bob