SciPyのインストール: SciPyを古いFedora Coreで使うときに、Lapack/BLASパッケージが壊れている問題を直す

ようやく数値計算のコアの部分をPythonで作れる段階に参った。手始めに、単純な行列計算をテストしようと思い線形代数のモジュール scipy.linalg を呼び出してみたのだが、、、

>>> import scipy.linalg

Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/lib/python2.3/site-packages/scipy/linalg/__init__.py", line 8, in ?
    from basic import *
  File "/usr/lib/python2.3/site-packages/scipy/linalg/basic.py", line 227, in ?
    import decomp
  File "/usr/lib/python2.3/site-packages/scipy/linalg/decomp.py", line 21, in ?
    from blas import get_blas_funcs
  File "/usr/lib/python2.3/site-packages/scipy/linalg/blas.py", line 14, in ?
    from scipy.linalg import fblas

ImportError: /usr/lib/python2.3/site-packages/scipy/linalg/fblas.so: undefined symbol: srotmg_

srotmgという関数がないために、importができない。これではMATLABからの移植ができんので大問題だ。


ScipyのInstalling Scipy/Linuxのページによると、BLAS rpmパッケージがBLASパッケージでなく、LAPACKパッケージから作られたためにいくつかの関数 (この問題ではCSROT, DROTM, DROTMG, DSDOT, SDSDOT, SROTM, SROTMG, ZDROTの8つ)が含まれていないそうな。多分、古いFedora Coreを使っているのも原因なのだろう。

Broken BLAS

SUSE (and Red Hat) regularly shipped versions of the BLAS library where some functions were missing. This bug has finally been fixed in March 2007. So, for SUSE 10.2 and prior you should install the BLAS rpm from the science repositories above. In later versions SUSE's original BLAS should work.

The bug's cause was as follows: The BLAS rpm is created from Netlib's LAPACK package and not from the BLAS package. Until March 2007 however the LAPACK library did only contain a subset of the functions that were in BLAS. Finally someone begged the LAPACK developers to include the whole BLAS library in the LAPACK package, and they did.

For details see: https://bugzilla.novell.com/show_bug.cgi?id=228824


というわけでLAPACKとATLASを始めからインストールし直して問題を解決してみることにした。

参考: http://cens.ioc.ee/~pearu/scipy/INSTALL.html
(以下はほぼそのままです。。。)

大まかな手順は:

  1. NumPyのインストール
  2. LAPACKのインストール
  3. ATLASのインストール
  4. SciPyのインストール

と4段階。

1. NumPy

$ svn co http://svn.scipy.org/svn/numpy/trunk numpy
$ cd numpy
$ sudo python setup.py install

2. LAPACK

$ wget http://www.netlib.org/lapack/lapack.tgz (ソースの取得)
$ cd lapack-3.1.1
$ cp INSTALL/make.inc.LINUX make.inc
$ make lapacklib
$ make blaslib      <----- ここと、
$ make              <----- ここは不要かもしれない。

この作業の末に、lapack_LINUX.aがlapack-3.1.1/のディレクトリに作られる。

3. ATLAS

ソースは以下より入手。3.6.0をダウンロードしました。

http://math-atlas.sourceforge.net/

$ tar xzfp atlas3.6.0.tar.gz
$ cd ATLAS
$ make
$ sudo make install arch=Linux=P4SSE2_4 (3,4年前のP4マシンで30-40分かかりました。)

LAPACKライブラリの最適化作業:

$ cd  ATLAS/lib/Linux_PII/
$ mkdir tmp
$ cd tmp
$ ar x ../liblapack.a
$ ar x ../libcblas.a  <------- 不要かも。
$ cp /path/to/src/lapack-3.1.1/lapack_LINUX.a  ../liblapack.a
$ ar r ../liblapack.a *.o

次に、lib*aファイルを移動。

cp /path/to/src/ATLAS/lib/Linux_P4SSE2/lib*.a, /usr/local/lib/atlas/
cp /path/to/src/ATLAS/include/{cblas.h,clapack.h} /usr/local/lib/atlas/
export ATLAS=/usr/local/lib/atlas  (/etc/profile にも追記)

4. SciPy

$ svn co http://svn.scipy.org/svn/scipy/trunk scipy
$ cd scipy
$ sudo python setup.py install

テスト: LAPACK, ATLASをインストールしたお陰で上手く動作しました。

#! /usr/bin/env python

from numpy import matrix
from scipy.linalg import inv, det, eig

A = matrix( [ [ 1,1,1 ], [ 4,4,3 ], [ 7,8,5 ] ] )
b = matrix( [ 1,2,1 ] ).transpose()

print A
print det(A)
print inv(A)*b
print eig(A)


例はSciPy Tutorialを参照しました(=パクりました)。

http://www.scipy.org/SciPy_Tutorial


MATLABとSciPyの比較表も使えそう。

http://scipy.org/NumPy_for_Matlab_Users