CUDA™ 4.0 開発環境をインストールする (Linux版)

対象 GPU CUDA 4.0 インストール CentOS 5.5

 PGIアクセラレータ™ コンパイラの使用する前に、NVIDIA社の CUDA™ 環境環境をインストールしておく必要があります。ここでは、 一例として CentOS 5.5上に CUDA 4.0環境をインストールする手順を説明します。ここでの前提は、ハードウェアシステムの中に、NVIDIA社の GPU が実装されていることとします。
2011年6月24日 Copyright © 株式会社ソフテック 加藤

必要とするハードウェア、ソフトウェア環境

 PGIアクセラレータ™ コンパイラを使用するためには、NVIDIA社の CUDA-enable な GeForce、Quadro、Tesla カードを有した CUDA ソフトウェアがインストールされたシステムが必要です。サポートされる GPU の詳細なリストは、NVIDIA の ウェブサイトでご覧下さい。www.nvidia.com/object/cuda_learn_products_jp.html
なお、現在、Linuxシステム(Red Hat/ CentOS /Fedora) に搭載している GPU ボードの型番を調べたい場合は、以下のコマンドを使用します。

[root@photon29 ~]# lspci | grep -i nVidia
(一例)
04:00.0 VGA compatible controller: nVidia Corporation GT200 [GeForce GTX 280] (rev a1)
06:00.0 VGA compatible controller: nVidia Corporation GT200b [GeForce GTX 285] (rev a1)
(私のマシンの場合、型番が出てこない例)
02:00.0 PCI bridge: nVidia Corporation Unknown device 05b1 (rev a3)
03:00.0 PCI bridge: nVidia Corporation Unknown device 05b1 (rev a3)
03:02.0 PCI bridge: nVidia Corporation Unknown device 05b1 (rev a3)
04:00.0 VGA compatible controller: nVidia Corporation Unknown device 06c0 (rev a3)
04:00.1 Audio device: nVidia Corporation Unknown device 0be5 (rev a1)
06:00.0 VGA compatible controller: nVidia Corporation Unknown device 1080 (rev a1)
06:00.1 Audio device: nVidia Corporation Unknown device 0e09 (rev a1)

 また、ソフトウェアでは、Linuxシステム上でNVIDIAドライバーコンポーネントをインストールしておく必要があります。 これらは、NVIDIA の Webサイト( www.nvidia.com/cuda/) からダウンロード可能です。これらは、PGIコンポーネントではありませんので、NVIDIA 社によってライセンス並びにサポートされます。さらに、最新の PGI リリースとCUDAソフトウェア、ドライバーの両方が動作する Linux システムが必要とされます。

  • NVIDIA Driver
  • CUDA Toolkit
  • GPU Computing SDK code samples

CUDA ソフトウェアのインストール

 今回は、CentOS 5.5 にインストールするため、CUDA Zone で Red Hat Enterprise Linux 5.5 用の以下のソフトウェアをダウンロードします。これらは、インストールスクリプト形式ですので、「ルート権限」でスクリプトの実行を行います。

  • devdriver_4.0_linux_64_270.41.19.run(この時点で最新ドライバー)
  • cudatoolkit_4.0.17_linux_64_rhel5.5.run (任意でインストールする。今回は実装する)

CentOS上での kernel-devel (kernel-source) 実装の確認

 CUDAのドライバーをインストールする際、kernelの一部のモジュールの再構築が必要となります。この再構築は、CUDA driverのインストーラが行いますが、一般的に言う、kernel-sourceがシステム内に存在しない場合は、予め、その実装を行っておく必要があります。この目的では、kernel driver module のリビルドですので、kernelの全ソースは必要ではなく、RedHat/CentOSの場合は、kernel-devel の実装だけでよいです。以下のように yum ユーティリティを使用して、kernel-devel をシステムに実装します。yum については、この FAQ 記事でもその使用法を紹介しています。特に、/etc/yum.conf ファイルの中に、組織のPROXY Serverを定義することを忘れないようにして下さい。
(一例)PROXY=http://proxy.softek.co.jp:3333/)

 参考 http://wiki.centos.org/HowTos/I_need_the_Kernel_Source

[root@photon29 ~]# yum install kernel-devel

(既に実装されている場合は、"Nothing to do "と言ったメッセージとなる
実装されていなければ、以下のようにyumがミラーサイトからダウンロードしてインストールする。
以下は、一例です。現システムのリビジョンにより、以下のバージョン番号が異なる場合があります。)
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * addons: ftp.oss.eznetsols.org * base: ftp.oss.eznetsols.org * centosplus: ftp.oss.eznetsols.org * contrib: ftp.oss.eznetsols.org * extras: ftp.oss.eznetsols.org * updates: ftp.oss.eznetsols.org addons| 951 B 00:00 adobe-linux-i386| 951 B 00:00 base| 2.1 kB 00:00 file:///media/CentOS_5.4_Final/repodata/repomd.xml: [Errno 5] OSError: [Errno 2] そのようなファイルやディレクトリはありません: '/media/CentOS_5.4_Final/repodata/repomd.xml' Trying other mirror. centosplus| 1.9 kB 00:00 contrib| 951 B 00:00 extras| 2.1 kB 00:00 updates| 1.9 kB 00:00 Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package kernel-devel.x86_64 0:2.6.18-238.12.1.el5.centos.plus set to be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================== Package Arch Version Repository Size ==================================================================================== Installing: kernel-devel x86_64 2.6.18-238.12.1.el5.centos.plus centosplus 5.6 M Transaction Summary ==================================================================================== Install 1 Package(s) Upgrade 0 Package(s) Total download size: 5.6 M Is this ok [y/N]: y Downloading Packages: kernel-devel-2.6.18-238.12.1.el5.centos.plus.x86_64.rpm | 5.6 MB 01:36 Running rpm_check_debug Running Transaction Test Finished Transaction Test Transaction Test Succeeded Running Transaction Installing : kernel-devel 1/1 Installed: kernel-devel.x86_64 0:2.6.18-238.12.1.el5.centos.plus Complete! kernel-develの場所を確認する。 [[root@photon29 ~]# cd /usr/src/ [root@photon29 src]# ls kernels redhat [root@photon29 src]# cd kernels/ [root@photon29 kernels]# ls 2.6.18-164.15.1.el5.centos.plus-x86_64 2.6.18-194.11.1.el5.centos.plus-x86_64 2.6.18-238.12.1.el5.centos.plus-x86_64 2.6.18-164.9.1.el5.centos.plus-x86_64 2.6.18-194.26.1.el5.centos.plus-x86_64 [root@photon29 kernels]# cd 2.6.18-238.12.1.el5.centos.plus-x86_64 [root@photon29 2.6.18-238.12.1.el5.centos.plus-x86_64]# pwd /usr/src/kernels/2.6.18-238.12.1.el5.centos.plus-x86_64

CUDAドライバのインストール

 CUDAのドライバーは、基本的には、X display のドライバでもありますので、これをインストールする時は、X が動作していない状態で行う必要があります。Linux システムの起動では、デフォルト Run level 5 で起動されますので、Xが動作しています。これを一旦、Run level 3 に戻してから CUDA のドライバのインストールを行います。実行スクリプトの引数に、--kernel-source-path でカーネルソースの場所を明示的に指定します。スクリプト実行時に、ライセンスの許諾と 32bit OpenGL のインストールを行うかどうかの問いの後、以下のような設問が出ます。CUDA ドライバーのインストールスクリプトの実行中に、Nouveauドライバーに関するエラーが出た場合は、こちらをご参照下さい

[root@photon29 ~]# init 3

[root@photon29 tmp]# bash devdriver_4.0_linux_64_270.41.19.run (以下、続く)
--kernel-source-path /usr/src/kernels/2.6.18-238.12.1.el5.centos.plus-x86_64 Verifying archive integrity... OK Uncompressing NVIDIA Accelerated Graphics Driver for Linux-x86_64 260.19.26.......... Please read the following LICENSE and then select either "Accept" to accept the license and continue with the installation, or select "Do Not Accept" to abort the installation. Accept Do Not Accept ※ Accept を選択 (中略) TThere appears to already be a driver installed on your system (version: 260.19.26). As part of installing this driver (version: 270.41.19), the existing driver will be uninstalled. Are you sure you want to continue? ('no' will abort installation) Yes No ※古いドライバーバージョンが実装されているので、これを削除する。Yesを選択。 Install NVIDIA's 32-bit compatibility OpenGL libraries? Yes No ※Yesを選択。その後、warning メッセージを出してから、以下に続く。 WARNING: Your driver installation has been altered since it was initially installed; this may happen, for example, if you have since installed the NVIDIA driver through a mechanism other than nvidia-installer (such as your distribution's native package management system). nvidia-installer will attempt to uninstall as best it can. Please see the file '/var/log/nvidia-installer.log' for details. OK ※OKを選択 Would you like to run the nvidia-xconfig utility to automatically update your X configuration file so that the NVIDIA X driver will be used when you restart X? Any pre-existing X configuration file will be backed up. Yes No ※Yesを選択 Your X configuration file has been successfully updated. Installation of the NVIDIA Accelerated Graphics Driver for Linux-x86_64 (version: 270.41.19) is now complete. OK

Nouveau kernel driverが存在するために、CUDAドライバーがインストールできない場合の対処

 上記のNVIDIA ドライバーをインストールする際にに、システム内に既存のNouveau ドライバーが存在しており、NVIDIA ドライバーと競合する場合、NVIDIA インストースクリプトは、以下のようなエラーを起こします。これに対する対応方法を記します。

 ERROR: The Nouveau kernel driver is currently in use by your system.
        This driver is incompatible with the NVIDIA driver, and
         must be disabled before proceeding.  Please consult the NVIDIA driver
         README and your Linux distribution's documentation for details 
         on how to correctly disable the Nouveau kernel driver.
         
         For some distributions, Nouveau can be disabled by adding a file 
         in the modprobe configuration directory.  Would you like
         nvidia-installer to attempt to create this modprobe file for you?

                                                          Yes           No
                                                          
  The modprobe configuration file to disable Nouveau, 
  /etc/modprobe.d/nvidia-installer-disable-nouveau.conf, has been written.  
  For   some distributions, this may be sufficient to disable Nouveau; other 
  distributions may require modification of the initial   ramdisk.  
  Please reboot your system and attempt NVIDIA driver installation again.  
  Note if you later wish to reenable Nouveau, you will need to delete 
  the file /etc/modprobe.d/nvidia-installer-disable-nouveau.conf.

                                                                 OK

 ERROR: Installation has failed.  Please see the file '/var/log/nvidia-installer.log' 
        for details.  You may find suggestions on fixing installation problems 
        in the README available on the Linux driver download page at www.nvidia.com.

                                                                 OK
ここで、一旦、エラーで終了する。この後、Nouveau driverを disable にするための作業を行う。 

root権限で、以下の操作を行います。以下の例は、Red Hat 6.0(CentOS 6.0)の kernelバージョンの例を示していますが、Red Hat 5.x 系でも同じように設定して下さい。

  1. nouveau driverをブラックリスト化 /etc/modprobe.d/nvidia-installer-disable-nouveau.confを開き、以下を行を挿入する。すでに、nvidia-installerによって作成されている場合もある。
    blacklist nouveau 
    options nouveau modeset=0 
  2. /etc/grub/confファイルを開き、kernel 行に rdblacklist=nouveau オプションを追加する。
    kernel /boot/vmlinuz-2.6.32-71.29.1.el6.x86_64 ro (続く)
    root=UUID=0ab095d2-83b6-411c-a245-035623352cd4 rd_NO_LUKS rd_NO_LVM (続く)
    rd_NO_MD rd_NO_DM LANG=ja_JP.UTF-8 KEYBOARDTYPE=pc KEYTABLE=jp106 (続く)
    crashkernel=auto rhgb rdblacklist=nouveau quiet
  3. reboot する

CUDA toolkit のインストール

 CUDA tool kit をインストールします。デフォルトのインストール先は、/usr/local/cudaです。スクリプトを実行します。インストールを行うディレクトリとして、その「ルート」の場所を指定します。/usr/local/cuda にインストールしたい場合は、/usr/local のみを指定して下さい。その後の "cuda" directory は自動的に追加されます。

[root@photon29 tmp]# bash cudatoolkit_4.0.17_linux_64_rhel5.5.run
------
(中略)
Enter install path (default /usr/local/cuda, '/cuda' will be appended): /usr/local
A previous version of CUDA was found in /usr/local/cuda/bin (古いバージョンが残っている)
Would you like to uninstall? (yes/no/abort): yes (削除する)
(中略)
* Please read the release notes in /usr/local/cuda/doc/
* To uninstall CUDA, delete /usr/local/cuda
* Installation Complete

GPU Computing SDK code samples のインストール

 PGIコンパイラを使用する場合は、GPU Computing SDK code samplesのインストールは必要ありませので、ここでは省略します。任意にインストールして下さい。なお、このインストールは、ルート特権は必要なく、個々のユーザ権限でインストール可能です。

CUDAインストール後の Linux 設定

環境変数 PATH と LD_LIBRARY_PATH

 この環境変数の設定は、PGIコンパイラを使用するだけであるのであれば、必須ではありません。CUDA Tool Kit 内の cudaprof 等の utility を使用する場合や、CUDA C言語を使用する場合は必要ですので、予め設定しておいた方が無難でしょう。CUDAソフトウェアの置かれている場所とCUDA shared library の場所を指定するため、以下の環境変数の設定を $HOME/.bashrc 等の起動 rc ファイルに定義します。ここでは、個々のユーザ id 環境下の .bashrc に設定することにします。CUDA 2.3 以降は、/usr/local/cuda/lib と /usr/local/cuda/lib64 が存在します。64ビットの Linux の場合は、LD_LIBRARY_PATHは、/usr/local/cuda/lib64を指定します

 【64ビットLinux】LD_LIBRARY_PATH=/usr/local/cuda/lib64
 【32ビットLinux】LD_LIBRARY_PATH=/usr/local/cuda/lib

[kato@photon29 tmp]# cd 
[kato@photon29 etc]# vi .bashrc
(前略)
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib:$LD_LIBRARY_PATH
:wq
[root@photon29 etc]# 

CUDAコマンドの実行検証

 設定した PATH 環境変数等が反映された別の端末セッションを開き、CUDAコマンドを実行してみます。

[kato@photon29 ~]$ which nvcc
 /usr/local/cuda/bin/nvcc

(テストプログラム作成)
[kato@photon29 ~]$ vi test.c
main()
{printf("hello\n");}
:wq

[kato@photon29 ~]$ nvcc -V (nvcc コンパイラのバージョン確認)
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2011 NVIDIA Corporation
Built on Thu_May_12_11:09:45_PDT_2011
Cuda compilation tools, release 4.0, V0.2.1221

[kato@photon29 ~]$ nvcc test.c
 
[kato@photon29 ~]$ ./a.out
 hello
[kato@photon29 ~]$ ldd a.out (shared library のリンク確認)
        libcudart.so.4 => /usr/local/cuda/lib64/libcudart.so.4 (0x00002b2060950000) (0x00002b7c89906000)
        libstdc++.so.6 => /opt/open64/lib/gcc-lib/x86_64-open64-linux/4.2.3/libstdc++.so.6 (0x00002b2060bbd000)
        libm.so.6 => /lib64/libm.so.6 (0x00000039b5e00000)
        libgcc_s.so.1 => /opt/open64/lib/gcc-lib/x86_64-open64-linux/4.2.3/libgcc_s.so.1 (0x00002b2060ed8000)
        libc.so.6 => /lib64/libc.so.6 (0x00000039b5a00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00000039b6200000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00000039b6600000)
        librt.so.1 => /lib64/librt.so.1 (0x00000039b6a00000)
        /lib64/ld-linux-x86-64.so.2 (0x00000039b5600000)

 以上で、CUDA の開発環境のセットアップは終了します。

PGIコンパイラの pgaccelinfo コマンドでGPUボード情報を出力

 PGIコンパイラが既にインストールされている場合は、pgaccelinfo コマンドで、実装されているNVIDIA GPUボード情報が出力できます。

[kato@photon29 GPGPU]$ pgaccelinfo
CUDA Driver Version            4000  <== この番号は、NVIDIAドライバーのバージョン番号 4.0の意味
NVRM version: NVIDIA UNIX x86_64 Kernel Module  270.41.19  Mon May 16 23:32:08 PDT 2011

Device Number:                 0
Device Name:                   GeForce GTX 580
Device Revision Number:        2.0
Global Memory Size:            1609760768
Number of Multiprocessors:     16
Number of Cores:               512
Concurrent Copy and Execution: Yes
Total Constant Memory:         65536
Total Shared Memory per Block: 49152
Registers per Block:           32768
Warp Size:                     32
Maximum Threads per Block:     1024
Maximum Block Dimensions:      1024, 1024, 64
Maximum Grid Dimensions:       65535 x 65535 x 65535
Maximum Memory Pitch:          2147483647B
Texture Alignment:             512B
Clock Rate:                    1544 MHz
Initialization time:           602320 microseconds
Current free memory:           1533140992
Upload time (4MB):             1042 microseconds ( 730 ms pinned)
Download time:                 1106 microseconds ( 644 ms pinned)
Upload bandwidth:              4025 MB/sec (5745 MB/sec pinned)
Download bandwidth:            3792 MB/sec (6512 MB/sec pinned)

Device Number:                 1
Device Name:                   GeForce GTX 480
Device Revision Number:        2.0
Global Memory Size:            1610285056
Number of Multiprocessors:     15
Number of Cores:               480
Concurrent Copy and Execution: Yes
Total Constant Memory:         65536
Total Shared Memory per Block: 49152
Registers per Block:           32768
Warp Size:                     32
Maximum Threads per Block:     1024
Maximum Block Dimensions:      1024, 1024, 64
Maximum Grid Dimensions:       65535 x 65535 x 65535
Maximum Memory Pitch:          2147483647B
Texture Alignment:             512B
Clock Rate:                    1401 MHz
Initialization time:           602320 microseconds
Current free memory:           1537335296
Upload time (4MB):              894 microseconds ( 730 ms pinned)
Download time:                 1013 microseconds ( 649 ms pinned)
Upload bandwidth:              4691 MB/sec (5745 MB/sec pinned)
Download bandwidth:            4140 MB/sec (6462 MB/sec pinned)
PGI GPU 用コンパイラ情報サイト