如何在 Visual Studio 2008 中启动新的 CUDA 项目?
这是一个非常基本的问题,但如何在 Visual Studio 2008 中启动一个新的 CUDA 项目?我找到了大量关于 CUDA 相关问题的文档,但没有找到关于如何开始一个新项目的文档.我正在使用 Windows 7 x64 Visual Studio 2008 C++.我真的很想找到某种非常非常基本的 Hello World 应用程序来编译和运行基本程序.
我试过你的步骤汤姆.我设置了一个控制台应用程序.然后我删除了它放入的默认 .cpp 并复制了模板项目中的三个文件,只是为了编译一些东西.当我编译它时,template_gold.cpp 抱怨没有包含 stdafx.h,所以我包含了它.现在构建失败了:
<前>1>------ 构建开始:项目:CUDASandbox,配置:调试 x64 ------1>编译...1>template_gold.cpp1>链接...1>LIBCMT.lib(crt0.obj): 错误 LNK2019: 函数 __tmainCRTStartup 中引用的未解析的外部符号 main1>D:StuffProgrammingVisual Studio 2008ProjectsCUDASandboxx64DebugCUDASandbox.exe:致命错误 LNK1120:1 个未解析的外部1>构建日志保存在file://d:StuffProgrammingVisual Studio 2008ProjectsCUDASandboxCUDASandboxx64DebugBuildLog.htm"1>CUDASandbox - 2 个错误,0 个警告========== 构建:0 成功,1 失败,0 最新,0 跳过 ========== 解决方案注意 随着 CUDA Toolkit 3.2 版的发布,NVIDIA 现在包含了 rules 文件与 Toolkit 而不是 SDK.因此,我将这个答案分为两部分,请使用适用于您的 Toolkit 版本的正确说明.
注意这些说明适用于 Visual Studio 2005 和 2008.对于 Visual Studio 2010,请参阅 这个答案.
<小时>CUDA TOOLKIT 3.2 及更高版本
我推荐使用NVIDIA提供的NvCudaRuntimeApi.rules
文件(或者NvCudaDriverApi.rules
如果使用驱动API),这是随工具包发布的,支持最新的编译器以友好的方式标记.我个人建议不要使用 VS 向导,但这只是因为我真的认为您不需要它.
规则文件(安装在 Program FilesMicrosoft Visual Studio 9.0VCVCProjectDefaults
目录中)教导"Visual Studio 如何编译项目中的任何 .cu 文件并将其链接到应用程序中.
- 使用标准 MS 向导创建一个新项目(例如一个空的控制台项目)
- 在 .c 或 .cpp 文件中实现您的主机(串行)代码
- 在 .cu 文件中实现包装器和内核
- 添加
NvCudaRuntimeApi.rules
(右击项目,自定义构建规则,勾选相关框),见注释1 - 添加 CUDA 运行时库(右键单击项目并选择 Properties,然后在 Linker -> General 中添加
$(CUDA_PATH)lib$(PlatformName)
到 Additional Library Directory 并在 Linker -> Input 中将cudart.lib
添加到 Additional Dependencies),见注释 [2] 和 [3] - 可选择将 CUDA 包含文件添加到搜索路径,如果您在 .cpp 文件(而不是 .cu 文件)中包含任何 CUDA 文件,则需要(右键单击项目并选择属性,然后在 C/C++ -> General 中将
$(CUDA_PATH)include
添加到 Additional Include Directories),参见注释 [3] - 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接中
其他一些提示:
- 更改代码生成以使用静态加载的 C 运行时以匹配 CUDA 运行时;右键单击项目并选择 Properties,然后在 C/C++ -> Code Generation 中将 Runtime Library 更改为/MT(或/MTd对于调试,在这种情况下,您需要在 Runtime API -> Host -> Runtime Library 中进行镜像,请参阅注释 [4]
- 使用 SDK 附带的 usertype.dat 文件启用语法高亮,请参阅
<sdk_install_dir>Cdocsyntax_highlightingvisual_studio_8
中的 readme.txt
我还建议使用以下注册表项启用 Intellisense 支持(将 9.0 替换为 VS2005 而不是 VS2008 的 8.0):
[HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0LanguagesLanguage ServicesC/C++]"NCB 默认 C/C++ 扩展"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.CL"
顺便说一句,如果可能的话,我会提倡避免cutil,而是自己进行检查.Cutil 不受 NVIDIA 支持,它只是用来尝试使 SDK 中的示例专注于实际程序和算法设计,并避免在每个示例中重复相同的事情(例如命令行解析).如果您自己编写,那么您将有更好的控制权,并且会知道发生了什么.例如,如果函数失败,cutilSafeCall
包装器会调用 exit()
- 真正的应用程序(而不是示例)可能应该更优雅地处理失败!
CUDA TOOLKIT 3.1 及更早版本
我会将 NVIDIA 提供的 Cuda.rules
文件与 SDK 一起使用,它与工具包一起发布,并以友好的方式支持最新的编译器标志.我个人建议不要使用 VS 向导,但这只是因为我真的认为您不需要它.
规则文件(位于 SDK 的 Ccommon 目录中)教导"Visual Studio 如何编译项目中的任何 .cu 文件并将其链接到应用程序中.
- 使用标准 MS 向导创建一个新项目(例如一个空的控制台项目)
- 在 .c 或 .cpp 文件中实现您的主机(串行)代码
- 在 .cu 文件中实现包装器和内核
- 添加
Cuda.rules
(右键单击项目,自定义构建规则,浏览规则文件并确保勾选) - 添加 CUDA 运行时库(右键单击项目并选择 Properties,然后在 Linker -> General 中添加
$(CUDA_LIB_PATH)
到 Additional Library Directory 并在 Linker -> Input 中添加cudart.lib
到 Additional Dependencies),见请注意下面的 [2] - 可选择将 CUDA 包含文件添加到搜索路径,如果您在 .cpp 文件(而不是 .cu 文件)中包含任何 CUDA 文件,则需要(右键单击项目并选择属性,然后在 C/C++ -> General 中将
$(CUDA_INC_PATH)
添加到 Additional Include Directories) - 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接中
其他一些提示:
- 将代码生成更改为使用静态加载的 C 运行时以匹配 CUDA 运行时,右键单击项目并选择属性,然后在 C/C++ -> 代码生成 将 Runtime Library 更改为/MT(或/MTd 用于调试,在这种情况下,您需要在 CUDA Build Rule -> Hybrid CUDA/C++ Options 中进行镜像), 见注释 [4]
- 使用 SDK 附带的 usertype.dat 文件启用语法高亮,请参阅
<sdk_install_dir>Cdocsyntax_highlightingvisual_studio_8
中的 readme.txt
我还建议使用以下注册表项启用 Intellisense 支持(将 9.0 替换为 VS2005 而不是 VS2008 的 8.0):
[HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0LanguagesLanguage ServicesC/C++]"NCB 默认 C/C++ 扩展"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.CL"
顺便说一句,如果可能的话,我会提倡避免cutil,而是自己进行检查.Cutil 不受 NVIDIA 支持,它只是用来尝试使 SDK 中的示例专注于实际程序和算法设计,并避免在每个示例中重复相同的事情(例如命令行解析).如果您自己编写,那么您将有更好的控制权,并且会知道发生了什么.例如,如果函数失败,cutilSafeCall
包装器会调用 exit()
- 真正的应用程序(而不是示例)可能应该更优雅地处理失败!
注意
- 您还可以使用特定于 Toolkit 版本的规则,例如
NvCudaRuntimeApi.v3.2.rules
.这意味着不是在 %CUDA_PATH% 中查找 CUDA Toolkit,而是在 %CUDA_PATH_V3_2% 中查找,这反过来意味着您可以在您的系统上安装多个版本的 CUDA Toolkit,并且不同的项目可以针对不同的版本.另见注释 [3]. - 规则文件无法修改 C/C++ 编译和链接器设置,因为它只是为 CUDA 代码添加编译设置.因此,您需要手动执行此步骤.请记住对所有配置都执行此操作!
- 如果您想在特定的 CUDA Toolkit 版本上保持稳定,那么您应该将 CUDA_PATH 替换为 CUDA_PATH_V3_2.另请参阅注释 1一>.
- C 运行时版本不匹配会导致各种问题;特别是如果您有任何关于 LIBCMT 的错误(例如
LNK4098: defaultlib 'LIBCMT' 与其他库的使用发生冲突
)或标准库函数的多重定义符号,那么这应该是您的第一个怀疑对象.莉>
This is an incredibly basic question, but how do I start a new CUDA project in Visual Studio 2008? I have found tons and tons of documentation about CUDA related matters, but nothing about how to start a new project. I am working with Windows 7 x64 Visual Studio 2008 C++. I would really like to find some sort of really really basic Hello World app to just get a basic program compiling and running.
Edit:
I tried your steps Tom. I setup a console app. I then deleted the default .cpp it drops in and copied over the three files from the template project just to have something to compile. When I compile that, template_gold.cpp complained about not having stdafx.h included, so i included that. Now the build fails with this:
1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------ 1>Compiling... 1>template_gold.cpp 1>Linking... 1>LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup 1>D:StuffProgrammingVisual Studio 2008ProjectsCUDASandboxx64DebugCUDASandbox.exe : fatal error LNK1120: 1 unresolved externals 1>Build log was saved at "file://d:StuffProgrammingVisual Studio 2008ProjectsCUDASandboxCUDASandboxx64DebugBuildLog.htm" 1>CUDASandbox - 2 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
解决方案
NOTE With the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the rules file with the Toolkit as opposed to the SDK. Therefore I've split this answer into two halves, use the correct instructions for your version of the Toolkit.
NOTE These instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see this answer.
CUDA TOOLKIT 3.2 and later
I recommend using the NvCudaRuntimeApi.rules
file (or NvCudaDriverApi.rules
if using the driver API) provided by NVIDIA, this is released with the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.
The rules file (installed into the Program FilesMicrosoft Visual Studio 9.0VCVCProjectDefaults
directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.
- Create a new project using the standard MS wizards (e.g. an empty console project)
- Implement your host (serial) code in .c or .cpp files
- Implement your wrappers and kernels in .cu files
- Add the
NvCudaRuntimeApi.rules
(right click on the project, Custom Build Rules, tick the relevant box), see note 1 - Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add
$(CUDA_PATH)lib$(PlatformName)
to the Additional Library Directories and in Linker -> Input addcudart.lib
to the Additional Dependencies), see notes [2] and [3] - Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add
$(CUDA_PATH)include
to the Additional Include Directories), see note [3] - Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
Some other tips:
- Change the code generation to use statically loaded C runtime to match the CUDA runtime; right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in Runtime API -> Host -> Runtime Library), see note [4]
- Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in
<sdk_install_dir>Cdocsyntax_highlightingvisual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):
[HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0LanguagesLanguage ServicesC/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall
wrapper calls exit()
if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!
CUDA TOOLKIT 3.1 and earlier
I would use the Cuda.rules
file provided by NVIDIA with the SDK, this is released alongside the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.
The rules file (in the Ccommon directory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.
- Create a new project using the standard MS wizards (e.g. an empty console project)
- Implement your host (serial) code in .c or .cpp files
- Implement your wrappers and kernels in .cu files
- Add the
Cuda.rules
(right click on the project, Custom Build Rules, browse for the rules file and ensure it is ticked) - Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add
$(CUDA_LIB_PATH)
to the Additional Library Directories and in Linker -> Input addcudart.lib
to the Additional Dependencies), see note [2] below - Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add
$(CUDA_INC_PATH)
to the Additional Include Directories) - Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
Some other tips:
- Change the code generation to use statically loaded C runtime to match the CUDA runtime, right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in CUDA Build Rule -> Hybrid CUDA/C++ Options), see note [4]
- Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in
<sdk_install_dir>Cdocsyntax_highlightingvisual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):
[HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0LanguagesLanguage ServicesC/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall
wrapper calls exit()
if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!
NOTE
- You can also use a Toolkit-version-specific rules fule e.g.
NvCudaRuntimeApi.v3.2.rules
. This means that instead of looking for the CUDA Toolkit in %CUDA_PATH% it will look in %CUDA_PATH_V3_2%, which in turn means that you can have multiple versions of the CUDA Toolkit installed on your system and different projects can target different versions. See also note [3]. - The rules file cannot modify the C/C++ compilation and linker settings, since it is simply adding compilation settings for the CUDA code. Therefore you need to do this step manually. Remember to do it for all configurations!
- If you want to stabilise on a specific CUDA Toolkit version then you should replace CUDA_PATH with CUDA_PATH_V3_2. See also note 1.
- Having mismatched version of the C runtime can cause a variety of problems; in particular if you have any errors regarding LIBCMT (e.g.
LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs
) or multiply defined symbols for standard library functions, then this should be your first suspect.
相关文章