conta's diary

思ったこと、やったことを書いてます。 twitter: @conta_

Ubuntu server 12.04にNvidia CUDAをインストールしてOpenCVのgpuモジュールを利用する

1.ドライバーのインストール

リポジトリを追加してインストール。

sudo apt-add-repository ppa:ubuntu-x-swat/x-updates
sudo apt-get update 
sudo apt-get install nvidia-current

2.CUDAのインストール

CUDAの公式サイトCUDA Downloads | NVIDIA Developer Zoneから、最新のものをDLする。最新のものは11.10になっているけど、12.04に入れても大丈夫かな?

wget http://developer.download.nvidia.com/compute/cuda/5_0/rel-update-1/installers/cuda_5.0.35_linux_64_ubuntu11.10-1.run

下記コマンドにてインストール。

sudo zsh ./cuda_5.0.35_linux_64_ubuntu11.10-1.run

Agreement的なものがでるけど、よく読んで"s"ボタンでスキッp"accept"と入力。
ドライバーはインストールしているので”n”を入力、後は"y"を入力。

下記出力結果。

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-5.0
Samples:  Installed in /usr/local/cuda-5.0/samples (pristine) and /home/===/NVIDIA_CUDA-5.0_Samples (writable)

* Please make sure your PATH includes /usr/local/cuda-5.0/bin
* Please make sure your LD_LIBRARY_PATH *   for 32-bit Linux distributions includes /usr/local/cuda-5.0/lib
*   for 64-bit Linux distributions includes /usr/local/cuda-5.0/lib64:/lib
* OR
*   for 32-bit Linux distributions add /usr/local/cuda-5.0/lib
*   for 64-bit Linux distributions add /usr/local/cuda-5.0/lib64 and /lib
* to /etc/ld.so.conf and run ldconfig as root
 * To uninstall CUDA, remove the CUDA files in /usr/local/cuda-5.0
* Installation Complete

Please see CUDA_Getting_Started_Guide_For_Linux.pdf in /usr/local/cuda-5.0/doc/pdf for detailed information on setting up CUDA.

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 304.54 is required for CUDA 5.0 functionality to work.
            To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
                sudo <CudaInstaller>.run -silent -driver

ここで、

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-5.0
Samples:  Installation Failed. Missing required libraries.

となっていて、"Missing required library libglut.so"というログが残っている場合には、

sudo ln -s /usr/lib/x86_64-linux-gnu/libglut.so.3 /usr/lib/libglut

でうまくいくらしい。

もし、glutが入ってなければ下記コマンドでインストール。

sudo apt-get install freeglut3 freeglut3-dev

*参考How to Install OpenGL/Glut libraries in Ubuntu 12.04 | Singh Gurjot


インストール後、CUDAのパスを通す。

export PATH=/usr/local/cuda-5.0/bin:${PATH}
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-5.0/lib64:/usr/local/cuda-5.0/lib

3.CUDAサンプルのビルド

NVIDIA_CUDA-5.0_Samplesにサンプルがインストールされるらしいので、試しにビルドしてみる。

cd ~/NVIDIA_CUDA-5.0_Samples
make

ここで、

 /usr/bin/ld: cannot find -lcuda

と言われたので、

LIBRARY_PATH=/usr/lib/nvidia-current:$LIBRARY_PATH

を追加。

*サンプルを全部ビルドしたい場合はOpen MPI 関連のパッケージも必要。
下記コマンドでインストール。

sudo apt-get install openmpi-bin openmpi-dev

4. OpenCVでgpuモジュールをビルド

cmakeのオプションをWITH_CUDA=ONにして実行。

下記のように怒られた。

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
CUDA_nvcuvid_LIBRARY (ADVANCED)
    linked by target "opencv_gpu" in directory /home/XXX/opencv/OpenCV-2.4.3/modules/gpu

-- Configuring incomplete, errors occurred!

下記のように対応。

sudo ln -s /usr/lib/nvidia-current/libnvcuvid.so /usr/lib/libnvcuvid.so

*参考opencv-users - [CUDA] cmake error when when running WITH_CUDA on linux

最終的な実行コマンドは下記の通り。

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON WITH_CUDA=ON  CUDA_nvcuvid_LIBRARY=/usr/local/cuda-5.0 ..

あとは、

make
make install

でインストール完了。

と思ったら、下記エラーが発生。

[ 22%] Built target opencv_ts
Linking CXX executable ../../bin/opencv_perf_core
../../lib/libopencv_core.so.2.4.3: error: undefined reference to 'cuDeviceGetAttribute'
collect2: ld はステータス 1 で終了しました
make[2]: *** [bin/opencv_perf_core] エラー 1
make[1]: *** [modules/core/CMakeFiles/opencv_perf_core.dir/all] エラー 2
make: *** [all] エラー 2

取り急ぎ、CMakeCache.txt内のCUDA_CUDA_LIBRARY:FILEPATH
CUDA_CUDA_LIBRARY:FILEPATH=/usr/lib/nvidia-current/libcuda.so
のように変更。
*参考 Compiling OpenCV 2.4.3 on Ubuntu 12.10 « AmmarkoV `s Personal Website

動かしてみる

ちょうどいいサンプルが、sample/gpu/hog.hppにあったのでビルドしてみる。

g++ hog.cpp `/usr/bin/pkg-config --cflags --libs opencv`

このサンプルはCPUとGPUの切り替えができる。
大体4倍ぐらい性能差が出ました。


*ここを参考に参考にさせて頂きました。
Nvidia drivers for Ubuntu 12.10 Quantal/Ubuntu 12.04 Precise/Ubuntu 11.10 Oneiric Ocelot/Linux Mint ~ Noobs on Ubuntu and Windows, HD Wallpapers, Tutorials