Skip to main content

Link numpy & scipy against the MKL

· 2 min read
Leopold Talirz

The pip package management system is a very convenient way of managing custom python installations that are not managed by the operating system (such as virtual python environments).

pip install numpy scipy essentially works out of the box. Yet, when it comes to numpy and scipy, significant speed can be gained by linking these modules against the Intel Math Kernel Library (MKL) for linear algebra operations.

You can tell pip to do that in the following way:

(1) Tell numpy where to find the MKL

Create a file .numpy-site.cfg in your home directory with a content similar to

[mkl]
library_dirs = /opt/intel/composer_xe_2013.1.117/mkl/lib/intel64
include_dirs = /opt/intel/composer_xe_2013.1.117/mkl/include
mkl_libs = mkl_rt
lapack_libs =

(2) Tell pip to use the Intel compiler

pip install numpy \
--global-option="config" \
--global-option="--compiler=intelem" \
--global-option="build_clib" \
--global-option="--compiler=intelem" \
--global-option="build_ext" \
--global-option="--compiler=intelem"

pip install scipy \
--global-option="config" \
--global-option="--compiler=intelem" \
--global-option="--fcompiler=intelem" \
--global-option="build_clib" \
--global-option="--compiler=intelem" \
--global-option="--fcompiler=intelem" \
--global-option="build_ext" \
--global-option="--compiler=intelem" \
--global-option="--fcompiler=intelem"

Alternatively, you may also create a file .pydistutils.cfg in your home directory with content

[config]
compiler=intelem
fcompiler=intelem
[build_clib]
compiler=intelem
fcompiler=intelem
[build]
compiler=intelem
[build_ext]
compiler=intelem
fcompiler=intelem

and then simply run

pip install numpy
pip install scipy

Note: If you don't provide this info to pip, it will look for and use the GNU compiler collection by default.

Note: While numpy installed without problems I did receive a compilation error with icc 14.0.0. Possibly related to this.

Parts (1) and (2) relied on the following StackExchange posts