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

发布于 2024-08-17 16:36:29 字数 990 浏览 2 评论 0原文

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

编辑:

我尝试了你的步骤汤姆。我设置了一个控制台应用程序。然后,我删除了它放入的默认 .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 ==========

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:\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 ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似最初 2024-08-24 16:36:29

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

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


CUDA TOOLKIT 3.2 及更高版本

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

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

  • 使用标准 MS 向导创建一个新项目(例如空控制台项目)
  • 在 .c 或 .cpp 文件中实现主机(串行)代码
  • 在 .cu 文件中实现包装器和内核
  • 添加 NvCudaRuntimeApi.rules(右键单击项目,自定义构建规则,勾选相关框),请参阅注释1
  • 添加 CUDA 运行时库(右键单击项目并选择属性,然后在链接器 -> 常规$(CUDA_PATH)\lib\$(PlatformName) 添加到其他库目录链接器 - > 输入cudart.lib添加到附加依赖项),请参阅注释[2]和[3]
  • (可选)将CUDA包含文件添加到搜索路径,如果您在 .cpp 文件(而不是 .cu 文件)中包含任何 CUDA 文件,则需要(右键单击项目并选择属性,然后在C/C++ -> 中)一般$(CUDA_PATH)\include 添加到其他包含目录),请参阅注释 [3]
  • 然后只需构建您的项目,.cu 文件就会编译为 .obj 并自动添加到链接

其他一些提示:

  • 更改代码生成以使用静态加载的 C 运行时以匹配 CUDA 运行时;右键单击该项目并选择属性,然后选择C/C++ ->代码生成将运行时库更改为/MT(或用于调试的/MTd,在这种情况下,您需要将其镜像到运行时 API -> 主机 -> 运行时Library),请参阅注释 [4]
  • 使用 SDK 附带的 usertype.dat 文件启用语法突出显示,请参阅 \C\doc\syntax_highlighting\visual_studio_8 中的 readme.txt >

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

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.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"

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


CUDA TOOLKIT 3.1 及更早版本

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

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

  • 使用标准 MS 向导创建一个新项目(例如空控制台项目)
  • 在 .c 或 .cpp 文件中实现主机(串行)代码
  • 在 .cu 文件中实现包装器和内核
  • 添加 Cuda.rules(右键单击项目,自定义构建规则,浏览规则文件并确保选中它)
  • 添加 CUDA 运行时库(右键单击项目并选择属性 >,然后在链接器->常规中将$(CUDA_LIB_PATH)添加到附加库目录并在链接器->输入中cudart.lib 添加到其他依赖项),请参阅下面的注释 [2]
  • 可以选择将 CUDA 包含文件添加到搜索路径,如果包含任何文件,则需要这样做.cpp 文件中的 CUDA 文件(而不是 .cu 文件)(右键单击项目并选择“属性”,然后在“C/C++ -> 常规”中添加 < code>$(CUDA_INC_PATH) 到其他包含目录
  • 然后只需构建您的项目,.cu 文件将被编译为 .obj 并自动添加到链接

其他一些提示:

  • 更改代码生成使用静态加载的 C 运行时来匹配 CUDA 运行时,右键单击项目并选择“属性”,然后在“C/C++ ->”中代码生成将运行时库更改为/MT(或用于调试的/MTd,在这种情况下,您需要将其镜像到CUDA构建规则 -> 混合CUDA/C++中选项),请参阅注释 [4]
  • 使用 SDK 附带的 usertype.dat 文件启用语法突出显示,请参阅 \C\doc\syntax_highlighting\visual_studio_8 中的 readme.txt >

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

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.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"

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


注意

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

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 Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults 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 add cudart.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>\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):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.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 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 C\common 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 add cudart.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>\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):

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.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 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

  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.
子栖 2024-08-24 16:36:29

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

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

  1. 从 SDK 中复制“模板”示例,并重命名该目录 - 目录中唯一必要的内容是源代码和 VS 。 sln 和 .vcproj 文件
  2. 重命名 .sln 和 .vcproj 文件
  3. 在文本编辑器中打开 .sln 文件,然后重命名第 3 行中的 Project 变量和 .vcproj 文件名文件的
  4. 在文本编辑器中打开.vcproj文件,并重命名文件前几行的NameRootNamespace变量
  5. 用VS打开项目,然后打开属性页(在解决方案资源管理器窗格中右键单击项目名称,选择“属性”)
  6. 更改属性页中的输出文件名称(在配置属性 -> 链接器 -> 常规下) ...在更改文件名之前,我从“配置”下拉列表中选择“所有配置”,并从“平台”下拉列表中选择“x64”,因为我在 64 位系统上
  7. 更改属性页中的程序数据库文件名称(在“配置属性”->“属性页”下)链接器->调试)... 在更改文件名之前,我在下拉菜单中选择“调试”“x64”

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, correct way to get VS to produce a CUDA program from scratch.

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 is a quick way to get a CUDA-capable project working in VS with all the correct project/file names...

  1. Make a copy of the "template" example from the SDK, and rename the directory -- the only necessary contents 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 Project variable and .vcproj filename in the 3rd line of the file
  4. Open the .vcproj file in a text editor, and rename the Name and RootNamespace variables 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 File name 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 File name in the Property Pages (under Configuration Properties -> Linker -> Debugging) ... Before I change the filename, I select "Debug" and "x64" in the pull-downs.
多彩岁月 2024-08-24 16:36:29
  • 安装 CUDA VS 向导。它将设置 VS 并将 CUDA 项目添加到“新项目”菜单中。
  • 确保您安装了 x64 编译器(必须在 VS 安装期间检查)。
  • 检查您是否有 x64 库、包含、nvcc 目录并在搜索路径中。
  • 使用 CUDA 模板创建新项目。
  • 将项目类型更改为 x64,并将 CUDA 设置更改为 Native(如果您有支持 nv cuda 的卡)或仿真。
  • 该模板将创建自定义构建规则,使用 nvcc 编译 .cu 文件,并使用默认编译器编译其他文件。
  • 如果 vs 尝试使用 C/C++ 编译器编译 .cu 文件,请在解决方案资源管理器中单击该文件并禁用该文件的编译(文件图标上的红点)

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

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

  • 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)

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

[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文