Ubuntu14.04 LTS にGTX1080 をセットアップ（2016年11月版）

# 2016年11月版CUDAセットアップ

## 0.マシンスペック
* CPU : i5-6600
* MB : H170 Pro (ASUS)
* RAM : DDR4 PC4-17000 8GB * 2 (Corsair)
* VGA : GTX 1080 (MSI)
* HDD : 1TB
* PSU : 650W (Corsair)
* OS : Ubuntu14.04 LTS

OSは以下よりダウンロード
https://www.ubuntulinux.jp/News/ubuntu1404-ja-remix

OSのインストールは以下の記事の手順2から先を参考に
http://qiita.com/salty-vanilla/items/a1cddd365b4c106fd446

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

```bash
sudo add-apt-repository ppa:xorg-edgers/ppa
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-cache search 'nvidia-[0-9]+$'
```

以下のように表示される

```
nvidia-173 - NVIDIA legacy binary driver - version 173.14.39
nvidia-310 - Transitional package for nvidia-310
nvidia-319 - Transitional package for nvidia-319
nvidia-331 - Transitional package for nvidia-331
nvidia-346 - Transitional package for nvidia-346
nvidia-304 - NVIDIA legacy binary driver - version 304.132
nvidia-340 - NVIDIA binary driver - version 340.98
nvidia-352 - NVIDIA binary driver - version 352.79
nvidia-355 - NVIDIA binary driver - version 355.11
nvidia-358 - NVIDIA binary driver - version 358.16
nvidia-361 - NVIDIA binary driver - version 361.45.18
nvidia-364 - NVIDIA binary driver - version 364.19
nvidia-367 - NVIDIA binary driver - version 367.57
nvidia-370 - NVIDIA binary driver - version 370.28
```

370.28をインストール

```bash
sudo apt-get install nvidia-370
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
```

## 2. CUDA TOOLKITのインストール

https://developer.nvidia.com/cuda-toolkit　から
CUDA Toolkit 8.0をダウンロード
![クリップボード01.jpg](https://qiita-image-store.s3.amazonaws.com/0/136317/4a514c3d-b747-8ed4-ff64-313e0ea9981f.jpeg)

インストールする

```bash
cd ~/Downloads
sudo sh cuda_8.0.44_linux.run
```

長い文章が出てきたら、Qキーを押して、以下のように進めていく

```
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48?
(y)es/(n)o/(q)uit: n

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
 [ default is /usr/local/cuda-8.0 ]: 

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: y

Enter CUDA Samples Location
 [ default is /home/gpu6 ]: 

Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...
Missing recommended library: libXi.so
Missing recommended library: libXmu.so

Installing the CUDA Samples in /home/gpu6 ...
Copying samples to /home/gpu6/NVIDIA_CUDA-8.0_Samples now...
Finished copying samples.

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

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /home/gpu6, but missing recommended libraries

Please make sure that
 -   PATH includes /usr/local/cuda-8.0/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/bin

Please see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.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 361.00 is required for CUDA 8.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

Logfile is /tmp/cuda_install_16037.log
```
パスを通す

```bash
echo export PATH=/usr/local/cuda/bin${PATH:+:${PATH}} >> ~/.bashrc
echo export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} >> ~/.bashrc
echo export CUDA_HOME=/usr/local/cuda >> ~/.bashrc
```

g++のインストール

```bash
sudo apt-get install g++
```

一回リブートして、CUDA_TOOL_KITの動作確認

```bash
cd ~/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery
make
./deviceQuery
```

```
./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1080"
  CUDA Driver Version / Runtime Version          8.0 / 8.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 8110 MBytes (8504279040 bytes)
  (20) Multiprocessors, (128) CUDA Cores/MP:     2560 CUDA Cores
  GPU Max Clock rate:                            1823 MHz (1.82 GHz)
  Memory Clock rate:                             5005 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 2097152 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 4 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1080
Result = PASS
```

## 3. cuDNNのインストール
https://developer.nvidia.com/rdp/form/cudnn-download-survey
からアンケートに答えて、cuDNNをダウンロード

Download cuDNN v5.1 (August 10, 2016), for CUDA 8.0
┠ cuDNN v5.1 Library for Linux

```bash
cd ~/Downloads
tar xvzf cudnn-8.0-linux-x64-v5.1.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
```

以上で、CUDAのセットアップは完了です。