Cluster GPU InstanceにPyCUDA環境を構築

Cluster GPU InstanceはOSがCent OS 5.5なのでPythonのバージョンが2.4です.このままではいろいろと不便なのでまずは最新のPython(2.7.1)をソースコードからインストールしましょう.URLやバージョンは執筆時点でのものですので,適当に読み替えてください.インストール先は/opt/localとします.

# wget http://www.python.org/ftp/python/2.7.1/Python-2.7.1.tar.bz2
# tar jxvf Python-2.7.1.tar.bz2
# cd Python-2.7.1
# ./configure --prefix=/opt/local
# make

makeすると,ライブラリが足りなくていくつかのモジュールがビルドされていないことがわかります.

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _curses            _curses_panel   
_sqlite3           _ssl               _tkinter        
bsddb185           bz2                dbm             
dl                 gdbm               imageop         
readline           sunaudiodev        zlib            

yumでインストールしていきましょう.

# yum install sqlite-devel readline-devel zlib-devel ncurses-devel tk-devel gdbm-devel openssl-devel bzip2-devel

再度makeします.

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             bsddb185           dl              
imageop            sunaudiodev                        

まだいくつかのモジュールがビルドされていませんが,必要なライブラリがわかりませんでした.ご存知の方は教えていただけると助かります.

# make
# make install

Python本体のビルドは完了なはずです.PATHの設定をしておきます.

# echo 'export PATH=$PATH:/usr/local/cuda/bin:/opt/local/bin' >> /etc/profile
# echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/local/lib64' >> /etc/profile
# source /etc/profile

Pythonの動作確認.pythonだけだとデフォルトのpython2.4を見に行ってしまうので,python2.7と打ちます.

# python2.7
Python 2.7.1 (r271:86832, Feb 12 2011, 23:42:46) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

pipを使えばPyCUDAを簡単にインストールできるので,まずpipをインストールしましょう.

# cd
# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
# tar xvzf setuptools-0.6c11
# cd setuptools-0.6c11
# python2.7 setup.py build
# python2.7 setup.py install
# cd
# wget http://pypi.python.org/packages/source/p/pip/pip-0.8.2.tar.gz#md5=df1eca0abe7643d92b5222240bed15f6
# tar xvzf pip-0.8.2.tar.gz
# cd pip-0.8.2
# python2.7 setup.py build
# python2.7 setup.py install

CentOSgccのバージョンも古く,PyCUDAのビルド時にエラーが発生します.gccソースコードからビルドしましょう.

# cd
# yum install gmp-devel glibc-devel
# wget ftp://ftp.dti.ad.jp/pub/lang/gcc/releases/gcc-4.3.5/gcc-4.3.5.tar.bz2
# tar jxvf gcc-4.3.5.tar.bz2
# cd gcc-4.3.5
# ./configure --prefix=/opt/local --enable-languages=c,c++
# make
# make install

いよいよPyCUDAのインストールです.pip実行時には,/opt/local/binにインストールしたgccを使ってもらうようにPATHを変更しています.

# pip install numpy
# yum install gcc-c++
# PATH=/opt/local/bin:$PATH pip install pycuda

これでインストールは完了です.pycuda.autoinit,pycuda.driverをimportして,エラーが発生しなければ問題ないでしょう.

# python2.7
Python 2.7.1 (r271:86832, Feb 13 2011, 02:53:05) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pycuda.autoinit
>>> import pycuda.driver
>>>