How the Python note compiles the NumPy package that relies on the Lapack and Atlas libraries

Source: Internet
Author: User
Tags lapack virtualenv gfortran

The previous note describes the NumPy package source code compilation/installation method that does not rely on the Lapack and Atlas libraries, but the "pure version" of NumPy will lose performance, so this note explains how to compile and install the NumPy package that relies on Lapack and Atlas libraries.

1. GCC Version Requirements
Use a newer version of the GCC toolset (try not to lower v4.7) and have the Gfortran compiler integrated.
Note 1: the capitalized "GCC" refers to the GNU Compiler Collection, which contains a compiler for many other languages (such as G++/gfortran, etc.) in addition to the C language compiler GCC
NOTE 2: Version 3.x of the C language compiler GCC will cause the compilation of the Atlas library error due to missing header files
Note 3: If there is no Gfortran compiler in the GCC toolset, there are some inexplicable errors when compiling the Lapack library (because Lapack is written in Fortran), Fortunately, the Gfortran compiler has been integrated in GCC4.7 and above
If the GCC version meets the requirements, temporarily add it to the environment variable path and set the dynamic library lookup path:

$ export path=/home/slvher/tools/gcc48/bin/: $PATH $ export ld_library_path=/home/slvher/tools/gcc48/lib64:/home/ Slvher/tools/gcc48/lib
Note 4:Temporarily setting Ld_library_path in the current shell session ensures that the GCC library is properly searched during compilation, but it is best not to set it to. Bash_profile because that will affect the lookup path of other programs and may step into the pit.
Note 5: the GCC version requirements mentioned here and environment variable settings if there is no error, then the following compilation will be more smooth, otherwise you will encounter a variety of compile/link problems, follow-up I will use a note to record the process of these pits and the analysis of these strange problems, Don't dwell on it here.

2. Compiling the Lapack and Atlas libraries
Lapack is a specially optimized linear algebra computing library developed in FORTRAN; Atlas is also an optimized linear algebra computing library that provides all the APIs for the Blas library (including the C interface and the Fortran interface), and also implements some functions in the Lapack library. In compiling, Atlas adjusts the parameters of the scientific calculation function according to the machine's configuration parameters to achieve better computational performance on the machine.
At first glance, it is necessary to compile the Lapack and Atlas two libraries separately, fortunately, the Atlas library supports compile-time lapack libraries, so just complete the compilation configuration of the Atlas library correctly, compile the Atlas library.
The following are the main steps to compiling the Atlas/lapack library.
1)
download Lapack source package and Atlas source package from official website respectively, I download the current version of Lapack-3.5.0.tgz and atlas3.10.2.tar.bz2
2) unzip the atlas source tarball: TAR-JXVF atlas3.10.2.tar.bz2
3) CD ATLAS && mkdir blddir && cd Blddir
4) Execute the Configure command to configure the compilation parameters
Where--shared indicates that the Atlas shared library is to be compiled (configure automatically inserts the "-fpic" parameter in the compilation command, which does not need to be explicitly specified here);--prefix Specifies the installation path of the compilation result --with-netlib-lapack-tarfile indicates that compiling the Atlas library will automatically compile the Lapack library with the same compiler and compile/link parameters, after specifying the path of the Lapack source package, Configure will automatically unzip the Lapack source code and copy it to the blddir/src/lapack/reference/directory after running.
5)After the Configure run, the Make.inc file is generated in the Blddir directory, which sets up many compilation parameters (such as Find path, compile output path, compiler, parameters passed to compiler, etc.), Blddir Sub-directory of many modules makefile will include this make.inc, including the source code independent LAPACK package, it can be seen that the Make.inc file to achieve the purpose of unified compilation environment.
6)Make Build
7)Make check
8)Make Ptcheck
9)Make install
If all of these commands are successful, the compiled *.a and *.so libraries are installed to the path specified by the--prefix parameter, and the header files of those libraries are also copied to the Include directory under the installation path.
At this point, both the Atlas and LAPACK libraries are compiled, where the Lapack library is. A static library, and the Atlas Library is a. so dynamic library. In fact, the dynamic library in Atlas already contains all the symbols and code for the Lapack static library.
The following can start compiling the NumPy package that relies on the Lapack and Atlas libraries.

3. Compiling the optimized version of the NumPy package
Prerequisite: Download NumPy source package and unzip the website, here the latest version of Numpy-1.9.2.tar.gz as an example to illustrate.
1) CD to unzip directory numpy-1.9.2
2) CP Site.cfg.example SITE.CFG
3) Configure the Atlas entry in Site.cfg, where Include_dirs and Library_dirs are the include and Lib directories under the Atlas Library installation path

[Atlas]atlas_libs   = Lapack,f77blas,cblas,atlaslibrary_dirs =/home/slvher/tools/scikit-learn-virtualenv/ Dep-libs/sklearn-libs/libinclude_dirs =/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include
4) Python setup.py config
5) Python setup.py build--fcompiler=gnu95 # # Specifies the Fortran compiler for GCC4.8 toolset Gfortran
6) Python setup.py install
normally, after the build succeeds, the install copies the compiled output to the Lib/python2.7/site-packages directory under the current Python interpreter installation path.
at this point, the following example can be used to view the configuration of the NumPy package:
>>> import NumPy as np>>> np.__config__.show () atlas_3_10_blas_threads_info:libraries = [' Lapack ', ' F 77blas ', ' Cblas ', ' Atlas '] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib '] D Efine_macros = [(' Have_cblas ', None), (' Atlas_info ', ' "\ \" 3.10.2\\ "")] language = c Include_dirs = ['/home/slvher/t Ools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include ']lapack_opt_info:libraries = [' Tatlas ', ' lapack ', '    F77blas ', ' Cblas ', ' Atlas '] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib '] Define_macros = [(' Atlas_info ', ' "\ \" 3.10.2\\ "" ')] language = f77 include_dirs = ['/home/slvher/tools/scikit-learn- Virtualenv/dep-libs/sklearn-libs/include ']blas_opt_info:libraries = [' Lapack ', ' f77blas ', ' Cblas ', ' Atlas '] Library _dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib '] Define_macros = [(' HAVE_CBLAS ', None), (' Atlas_info ', ' "\ \" 3.10.2\\ "")] Language = c Include_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/include ']openblas_i Nfo:not availableopenblas_lapack_info:not availableatlas_3_10_threads_info:libraries = [' Tatlas ', ' lapack ', ' F77BL As ', ' Cblas ', ' Atlas '] library_dirs = ['/home/slvher/tools/scikit-learn-virtualenv/dep-libs/sklearn-libs/lib '] Defin E_macros = [(' Atlas_info ', ' "\ \" 3.10.2\\ "" ')] language = f77 include_dirs = ['/home/slvher/tools/scikit-learn-virtua Lenv/dep-libs/sklearn-libs/include ']lapack_mkl_info:not availableblas_mkl_info:not availablemkl_info:not AVAILABLE
You can also use specific examples to verify that the functionality is normal:
>>> import NumPy as Np>>> np.arange (. Reshape (3, 5) array ([[0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [Ten, one, one, and up]]) >>> >>> a = Np.arange (at). Reshape (3, 5) > >> Aarray ([[0,  1,  2,  3,  4],       [5,  6,  7,  8,  9],       [10, 11, 12, ] >>> type (a) <type ' Numpy.ndarray ' >>>>>>>>>> from Numpy.linalg Import *>>> B = Np.array ([[[1.0, 2.0], [3.0, 4.0]]) >>> Barray ([[1.,  2.],       [3.,  4.]) >>> b.transpose () array ([[1.,  3.],       [2.,  4.]) >>> INV (b) Array ([[ -2.,  1.],       [1.5, -0.5]]) >>>

Resources
1. example:installing ATLAS with full LAPACK on LINUX/AMD64
2. Scipy.org-building everything from source with Gfortran on Ubuntu
3. scipy.org-building and installing NumPy

======================= EOF ======================


How the Python note compiles the NumPy package that relies on the Lapack and Atlas libraries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.