Building Numpy/Scipy with Intel® MKL and Intel® Compilers on Windows

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), Intel® Fortran and Intel® C++ Compilers on Microsoft Windows platform.

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.

The instructions given in this articles apply to Intel MKL 11.0 and above and Intel Compiler 15.0 and above. The procedures described in this article have been tested for Python 2.7.3 and Python 3.4.2. These have been verified with Intel MKL 11.2, 11.3, Intel Compilers 15.0, 16.0, NumPy 1.8.2, 1.9.1, 1.10.x and SciPy 0.15.1.

Intel C++ compiler support on Windows has been integrated into Numpy Master and Maintenance-1.10.x branches.

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 and Intel® Fortran are also bundled with Intel® Parallel Studio XE Professional, Composer or Cluster Editions.

Visual Studio 2008 or above.

Python 2.7.3 (compatible with Numpy 1.8.2) or Python 3.4.2 (compatible with Numpy 1.9.1 or above).

Note: You will need to rebuild Python from source files. This is due to the downloaded Python binary on Windows might be incompatible with the Visual Studio version you used. Otherwise you will encounter runtime crash when run numpy or script tests.

Cython 0.22. (Starting from Numpy 1.10.x, Cython is required to build Numpy. You can obtain it from http://cython.org/)

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.

Set Intel(R) Fortran compiler environment on Windows. Go to All Programs -> Intel Parallel Studio XE 20XX -> Command Prompt, select IA32/Intel64 Visual Studio XX mode for IA32 or Intel64 build binaries specifically.

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 or Intel Composer XE versions. You may adjust the exact location according to your MKL installation path. For example, if you are using Intel Composer XE 2015, Update 4, you will have:

[mkl]
library_dirs = C:\Program Files (x86)\Intel\Composer XE 2015.4.221\mkl\lib\intel64;C:\Program Files (x86)\Intel\Composer XE 2015.4.221\compiler\lib\intel64
include_dirs = C:\Program Files (x86)\Intel\Composer XE 2015.4.221\mkl\include
mkl_libs = mkl_lapack95_lp64,mkl_blas95_lp64,mkl_intel_lp64,mkl_intel_thread,mkl_core,libiomp5md
lapack_libs = mkl_lapack95_lp64,mkl_blas95_lp64,mkl_intel_lp64,mkl_intel_thread,mkl_core,libiomp5md

If you are building NumPy for 32 bit, you may add as the following:

[mkl]
library_dirs = C:\Program Files (x86)\Intel\Composer XE 2015.4.221\mkl\lib\ia32;C:\Program Files (x86)\Intel\Composer XE 2015.4.221\compiler\lib\ia32
include_dirs = C:\Program Files (x86)\Intel\Composer XE 2015.4.221\mkl\include
mkl_libs = mkl_lapack95,mkl_blas95,mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md
lapack_libs = mkl_lapack95,mkl_blas95,mkl_intel_c,mkl_intel_thread,mkl_core,libiomp5md

For old version numpy, modify Intel Fortran compiler configuration in numpy-x.x.x/numpy/distutil/fcompiler/intel.py to use the following compiler options in class IntelVisualFCompiler(BaseIntelFCompiler):

    def get_flags(self):
        opt = ['/nologo', '/MD', '/nbs', '/names:lowercase', '/assume:underscore']

Above change is unnecessary for Numpy 1.10.x or above.

For old version Numpy without Intel C++ Compiler for Windows* integration, compile and install NumPy using Intel Fortran and Microsoft Visual C++ compiler. For Intel64 and IA32 platforms run:

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

For Numpy 1.10.x and above versions, 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

For IA32 build:

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

Intel Compiler uses different icl.exe to generate code running on IA32 or Intel64 platforms, besides above configuration It depends on which Intel compiler environment you configured in Step 3 for different platforms. You may pass --prefix=<install dir>, in case, if you want to install in a directory of your choice.

Build and Install SciPy

For old version Numpy without Intel C++ Compiler for Windows* integration, compile and install SciPy using Intel Fortran and Microsoft Visual C++ compilers, for Intel64 build:

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

and for the IA32 builds:

python setup.py config --compiler=msvc --fcompiler=intelv build_clib --compiler=msvc --fcompiler=intelv build_ext --compiler=msvc --fcompiler=intelv install

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

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

and for the IA32 builds:

python setup.py config --compiler=intelw --fcompiler=intelv build_clib --compiler=intelw --fcompiler=intelv build_ext --compiler=intelw --fcompiler=intelv install

The same as mentioned above, Intel Compiler uses different icl.exe to generate code running on IA32 or Intel64 platforms. Besides above configuration It depends on which Intel compiler environment you configured in Step 3 for different platforms.

Trouble Shooting

1. Unresolved external symbol errors.

If you received unresolved external symbol errors when build Scipy with Intel Fortran like followings:

zfft.obj : error LNK2019: unresolved external symbol zfftf_ referenced in function zfft
zfft.obj : error LNK2019: unresolved external symbol zfftb_ referenced in function zfft
zfft.obj : error LNK2019: unresolved external symbol zffti_ referenced in function get_cache_id_zfft

It probably due to using of removed Intel Fortran options: '/nbs','/Qlowercase'. The patch on fcompiler/intel.py in Step 4 will resolve this problem.

2. Runtime crash when run numpy or scipy tests on Windows.

The downloaded Python libraries were built by old Visual Studio versions(2010 or 2008). When build Numpy with newer version Visual Studio, like 2012, you will encounter Python runtime crash for numpy and scipy tests.

This can be resolved by downloading the source code of Python and rebuild it with the same Visual Studio version for Numpy build.

3. You normally will receive a "Too many open files" error when building Scipy for a while:  

error: [Errno 24] Too many open files

This is a bug with openning more than 16K files simultaneously. Running the same python build command for the second time is a simple work around.

4. One known runtime issue for ia32 build scripy test: linalg.interpolative, TestInterpolativeDecomposition.test_id.

Since linalg.interpolative is a large module, we are going to open a Scipy issue for it.

Reference

https://software.intel.com/en-us/articles/numpyscipy-with-intel-mkl

For more complete information about compiler optimizations, see our Optimization Notice.

4 comments

Top
Chen, Yuan (Intel)'s picture

Hi, Bob

Have you configured Intel Fortran in Step 3:

"Set Intel(R) Fortran compiler environment on Windows. Go to All Programs -> Intel Parallel Studio XE 20XX -> Command Prompt, select IA32/Intel64 Visual Studio XX mode for IA32 or Intel64 build binaries specifically."

The 'libiomp5md.lib' is located under installation folder, e.g.:

C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\compiler\lib\intel64_win

Please check whether this is included in your "LIB" environment variable setting.

Thanks.

Bo's picture

Hi,

I'm now building numpy on my Windows 7 following this guide. I have Intel Parallel Studio XE 2017 installed including MKL, icl and ifort. The version of numpy is the latest one because I've downloaded just now. 

When I did the step 4, complied numpy with the indicated line: 

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

then an error was raised, the key part is

LINK : fatal error LNK1104: cannot open file 'libiomp5md.lib'

while actually the file libiomp5md.lib did lie in the directory I configured in site.cfg, and it was found during the process as the following log said. So I have no idea where the problem is.

Could anyone give me some hints? Thanks.

 

The log is as follows:

Running from numpy source directory.
blas_opt_info:
blas_mkl_info:
  FOUND:
    libraries = ['mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md']
    library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\mkl\\lib\\intel64_win', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\compiler\\lib\\intel64_win']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\parallel_studio_xe_2017.0.036\\compilers_and_libraries_2017\\windows\\mkl\\include']

  FOUND:
    libraries = ['mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md']
    library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\mkl\\lib\\intel64_win', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\compiler\\lib\\intel64_win']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\parallel_studio_xe_2017.0.036\\compilers_and_libraries_2017\\windows\\mkl\\include']

F2PY Version 2
lapack_opt_info:
openblas_lapack_info:
  libraries openblas not found in ['C:\\Python27\\lib', 'C:\\', 'C:\\Python27\\libs']
  NOT AVAILABLE

lapack_mkl_info:
mkl_info:
  FOUND:
    libraries = ['mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md']
    library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\mkl\\lib\\intel64_win', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\compiler\\lib\\intel64_win']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\parallel_studio_xe_2017.0.036\\compilers_and_libraries_2017\\windows\\mkl\\include']

  FOUND:
    libraries = ['mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md', 'mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md']
    library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\mkl\\lib\\intel64_win', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\compiler\\lib\\intel64_win']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\parallel_studio_xe_2017.0.036\\compilers_and_libraries_2017\\windows\\mkl\\include']

  FOUND:
    libraries = ['mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md', 'mkl_lapack95_lp64', 'mkl_blas95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'libiomp5md']
    library_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\mkl\\lib\\intel64_win', 'C:\\Program Files (x86)\\IntelSWTools\\compilers_and_libraries_2017.0.109\\windows\\compiler\\lib\\intel64_win']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['C:\\Program Files (x86)\\IntelSWTools\\parallel_studio_xe_2017.0.036\\compilers_and_libraries_2017\\windows\\mkl\\include']

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option: 'define_macros'
  warnings.warn(msg)
running config
running build_clib
running build_src
build_src
building py_modules sources
building library "npymath" sources
Could not locate executable icc
Could not locate executable ecc
customize GnuFCompiler
Could not locate executable g77
Could not locate executable f77
customize IntelVisualFCompiler
Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\bin\intel64\ifort.exe
Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\bin\intel64\ifort.exe
customize AbsoftFCompiler
Could not locate executable f90
customize CompaqVisualFCompiler
Could not locate executable DF
customize IntelItaniumVisualFCompiler
Could not locate executable efl
customize Gnu95FCompiler
Could not locate executable gfortran
Could not locate executable f95
customize G95FCompiler
Could not locate executable g95
customize IntelEM64VisualFCompiler
customize IntelEM64VisualFCompiler
customize IntelEM64VisualFCompiler using config
C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\bin\intel64\icl.exe /c /nologo /O3 /MD /W3 /Qstd=c99 /QxHost /fp:strict /Qopenmp -Inumpy\core\src\private -Inumpy\core\src -Inumpy\core -Inumpy\core\src\npymath -Inumpy\core\src\multiarray -Inumpy\core\src\umath -Inumpy\core\src\npysort -IC:\Python27\include -IC:\Python27\PC /Tc_configtest.c /Fo_configtest.obj
Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\bin\intel64\icl.exe
xilink /nologo /INCREMENTAL:NO _configtest.obj /OUT:_configtest.exe /MANIFEST /MANIFESTFILE:_configtest.exe.manifest
Found executable C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2017.0.109\windows\bin\intel64\xilink.exe
LINK : fatal error LNK1104: cannot open file 'libiomp5md.lib'
failure.
removing: _configtest.c _configtest.obj

 

Ying H. (Intel)'s picture

Hi Stacy,

You may either link mkl_rt.lib under both windows and Linux  or using the long list under both windows and Linux.  Simply speaking, they should be same functionality here.

(But different way to use. if  you have any further issue, please create a thread in MKL forum, we will look into detail).    

Here are some explanation about mkl_rt  in mkl user guide: https://software.intel.com/en-us/node/528342

You can simplify your link line through the use of the Intel MKL Single Dynamic Library (SDL).

To use SDL, place mkl_rt.lib on your link line. For example:

icl.exe application.c mkl_rt.lib

mkl_rt.lib is the import library for

mkl_rt.dll.

SDL enables you to select the interface and threading library for Intel MKL at run time. By default, linking with SDL provides:

  • Intel LP64 interface on systems based on the Intel® 64 architecture
  • Intel threading

Best Regards,

Ying

Stacy L.'s picture

How come the linux page only lists mkl_rt as a library and the Windows page has a huge list of libraries?

I would like to build the two platforms in a consistent manor but if it is not possible, then OK.

Thanks,

Stacy

Add a Comment

Have a technical question? Visit our forums. Have site or software product issues? Contact support.