Numpy, Scipy 搭建

1) 先裝 Atlas

To use this repository with yum, simply pick up your arch/distribution from http://download.opensuse.org/repositories/science:/ScientificLinux/ , and take the corresponding .repo file. Put this .repo file into /etc/yum.repo.d/, and then install numpy/scipy with yum:

yum install python-numpy python-scipy

I also packaged timers and testers for blas and lapack, which can be useful if you intend to compile special optimized versions of BLAS/LAPACK (eg GOTO or ATLAS). You can also find the package lapack3-pic, which can be used to build a complete LAPACK with ATLAS: it is a static version, but as it is built with the -fPIC compiler flag, it can be used to build python extensions; this is particularly useful for x86_64 arch.

Building Atlas

 

ATLAS is a BLAS/LAPACK implementation which tuned itself on the machine to provide ideal performances, and often match vendor specific implementations. Unfortunately, building ATLAS is not easy. But, it is getting easier all the time.

Building Atlas by Hand

 

These instructions show how to build ATLAS (and LAPACK) from their official distributions.

First, download and unpackage the LAPACK distribution from netlib (you need these to build a complete version of LAPACK).

wget http://www.netlib.org/lapack/lapack-3.1.1.tgz
tar zxvf lapack-3.1.1.tgz
cd lapack-3.1.1

 

There are several make.inc files in the INSTALL directory of the lapack distribution. Copy one of those files to the main directory. For example:

cp INSTALL/make.inc.gfortran make.inc

 

Now, you must edit the make.inc file to ensure that the OPTS and NOOPT lines both contain the flag for compiling position-independent code on your platform (e.g. with gcc/gfortran it is -fPIC). For example:

OPTS = -O2 -fPIC
NOOPT = -O0 -fPIC

 

(Note: Make sure that if you build with gfortran that g77 is not installed on your system (or at least is not in your PATH when numpy is being built) as you need to link with the same compiler that you built lapack with when numpy builds. It will try and find g77 first which will lead to linking errors if you have built lapack with gfortran). Then change to the SRC directory and run make

cd SRC
make

 

This will create an lapack_<XXXX>.a file in the head lapack directory. You will need the location of this file to configure atlas.

Now, download the latest release of ATLAS (these instructions worked on 3.7.37). See, for example, http://sourceforge.net/project/showfiles.php?group_id=23725. Unpackage the result, change to the directory created, and create a directory to contain the resulting build. This directory should be named appropriate for the platform (you can build for multiple platforms from the same SOURCE tree --- perhaps the source is on a network drive and builds are taking place for multiple platforms).

tar jxvf atlas3.7.37.tar.bz2
cd ATLAS
mkdir ATLAS_<my_platform_type>

 

cd ATLAS_<my_platform_type>
../configure -Fa alg -fPIC --with-netlib-lapack=/path/to/lapack/lapack_<XXXX>.a
make

 

Your atlas libraries should now be in the lib subdirectory of the current directory. You should copy them to some-place that you can tell site.cfg about so that numpy and scipy can pick them up. If you want to create shared libraries, then you can do that by

cd lib
make shared     # for sequential libraries
make ptshared   # for threaded libraries

 

after changing to the lib directory where the .a files are already located.

 

 

2)安裝NUMPY SCIPY

Extract the numpy source package. My result is a folder called numpy-1.3.0. Enter this folder.

3. Edit site.cfg. If you don't have it, create a blank one. You may copy it from site.cfg.example. Make sure the [DEFAULT] section is configured as follows

[DEFAULT}
libraries = gfortran, gfortranbegin
library_dirs = /usr/local/lib:/usr/lib/gcc/x86_64-redhat-linux/4.1.2

4.

python setup.py build

You should see things like this

creating build/temp.linux-x86_64-2.4/numpy/linalg
compile options: '-DATLAS_INFO="\"3.9.23\"" -Inumpy/core/include -Ibuild/src.linux-x86_64-2.4/numpy/core/include/numpy -Inumpy/core/src -Inumpy/core/include -I/usr/include/python2.4 -c'
gcc: numpy/linalg/lapack_litemodule.c
gcc: numpy/linalg/python_xerbla.c
/usr/bin/gfortran -Wall -Wall -shared build/temp.linux-x86_64-2.4/numpy/linalg/lapack_litemodule.o build/temp.linux-x86_64-2.4/numpy/linalg/python_xerbla.o -L/home/bao/installtest/ATLAS/DONE/lib -L/usr/lib/gcc/x86_64-redhat-linux/4.1.2 -Lbuild/temp.linux-x86_64-2.4 -llapack -lptf77blas -lptcblas -latlas -lgfortran -lgfortranbegin -lgfortran -lgfortranbegin -lgfortran -o build/lib.linux-x86_64-2.4/numpy/linalg/lapack_lite.so
building 'numpy.random.mtrand' extension
compiling C sources
C compiler: gcc -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fPIC

As you can see, the ATLAS library compiled is used in numpy compilation.

5. Test (optional). You can test on any numpy functions

cd build/lib.linux-x86_64-2.4

start your Python interpreter and do like this.

>>> from numpy import *
>>> linalg.svd(array([[1,2],[3,4]]))
(array([[-0.40455358, -0.9145143 ],
       [-0.9145143 ,  0.40455358]]), array([ 5.4649857 ,  0.36596619]), array([[-0.57604844, -0.81741556],
       [ 0.81741556, -0.57604844]]))

6. Install numpy into a target diretory.
Go back to your numpy compilation directory if you've done step 5.

I do not like install anything to the system level directory which requires root privileges. I prefer to install into any directory and set proper environmental variable to point - thanks to the great "everything is file" idea of UNIX. The target directory will contain two folders, bin and lib64. The directory you should add into your PYTHONPATH Linux environmental variable is TARGET_DIRECTORY/lib64/python2.4/

Please use --prefix to specify your target directory. This is what I did:

python setup.py install --prefix=/home/bao/installtest

Then I set /home/bao/installtest/lib64/python2.4/site-packages as PYTHONPATH and export it in my ~/.bashrc

If you do not specify --prefix, the default installation location is /usr/local and you don't need to set the PYTHONPATH as it is default.

You can check it later by doing so in your Python interpreter:
>>> import sys
>>> sys.path
['', '/home/bao', '/home/bao/installtest/lib64/python2.4/site-packages', '/usr/lib64/python24.zip', '/usr/lib64/python2.4', '/usr/lib64/python2.4/plat-linux2', '/usr/lib64/python2.4/lib-tk', '/usr/lib64/python2.4/lib-dynload', '/usr/lib64/python2.4/site-packages', '/usr/lib64/python2.4/site-packages/Numeric', '/usr/lib64/python2.4/site-packages/gtk-2.0', '/usr/lib/python2.4/site-packages']

Part II: Compiling and installing Scipy

You need numpy and ATLAS to compile Scipy.

1. Download Scipy source package from their official website.

2. Extract the source package. The extracted folder on my computer is scipy-0.7.2. Enter this folder.

3. Create site.cfg file if it doesn't exist and make sure these three lines are in it.

[DEFAULT]
libraries = gfortran, gfortranbegin
library_dirs = /usr/local/lib:/usr/lib/gcc/x86_64-redhat-linux/4.1.2

4.

python setup.py build

5. Test (optional) Actually, there isn't much to test. So I skipped it.

6. Install scipy into a target directory.
This step is very similar to that in Numpy part. I did

python setup.py install --prefix=/home/bao/installtest

If you have already pointed PYTHONPATH in Numpy part, then you are already done. The default installation directory is /usr/local.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章