BLAS 和 LAPACK#

BLAS 和 LAPACK 选择的默认行为#

当调用 NumPy 构建时,会自动进行 BLAS 和 LAPACK 库的检测.构建系统将尝试找到合适的库,并按照一定的顺序尝试一些已知的库 - 从性能最高到最低. 典型的顺序是:MKL,Accelerate,OpenBLAS,FlexiBLAS,BLIS,普通的 libblas / liblapack . 这可能因平台或版本而异. 该顺序以及尝试哪些库可以通过 blas-orderlapack-order 构建选项来更改,例如:

$ python -m pip install . -Csetup-args=-Dblas-order=openblas,mkl,blis -Csetup-args=-Dlapack-order=openblas,mkl,lapack

将使用找到的第一个合适的库. 如果没有找到合适的库,NumPy 构建将打印一个警告,然后使用(速度慢!)NumPy 内部的回退例程. 为了禁止使用这些慢速例程,可以使用 allow-noblas 构建选项:

$ python -m pip install . -Csetup-args=-Dallow-noblas=false

默认情况下,将使用 BLAS 和 LAPACK 的 LP64(32 位整数)接口. 要构建 ILP64(64 位整数)接口,必须使用 use-ilp64 构建选项:

$ python -m pip install . -Csetup-args=-Duse-ilp64=true

选择特定的 BLAS 和 LAPACK 库#

默认情况下, blaslapack 构建选项设置为“auto”,这意味着尝试所有已知的库. 如果要使用特定的库,可以将这些构建选项设置为库名称(通常是 pkg-config 期望的小写名称). 例如,要选择普通的 libblasliblapack (这通常是 Linux 发行版上的 Netlib BLAS/LAPACK,并且可以在 conda-forge 上动态切换实现),请使用:

$ # for a development build
$ spin build -C-Dblas=blas -C-Dlapack=lapack

$ # to build and install a wheel
$ python -m build -Csetup-args=-Dblas=blas -Csetup-args=-Dlapack=lapack
$ pip install dist/numpy*.whl

$ # Or, with pip>=23.1, this works too:
$ python -m pip install . -Csetup-args=-Dblas=blas -Csetup-args=-Dlapack=lapack

其他应该有效的选项(只要它们安装了 pkg-config 支持;否则它们仍然可能被检测到,但事情本质上更脆弱)包括 openblas , mkl , accelerate , atlasblis .

使用 pkg-config 检测非标准位置的库#

BLAS 和 LAPACK 检测的底层工作方式是 Meson 尝试首先使用 pkg-config ,然后使用 CMake 发现指定的库. 如果你只有一个独立的共享库文件(例如, /a/random/path/lib/ 中的 armpl_lp64.so/a/random/path/include/ 中对应的头文件),那么你必须做的是制作你自己的 pkg-config 文件. 它应该有一个匹配的名称(因此在本例中,为 armpl_lp64.pc ),并且可以位于任何位置. 应该设置 PKG_CONFIG_PATH 环境变量以指向 .pc 文件的位置. 该文件的内容应为:

libdir=/path/to/library-dir      # e.g., /a/random/path/lib
includedir=/path/to/include-dir  # e.g., /a/random/path/include
version=1.2.3                    # set to actual version
extralib=-lm -lpthread -lgfortran   # if needed, the flags to link in dependencies
Name: armpl_lp64
Description: ArmPL - Arm Performance Libraries
Version: ${version}
Libs: -L${libdir} -larmpl_lp64      # linker flags
Libs.private: ${extralib}
Cflags: -I${includedir}

要检查这是否按预期工作,你应该能够运行:

$ pkg-config --libs armpl_lp64
-L/path/to/library-dir -larmpl_lp64
$ pkg-config --cflags armpl_lp64
-I/path/to/include-dir