C++ 如何在 Visual Studio 2008 中启动新的 CUDA 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2046228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:56:35  来源:igfitidea点击:

How do I start a new CUDA project in Visual Studio 2008?

c++visual-studiocuda

提问by Mr Bell

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.

这是一个非常基本的问题,但如何在 Visual Studio 2008 中启动一个新的 CUDA 项目?我找到了大量关于 CUDA 相关问题的文档,但没有找到关于如何开始一个新项目的文档。我正在使用 Windows 7 x64 Visual Studio 2008 C++。我真的很想找到某种非常非常基本的 Hello World 应用程序来编译和运行一个基本的程序。

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:

我试过你的步骤汤姆。我设置了一个控制台应用程序。然后我删除了它放入的默认 .cpp 并复制了模板项目中的三个文件,只是为了编译一些东西。当我编译它时,template_gold.cpp 抱怨没有包含 stdafx.h,所以我包含了它。现在构建失败了:

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:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm"
1>CUDASandbox - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

回答by Tom

NOTEWith the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the rulesfile 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.

注意随着 CUDA 工具包 3.2 版的发布,NVIDIA 现在将规则文件与工具包一起包含在工具包中,而不是 SDK。因此,我将这个答案分成两半,请使用适用于您的 Toolkit 版本的正确说明。

NOTEThese instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see this answer.

注意这些说明对 Visual Studio 2005 和 2008 有效。对于 Visual Studio 2010,请参阅此答案



CUDA TOOLKIT 3.2 and later

CUDA 工具包 3.2 及更高版本

I recommend using the NvCudaRuntimeApi.rulesfile (or NvCudaDriverApi.rulesif 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.

我建议使用NVIDIA 提供的NvCudaRuntimeApi.rules文件(或者NvCudaDriverApi.rules如果使用驱动程序 API),它是随工具包一起发布的,并以友好的方式支持最新的编译器标志。我个人建议不要使用 VS 向导,但这只是因为我真的认为您不需要它。

The rules file (installed into the Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaultsdirectory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.

规则文件(安装到Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults目录中)“教”Visual Studio 如何编译项目中的任何 .cu 文件并将其链接到应用程序中。

  • 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 -> Generaladd $(CUDA_PATH)\lib\$(PlatformName)to the Additional Library Directoriesand in Linker -> Inputadd cudart.libto 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++ -> Generaladd $(CUDA_PATH)\includeto 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
  • 使用标准 MS 向导创建一个新项目(例如一个空的控制台项目)
  • 在 .c 或 .cpp 文件中实现您的主机(串行)代码
  • 在 .cu 文件中实现您的包装器和内核
  • 添加NvCudaRuntimeApi.rules(右键单击项目,自定义构建规则,勾选相关框),见注释1
  • 添加 CUDA 运行时库(右键单击项目并选择Properties,然后在Linker -> General添加$(CUDA_PATH)\lib\$(PlatformName)Additional Library Directories和在Linker -> Input添加cudart.libAdditional Dependencies),参见注释 [2] 和 [3]
  • 可以选择将 CUDA 包含文件添加到搜索路径,如果您在 .cpp 文件(而不是 .cu 文件)中包含任何 CUDA 文件,则需要(右键单击项目并选择Properties,然后在C/C++ -> Generaladd$(CUDA_PATH)\include附加包含目录),请参阅注释 [3]
  • 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接中

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 Generationchange the Runtime Libraryto /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>\C\doc\syntax_highlighting\visual_studio_8
  • 更改代码生成以使用静态加载的 C 运行时以匹配 CUDA 运行时;右键单击项目并选择Properties,然后在C/C++ -> Code Generation中将Runtime Library更改为 /MT(或 /MTd 进行调试,在这种情况下,您需要在Runtime API -> Host -> Runtime 中镜像它),见注释 [4]
  • 使用 SDK 附带的 usertype.dat 文件启用语法突出显示,请参阅中的 readme.txt <sdk_install_dir>\C\doc\syntax_highlighting\visual_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):

我还建议使用以下注册表项启用 Intellisense 支持(将 9.0 替换为 VS2005 而不是 VS2008 的 8.0):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio.0\Languages\Language Services\C/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 cutilif 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 cutilSafeCallwrapper calls exit()if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!

顺便说一句,如果可能的话,我会提倡避免使用 cutil,而是自己进行检查。NVIDIA 不支持Cutil,它只是用来尽量使SDK 中的示例专注于实际程序和算法设计,避免在每个示例中重复相同的事情(例如命令行解析)。如果你自己写,那么你将有更好的控制,并且会知道发生了什么。例如,如果函数失败,cutilSafeCall包装器会调用exit()——一个真正的应用程序(而不是一个示例)可能应该更优雅地处理失败!



CUDA TOOLKIT 3.1 and earlier

CUDA 工具包 3.1 及更早版本

I would use the Cuda.rulesfile 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.

我会将Cuda.rulesNVIDIA 提供的文件与 SDK 一起使用,它与工具包一起发布,并以友好的方式支持最新的编译器标志。我个人建议不要使用 VS 向导,但这只是因为我真的认为您不需要它。

The rules file (in the C\commondirectory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.

规则文件(位于SDK的C\common目录中)“教导”Visual Studio 如何编译项目中的任何 .cu 文件并将其链接到应用程序中。

  • 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 -> Generaladd $(CUDA_LIB_PATH)to the Additional Library Directoriesand in Linker -> Inputadd cudart.libto 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++ -> Generaladd $(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
  • 使用标准 MS 向导创建一个新项目(例如一个空的控制台项目)
  • 在 .c 或 .cpp 文件中实现您的主机(串行)代码
  • 在 .cu 文件中实现您的包装器和内核
  • 添加Cuda.rules(右键单击项目,自定义构建规则,浏览规则文件并确保已勾选)
  • 添加 CUDA 运行时库(右键单击项目并选择Properties,然后在Linker -> General添加$(CUDA_LIB_PATH)Additional Library Directories并在Linker -> Input添加cudart.libAdditional Dependencies),请参阅下面的注释 [2]
  • 可以选择将 CUDA 包含文件添加到搜索路径,如果您在 .cpp 文件(而不是 .cu 文件)中包含任何 CUDA 文件,则需要(右键单击项目并选择Properties,然后在C/C++ -> Generaladd$(CUDA_INC_PATH)附加包含目录
  • 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接中

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 Generationchange the Runtime Libraryto /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>\C\doc\syntax_highlighting\visual_studio_8
  • 将代码生成更改为使用静态加载的 C 运行时以匹配 CUDA 运行时,右键单击项目并选择Properties,然后在C/C++ -> Code Generation中将Runtime Library更改为 /MT(或 /MTd 进行调试,其中如果您需要在CUDA Build Rule -> Hybrid CUDA/C++ Options 中对此进行镜像,请参阅注释 [4]
  • 使用 SDK 附带的 usertype.dat 文件启用语法突出显示,请参阅中的 readme.txt <sdk_install_dir>\C\doc\syntax_highlighting\visual_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):

我还建议使用以下注册表项启用 Intellisense 支持(将 9.0 替换为 VS2005 而不是 VS2008 的 8.0):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio.0\Languages\Language Services\C/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 cutilif 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 cutilSafeCallwrapper calls exit()if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!

顺便说一句,如果可能的话,我会提倡避免使用 cutil,而是自己进行检查。NVIDIA 不支持Cutil,它只是用来尽量使SDK 中的示例专注于实际程序和算法设计,避免在每个示例中重复相同的事情(例如命令行解析)。如果你自己写,那么你将有更好的控制,并且会知道发生了什么。例如,如果函数失败,cutilSafeCall包装器会调用exit()——一个真正的应用程序(而不是一个示例)可能应该更优雅地处理失败!



NOTE

笔记

  1. 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].
  2. 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!
  3. 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.
  4. 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.
  1. 您还可以使用特定于 Toolkit 版本的规则,例如NvCudaRuntimeApi.v3.2.rules. 这意味着不是在 %CUDA_PATH% 中查找 CUDA Toolkit,而是在 %CUDA_PATH_V3_2% 中查找,这反过来意味着您可以在您的系统上安装多个版本的 CUDA Toolkit,并且不同的项目可以针对不同的版本。另见注释 [3]。
  2. 规则文件不能修改 C/C++ 编译和链接器设置,因为它只是为 CUDA 代码添加编译设置。因此,您需要手动执行此步骤。请记住对所有配置都执行此操作!
  3. 如果您想在特定的 CUDA Toolkit 版本上保持稳定,那么您应该将 CUDA_PATH 替换为 CUDA_PATH_V3_2。另见注1
  4. C 运行时版本不匹配会导致各种问题;特别是如果您对 LIBCMT(例如LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs)或标准库函数的多重定义符号有任何错误,那么这应该是您的第一个怀疑对象。

回答by tpm1510

What a great question!! For all the CUDA documentation out there, this is something that I've always thought was an obvious omission... In fact, I'm really glad I found this post, because after using CUDA for quite a while, I still hadn't found an official, correctway to get VS to produce a CUDA program from scratch.

多么好的问题!!对于那里的所有 CUDA 文档,我一直认为这是一个明显的遗漏......事实上,我很高兴我找到了这篇文章,因为在使用 CUDA 一段时间后,我仍然没有' t 找到了让 VS 从头开始​​生成 CUDA 程序的官方正确方法。

When I've needed to start a new CUDA program, I've always just copied and modified the "template" example from the SDK directory. This may not be exactly what you're looking for, because it doesn't start fresh, but it isa quick way to get a CUDA-capable project working in VS with all the correct project/file names...

当我需要启动一个新的 CUDA 程序时,我总是从 SDK 目录中复制和修改“模板”示例。这可能不是您正在寻找的,因为它不是全新的,但它一种快速的方法,可以让具有 CUDA 功能的项目在 VS 中使用所有正确的项目/文件名工作......

  1. Make a copy of the "template" example from the SDK, and rename the directory -- the only necessarycontents in the directory are source code and VS .sln and .vcproj files
  2. Rename both .sln and .vcproj files
  3. Open the .sln file in a text editor, and rename the Projectvariable and .vcprojfilename in the 3rd line of the file
  4. Open the .vcproj file in a text editor, and rename the Nameand RootNamespacevariables in the first few lines of the file
  5. Open the project with VS, and open the Property Pages (right click on the project name in the solution explorer pane, select "Properties")
  6. Change the Output Filename in the Property Pages (under Configuration Properties -> Linker -> General) ... Before I change the filename, I select "All Configurations"from the Configuration pull-down and "x64"from the Platform pull-down, since I'm on a 64-bit system
  7. Change the Program Database Filename in the Property Pages (under Configuration Properties -> Linker -> Debugging) ... Before I change the filename, I select "Debug"and "x64"in the pull-downs.
  1. 从 SDK 中复制“模板”示例,并重命名目录——目录中唯一需要的内容是源代码和 VS .sln 和 .vcproj 文件
  2. 重命名 .sln 和 .vcproj 文件
  3. 在文本编辑器中打开 .sln 文件,并在文件的第 3 行重命名Project变量和.vcproj文件名
  4. 在文本编辑器中打开 .vcproj 文件,并在文件的前几行中重命名NameRootNamespace变量
  5. 用VS打开项目,然后打开属性页(在解决方案资源管理器面板中右键单击项目名称,选择“属性”)
  6. 更改属性页中的输出文件名(在配置属性 -> 链接器 -> 常规下)...在更改文件名之前,我从配置下拉菜单中选择“所有配置”,从平台下拉菜单中选择“x64”-下来,因为我在 64 位系统上
  7. 更改属性页中的程序数据库文件名(在配置属性 -> 链接器 -> 调试下)...在更改文件名之前,我在下拉列表中选择“调试”“x64”

回答by beermann

  • Install CUDA VS wizard. It will setup VS and add CUDA Project to the "new project" menu.
  • Make sure that you have x64 compiler installed (must be checked during VS install).
  • Check if you have x64 libs, includes, nvcc dir and in the search path.
  • Create new project using CUDA template.
  • Change project type to x64 and CUDA setting to Native (if you have nv cuda-enabled card) or emulation otherwise.
  • The template will create custom build rules that compile .cu files with nvcc and other files with default compiler.
  • if, vs is trying to compile .cu files with C/C++ compiler, click on that file in solution explorer and disable compilation for that files (red dot on file's icon)
  • 安装 CUDA VS 向导。它将设置 VS 并将 CUDA 项目添加到“新项目”菜单中。
  • 确保安装了 x64 编译器(必须在 VS 安装期间检查)。
  • 检查您是否有 x64 库、包含、nvcc 目录和搜索路径。
  • 使用 CUDA 模板创建新项目。
  • 将项目类型更改为 x64,将 CUDA 设置更改为 Native(如果您有启用 nv cuda 的卡)或仿真。
  • 该模板将创建自定义构建规则,使用 nvcc 编译 .cu 文件,并使用默认编译器编译其他文件。
  • 如果 vs 尝试使用 C/C++ 编译器编译 .cu 文件,请在解决方案资源管理器中单击该文件并禁用该文件的编译(文件图标上的红点)

Additional info about installing CUDA wizard on VS2008 can be found hereand here

可以在此处此处找到有关在 VS2008 上安装 CUDA 向导的其他信息

[edit]
If you don't want to use wizard you have to setup CUDA lib/include/nvcc paths manually and add custom build rules to each new CUDA program. For additional info how to do it take a look at Tom's Answer.

[编辑]
如果您不想使用向导,您必须手动设置 CUDA lib/include/nvcc 路径并将自定义构建规则添加到每个新的 CUDA 程序。有关如何操作的其他信息,请查看 Tom's Answer。

回答by Salman Ul Haq