Saturday, February 25, 2006

Fedora Core 5 Test 3 で、まだ SciPy がインストールできない件

[追記] scipy のインストールには blas、blas-devel、lapack、lapack-devel のパッケージが*必要*だと教えてもらい(下の説明だと要らないみたいに書いてあるのにねぇ)、Fedora Core 5 の「Add/Remove Software」からそれらのパッケージをインストールすると無事 scipy がインストールができたっ!でも、まだ Fortran がらみの問題はあるみたいです。

scipy problem for fedora extras
(Date: 2005/10/19)

ぐわあっ、Fedora Core 4 で Fortran やら GCC やらの問題で SciPy がインストールできないというレポートを発見…(どうしてもっと早く気づかないのか。やっぱり僕はアホだ)。FC5T3 でもまだ直っていない様子。はやく直るとうれしいな (ちなみに、FC5 ではインストーラとかパッケージ・マネージャとか loading の時の「ぐりんぐりん」とか(←見ないとわからんな…)、いろいろ便利にカッコよくなっております)。

以下のドキュメントを翻訳しながらインストールしようとしたのだけれど、残念。でも、役に立つと思う人がいるかもしれないのでかなり翻訳できてない部分があるけど公開する。

# # # # #

最新バージョンの SciPy をインストールする方法

How to Install the Latest Version of SciPy by Steve Baum

32 ビット Linux における基本的な SciPy のコンパイル手順(と機能向上のためのオプション)


イントロダクション


SciPy とは:

SciPy は数学、科学、工学のためのオープンソースソフトです。人気のあるオペレーティング・システムのすべてで動作し、インストールも簡単で、無料です。SciPy は使いやすく、かつ世界最前線の科学者やエンジニアにも頼りにされるほどパワフルです。

基本的には、あなたが Python に慣れ親しんでいて(もしそうでなくても Python は比較的とっかかり易い言語です)、科学計算をたびたび行うのなら(すなわち、行列計算をするなら)、SciPy は、同種の計算用途の商用パッケージに対する本当に、本当に優れたオープンソースの代替品です。

この文書は 32 ビットの Linux における GCC コンパイラを使った SciPy のコンパイルとインストール方法のチュートリアルです。これは少し制限的に聞こえるかもしれませんが、コンパイラの引数を中心に(特に GCC コンパイラが使われなかった場合)、少しあっちやこっちをいじったりすれば、同じ方法がほとんどの UNIX プラットフォームで使えるはずです。さらに、説明では bash シェルが使われています。もし他のシェルを使う場合は、文法が異なるので、適宜コマンドを修正してください。

SciPy の(2006 年 1 月時点での)最新版は Numarray プロジェクトを基にしており、またその後継者でもあります。Numarray プロジェクトは、Numeric Python プロジェクトの後継者と見ることができます。SciPy Wiki は SciPy と NumPy の関係を以下のように書いています:

SciPy は Python のための科学的ツールのオープンソース・ライブラリです。SciPy は配列の高速な操作のためのすべてのライブラリを含んでおり、人気の Numeric モジュールを置き換え、ひとつのパッケージとして高レベルな科学と工学のモジュールを集めたものです。

SciPy は、配列オブジェクトとその他の基本的な機能を備えた NumPy を基礎にしています。SciPy は統計、最適化、積分、線形代数、フーリエ変換、信号・イメージ処理、遺伝的アルゴリズム、ODE (常微分方程式) solver、特別な関数、その他のモジュールを提供します。

プラグマティックに要点を言うならば、ちゃんと動く SciPy を手に入れるには、まず NumPy をインストールしてそれから SciPy をインストールする、ということです。


NumPy と SciPy のソースコードを取得


現在(2006 年 1 月)の NumPy と SciPy を取得する一番の方法はダウンロードページ経由で Subversion のレポジトリから得る方法です。(Subversion がインストールされているかを svn コマンドで調べてください。もし入っていなければ、サイトに行ってそこのインストールの指示に従ってください。)Subversion がインストールされた後は、ベースになるディレクトリを作成して(例えば /home/rastus/SCIPY)、cd [訳注:ディレクトリ変更のためのコマンド]で SCIPY に入って以下の2つの svn コマンドを入力してください。


svn co http://svn.scipy.org/svn/numpy/trunk numpy


次に SciPy は:


svn co http://svn.scipy.org/svn/scipy/trunk scipy


これで以下の2つのディレクトリができます:

numpy
scipy


これらのディレクトリの中でコンポーネントがコンパイルされインストールされます。以下のインストラクションは Installing SciPy wiki ページを基にして、32 ビットの Linux プラットフォームに適用しています。


NumPy のコンパイルとインストール


numpy ディレクトリに入って、su で root になって、以下のコマンドを入力して NumPy のコンパイルとインストールを行ってください:

python setup.py install >& inst.log &

このコマンドはコンパイル処理の出力を inst.log ファイルにパイプします。以下のコマンドでインタラクティブに見ることができます:

tail -f inst.log


この処理はすべてのコンパイル処理の記録を保存してエラーの摘出をとても容易にします。これは Python が古すぎたり間違ってインストールされていない限り、問題なくスムーズに実行されるはずです。


BLAS と LAPACK について


もしこれがスムーズに行けば、すこしトリッキーな SciPy のコンパイルに進みましょう。Installing SciPy ページに詳しく説明されているように、必要なものではなくても、BLASLAPACK 両方のバージョンを SciPy をコンパイルするときに持っているのがよいでしょう。SciPy は独自の必要な線形代数機能の「軽量」バージョンを内部に持っていますが、BLAS と LAPACK のインストールによってさらに広い用途を得ることができます。もし SciPy がどんなものかを早く知りたくて、使いたくてウズウズしているのなら、「SciPy のコンパイルとインストール」セクションまでスキップしてください。必要になればいつでもここに戻ってきてオプションのコンポーネントは再インストールできます。


BLAS のコンパイルとインストール

A very important issue is that the same compilers be used throughout. In this example, we're using the GNU compiler suite, i.e. gcc and g77. And since these are usually the default compilers searched for by the various packages, this makes things a bit easier. The procedure to compile BLAS with g77 is:

cd SCIPY
mkdir -p blas
cd blas
wget http://www.netlib.org/blas/blas.tgz
tar xzf blas.tgz
g77 -fno-second-underscore -O2 -c *.f
ar r libfblas.a *.o
ranlib libfblas.a
rm -rf *.o
su
cp libfblas.a /usr/local/lib
export BLAS=/usr/local/lib/libfblas.a


LAPACK のコンパイルとインストール

Now on to LAPACK. This is the most basic of our available procedures. See the ATLAS VARIATION section below for a performance enhancing alternative.

cd SCIPY
wget http://www.netlib.org/lapack/lapack.tgz
tar xzf lapack.tgz
cd LAPACK
cp INSTALL/make.inc.LINUX make.inc


Now you must edit make.inc and change (if necessary) the following values:

OPTS = "-O2"


Now to finish the compilation:

make lapacklib >& make.log &
tail -f make.log
make clean
cp lapack_LINUX.a libflapack.a
su
cp libflapack.a /usr/local/lib
export LAPACK=/usr/local/lib/libflapack.a


SciPy のコンパイルとインストール

さてここからは SciPy のコンパイルとインストールに入ります。もし ATLAS VARIATION が済んでいるなら、ここがそれが終わった後の開始点です。最終的なパフォーマンス向上の代わりとしては、 DJBFFT VARIATION と FFTW VARIATION が下にあるインストラクションにより望むならインストールできます。これらのバリエーションはいくつかの高速フーリエ変換を基にしたアルゴリズムのパフォーマンスを劇的に向上させることができます。もしひとつあるいは両方インストールするなら、その後ここに戻ってきて完了してください。もしインストールしないなら、以下のコマンドで終了です:

cd scipy
su
python setup.py install >& inst.log &
tail -f inst.log

もしエラーがでなければ、うまく SciPy があなたのマシンにコンパイル・インストールできたということです。場の理論の統一でもなんでもやってください。

SciPy が VARIATION ライブラリを見つけて使うことができるかどうかを前のステップで作成した inst.log ファイルの始まりを見ることによって知ることができます。less inst.log コマンドで追加ライブラリが FOUND (発見)あるいは NOT AVAILABLE (利用不可)のどちらかであることがわかるはずです。


SciPy インストールのテスト

A suite of tests is supplied to test how the software does on your particular platform. This suite of tests can be performed by first invoking python from the command line (or, preferably, the very convenient and handy ipython). Once you get your new command-line prompt enter the following:

from numpy import *
from scipy import *


to load the full functionality of SciPy?. To run the tests, enter:

test(level=N)


where N can vary from 1 to 10. To obtain detailed messages about the tests that are being executed, enter:

test(level=N, verbosity=2)



THE ATLAS VARIATION THAT OUR PERFORMANCE MIGHT BE ENHANCED

ATLAS の FAQ が基本的な情報を提供しています。例えば……

ATLAS は Automatically Tuned Linear Algabra Software(自動調整された線形代数ソフト)を意味します。現在のバージョンは完全な(C と Fortran77 両方の)BLAS API と、LAPACK API のごく一部を提供します。すべてのサポートされた操作において、ATLAS はマシン用に調整されたライブラリと同等のパフォーマンスを達成します。

A slightly modified version of the above procedure for compiling and installing LAPACK can significantly improve performance. The procedure involves first compiling the ATLAS package, and then linking the subset of the LAPACK API optimized by ATLAS with the full LAPACK library. The LAPACK procedure is the same as before, i.e.:

cd SCIPY
wget http://www.netlib.org/lapack/lapack.tgz
tar xzf lapack.tgz
cd LAPACK
cp INSTALL/make.inc.LINUX make.inc
(Change OPTS to O2)
make lapacklib >& make.log &
tail -f make.log
cp lapack_LINUX.a libflapack.a


ATLAS のコンパイルとインストール

ここで ATLAS パッケージを取得してコンパイルします。以下のページから手に入れられます:

https://sourceforge.net/projects/math-atlas/


この例では、デベロッパ・バージョン(3.7.11)をダウンロードしました。以下のコマンドでコンパイルしてください:

make


この make 処理はインタラクティブであなたにいくつかの質問をします。デフォルトの答えを選べばだいたい大丈夫です。現在のバージョンの ATLAS が好まないのは 2.95 シリーズ以降の GCC コンパイラです。もし 2.95 以降の GCC が検出されれば、コンパイルを終了したいかどうかを聞いてくるでしょう。もしこのプロジェクトをさらに複雑にしたくないのであれば、n を入力しましょう。もし ATLAS によるパフォーマンス向上を欲しないのであれば、y を入力して下にある 2.95 シリーズの GCC パッケージの取得とコンパイル方法の情報を見てください。

もし n を入力したなら、次のようにしてください:

make install arch=Linux_* >& make.log &
tail -f make.log

もし y と入力したなら、GCC VARIATION セクションの説明に従って、ATLAS パッケージがビルドされればここに戻ってきてください。注意:ATLAS のコンパイルはとても高速なプラットフォームでさえ何時間もかかることがあります。


ATLAS と LAPACK を統合する

コンパイルが終了したら、SCIPY ディレクトリのサブディレクトリ


lib/Linux_*


に ATLAS のバイナリが置かれます。機能強化された LAPACK ライブラリを作成するには、以下を実行してください:

cd lib/Linux_*
mkdir tmp
cd tmp
ar x ../liblapack.a
cp ../../../../LAPACK/libflapack.a ../liblapack.a
ar r ../liblapack.a *.o
cd ..
rm -rf tmp
su
cp liblapack.a /usr/local/lib
export LAPACK=/usr/local/lib/liblapack.a
exit


これで SciPy によって使うことのできる ATLAS 強化された LAPACK ライブラリが生成されます。


他の ATLAS ライブラリのインストール

ATLAS により生成されるその他のライブラリも SciPy によって使われることができるので、インストールしましょう:

cd lib/Linux_*
su
mkdir /usr/local/lib/atlas
cp *.a /usr/local/lib/atlas
exit
export ATLAS=/usr/local/lib/atlas


ここであなたは 基本的な SciPy コンパイルのセクションに戻って SciPy パッケージをコンパイルすることができます。


THE GCC VARIATION THAT OUR PERFORMANCE MIGHT BE FURTHER ENHANCED


Here we will compile and install GCC version 2.95.3 in a location distinct from that of whatever version of GCC you already have. You could just overwrite your present version of GCC, but that might (i.e. will) open a Pandora's Box of additional annoyances that you almost certainly want to avoid.


GCC 2.95.3 のコンパイルとインストール

Here is the procedure for obtaining, compiling and installing the alternate GCC version.

cd SCIPY
wget ftp://cs.utk.edu/pub/rwhaley/gcc-2.95.3.tar.bz2
bunzip2 -c gcc-2.95.3.tar.bz2 | tar xf -
cd gcc-2.95.3
mkdir MyObj
cd MyObj
../configure --prefix=/usr/local/gcc2.95.3 --enable-languages=c,f77
make bootstrap-lean CFLAGS='-O' LIBCFLAGS='-g -O2' LIBCXXFLAGS='-g -O2 -fno-implicit-templates' >& make.log &
tail -f make.log
make install
su
ln -s /usr/local/gcc-2.95.3/bin/gcc /usr/bin/gcc-2.95.3
ln -s /usr/local/gcc-2.95.3/bin/g77 /usr/bin/g77-2.95.3


GCC 2.95.3 を使った ATLAS のコンパイルとインストール


Now that we have our alternate GCC safely tucked away, we can move on to compiling ATLAS with it. The procedure is, as distinguished from merely entering make in the previous example:

make xconfig
./xconfig -m gcc-2.95.3 -c gcc-2.95.3 -f g77-2.95.3


It will still complain that a post 2.95 version of GCC is being used, but the correct 2.95.3 GCC parameters have been inserted into the make file so you can proceed without fear of performance degradation.

make install arch=Linux_* >& make.log &
tail -f make.log


The ATLAS compile can take on the order of hours, so be patient here. After it's done, you can return to the end of the ATLAS VARIATION section to compile the ATLAS-enhanced LAPACK library.


THE FFTW VARIATION THAT OUR FFTS MIGHT BE ENHANCED

Note: There is an issue about whether to use FFTW version 2 or 3 here. In previous versions of the SciPy? family that I've installed, only version 2 would work. I've tried both options with the most recent SciPy?, and found no difference when running the test suite, i.e. with both versions some FFT tests fail identically.

The FFTW package is similar to ATLAS, except that it optimally tunes FFT-based algorithms to your processor rather than the linear algebra algorithms for which ATLAS is designed. Obtain the source code package from the given site and then:

tar xzvf fftw-3.0.1.tar.gz
cd fftw-3.0.1
./configure
make
su
make install


This will choose the GCC compiler suite by default and compile and install the package where SciPy? knows where to find it.

THE DJBFFT VARIATION THAT OUR FFTS MIGHT BE ENHANCED

The djbfft package is for performing FFTs. According to its creator, djbfft...


...is an extremely fast library for floating-point convolution. The current version holds most of the speed records for double-precision FFTs on general-purpose computers.


So if you're using a lot of FFTs in your work, this might be a useful thing to add to SciPy?. I've had no luck compiling this myself, but was lucky enough to find an RPM version for my Fedora Core 4 platform which I installed via:

rpm -ivh djbfft-0.76-0.lvn.4.4.i386.rpm


If you can't get this compiled try a Google search along the lines of "rpm djbfft" to see if you can find a binary version for your specific distribution. Now return to the end of the BASIC SCIPY COMPILATION section to compile SciPy?.

0 comments: