5. GPGPU を使った並列化
GPGPU の説明については
【再掲】【研究課題レポート抜粋】GPGPU によるパーソナルスーパーコンピュータの可能性
http://ameblo.jp/principia-ca/entry-10736425646.html
にて説明されているので割愛。
通常 C/C++から利用される NVidia CUDA の API を python から利用する。
python の CUDA バインディングとして pyCUDA が発表されているので、今回はこれを利用する
http://mathema.tician.de/software/pycuda
5
6. セットアップ
Numpy インストール
configure オプションは
python configure.py
--boost-inc-dir=/usr/local/include/boost/
--boost-lib-dir=/usr/local/lib/
--boost-python-libname=boost_python-xgcc42-mt
--cuda-root=/usr/local/cuda/
で指定した後に
make
make install
を行う。
Mac にセットアップする場合は export MACOSX_DEPLOYMENT_TARGET=10.6 を環
境変数にセットする必要あり
6
7. Boost インストール
./bootstrap.sh --prefix=/usr/local/
./b2 install
boost.python コンポーネントのインストールも必要なので、オプションを指定し
てから再ビルドをかける
./bootstrap.sh --prefix=/usr/local/ --with-libraries=python
./b2 install
Component configuration:
- chrono : not building
- date_time : not building
- exception : not building
- filesystem : not building
- graph : not building
- graph_parallel : not building
- iostreams : not building
- math : not building
- mpi : not building
- program_options : not building
- python : building
- random : not building
- regex : not building
- serialization : not building
- signals : not building
- system : not building
- test : not building
と出れば/usr/local/lib/libboost_python.a に出力されているのが確認できる
- thread : not building
- wave : not building
pyCUDA インストール
./configure.py --cuda-root=/home/x/cuda --cudadrv-lib-dir=/usr/lib64/nvidia
--boost-python-libname=boost_python-mt --boost-thread-libname=boost_thread-mt
make
7
8. make install
インストール完了後に
make tests
を実行してテストをパスすれば OK.
実行
pycuda モジュールを使用し、CUDA Thread を 64 * 64Thread 生成し並列で行列計算処理を行
う。
ソースコードは pycuda_execute.py に示す。
考察
C/C++を使う必要がある CUDA プログラミングにおいて、HOST 側のメモリやデバイス側のメモリ確保
など実際の計算処理の準備段階が非常に煩雑になりがちだが、pycuda を使うことによりかなり簡単に
(スクリプトっぽく)CUDA 計算処理が利用できるようになった。
CPU を使った計算処理(大規模なソートなど)をとりあえずの処理として行いたい場合は python +
CUDA で書くというのも選択肢だと感じた。
今後は pyOpenCL などの動向に注視していきたい。
8