Linux下pip安装的numpy(或scipy等)默认是链接openblas编译的。实测在个人电脑下矩阵不大(大约10k)的情况下做矩阵运算速度略优于Intel mkl,但是大矩阵下就明显慢了,而且处理并行不够智能。所以还是自己手动链接编译Intel mkl更好些。
- 以Debian为例。Debian 12 (bookworm)现在默认不让在系统层面用pip安装python package,必须新建虚拟环境(假设在个人用户主目录
/home/xxxx/
下创建虚拟环境文件夹venv):
$ python3 -m venv home/xxxx/venv
进入虚拟环境:
$ source /home/xxxx/venv/bin/activate
- 根据官网说明下载Intel mkl:https://www.intel.com/content/www/us/en/developer/tools/oneapi/base-toolkit-download.html?operatingsystem=linux&distributions=aptpackagemanager
$ sudo apt update
$ sudo apt install -y gpg-agent wget
$ wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB | gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null
$ echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list
$ sudo apt update
$ sudo apt install intel-basekit
当前mkl的版本是2024.1,在我本地的安装目录是/opt/intel/oneapi/mkl/2024.1/
。
- 在用户主目录(
/home/xxxx/
)下创建numpy的配置文件.numpy-site.cfg
并输入如下内容,指明mkl的位置:
[mkl]
library_dirs = /opt/intel/oneapi/mkl/2024.1/lib/intel64
include_dirs = /opt/intel/oneapi/mkl/2024.1/include
libraries = mkl_rt
- 编译numpy:
$ pip install numpy --no-binary :all:
编译通过后成功链接了mkl库:
>>> import numpy
>>> numpy.show_config()
Build Dependencies:
blas:
detection method: pkgconfig
found: true
include directory: /opt/intel//oneapi//mkl/2024.1/lib/pkgconfig/../../include
lib directory: /opt/intel//oneapi//mkl/2024.1/lib/pkgconfig/../../lib
name: mkl-dynamic-lp64-seq
openblas configuration: unknown
pc file directory: /usr/local/share/pkgconfig
version: '2024.1'
实测矩阵运算速度让人满意。
类似的方法可以安装其他python科学计算库,比如numba,scipy等。
参考网址:https://www.yodiw.com/setup-numpy-with-oneapi-mkl-in-ubuntu/