C++ 如何在 Visual Studio 2010 中启动 CUDA 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3778799/
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-28 13:41:53  来源:igfitidea点击:

How do I start a CUDA app in Visual Studio 2010?

c++visual-studio-2010cudaprojects-and-solutionsnsight

提问by M. Tibbits

Direct Question:How do I create a simple hello world CUDA project within visual studio 2010?

直接问题:如何在 Visual Studio 2010 中创建一个简单的 hello world CUDA 项目?

Background:I've written CUDA kernels. I'm intimately familiar with the .vcproj files from Visual Studio 2005 -- tweaked several by hand. In VS 2005, if I want to build a CUDA kernel, I add a custom build rule and then explicitly define the nvcc call to build the files.

背景:我写过 CUDA 内核。我非常熟悉 Visual Studio 2005 中的 .vcproj 文件 - 手动调整了几个。在 VS 2005 中,如果我想构建一个 CUDA 内核,我会添加一个自定义构建规则,然后显式定义 nvcc 调用来构建文件。

I have migrated to Win 7, and VS 2010 because I really want to try out nSight. I have nSight 1.5 installed. But this is where I'm totally lost. If I proceed as before, nvcc reports that it only supports msvc 8.0 & 9.0. But the website clearly states that it supports VS 2010.

我已迁移到 Win 7 和 VS 2010,因为我真的很想尝试使用 nSight。我安装了 nSight 1.5。但这是我完全迷失的地方。如果我像以前一样继续,nvcc 报告它只支持 msvc 8.0 和 9.0。但是该网站明确声明它支持VS 2010。

I read somewhere else that I need to have VS 2008 (msvc 9.0) also installed -- my word. Doing so now.

我在其他地方读到我需要安装 VS 2008(msvc 9.0)——我的话。现在这样做。

But I'm guessing that at least part of my problems stem from the homegrown custom build tool specifications. Severalwebsitestalk about adding a *.rules file to the build, but I've gathered that this is only applicable to VS 2008. Under "Build Customizations" I see CUDA 3.1 and 3.2, but when I add kernels to the project they aren't built. Another websiteproclaims that the key is three files: Cuda.props Cuda.xml Cuda.targets, but it doesn't say how or where to add these files -- or rather I'll gamble that I just don't understand the notes referenced in the website.

但我猜我的问题至少有一部分源于本土的定制构建工具规范。 有几个网站谈到将 *.rules 文件添加到构建中,但我发现这仅适用于 VS 2008。在“构建自定义”下,我看到 CUDA 3.1 和 3.2,但是当我向项目添加内核时,它们不是没有建。 另一个网站宣称关键是三个文件:Cuda.props Cuda.xml Cuda.targets,但它没有说明如何或在哪里添加这些文件 - 或者说我赌我只是不理解注释网站上有参考。

Sodoes anyone know how to create a simple project in VS 2010 which builds a CUDA kernel -- using either the nSight 1.5 setup or the NvCudaRuntimeApi.v3.2.rules file which ships with the CUDA 3.2 RC?

那么有谁知道如何在 VS 2010 中创建一个简单的项目来构建一个 CUDA 内核——使用 nSight 1.5 设置或 CUDA 3.2 RC 附带的 NvCudaRuntimeApi.v3.2.rules 文件?

Thanks in advance! I'd offer a bounty, but I only have 65 points total.

提前致谢!我会提供赏金,但我总共只有 65 分。

回答by Tom

CUDA TOOLKIT 4.0 and later

CUDA 工具包 4.0 及更高版本

The build customisations file (installed into the Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizationsdirectory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. If you chose to skip installing the customisations, or if you installed VS2010 after CUDA, you can add them later by following the instructions in Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\extras\visual_studio_integration.

构建自定义文件(安装到Program Files\MSBuild\Microsoft.Cpp\v4.0\BuildCustomizations目录中)“教”Visual Studio 如何编译项目中的任何 .cu 文件并将其链接到应用程序中。如果您选择跳过安装自定义,或者如果您在 CUDA 之后安装了 VS2010,您可以按照Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\extras\visual_studio_integration.

  • 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
  • Add the NVIDIA build customisation (right click on the project, Build customizations, tick the relevant CUDA box)
  • See note 1 if using CUDA 4.0
  • Implement your wrappers and kernels in .cu files
  • If you added .cufiles before the build customisations, then you'll need to set the type of the .cufiles to CUDA C/C++(right-click on the file, Properties, set Item Type)
  • Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> Inputadd cudart.libto the Additional Dependencies)
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
  • 使用标准 MS 向导创建一个新项目(例如一个空的控制台项目)
  • 在 .c 或 .cpp 文件中实现您的主机(串行)代码
  • 添加 NVIDIA 构建定制(右键单击项目,构建定制,勾选相关的 CUDA 框)
  • 如果使用 CUDA 4.0,请参阅注释 1
  • 在 .cu 文件中实现您的包装器和内核
  • 如果您.cu在构建自定义之前添加了文件,那么您需要将.cu文件类型设置为CUDA C/C++(右键单击文件,Properties,设置Item Type
  • 添加 CUDA 运行时库(右键单击项目并选择Properties,然后在Linker -> Input添加cudart.libAdditional Dependencies
  • 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接中

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. For CUDA 4.0 only you may need to apply this fixto the build customisations. This patch fixes the following message:
  1. 仅对于 CUDA 4.0,您可能需要将此修复程序应用于构建自定义。此补丁修复了以下消息:

The result "" of evaluating the value "$(CudaBuildTasksPath)" of the "AssemblyFile" attribute in the element is not valid

元素中“AssemblyFile”属性的值“$(CudaBuildTasksPath)”的求值结果“”无效

回答by Ade Miller

This answer applies to CUDA 3.2, from 4.0 onwards CUDA supports the VC 10 compiler directly, see other answers for more information

此答案适用于 CUDA 3.2,从 4.0 开始,CUDA 直接支持 VC 10 编译器,有关更多信息,请参阅其他答案

You need either VS 2008 or the 6.1 Windows SDK installed. That's because NSight 1.5 RC or the CUDA 3.2 SDK use the VC 9 compiler under the hood. I've got this working successfully with 2008 installed and am told it should work with the SDK but haven't tried.

您需要安装 VS 2008 或 6.1 Windows SDK。这是因为 NSight 1.5 RC 或 CUDA 3.2 SDK 在底层使用 VC 9 编译器。我已经在安装 2008 时成功地运行了它,并被告知它应该可以与 SDK 一起使用,但还没有尝试过。

With NSight 1.5 and/or the CUDA 3.2 SDK you shouldn't need to muck with any custom build rules. I've been there and it's painful. With the latest builds all that goes away:

使用 NSight 1.5 和/或 CUDA 3.2 SDK,您无需考虑任何自定义构建规则。我去过那里,很痛苦。使用最新版本,所有这些都消失了:

  • Create your VC++ project.
  • Add a .CU file to it.
  • Select the project file in the Solution Explorer.
  • Open Project | Build Customizations...
  • Check the "CUDA 3.2 (.targets, .props)" customization.
  • Select a .CU file in your project and hit Alt-Enter to show it's properties.
  • Make sure it's Item Type is set to "CUDA C/C++"
  • 创建您的 VC++ 项目。
  • 向其中添加 .CU 文件。
  • 在解决方案资源管理器中选择项目文件。
  • 打开项目 | 构建自定义...
  • 检查“CUDA 3.2 (.targets, .props)”自定义。
  • 在您的项目中选择一个 .CU 文件,然后按 Alt-Enter 以显示其属性。
  • 确保它的项目类型设置为“CUDA C/C++”

It should just build. Let me know if this helps and if you run into problems as this is from memory.

它应该只是构建。让我知道这是否有帮助,以及您是否遇到问题,因为这是来自记忆的。

The good news it getting CUDA working with VS 2010 just got much easier.

好消息是让 CUDA 与 VS 2010 一起工作变得更容易了。

Ade

阿德

BTW: I'll update my blog post.

BTW:我会更新我的博客文章。

回答by biaspoint

Another Good tutorial here:

另一个很好的教程在这里:

http://www.stevenmarkford.com/installing-nvidia-cuda-with-visual-studio-2010/

http://www.stevenmarkford.com/installing-nvidia-cuda-with-visual-studio-2010/

if you get an error about '<' note this step (from a previous answer):

如果您收到有关“<”的错误,请注意此步骤(来自上一个答案):

If you added .cu files before the build customisations, then you'll need to set the type of the .cu files to CUDA C/C++ (right-click on the file, Properties, set Item Type)

如果您在构建自定义之前添加了 .cu 文件,那么您需要将 .cu 文件的类型设置为 CUDA C/C++(右键单击文件,属性,设置项目类型)

But if you follow their steps, it should work!

但是,如果您按照他们的步骤进行操作,它应该会起作用!