BLAS and LAPACK#

Default behavior for BLAS and LAPACK selection#

When a NumPy build is invoked, BLAS and LAPACK library detection happensautomatically. The build system will attempt to locate a suitable library,and try a number of known libraries in a certain order - most to leastperformant. A typical order is: MKL, Accelerate, OpenBLAS, FlexiBLAS, BLIS,plainlibblas/liblapack. This may vary per platform or over releases.That order, and which libraries are tried, can be changed through theblas-order andlapack-order build options, for example:

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

The first suitable library that is found will be used. In case no suitablelibrary is found, the NumPy build will print a warning and then use (slow!)NumPy-internal fallback routines. In order to disallow use of those slow routines,theallow-noblas build option can be used:

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

By default the LP64 (32-bit integer) interface to BLAS and LAPACK will be used.For building against the ILP64 (64-bit integer) interface, one must use theuse-ilp64 build option:

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

Selecting specific BLAS and LAPACK libraries#

Theblas andlapack build options are set to “auto” by default, whichmeans trying all known libraries. If you want to use a specific library, youcan set these build options to the library name (typically the lower-case namethatpkg-config expects). For example, to select plainlibblas andliblapack (this is typically Netlib BLAS/LAPACK on Linux distros, and canbe dynamically switched between implementations on conda-forge), use:

$ # 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

Other options that should work (as long as they’re installed withpkg-config support; otherwise they may still be detected but things areinherently more fragile) includeopenblas,mkl,accelerate,atlas andblis.

Using pkg-config to detect libraries in a nonstandard location#

The way BLAS and LAPACK detection works under the hood is that Meson triesto discover the specified libraries first withpkg-config, and thenwith CMake. If all you have is a standalone shared library file (e.g.,armpl_lp64.so in/a/random/path/lib/ and a corresponding headerfile in/a/random/path/include/), then what you have to do is craftyour own pkg-config file. It should have a matching name (so in thisexample,armpl_lp64.pc) and may be located anywhere. ThePKG_CONFIG_PATH environment variable should be set to point to thelocation of the.pc file. The contents of that file should be:

libdir=/path/to/library-dir      # e.g., /a/random/path/libincludedir=/path/to/include-dir  # e.g., /a/random/path/includeversion=1.2.3                    # set to actual versionextralib=-lm -lpthread -lgfortran   # if needed, the flags to link in dependenciesName: armpl_lp64Description: ArmPL - Arm Performance LibrariesVersion: ${version}Libs: -L${libdir} -larmpl_lp64      # linker flagsLibs.private: ${extralib}Cflags: -I${includedir}

To check that this works as expected, you should be able to run:

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

Full list of BLAS and LAPACK related build options#

BLAS and LAPACK are complex dependencies. Some libraries have more options thatare exposed via build options (seemeson.options in the root of therepo for all of NumPy’s build options).

  • blas: name of the BLAS library to use (default:auto),

  • lapack: name of the LAPACK library to use (default:auto),

  • allow-noblas: whether or not to allow building without externalBLAS/LAPACK libraries (default:true),

  • blas-order: order of BLAS libraries to try detecting (default may vary per platform),

  • lapack-order: order of LAPACK libraries to try detecting,

  • use-ilp64: whether to use the ILP64 interface (default:false),

  • blas-symbol-suffix: the symbol suffix to use for the detected libraries (default:auto),

  • mkl-threading: which MKL threading layer to use, one ofseq,iomp,gomp,tbb (default:auto).