Build NumPy/SciPy with Intel® MKL and Intel® Compilers on Windows and Linux

Install NumPy/SciPy from Intel® Distribution for Python*

These application notes are only for simple reference for developers who want to build NumPy*/SciPy * from source. Furthermore, we strongly recommend developer to use  Intel®  Distribution for Python*, which has prebuilt NumPy/SciPy based on Intel® Math Kernel Library (Intel® MKL) and more. 
Please refer to Installing Intel® Distribution for Python* Install Guide

Build NumPy/SciPy from Source 

Step 1 - Overview

This guide is intended to help current NumPy/SciPy developer to take advantage of Intel® Math Kernel Library (Intel® MKL), Intel® Fortran and Intel® C++ Compilers on Microsoft Windows platforms and Linux platforms.
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 one of the most fundamental packages 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 https://en.wikipedia.org/wiki/NumPy

SciPy include modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and more.  The SciPy library utilizes 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, Intel® Fortran and Intel® C++ Compilers on Windows platforms and Linux platforms.
The instructions given in this article apply to Intel® MKL 2020 and Intel® Compiler19.1. The procedures described in this article have been tested for Python3.7.4. These have been verified with Intel® MKL 2020, Intel® Compilers 19.1 from Intel® Parallel Studio XE 2020 update 1, NumPy 1.19.0 and SciPy 1.5.0.

Step 2 - Download NumPy and SciPy Source Code

The NumPy source code can be downloaded from:
https://github.com/numpy/numpy
The SciPy source code can be downloaded from:
https://github.com/scipy/scipy

Prerequisites

Intel® MKL can be obtained from the following options:
FREE evaluation version of the Intel®MKL product.
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 and Intel® Fortran are also bundled with  Intel® Parallel Studio XE Professional, Composer or Cluster Editions.
Python 3.7.4
Cython 0.29.17 
If you build on Windows platforms, Visual Studio 2017 and Visual C++ Build Tools 2015 are also required.  When install VS2017, just select the “VC++ 2015.3 v14.00 (v140) toolset for desktop” at the bottom of the “Desktop development with C++” section.

Note: The Visual C++ build tools from VS2017 is the latest v141 by default. According to analyzing the source code of python 3.7.4, the v140 build tools is also required.  

Step 3 - Configuration

Extract the NumPy zip file from the downloaded numpy-x.x.x.zip, it will create a directory named numpy-x.x.x.
Extract the SciPy zip file from the downloaded scipy-x.x.x.zip, it will create directory named scipy-x.x.x.

Windows platforms

Set Intel®Fortran compiler environment on Windows. Go to All Programs -> Intel Parallel Studio XE 20XX -> Command Prompt, select Intel64 Visual Studio XX mode for Intel64 build binaries specifically.
Then run the following command to setup the environment for the v140 toolset. 

> "C:\Program Files (x86)\Microsoft Visual Studio \2017 \Professional \VC\Auxiliary\ Build \vcvarsall.bat" x64 -vcvars_ver=14.0

 

Linux platforms


Open a terminal, change directory to /opt/intel/bin/, and run the following command to set Intel compiler environment on Linux:

$source compilervars.sh intel64

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:


Windows platforms


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 or Intel Composer XE versions. 

[mkl]
library_dirs = C:\Program Files (x86)\IntelSWTools\compilers_and_libraries \windows\mkl\lib\intel64; 
include_dirs = C:\Program Files (x86)\IntelSWTools\compilers_and_libraries \windows\mkl\include
libraries = mkl_rt


Firstly modify self.compile_options in numpy/distutils/intelccompiler.py according to your needs. For example:

self.compile_options = [ '/nologo', '/O3', '/MD', '/W3', '/Qstd=c99', '/QxHost', '/fp:strict', '/Qopenmp']


Here we use, /O3, optimizations for speed and enables more aggressive loop transformations such as Fusion, Block-Unroll-and-Jam, and collapsing IF statements, /Qopenmp for OpenMP threading and /Qxhost 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 icl /help for more information on processor-specific options, and refer Intel Compiler documentation for more details on the various compiler flags.
Compile and install NumPy with the Intel Fortran and Intel C++ compilers: For Intel64 platform, run:

> python setup.py config --compiler=intelemw build_clib --compiler=intelemw build_ext --compiler=intelemw install

 

Linux Platforms

If you are building NumPy on Linux platforms, please add as the following, 

[mkl]
library_dirs = /opt/intel/compilers_and_libraries/linux/mkl/lib/intel64
include_dirs = /opt/intel/compilers_and_libraries/linux/mkl/include
mkl_libs = mkl_rt
lapack_libs =


Modify self.cc_exe line in numpy/distutils/intelccompiler.py, please modify this line part of the IntelEM64TCCompiler Class and compiler_type is 'intelem'

mpopt= 'openmp' if v and v < '15' else 'qopenmp'
self.cc_exe = ('icc -O3 -g -fPIC -fp-model strict -fomit-frame-pointer -xhost -{}').format(mpopt)


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 SIMD 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 Fortran compiler configuration in numpy-x.x.x /numpy /distutil /fcompiler /intel.py to use the following compiler options for the Intel Fortran Compiler:

mpopt= 'openmp' if v and v < '15' else 'qopenmp'
return ['-xhost -fp-model strict -fPIC -{}'.format(mpopt)]


If you are using the latest source, this is already modified in intel.py. You may explore using other compiler optimization flags.


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 with Intel compiler: for Intel64 platform, run:

$python setup.py config --compiler=intelem build_clib --compiler=intelem build_ext --compiler=intelem install


You may pass --prefix=<install_dir>, in case, if you want to install in a directory of your choice. In this case, after your successful NumPy build, you have to export PYTHONPATH environment variable pointing to your install folder.

$export PYTHONPATH=<install_dir>/lib64/python.x/site-packages

Build and Install SciPy

Windows Platforms

For NumPy versions later than 1.10.x, compile and install SciPy with the Intel Fortran and Intel C++ compilers on windows platforms, for Intel64 build:

> python setup.py config --compiler=intelemw --fcompiler=intelvem build_clib --compiler=intelemw --fcompiler=intelvem build_ext --compiler=intelemw --fcompiler=intelvem install

Linux Platforms

Compile and install SciPy with the Intel Compilers on Linux platforms, for Intel64 build:

$python setup.py config --compiler=intelem --fcompiler=intelem build_clib --compiler=intelem --fcompiler=intelem build_ext --compiler=intelem --fcompiler=intelem install

 

Setup Library path for Intel MKL and Intel Compilers

If you build NumPY/SciPy for Intel64 platforms on Linux platforms:

$export LD_LIBRARY_PATH=/opt/intel/compilers_and_libraries/linux/mkl/lib/intel64/:/opt/intel/compilers_and_libraries/linux/lib/intel64:$LD_LIBRARY_PATH   

It is possible that LD_LIBRARY_PATH causes a problem. This occurs when you have installed Intel MKL and Intel Composer XE in non-standard directories. The only solution we have found that always works is to build Python, NumPy and SciPy inside an environment where you've set the LD_RUN_PATH variable.

$export LD_Run_PATH=/opt/intel/compilers_and_libraries/linux/mkl/lib/intel64/:/opt/intel/compilers_and_libraries/linux/lib/intel64:$LD_LIBRARY_PATH  

 

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 to get better performance.

Product and Performance Information

1

Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.