Installing python/numpy with ATLAS support
May 5th, 2014
The reason MATLAB is so efficient at matrix operations is because it uses highly optimized fortran libraries which have been developed over the last 3 decades. The most basic of these is the BLAS (Basic Linear Algebra Subprograms) library which is a set of functions to efficiently evaluate simple matrix operations like the multiplication of a matrix with a scalar. LAPACK (Linear Algebra Package) is another library which builds upon BLAS and implements more complex matrix operations such as LU-factorization. There are many different ways by which the fundamental BLAS/LAPACK libraries can be implemented. Intel has a proprietary implementation called the MKL (Math Kernel Library) which is optimized for Intel's own processors. So if you have MATLAB on your system and if your processor is from Intel, chances are that MATLAB is using MKL for fast matrix calculations. There are several open source alternatives to MKL such as OpenBLAS and GOTOBLAS. ATLAS (Automatically Tuned Linear Algebra Software) is another such implementation particularly suitable for clusters and high performance computing over distributed nodes. Coupling ATLAS with python/numpy, therefore, turns out to be an open source alternative to MATLAB+MKL. I have recently had to learn how to install these and thought that it would be a good idea to list the steps required for doing so. These steps are appropriate for a linux CentOS cluster:
Installing Python: Python 2.7.6 can be downloaded using the wget command:
wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
and extracted using the tar -xvf command. In the absence of root access it can be installed in a specified directory using the --prefix command. It is also advisable to generate the dynamic shared libraries using the --enable-shared command. So doing something like:
./configure --enable-shared --prefix=/path to directory where you want to install python/
make install
will do the trick.
Obviously for this command to work you would need to be in the directory where python has been extracted to. Two further changes to the environment variables would make this installation of python the current installation:
export PATH=/path to the directory where you installed python/bin:$PATH
export LD_LIBRARY_PATH=/path to the directory where you installed python:$LD_LIBRARY_PATH
Installing ATLAS: Before installing numpy you would need to install the ATLAS library. The following commands can be used for the installation:
wget http://hivelocity.dl.sourceforge.net/project/math-atlas/Stable/3.10.1/atlas3.10.1.tar.bz2 (Download)
tar jxf atlas3.10.1.tar.bz2
mkdir atlas (Creating a directory for ATLAS)
mv ATLAS atlas/src-3.10.1
cd atlas/src-3.10.1
wget http://www.netlib.org/lapack/lapack-3.5.0.tgz (It may be possible that the atlas download already contains this file in which case this command is not needed)
mkdir intel(Creating a build directory)
cd intel
cpufreq-selector -g performance (This command requires root access. It is recommended but not essential)
../configure --prefix=/path to the directory where you want ATLAS installed/ --shared --with-netlib-lapack-tarfile=../lapack-3.5.0.tgz
make
make check
make ptcheck
make time
make install
The whole process of configuring and installing ATLAS can take several hours.
Installing numpy with ATLAS support: With Python and ATLAS installed and the PATH variable set to point to the newly installed version of python, numpy can now be installed. It is assumed that these commands are issued from the directory where Python is installed:
wget http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/numpy-1.8.1.tar.gz
tar -xvf numpy-1.8.1.tar.gz
cd numpy-1.8.1
cp site.cfg.example site.cfg
Now site.cfg needs to be modified to make numpy aware of where ATLAS is installed. Adding the following lines in the beginning should suffice:
[DEFAULT]
library_dirs = /Path to the ATLAS installation directory/lib
include_dirs = /Path to the ATLAS installation directory/include
[atlas]
atlas_libs = lapack, f77blas, cblas, atlas
[amd]
amd_libs = amd
Finally numpy can be installed using the following:
python setup.py build --fcompiler=gnu95 (or gnu depending upon whether ATLAS is built with g77 or gfortran compiler)
python setup.py install
Hopefully now you have a free open source alternative to MATLAB. Python is pretty amazing in how easy it is to learn and how extensible it is. And how free it is!
Thanks Ankit - this is really useful. Just a note for the OSX Mavericks (10.9.*) users. I found that you had to do a few additional things to get ATLAS to install:
1) Install GCC:
brew install gcc
Or to install a specific version (e.g. 4.8):
brew tap homebrew/versions && brew install gcc48
2) Link the executable to gcc:
ln -s /usr/local/bin/gcc-4.8 /usr/local/bin/gcc
3) Use an alternative configure command to the one given in Ankit's instructions:
../configure -t 0 -C acg /usr/local/bin/gcc-4.8 -Fa ac '-Xassembler -q' -C if /usr/local/bin/gfortran --shared --prefix=/usr/local/atlas3.10.2 --with-netlib-lapack-tarfile=../../lapack.tgz
This is because ATLAS does not really like clang and seems to only like it to be on the linux PATH. The "-Fa ac '-Xassembler -q'" part switches the assembler to be the default one in XCode/clang to be used. For more info see: http://sourceforge.net/p/math-atlas/support-requests/930/
Thanks Simon for your comment.
Correction - configure command used was:
../configure -t 0 -C acg /usr/local/bin/gcc-4.8 -Fa al '-Xassembler -q' -C if /usr/local/bin/gfortran --shared --prefix=/usr/local/atlas3.10.2 --with-netlib-lapack-tarfile=../../lapack.tgz
This also uses the correct assembler for Fortran
This was an excellent guide, thanks for sharing it - I've just installed an updated version of the packages on Centos7x64 following it 🙂