ksメモ

私が学んだことを記載したメモです。あとポエム。

CUDAのサンプルで"cannot find -lGL"でハマった件

ちょっとハマった、というか「しょーもな」って感じのポイントなんだけど。いちおうメモしておく。

CUDAをインストールしたら、まずはsampleを動かしてみたくなるんだけど、nbodyをビルドしようとしたら以下のエラーが発生した。OSはUbuntu 14.04.5、CUDAは7.5。

$ cd /usr/local/cuda/samples/5_Simulations/nbody
$ make
>>> WARNING - libGLU.so not found, refer to CUDA Getting Started Guide for how to find and install them. <<<
>>> WARNING - libX11.so not found, refer to CUDA Getting Started Guide for how to find and install them. <<<
>>> WARNING - gl.h not found, refer to CUDA Getting Started Guide for how to find and install them. <<<
>>> WARNING - glu.h not found, refer to CUDA Getting Started Guide for how to find and install them. <<<
>>> WARNING - Xlib.h not found, refer to CUDA Getting Started Guide for how to find and install them. <<<

はーん。OpenGL関連がないのかー。
なのでapt-getで必要そうなライブラリをインストールする。

$ sudo apt-get install freeglut3-dev libglu1-mesa-dev

これでmakeし直したらいいと思ったら、「libGLなんて見つからないよ」とエラーが出てきた。

$ sudo make
"/usr/local/cuda-7.5"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -ftz=true -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o bodysystemcuda.o -c bodysystemcuda.cu
"/usr/local/cuda-7.5"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -ftz=true -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o nbody.o -c nbody.cpp
"/usr/local/cuda-7.5"/bin/nvcc -ccbin g++ -I../../common/inc  -m64    -ftz=true -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o render_particles.o -c render_particles.cpp
"/usr/local/cuda-7.5"/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o nbody bodysystemcuda.o nbody.o render_particles.o  -L../../common/lib/linux/x86_64 -L/usr/lib/"nvidia-352" -lGL -lGLU -lX11 -lglut -lGLEW
/usr/bin/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
make: *** [nbody] Error 1

いや、そんなはずはない。さっきインストールしたでしょ?
と思ってlocaleとか使って調べたら、なんか変なシンボリックリンクが貼ってあってあるだけだった。
ブログだと色がついてないからわかりにくいと思うけど、libGL.soの参照先が存在しないファイルを指定してる。
#同ディレクトリに「libGL.so.1.2.0」はない
#素のUbuntuターミナルだと赤色になっていると思う。

$ ll /usr/lib/x86_64-linux-gnu/mesa/
total 76
drwxr-xr-x  2 root root  4096  9月 25 00:16 ./
drwxr-xr-x 95 root root 69632  9月 25 00:16 ../
-rw-r--r--  1 root root    31  7月  1 05:45 ld.so.conf
lrwxrwxrwx  1 root root    14  1月 13  2016 libGL.so -> libGL.so.1.2.0

下記の方法がよいかわかんないけど、libGLがちゃんとある場所にシンボリックリンクを指定してあげることにした。

$ cd /usr/lib
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libGL.so.1.0.0 libGL.so

これでビルドしたら上手くいった。
やっぱりGPGPUと言えばこのsampleですよねー。

$ sudo make
"/usr/local/cuda-7.5"/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_20,code=sm_20 -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_52,code=compute_52 -o nbody bodysystemcuda.o nbody.o render_particles.o  -L../../common/lib/linux/x86_64 -L/usr/lib/"nvidia-352" -lGL -lGLU -lX11 -lglut -lGLEW
mkdir -p ../../bin/x86_64/linux/release
cp nbody ../../bin/x86_64/linux/release

$ ./nbody 
Run "nbody -benchmark [-numbodies=<numBodies>]" to measure performance.
    -fullscreen       (run n-body simulation in fullscreen mode)
    -fp64             (use double precision floating point values for simulation)
    -hostmem          (stores simulation data in host memory)
    -benchmark        (run benchmark to measure performance) 
    -numbodies=<N>    (number of bodies (>= 1) to run in simulation) 
    -device=<d>       (where d=0,1,2.... for the CUDA device to use)
    -numdevices=<i>   (where i=(number of CUDA devices > 0) to use for simulation)
    -compare          (compares simulation results running once on the default GPU and once on the CPU)
    -cpu              (run n-body simulation on the CPU)
    -tipsy=<file.bin> (load a tipsy model file for simulation)

NOTE: The CUDA Samples are not meant for performance measurements. Results may vary when GPU Boost is enabled.

> Windowed mode
> Simulation data stored in video memory
> Single precision floating point simulation
> 1 Devices used for simulation
> Compute 5.2 CUDA device: [GeForce GTX 980 Ti]

f:id:ksakiyama134:20160925004830p:plain