本文将引导您完成以下工作:
1. 开发环境必须软件的下载和安装 2. 建立CUDA工程,编译第一个程序 3. 运行样例程序 4. 测试设备兼容性
5. 设置CUDA源文件的语法高亮和智能感知 6. 编写第一个自己的程序:A+B
CUDA软件包可以从NVIDIA官方网站上下载,我选择的是当前的最新版本3.2. 需要下载的东西有3个,分别是:
1. 开发驱动(cudatoolkit_3.2.7_win_32.msi)
2. 工具包(devdriver_3.2_winvista-win7_32_260.61_notebook.exe) 3. SDK样例代码(gpucomputingsdk_3.2_win_32.exe)
需要注意的问题是,这三个工具的版本号一定要匹配、还有就是要跟自己的操作系统和硬件匹配。比如笔记本电脑应选择带有notebook字样的下载,64位系统的要选择64位版本。 下载完成后进行安装,过程比较简单,一路下一步即可。安装完驱动后需要重启电脑。
IDE我选择的是Visual Studio 2008 Team System SP1,据说2005和2003都可以用。需要注意的是一定要安装Windows SDK,我的貌似是随着VS2008安装时自动安装的,但实验室的电脑就不是这样,具体表现为找不到windows.h头文件和kernel32.lib库文件。我估计这个是个别现象,这里不再详谈。
安装完这些软件后,我们可以开始创建一个CUDA的程序了。网上有很多手动配置工程的方法,非常繁琐。我们基本上都会推荐使用更加自动化的工具:CUDA VS Wizard,这个安装后会在VS中显示CUDA的项目模板。免去了繁琐的手工配置。
这个工具的项目主页是:http://sourceforge.net/projects/cudavswizard/ 建议大家选择新版本,我选的是2.1
安装好Wizard,剩下的工作就比较简单了。在VS中建立一个CUDA工程,先不要选择EmptyProject,建好后直接编译。顺利的话应该可以编译通过。运行时会提示找不到cutil32D.dll。如果你看到了找不到cutil32D.dll的错误,说明你之前的操作都是正确的。
如何正确运行程序呢?你需要将SDK安装目录下的\\C\\bin\\win32\\中Debug和Release两个文件夹加入系统Path环境变量中。什么,你不记得SDK装在哪儿了?不要紧,按Win+R组合键调出运行框,输入%NVSDKCOMPUTE_ROOT%,按回车,就会打开SDK的安装目录了~我的SDK装在C:\\GPUSDK目录,我在Path中加的就是下面的两个目录
C:\\GPUSDK\\C\\bin\\win32\\Release C:\\GPUSDK\\C\\bin\\win32\\Debug
加完环境变量需要注销一遍系统才能生效。注销完重新打开VS2008,重新运行,应该可以看到命令行的输出了。
下面我们来测试一下设备信息,编译运行%NVSDKCOMPUTE_ROOT%\\C\\src\\deviceQuery中的工程应该就可以看到了设备的信息了,我的输出是这样的: CUDA Device Query (Runtime API) version (CUDART static linking)
There is 1 device supporting CUDA
Device 0: \ CUDA Driver Version: 3.20 CUDA Runtime Version: 3.20
CUDA Capability Major/Minor version number: 1.1 Total amount of global memory: 253296640 bytes
Multiprocessors x Cores/MP = Cores: 2 (MP) x 8 (Cores/MP) = 16 (Cores) Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 16384 bytes Total number of registers available per block: 8192 Warp size: 32
Maximum number of threads per block: 512
Maximum sizes of each dimension of a block: 512 x 512 x 64 Maximum sizes of each dimension of a grid: 65535 x 65535 x 1 Maximum memory pitch: 2147483647 bytes Texture alignment: 256 bytes Clock rate: 0.80 GHz
Concurrent copy and execution: No Run time limit on kernels: Yes
Integrated: Yes
Support host page-locked memory mapping: Yes Compute mode: Default (multiple host threads can use this device simultaneously) Concurrent kernel execution: No Device has ECC support enabled: No Device is using TCC driver mode: No
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 3.20, CUDA Runtime Vers ion = 3.20, NumDevs = 1, Device = GeForce 9400M
PASSED
Press
-----------------------------------------------------------
如果你得到的是CUDA Driver and Runtime version may be mismatched.那就要检查一下驱动和工具包的版本是否匹配了。
看到了设备信息,CUDA的配置就算完成了。下面的工作是让开发工作更加方便和舒服。我们通过第三方软件Visual Assist X来完成语法高亮和智能感知。
首先下载Visual Assist X,我用的是最新版的10.6.1833,这个软件是收费的,不过有破解补丁。
装好并破解Visual Assist X,接下来的操作引用一段CUDA官方论坛上的一段文字 Add your extension to:
Tools | Options | Projects | VC++ Build | C/C++ File Extensions (VS.NET)
Tools | Options | Projects and Solutions | VC++ Project Settings | C/C++ File Extensions (VS2005, VS2008)
Exit the IDE and use regedit to clone the following entry to a like entry that ends in your extension:
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Languages\\File Extensions\\.cpp
If your extension denotes a header, use regedit to add the extension to ExtHeader in the following location. Remember to include the dot and terminating semicolon: HKEY_CURRENT_USER\\Software\\Whole Tomato\\Visual Assist X\\VANet8
If your extension denotes a source file that is not a header, add the extension to ExtSource in the following location.
HKEY_CURRENT_USER\\Software\\Whole Tomato\\Visual Assist X\\VANet8
Press Rebuild on the Performance tab of the Visual Assist X options dialog and restart your IDE.
Replace 8.0 with 9.0 if you use VS 2008. Replace 8.0 with 7.1 if you use VS.NET 2003. Replace 8.0 with 7.0 if you use VS.NET 2002. Replace VANet8 with VSNet9 if you use VS2008. Replace VANet8 with VSNet if you use VS.NET 2003. Replace VANet8 with VANet7.0 if you use VS.NET 2002.
估计大家看了一大片英文也有点晕了,几个要点提一下
Exit the IDE and use regedit to clone the following entry to a like entry that ends in your extension:这句话说的比较简略,具体的操作是建一个名为.cu的主键,然后复制.cpp主键下的内容到.cu主键。特别注意的是,修改注册表时必须要退出Visual Studio,否则修改会被自动改回去……
这段话里的your extension指的就是.cu和.cuh
还有后面那些替换看一下,根据你安装的版本,我是把所有的8都替换成9 了。
最后打开Visual Studio,点VAssistX-Visual Assist X Options
展开Projects,点C/C++ Directories,在Platform处选Custom,点下面那个\文件夹\图标新建一项,添加Toolkit的include路径,我的是C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v3.2\\include
相关推荐: