Boosting NumPy with MKL

The Intel Math Kernel Library (MKL) contains a collection of highly optimized numerical functions.  Among others, it provides implementations of Blas functions and Lapack functions for various linear algebra problems.

A program, which is dynamically linked against the standard Blas and Lapack libraries, can easily benefit from alternative optimized implementations by replacing and by optimized variants.  The MKL, however, does not come with a drop-in replacement for and But it comes with a tool for building such drop-in replacements.

The MKL is shipped with the Intel® Parallel Studio XE and with the Intel® System Studio.  The directory mkl/tools/builders under the installation directory of Intel® Parallel Studio XE and the Intel® System Studio contains a makefile to build and on the basis of the MKL.  I found it appropriate to tweak this makefile for better compatibility.

  • Replace “IFACE_COMP_PART=intel” by “IFACE_COMP_PART=gf”.
  • Replace “LIBM=-lm” by “LIBM=-lm -mkl”.
  • Replace all occurrences of “gcc” by “icc”.

With these little changes

# make libintel64 interface=lp64 export=blas_example_list name=libblas


# make libintel64 interface=lp64 export=lapack_example_list name=liblapack

will build new and  Now, one could overwrite the system’s (and and (and by the new version and all dynamically linked programs would benefit from the optimized libraries.  On Debian-based systems, however, one should use the alternatives mechanism instead.  This allows to switch easily between different Blas and Lapack implementations.  The following commands add the optimized libraries and make them the default choice (paths may differ on different Linux installations):

# update-alternatives --install /usr/lib/ /usr/local/intel/mkl/tools/builder/ 800
# update-alternatives --install /usr/lib/ /usr/local/intel/mkl/tools/builder/ 800
# update-alternatives --install /usr/lib/ /usr/local/intel/mkl/tools/builder/ 800
# update-alternatives --install /usr/lib/ /usr/local/intel/mkl/tools/builder/ 800

To test the effect of various Blas and Lapack implementations I wrote a small python script, which calculates the eigenvalues of Hermitian matrices of varying size.  Depending of the chosen Blas and Lapack implementations this more than an order of magnitude faster.  The Netlib reference implementation of Blas and Lapack is rather slow.  OpenBlas is much faster than Netlib, but the MKL is even faster.  Both are able to take advantage of multi-core architectures, which explains partly the performance boost.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# compatibilty for Python 3
from __future__ import absolute_import, division, print_function
from pylab import *
import timeit
sizes=array([64, 128, 256, 512, 1024, 2048, 4096])
for N in sizes:
    A=randn(N, N)+1j*rand(N, N)
    print(N, end-start)
savetxt('bench.dat', vstack((sizes, times)).T, fmt='%i\t%g')
Benchmark results for the code shown above on an Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz (dual core CPU).
Benchmark results for the code shown above on an Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz (dual core CPU) with various Blas and Lapack implementations.


Leave a Reply

Your email address will not be published. Required fields are marked *