開始接觸CUDA,我安裝好後完全不知道怎麼運行demo,當然也沒有見到詳細的安裝說明。下面說說我的經驗。
Platform:Win XP64
IDE:VS2008
1 擷取可訪問以下網址:
http://www.nvidia.com/object/cuda_get.html
共需下載3個應用程式,分別是:
1、CUDA Driver
2、CUDA Toolkit
3、CUDA SDK code samples
2 安裝
a)其中CUDA Driver是為了使CUDA和硬體互動,CUDA 2.2中驅動檔案版本是185.85,我原來的顯卡驅動是17*.*,所以需要安裝升級。
b)接下來安裝Toolkit,裡面是一些標頭檔和庫,nVidia的編譯器,還有協助文檔。安裝後,會自動在系統的環境變數裡註冊路徑
CUDA_BIN_PATH (defaults to C:/CUDA/bin) contains the compiler executables and runtime libraries.
CUDA_INC_PATH (defaults to C:/CUDA/include) contains the include files needed to compile CUDA programs.
CUDA_LIB_PATH (defaults to C:/CUDA/lib) contains the libraries needed for linking CUDA codes.
c)然後安裝SDK,裡面有很多樣本和模版,可以參照。安裝後進入/NVIDIA CUDA SDK/common檔案夾,將Release.sln(2005)或Release_vc90.sln(2008)編譯。新產生的.lib和.dll會在/NVIDIA CUDA SDK/common/lib裡。
3 使用
以Visual Studio 2008 TeamSuite為例:
1)文本編輯
由於CUDA使用的源檔案是.cu檔案,因此預設情況下VS不會識別它裡面的文法,會把它當成普通的txt檔案。
Tools->options,左側樹形圖展開Text Editor,在File Extension子項中:
Extension:cu Editor:Microsoft Visual C++
點擊Add,這樣cu檔案就會被當做cpp檔案顯示了。由於VS不會立即更新,需要重新啟動VS。
2)路徑包含
Tools->options,左側樹形圖展開Projects and Solutions,選VC++ Directories子項,
Platform: x64(預設是win32,但是我的是x64,所以需要選這個)
Show directories for:
Include files添加:
a) C:/CUDA/include
b) C:/Documents and Settings/All Users/Application Data/NVIDIA Corporation/NVIDIA CUDA SDK/common/inc
Library files添加:
a) C:/CUDA/lib
b) C:/Documents and Settings/All Users/Application Data/NVIDIA Corporation/NVIDIA CUDA SDK/common/lib
3) 編譯配置
Build->Configuration Manager,
Active Solution Configuration:Release(or Debug)
Active Solution Platform: x64(如果沒有需要new一個)
Project->Custom Build Rules,Find existing,找到/NVIDIA CUDA SDK/common/cuda.rules,開啟,並在左側勾選上,這樣就可以調用nvcc來編譯cu檔案了。
在工程上點右鍵->Properties,左側樹形控制項Configuration Properties->Linker->Input,在右側Additional Dependencies裡添加”cudart.dll cutil64.dll”(不包括引號),這樣基本上編譯就不會出問題了。
如果是driver API,需要添加”cuda.lib cudart.lib”,標頭檔#include <cuda.h>,#include <cuda_runtime.h>
4) 運行
運行時可能出現某個dll無法找到的情況,這是需要將dll複製到解決方案編譯下的exe所在檔案夾,或者乾脆複製到system32裡。