NumPy/SciPy Application Note
Step 1 - Overview
This guide is intended to help current NumPy/SciPy users to take advantage of Intel® Math Kernel Library (Intel® MKL).
NumPy automatically maps operations on vectors and matrices to the BLAS and LAPACK functions wherever possible. Since Intel® MKL supports these de-facto interfaces, NumPy can benefit from Intel MKL optimizations through simple modifications to the NumPy scripts.
NumPy is the fundamental package required for scientific computing with Python. It consists of:
- a powerful N-dimensional array object
- sophisticated (broadcasting) functions
- tools for integrating C/C++ and Fortran code
- useful linear algebra, Fourier transform, and random number capabilities.
Besides its obvious scientific uses, NumPy can also be used as an efficient multi-dimensional container of generic data.
For more information on NumPy, please visit http://NumPy.scipy.org/
SciPy include modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and more. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. The SciPy library is built to work with NumPy arrays, and provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization for python users. Please refer http://www.scipy.org for more details on SciPy.
Version Information
This application note was created to help NumPy/SciPy users to make use of the latest versions of Intel MKL on Linux platforms.
The instructions given in this articles apply to Intel MKL 11.0 and above and Intel Compiler 13.0 and above.
Step 2 - Downloading NumPy and SciPy Source Code
The NumPy source code can be downloaded from:
http://www.scipy.org/Download
Prerequisites
Intel MKL can be obtained from the following options:
Download a FREE evaluation version of the Intel MKL product.
Download the FREE non-commercial* version of the Intel MKL product.
All of these can be obtained at: Intel® Math Kernel Library product web page.
Intel® MKL is also bundled with the following products
Intel® Parallel Studio XE 2013
Intel® Composer XE 2013
Intel® Cluster Studio XE 2013
Step 3 - Configuration
Use the following commands to extract the NumPy tar files from the downloaded NumPy-x.x.x.tar.gz.
$gunzip numpy-x.x.x.tar.gz $tar -xvf numpy-x.x.x.tar
The above will create a directory named numpy-x.x.x
And to extract SciPy, use the below commands
$gunzip scipy-x.x.x.tar.gz $tar -xvf scipy-x.x.x.tar.gz
The scipy-x.x.x directory will be created with extracted files.
Make sure that C++ and FORTRAN compilers are installed and they are in PATH. Also set LD_LIBRARY_PATH to your compiler (C++ and FORTRAN), and MKL libraries.
Step 4 - Building and Installing NumPy
Change directory to numpy-x.x.x
Create a site.cfg from the existing one
Edit site.cfg as follows:
Add the following lines to site.cfg in your top level NumPy directory to use Intel® MKL, if you are building on Intel 64 platform, assuming the default path for the Intel MKL installation from the Intel Parallel Studio XE 2013 or Intel Composer XE 2013 versions:
[mkl]
library_dirs = /opt/intel/mkl/composer_xe_2013/lib/intel64
include_dirs = /opt/intel/mkl/include
mkl_libs = mkl_rt
lapack_libs =
If you are building NumPy for 32 bit, please add as the following
[mkl] library_dirs = /opt/intel/composer_xe_2013/mkl/lib/ia32
include_dirs = /opt/intel/mkl/include
mkl_libs = mkl_rt
lapack_libs =
Modify cc_exe in numpy/distutils/intelccompiler.py to be something like:
self.cc_exe = 'icc -O3 -g -fPIC -fp-model strict -fomit-frame-pointer -openmp -xhost'
Here we use, -O3, optimizations for speed and enables more aggressive loop transformations such as Fusion, Block-Unroll-and-Jam, and collapsing IF statements, -openmp for OpenMP threading and -xhost option tells the compiler to generate instructions for the highest instruction set available on the compilation host processor. If you are using the ILP64 interface, please add -DMKL_ILP64 compiler flag.
Run icc --help for more information on processor-specific options, and refer Intel Compiler documentation for more details on the various compiler flags.
Modify the the Fortran compiler configuration in numpy-x.x.x/numpy/distutil/fcompiler/intel.py to use the following compiler options for the Intel Fortran Compiler:
For ia32 and Intel64
ifort -xhost -openmp -fp-model strict -fPIC
If you are using ILP64 interface of Intel MKL, please add -i8 flag above. If you are using older versions of Numpy/SciPy, please refer the new intel.py for your reference from the latest version of NumPy, which can be replaced to use the above mentioned compiler options.
Compile and install NumPy with the Intel compiler: (on 64-bit platforms replace "intel" with "intelem")
$python setup.py config --compiler=intel build_clib --compiler=intel build_ext --compiler=intel install
Build and Install SciPy
Compile and install SciPy with the Intel Compilers: (On 64-bit platforms replace "intel" with "intelem")
$python setup.py config --compiler=intel --fcompiler=intel build_clib --compiler=intel
--fcompiler=intel build_ext --compiler=intel --fcompiler=intel install
Setup Library path for Intel MKL and Intel Compilers
If you build NumPY/SciPy for Intel64 bit platforms:
$export LD_LIBRARY_PATH=/opt/intel/composer_xe_2013/mkl/lib/intel64:/opt/intel/composer_xe_2013/lib/intel64:$LD_LIBRARY_PATH
If you build NumPY for ia32 bit platforms:
$export LD_LIBRARY_PATH=/opt/intel/composer_xe_2013/mkl/lib/ia32:/opt/intel/composer_xe_2013/lib/ia32:$LD_LIBRARY_PATH
It is possible that LD_LIBRARY_PATH causes a problem, if you have installed Intel MKL and Intel Composer XE in other directories than the standard ones. The only solution we ha've found that always works is to build Python, NumPy and SciPy inside an environment where you've set the LD_RUN_PATH variable, e.g: for ia32 platform:
$export LD_RUN_PATH=/opt/intel/composer_xe_2013/lib/ia32:/opt/intel/composer_xe_2013/mkl/lib/ia32
Note:We recommend users to use arrays with 'C' ordering style which is row-major, which is default than Fortran Style which is column-major, and this is because NumPy uses CBLAS and also to get better performance.
Appendex A: Example:
Please see below an example Python script for matrix multiplication that you can use Numply installed with Intel MKL which has been provided for illustration purpose.
import numpy as np
import time
N = 6000
M = 10000
k_list = [64, 80, 96, 104, 112, 120, 128, 144, 160, 176, 192, 200, 208, 224, 240, 256, 384]
def get_gflops(M, N, K):
return M*N*(2.0*K-1.0) / 1000**3
np.show_config()
for K in k_list:
a = np.array(np.random.random((M, N)), dtype=np.double, order='C', copy=False)
b = np.array(np.random.random((N, K)), dtype=np.double, order='C', copy=False)
A = np.matrix(a, dtype=np.double, copy=False)
B = np.matrix(b, dtype=np.double, copy=False)
C = A*B
start = time.time()
C = A*B
C = A*B
C = A*B
C = A*B
C = A*B
end = time.time()
tm = (end-start) / 5.0
print "{0:4}, {1:9.7}, {2:9.7}".format(K, tm, get_gflops(M, N, K) / tm)
Appendix B: Performance Comparison


Please click Examples.py to download the examples for LU, Cholesky and SVD.
Please note the charts are generated with the Intel MKL 11.0 version.

Comments
The link to the intel.py file is broken!
But I could find it here:
http://software.intel.com/sites/default/files/m/d/4/1/d/8/intel.py
Note for Ubuntu and maybe Mac OS X users (<- from what I've read on the net), the building and install parts of Numpy and Scipy should be done in two steps:
First to build (with intelem instead of intel for 64 bits processors):
python setup.py config --compiler=intel build_clib --compiler=intel build_ext --compiler=intel
Then install:
sudo python setup.py install
Same for Scipy.
For numpy to find the libraries (in Ubuntu), add the path to the needed libraries in /etc/ld.so.conf and then run ldconfig
Cheers
Note for Ubuntu and maybe Mac OS X users (<- from what I've read on the net), the building and install parts of Numpy and Scipy should be done in two steps:
First to build (with intelem instead of intel for 64 bits processors):
python setup.py config --compiler=intel build_clib --compiler=intel build_ext --compiler=intel
Then install:
sudo python setup.py install
Same for Scipy.
For numpy to find the libraries (in Ubuntu), add the path to the needed libraries in /etc/ld.so.conf and then run ldconfig
Cheers
In order to be able to install Scipy you must first set the path to the libraries (as explained in the previous comment) for Numpy, because Scipy import Numpy during the building or installation process.
If you are using the latest NumPy/SciPy, you don't need to replace the intel.py, since has been updated in the NumPy/SciPy source.
I used this page as a guide to build NumPy with Intel Composer 2013 and the latest MKL. I would like to clarify the modifications that need to be made to the distutils files:
When you edit intelccompiler.py, make sure you are editing the section of the file that corresponds to the command-line flag that you will use when you run setup.py. For example, for a 64-bit system, you need to edit the class called IntelEM64TCCompiler. In the __init__ function, set
You should then build NumPy with the commandThen modify the file numpy/distutils/fcompiler/intelccompiler.py Modify the class that corresponds to the command-line flags you will use to build NumPy. For a 64-bit system, modify the class IntelEM64TFCompiler. Look for the function called get_flags_arch and add the command-line options as follows:
Thanks for posting this article! More information can be found on my blog (see link below).
After building both numpy and scipy using the instructions above, I get the error when importing scipy:
ImportError: /usr/lib/atlas/libblas.so.3gf: undefined symbol: _gfortran_st_write_done
http://www.scipy.org/Installing_SciPy/BuildingGeneral states that I should NOT use ifort when building numpy ... otherwise I'll get this error ...which defeats the point of using the Intel compilers, right?
Any suggestions on how to fix this problem?
Thanks!
John