Ubuntu Linux 12.04 LTS 安裝 NVIDIA CUDA 4.2

Ubuntu Linux 12.04 LTS(Long Term Support)在 2012 年 4 月釋出,代號為 Precise Pangolin,由於這個版本是 LTS 長期支援版,所以 Ubuntu 對於這個版本所提供的更新服務會持續五年(一般的版本只有三年),也就是可以更新到 2017 年,所以若是作為伺服器使用的系統可以選擇這樣的版本。

NVIDIA CUDA 目前的最新版本是 4.2,但是因為 Ubuntu Linux 12.04 LTS 才剛剛釋出,所以 NVIDIA 官方支援的 Linux 不會包含 Ubuntu Linux 12.04 LTS,不過其實下載 CUDA for Ubutnu 11.04 的版本,也是可以在 Ubuntu Linux 12.04 LTS 上使用的。

要安裝的第一步就是到 NVIDIA CUDA 網站下載 CUDA Toolkit 與 CUDA SDK,而 Driver 的部分其實可以不用下載,直接使用 Ubutnu 內建的就可以了,雖然版本不是最新的,不過其實只差一點點,通常沒什麼影響,而且這樣安裝上方便很多。



安裝 NVIDIA 驅動程式


首先安裝 NVIDIA Driver,直接用 Ubuntu 內建的 Driver 安裝方式是最簡單的,到 System Setting 中選擇 Additional Drivers:


然後則要安裝的版本,建議直接裝 current 版本(也就是最新的版本),安裝完成後還要記得重新開機,下面這個是安裝完成後的 Additional Drivers 畫面:




安裝 NVIDIA CUDA Tookit


接著就可以安裝 CUDA Toolkit 了,直接執行下載下來的安裝檔:
chmod +x cudatoolkit_4.2.9_linux_64_ubuntu11.04.run
sudo ./cudatoolkit_4.2.9_linux_64_ubuntu11.04.run


預設是安裝在 /usr/local/cuda 目錄下,建議就依照他的預設路徑安裝,安裝完成後要設定 Library 的 Path,有兩個方式,一種是更改 LD_LIBRARY_PATH 環境變數,但我習慣直接加在 /etc/ld.so.conf.d/ 裡面:
sudo echo "/usr/local/cuda/lib64" > /etc/ld.so.conf.d/cuda.conf
sudo echo "/usr/local/cuda/lib" >> /etc/ld.so.conf.d/cuda.conf
sudo ldconfig

再設定 PATH:
echo 'export PATH=$PATH:/usr/local/cuda/bin' >> ~/.bashrc


安裝 NVIDIA CUDA SDK


直接執行下載回來的安裝檔:
chmod +x gpucomputingsdk_4.2.9_linux.run
./gpucomputingsdk_4.2.9_linux.run


接著安裝程式會詢問安裝路徑,建議直接按 Enter 使用預設值,

Enter install path (default ~/NVIDIA_GPU_Computing_SDK):

然後會詢問 CUDA Toolkit 的安裝路徑:

Could not locate CUDA. Enter the full path to CUDA.
If you do not know the path, accept the default and then
modify the CUDA_INSTALL_PATH variable in
/home/seal/NVIDIA_GPU_Computing_SDK/C/common/common.mk.

Enter CUDA install path (default /usr/local/cuda):

若 CUDA 都是依照預設路徑安裝,則這裡就不需要更動,直接按 Enter 就可以了。


編譯


接著要進行編譯的部分,首先安裝一些基本編譯用的套件:
sudo apt-get install build-essential libx11-dev libglu1-mesa-dev freeglut3-dev libxi-dev libxmu-dev
另外因為 CUDA 需要使用 gcc 4.4 來編譯,所以要再安裝 4.4 版的 gcc:
sudo apt-get install gcc-4.4 g++-4.4
然後在更改一下編譯的設定,編輯 ~/NVIDIA_GPU_Computing_SDK/C/common/common.mk 這個設定檔,大約在第 58 行附近,將編譯器都改為 4.4 版的 gcc:
# Compilers
NVCC := $(CUDA_INSTALL_PATH)/bin/nvcc
CXX := g++-4.4 -fPIC
CC := gcc-4.4 -fPIC
LINK := g++-4.4 -fPIC

另外在第 255 行附近,加入 NVIDIA 的 Library 路徑(-L/usr/lib/nvidia-current):
ifeq "$(strip $(HP_64))" ""
ifeq ($(x86_64),1)
LIB := -L$(CUDA_INSTALL_PATH)/lib64 -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER) -L$(SHAREDDIR)/lib -L/usr/lib/nvidia-current
else
LIB := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER) -L$(SHAREDDIR)/lib -L/usr/lib/nvidia-current
endif
else
ifeq ($(i386),1)
LIB := -L$(CUDA_INSTALL_PATH)/lib -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER) -L$(SHAREDDIR)/lib -L/usr/lib/nvidia-current
else
LIB := -L$(CUDA_INSTALL_PATH)/lib64 -L$(LIBDIR) -L$(COMMONDIR)/lib/$(OSLOWER) -L$(SHAREDDIR)/lib -L/usr/lib/nvidia-current
endif
endif

這樣就可以開始編譯了,進到 ~/NVIDIA_GPU_Computing_SDK/C 這個目錄,執行 make 編譯:
cd ~/NVIDIA_GPU_Computing_SDK/C
make

等待編譯完成就可以使用 CUDA 了。

另外提醒一點,要使用有 OpenGL 的程式時,最好使用 Ubuntu 2D 的模式登入,不然執行程式時,畫面會很不順。


NVIDIA CUDA in Ubuntu 12.04 LTS

CUDA Device Query (Driver API)

CUDA Particles

CUDA N-Body

Cuda/GL Stable Fluids

CUDA FFT Ocean Simulation
本站已經搬家了,欲查看最新的文章,請至 G. T. Wang 新網站

21 則留言:

  1. 您好 我想請問,照您的方式我卡在設定path的地方
    指令打上去,他顯示權限不足,去該資料夾下看沒有cuda.conf這個檔案 是我哪裡漏做了嗎@@
    網路上找到的資源都是對其他作業系統的 不知道如何是好

    回覆刪除
  2. 有,有使用sudo,昨天稍晚又再另一篇大陸的文章找到,說cuda.conf要自己跑進去建,昨晚用vim建建不起來,要上課,晚點要再挖文章看@@ 感謝您!!

    回覆刪除
  3. 謝謝您 問題已經解決了!

    回覆刪除
  4. 您好,在安装cuda驱动的过程中提示我要不要更新X-config,我不小心选了yes,现在进不了图形界面只能进命令行了,请问有什么处理方法吗?谢谢!

    回覆刪除
  5. To 大南瓜:
    通常 Nvidia 在更新 X-config 的時候,會保留一份原始設定的備份,例如 /etc/X11/xorg.conf.backup,您可以看看您的 /etc/X11/ 目錄中有沒有類似的備份檔案。

    回覆刪除
  6. 作者您好,我最近在Ubuntu12.04上安装CUDA,安装您的教程,我可以成功make,SDK下面的那些例子。但是在运行./deviceQuery,出现:cudaGetDeviceCount returned 35
    -> CUDA driver version is insufficient for CUDA runtime version。
    运行./nbody 出现 Error: only 0 Devices available, 1 requested. Exiting.

    我的环境是:nvcc --version
    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2012 NVIDIA Corporation
    Built on Thu_Apr__5_00:24:31_PDT_2012
    Cuda compilation tools, release 4.2, V0.2.1221

    cat /proc/driver/nvidia -version
    NVRM version: NVIDIA UNIX x86_64 Kernel Module 290.10 Wed Nov 16 17:39:29 PST 2011
    GCC version: gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

    但是我的gcc --version
    gcc (Ubuntu/Linaro 4.4.7-1ubuntu2) 4.4.7

    实在不知道出了什么问题,请您帮我看看。万分感激

    回覆刪除
  7. To Yasin:
    看起來是您的電腦沒有 NVIDIA 的顯示卡,如果方便的話,請執行 lspci 並把輸出貼上來看看。

    回覆刪除
  8. lspci
    00:00.0 Host bridge: Intel Corporation 5520 I/O Hub to ESI Port (rev 22)
    00:01.0 PCI bridge: Intel Corporation 5520/5500/X58 I/O Hub PCI Express Root Port 1 (rev 22)
    00:03.0 PCI bridge: Intel Corporation 5520/5500/X58 I/O Hub PCI Express Root Port 3 (rev 22)
    00:07.0 PCI bridge: Intel Corporation 5520/5500/X58 I/O Hub PCI Express Root Port 7 (rev 22)
    00:14.0 PIC: Intel Corporation 5520/5500/X58 I/O Hub System Management Registers (rev 22)
    00:14.1 PIC: Intel Corporation 5520/5500/X58 I/O Hub GPIO and Scratch Pad Registers (rev 22)
    00:14.2 PIC: Intel Corporation 5520/5500/X58 I/O Hub Control Status and RAS Registers (rev 22)
    00:1a.0 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #4
    00:1a.1 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #5
    00:1a.2 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #6
    00:1a.7 USB controller: Intel Corporation 82801JI (ICH10 Family) USB2 EHCI Controller #2
    00:1b.0 Audio device: Intel Corporation 82801JI (ICH10 Family) HD Audio Controller
    00:1c.0 PCI bridge: Intel Corporation 82801JI (ICH10 Family) PCI Express Root Port 1
    00:1c.5 PCI bridge: Intel Corporation 82801JI (ICH10 Family) PCI Express Root Port 6
    00:1d.0 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #1
    00:1d.1 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #2
    00:1d.2 USB controller: Intel Corporation 82801JI (ICH10 Family) USB UHCI Controller #3
    00:1d.7 USB controller: Intel Corporation 82801JI (ICH10 Family) USB2 EHCI Controller #1
    00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 90)
    00:1f.0 ISA bridge: Intel Corporation 82801JIR (ICH10R) LPC Interface Controller
    00:1f.2 RAID bus controller: Intel Corporation 82801 SATA Controller [RAID mode]
    00:1f.3 SMBus: Intel Corporation 82801JI (ICH10 Family) SMBus Controller
    01:00.0 PCI bridge: Pericom Semiconductor PCI Express to PCI-XPI7C9X130 PCI-X Bridge (rev 04)
    03:00.0 VGA compatible controller: NVIDIA Corporation GF110 [GeForce GTX 580] (rev a1)
    03:00.1 Audio device: NVIDIA Corporation GF110 High Definition Audio Controller (rev a1)
    04:00.0 VGA compatible controller: NVIDIA Corporation G98 [Quadro NVS 295] (rev a1)
    06:00.0 Ethernet controller: Broadcom Corporation NetXtreme BCM5761 Gigabit Ethernet PCIe (rev 10)
    20:07.0 PCI bridge: Intel Corporation 5520/5500/X58 I/O Hub PCI Express Root Port 7 (rev 22)
    20:09.0 PCI bridge: Intel Corporation 5520/5500/X58 I/O Hub PCI Express Root Port 9 (rev 22)
    20:14.0 PIC: Intel Corporation 5520/5500/X58 I/O Hub System Management Registers (rev 22)
    20:14.1 PIC: Intel Corporation 5520/5500/X58 I/O Hub GPIO and Scratch Pad Registers (rev 22)
    20:14.2 PIC: Intel Corporation 5520/5500/X58 I/O Hub Control Status and RAS Registers (rev 22)
    3f:00.0 Host bridge: Intel Corporation Xeon 5600 Series QuickPath Architecture Generic Non-core Registers (rev 02)

    回覆刪除
  9. 3f:00.1 Host bridge: Intel Corporation Xeon 5600 Series QuickPath Architecture System Address Decoder (rev 02)
    3f:02.0 Host bridge: Intel Corporation Xeon 5600 Series QPI Link 0 (rev 02)
    3f:02.1 Host bridge: Intel Corporation Xeon 5600 Series QPI Physical 0 (rev 02)
    3f:02.2 Host bridge: Intel Corporation Xeon 5600 Series Mirror Port Link 0 (rev 02)
    3f:02.3 Host bridge: Intel Corporation Xeon 5600 Series Mirror Port Link 1 (rev 02)
    3f:02.4 Host bridge: Intel Corporation Xeon 5600 Series QPI Link 1 (rev 02)
    3f:02.5 Host bridge: Intel Corporation Xeon 5600 Series QPI Physical 1 (rev 02)
    3f:03.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Registers (rev 02)
    3f:03.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Target Address Decoder (rev 02)
    3f:03.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller RAS Registers (rev 02)
    3f:03.4 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Test Registers (rev 02)
    3f:04.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Control (rev 02)
    3f:04.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Address (rev 02)
    3f:04.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Rank (rev 02)
    3f:04.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Thermal Control (rev 02)
    3f:05.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Control (rev 02)
    3f:05.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Address (rev 02)
    3f:05.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Rank (rev 02)
    3f:05.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Thermal Control (rev 02)
    3f:06.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Control (rev 02)
    3f:06.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Address (rev 02)
    3f:06.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Rank (rev 02)
    3f:06.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Thermal Control (rev 02)

    回覆刪除
  10. 3f:00.1 Host bridge: Intel Corporation Xeon 5600 Series QuickPath Architecture System Address Decoder (rev 02)
    3f:02.0 Host bridge: Intel Corporation Xeon 5600 Series QPI Link 0 (rev 02)
    3f:02.1 Host bridge: Intel Corporation Xeon 5600 Series QPI Physical 0 (rev 02)
    3f:02.2 Host bridge: Intel Corporation Xeon 5600 Series Mirror Port Link 0 (rev 02)
    3f:02.3 Host bridge: Intel Corporation Xeon 5600 Series Mirror Port Link 1 (rev 02)
    3f:02.4 Host bridge: Intel Corporation Xeon 5600 Series QPI Link 1 (rev 02)
    3f:02.5 Host bridge: Intel Corporation Xeon 5600 Series QPI Physical 1 (rev 02)
    3f:03.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Registers (rev 02)
    3f:03.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Target Address Decoder (rev 02)
    3f:03.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller RAS Registers (rev 02)
    3f:03.4 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Test Registers (rev 02)
    3f:04.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Control (rev 02)
    3f:04.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Address (rev 02)
    3f:04.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Rank (rev 02)
    3f:04.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 0 Thermal Control (rev 02)
    3f:05.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Control (rev 02)
    3f:05.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Address (rev 02)
    3f:05.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Rank (rev 02)
    3f:05.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 1 Thermal Control (rev 02)
    3f:06.0 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Control (rev 02)
    3f:06.1 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Address (rev 02)
    3f:06.2 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Rank (rev 02)
    3f:06.3 Host bridge: Intel Corporation Xeon 5600 Series Integrated Memory Controller Channel 2 Thermal Control (rev 02)

    回覆刪除
  11. 我的电脑里有2个显卡:NVIDIA GTX580 和 NVS 295.

    回覆刪除
  12. To Yasin:
    您安裝的 NVIDIA Driver 版本是哪一版?Linux 系統有啓動 X window 嗎?執行 ls /dev | grep nvidia 的輸出是什麼?

    回覆刪除
  13. NVIDIA driver 是 290.10
    我不知道 Linux 有没有启动 X window。

    ls /dev | grep nvidia
    nvidia0
    nvidia1
    nvidiactl

    回覆刪除
  14. To Yasin:
    我看起來您的系統都很正常,剩下的可能就是 NVIDIA Driver 的版本太舊了,可以安裝最新版的 Driver 試試看。

    回覆刪除
  15. 我的2个显卡一个用于显示(NVS 295),一个用于计算(GTX580). 那我是不是应该搜索 NVS295 的driver?? 我搜索了,最新的是 295.59
    也没用比我现在的新多少啊。。。。

    回覆刪除
  16. To Yasin:
    這兩張卡適用的 Driver 是同一個,所以沒差,直接裝 295.59 就可以了。

    在 NVIDIA CUDA Zone 的網頁建議的版本是 295.41:
    http://www.nvidia.com/content/cuda/cuda-downloads.html
    所以我猜應該是 Driver 版本的問題。

    回覆刪除
  17. 好的最后一个问题。在我安装完 driver后,是否还需要安装一遍 cudatoolkit,sdk,编译一遍sdk下面的例子?

    回覆刪除
  18. To Yasin:
    如果您之前的安裝與編譯都沒問題,換 Driver 應該是不需要重新安裝與編譯 CUDA Toolkit 與 SDK。

    回覆刪除
  19. 你好。我还想请教一个问题。我的电脑里的2个GPU。当我运行CUDA code 的时候,我怎么知道是哪个GPU在运行呢? 还是2个GPU一起运行? 我的想法是,一个只用来计算,一个只用来显示。这样即使在计算的时候,屏幕也不会卡。请问有方法实现吗? 谢谢

    回覆刪除
  20. To Yasin:
    使用 cudaSetDevice() 函數可以指定 CUDA Device,詳細用法請看 CUDA C Programming Guide 中的 3.2.6.2 Device Selection

    回覆刪除