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

対象 GPU CUDA 5.0 インストール Scientific Linux / CentOS / Red Hat EL 6.3

 PGIアクセラレータ™ コンパイラを使用する前に、NVIDIA社の CUDA™ 開発環境をインストールしておく必要があります。ここでは、 一例として Sceintific Linux 6.3 (RHEL/CentOS 6.3) 上に CUDA 5.0 環境をインストールする手順を説明します。ここでの前提は、ハードウェアシステムの中に、NVIDIA 社の GPU が実装されていることとします。
2012年12月12日 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 ボードの型番を調べたい場合は、以下のコマンドを使用します。以下の例は、NVIDIA Tesla K20(NF200) と GeForce 580(GF110) ボードが搭載された場合の状況を示しています。

[root@photon29 ~]# lspci | grep -i nVidia
(一例)
02:00.0 PCI bridge: NVIDIA Corporation NF200 PCIe 2.0 switch (rev a3)
03:00.0 PCI bridge: NVIDIA Corporation NF200 PCIe 2.0 switch (rev a3)
03:02.0 PCI bridge: NVIDIA Corporation NF200 PCIe 2.0 switch (rev a3)
04:00.0 VGA compatible controller: NVIDIA Corporation GF110 [GeForce GTX 580] (rev a1)
04:00.1 Audio device: NVIDIA Corporation GF110 High Definition Audio Controller (rev a1)
06:00.0 3D controller: NVIDIA Corporation Device 1022 (rev a1)

 また、ソフトウェアでは、Linuxシステム上でNVIDIAドライバーコンポーネントをインストールしておく必要があります。 これらは、NVIDIA の Webサイト( https://developer.nvidia.com/cuda-downloads) からダウンロード可能です。これらは、PGIコンポーネントではありませんので、NVIDIA 社によってライセンス並びにサポートされます。現在、CUDA 5.0 のダウンロードサイトでは、以下に示す CUDA Toolkit、SDK code samples、ドライバーの全てが含まれた1ファイルで提供されています。

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

また、CUDA 5.0 をインストールする前に、GPUデバイス用のNVIDIAドライバを以下のサイトからダウンロードして、実装した方が良いでしょう。私の場合は、このページから「Linux x64 (AMD64/EM64T) Display Driver」を検索して、CUDA 5.0 ソフトウェアの実装前に 310.19バージョンのドライバーを最初に組み込みました。このドライバーの実装時においても、以下で述べるkernel-devel (kernel-source) が必要となりますので、まずは、kernel-source の実装から始めてください。

  • http://www.nvidia.co.jp/Download/index.aspx?lang=jp
  • http://www.nvidia.co.jp/object/linux-display-amd64-310.19-driver-jp.html

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

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

 CUDAのドライバーをインストールする際、kernelの一部のモジュールの再構築が必要となります。この再構築は、CUDA driverのインストーラが行いますが、一般的に言う、kernel-sourceがシステム内に存在しない場合は、予め、その実装を行っておく必要があります。この目的では、kernel driver module のリビルドですので、kernelの全ソースは必要ではなく、RedHat/CentOS/Scientific Linux の場合は、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がミラーサイトからダウンロードしてインストールする。
以下は、一例です。現システムのリビジョンにより、以下のバージョン番号が異なる場合があります。)
Resolving Dependencies --> Running transaction check ---> Package kernel-devel.x86_64 0:2.6.32-279.14.1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ========================================================================================== Package Arch Version Repository Size ========================================================================================== Installing: kernel-devel x86_64 2.6.32-279.14.1.el6 sl-security 7.6 M Transaction Summary ========================================================================================== Install 1 Package(s) Total download size: 7.6 M Installed size: 23 M Is this ok [y/N]: y Downloading Packages: kernel-devel-2.6.32-279.14.1.el6.x86_64.rpm | 7.6 MB 00:51 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : kernel-devel-2.6.32-279.14.1.el6.x86_64 1/1 Verifying : kernel-devel-2.6.32-279.14.1.el6.x86_64 1/1 Installed: kernel-devel.x86_64 0:2.6.32-279.14.1.el6 Complete! kernel-develの場所を確認する。 [root@photon30 tmp]# cd /usr/src [root@photon30 src]# ls debug kernels [root@photon30 src]# cd kernels/ [root@photon30 kernels]# ls 2.6.32-279.14.1.el6.x86_64 この source バージョンが現在の kernel バージョンと一致するか確認すること。 バージョンが合致しないと、CUDAドライバーのインストールでエラーとなる。 [root@photon30 ~]$ uname -a Linux photon30 2.6.32-279.14.1.el6.x86_64 #1 SMP Thu Jun 21 07:08:44 CDT 2012 x86_64

CUDA 5.0のインストール

 今回は、Scientific Linux 6.3 にインストールするため、CUDA ダウンロードサイトにて Red Hat Enterprise Linux 6.x (64bit) 用の以下のソフトウェアをダウンロードします。これらは、インストールスクリプト形式ですので、「ルート権限」でスクリプトの実行を行います。

  • cuda_5.0.35_linux_64_rhel6.x-1.run

 CUDA 5.0 から以前のバージョンとは異なり、一つのスクリプトで、ドライバー、Toolkit、SDK samples 全てがインストールする形態に変わりました。その中の CUDAドライバーは、基本的には、X display のドライバでもありますので、これをインストールする時は、X が動作していない状態で行う必要があります。Linux システムの起動では、デフォルト Run level 5 で起動されますので、X が動作しています。これを一旦、Run level 3 に戻してから CUDA のドライバのインストールを行います。以下のように、スクリプトを実行します。もし、この実行が Nouveau kernel driver に関するエラーで終了した場合は、こちらを参照してこの問題を回避して下さい

[root@photon29 ~]# init 3

[root@photon29 tmp]# chmod 777 cuda_5.0.35_linux_64_rhel6.x-1.run
[root@photon29 tmp]# bash cuda_5.0.35_linux_64_rhel6.x-1.run 
Logging to /tmp/cuda_install_13110.log
Usage: which [options] [--] COMMAND [...]
Write the full path of COMMAND(s) to standard output.
.....(snip)

Do you accept the previously read EULA? (accept/decline/quit): accept
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 304.54? ((y)es/(n)o/(q)uit): y
Install the CUDA 5.0 Toolkit? ((y)es/(n)o/(q)uit): y
Enter Toolkit Location [ default is /usr/local/cuda-5.0 ]:
Install the CUDA 5.0 Samples? ((y)es/(n)o/(q)uit): y
Enter CUDA Samples Location [ default is /usr/local/cuda-5.0/samples ]:
Installing the NVIDIA display driver...
Installing the CUDA Toolkit in /usr/local/cuda-5.0 ...
   Missing required library libglut.so

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

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

* 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

Logfile is /tmp/cuda_install_13110.log(実行ログファイル)
 

 参考のため、上記のスクリプトを実行した際のログ/tmp/cuda_install_13110.log の内容は、以下の通りです。

Using more to view the EULA.
Installing the NVIDIA display driver...

Verifying archive integrity... OK
Uncompressing NVIDIA Accelerated Graphics Driver for Linux-x86_64 304.54..................

Welcome to the NVIDIA Software Installer for Unix/Linux

License accepted by command line option.
Installing NVIDIA driver version 304.54.
There appears to already be a driver installed on your system (version:
310.19).  As part of installing this driver (version: 304.54), the existing
driver will be uninstalled.  Are you sure you want to continue? ('no' will
abort installation) (Answer: Yes)
Performing CC sanity check with CC="cc".
Performing CC version check with CC="cc".
Kernel source path: '/lib/modules/2.6.32-279.14.1.el6.x86_64/source'
Kernel output path: '/lib/modules/2.6.32-279.14.1.el6.x86_64/build'
Performing rivafb check.
Performing nvidiafb check.
Performing Xen check.
Cleaning kernel module build directory.
Building kernel module:

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?
                                                          
  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.


 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.

ここで、一旦、エラーで終了する。この後、Nouveaucd 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-279.14.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インストール後の Linux 設定

環境変数 PATH と LD_LIBRARY_PATH

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

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

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

CUDAコマンドの実行検証

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

[kato@photon29 ~]$ which nvcc
/usr/local/cuda-5.0/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-2012 NVIDIA Corporation
Built on Fri_Sep_21_17:28:58_PDT_2012
Cuda compilation tools, release 5.0, V0.2.1221

[kato@photon29 ~]$ nvcc test.c
 
[kato@photon29 ~]$ ./a.out
 hello
[kato@photon29 ~]$ ldd a.out (shared library のリンク確認)
        linux-vdso.so.1 =>  (0x00007fff0c5ff000)
        libcudart.so.5.0 => /usr/local/cuda-5.0/lib64/libcudart.so.5.0 (0x00007f0d82157000)
        libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003ec2a00000)
        libm.so.6 => /lib64/libm.so.6 (0x00007f0d81ec2000)
        libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003ec2200000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f0d81b2e000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f0d8192a000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0d8170d000)
        librt.so.1 => /lib64/librt.so.1 (0x00007f0d81504000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f0d823b2000)

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

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

 PGIコンパイラが既にインストールされている場合は、pgaccelinfo コマンドで、実装されているNVIDIA GPUボード情報が出力できます。もし、GPUのボード特性が表示されない場合は、こちらの FAQ をご参照下さい。Linux 上で、CUDA module のロードが行われておらず、/dev 上の nvidia エントリが作成されていない可能性があります。

[kato@photon29 GPGPU]$ pgaccelinfo
CUDA Driver Version            5000  <== この番号は、NVIDIAドライバーのバージョン番号 5.0 の意味
NVRM version: NVIDIA UNIX x86_64 Kernel Module  304.54  Sat Sep 29 00:05:49 PDT 2012

Device Number:                 0
Device Name:                   Tesla K20c
Device Revision Number:        3.5
Global Memory Size:            5032706048
Number of Multiprocessors:     13
Number of SP Cores:            2496
Number of DP Cores:            832
Concurrent Copy and Execution: Yes
Total Constant Memory:         65536
Total Shared Memory per Block: 49152
Registers per Block:           65536
Warp Size:                     32
Maximum Threads per Block:     1024
Maximum Block Dimensions:      1024, 1024, 64
Maximum Grid Dimensions:       2147483647 x 65535 x 65535
Maximum Memory Pitch:          2147483647B
Texture Alignment:             512B
Clock Rate:                    705 MHz
Execution Timeout:             No
Integrated Device:             No
Can Map Host Memory:           Yes
Compute Mode:                  default
Concurrent Kernels:            Yes
ECC Enabled:                   Yes
Memory Clock Rate:             2600 MHz
Memory Bus Width:              320 bits
L2 Cache Size:                 1310720 bytes
Max Threads Per SMP:           2048
Async Engines:                 2
Unified Addressing:            Yes
Initialization time:           1901539 microseconds
Current free memory:           4952023040
Upload time (4MB):              919 microseconds ( 722 ms pinned)
Download time:                 1061 microseconds ( 644 ms pinned)
Upload bandwidth:              4563 MB/sec (5809 MB/sec pinned)
Download bandwidth:            3953 MB/sec (6512 MB/sec pinned)

Device Number:                 1
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
Execution Timeout:             No
Integrated Device:             No
Can Map Host Memory:           Yes
Compute Mode:                  default
Concurrent Kernels:            Yes
ECC Enabled:                   No
Memory Clock Rate:             2004 MHz
Memory Bus Width:              384 bits
L2 Cache Size:                 786432 bytes
Max Threads Per SMP:           1536
Async Engines:                 1
Unified Addressing:            Yes
Initialization time:           1901539 microseconds
Current free memory:           1541660672
Upload time (4MB):              924 microseconds ( 729 ms pinned)
Download time:                 1032 microseconds ( 649 ms pinned)
Upload bandwidth:              4539 MB/sec (5753 MB/sec pinned)
Download bandwidth:            4064 MB/sec (6462 MB/sec pinned)
PGI GPU 用コンパイラ情報サイト