简体   繁体   English

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

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

This is an incredibly basic question, but how do I start a new CUDA project in Visual Studio 2008? 这是一个非常基本的问题,但如何在Visual Studio 2008中启动新的CUDA项目? I have found tons and tons of documentation about CUDA related matters, but nothing about how to start a new project. 我已经找到了大量关于CUDA相关问题的文档,但没有关于如何开始新项目的文档。 I am working with Windows 7 x64 Visual Studio 2008 C++. 我正在使用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. 我真的想找到一些真正非常基本的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. 然后我删除了默认的.cpp,它从模板项目中删除并复制了三个文件,只是为了编译。 When I compile that, template_gold.cpp complained about not having stdafx.h included, so i included that. 当我编译它时,template_gold.cpp抱怨没有包含stdafx.h,所以我把它包括在内。 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 ==========

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. 注意随着CUDA Toolkit 3.2版的发布,NVIDIA现在包含带有Toolkit的规则文件而不是SDK。 Therefore I've split this answer into two halves, use the correct instructions for your version of the Toolkit. 因此,我将这个答案分成两半,使用适用于您的Toolkit版本的正确说明。

NOTE These 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 TOOLKIT 3.2及更高版本

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. 我建议使用NVIDIA提供的NvCudaRuntimeApi.rules文件(如果使用驱动程序API,则使用NvCudaDriverApi.rules ),它随工具包一起发布,并以友好的方式支持最新的编译器标志。 Personally I would advise against using the VS wizard, but only because I really don't think you need it. 就个人而言,我建议不要使用VS向导,但这只是因为我真的不认为你需要它。

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. 规则文件(安装在Program Files\\Microsoft Visual Studio 9.0\\VC\\VCProjectDefaults目录中)“教”Visual Studio如何编译和链接项目中的任何.cu文件到您的应用程序中。

  • Create a new project using the standard MS wizards (eg an empty console project) 使用标准MS向导创建一个新项目(例如一个空的控制台项目)
  • Implement your host (serial) code in .c or .cpp files 在.c或.cpp文件中实现主机(串行)代码
  • Implement your wrappers and kernels in .cu files 在.cu文件中实现包装器和内核
  • Add the NvCudaRuntimeApi.rules (right click on the project, Custom Build Rules , tick the relevant box), see note 1 添加NvCudaRuntimeApi.rules (右键单击项目, 自定义构建规则 ,勾选相关框),参见注释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] 添加CUDA运行时库(右键单击项目并选择Properties ,然后选择Linker - > General add $(CUDA_PATH)\\lib\\$(PlatformName)附加库目录链接器 - >输入添加cudart.lib附加依赖项 ),见注释[2]和[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] (可选)将CUDA包含文件添加到搜索路径中,如果在.cpp文件中包含任何CUDA文件(而不是.cu文件),则需要这些文件(右键单击项目并选择“ 属性” ,然后在“ C / C ++”中 - >“常规”添加$(CUDA_PATH)\\include附加包含目录中 ,参见注释[3]
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically 然后只需构建您的项目,.cu文件将被编译为.obj并自动添加到链接中

Some other tips: 其他一些提示:

  • Change the code generation to use statically loaded C runtime to match the CUDA runtime; 更改代码生成以使用静态加载的C运行时来匹配CUDA运行时; 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] 右键单击项目并选择Properties ,然后在C / C ++中 - >代码生成运行时库更改为/ MT(或/ MTd进行调试,在这种情况下,您需要在运行时API中镜像它- >主机 - >运行时图书馆 ),见注[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 使用SDK附带的usertype.dat文件启用语法突出显示,请参阅<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支持(对于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"

Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. 顺便提一句,如果可能的话,我会提倡避免使用cutil ,而是自己检查。 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 (eg command line parsing). NVIDIA不支持Cutil,它仅用于尝试将SDK中的示例集中在实际程序和算法设计上,并避免在每个示例中重复相同的事情(例如命令行解析)。 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! 例如,如果函数失败, cutilSafeCall包装器调用exit() - 一个真正的应用程序(而不是样本)应该可以更优雅地处理失败!


CUDA TOOLKIT 3.1 and earlier CUDA TOOLKIT 3.1及更早版本

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. 我会使用NVIDIA提供的Cuda.rules文件和SDK,它与工具包一起发布,并以友好的方式支持最新的编译器标志。 Personally I would advise against using the VS wizard, but only because I really don't think you need it. 就个人而言,我建议不要使用VS向导,但这只是因为我真的不认为你需要它。

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. 规则文件(在SDK的C \\ common目录中)“教”Visual Studio如何编译和链接项目中的任何.cu文件到您的应用程序中。

  • Create a new project using the standard MS wizards (eg an empty console project) 使用标准MS向导创建一个新项目(例如一个空的控制台项目)
  • Implement your host (serial) code in .c or .cpp files 在.c或.cpp文件中实现主机(串行)代码
  • Implement your wrappers and kernels in .cu files 在.cu文件中实现包装器和内核
  • Add the Cuda.rules (right click on the project, Custom Build Rules , browse for the rules file and ensure it is ticked) 添加Cuda.rules (右键单击项目, 自定义构建规则 ,浏览规则文件并确保勾选)
  • 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 添加CUDA运行时库(右键单击项目并选择Properties ,然后选择Linker - > General add $(CUDA_LIB_PATH)Additional Library DirectoriesLinker - > Input add cudart.libAdditional Dependencies ),参见注释[ 2]下面
  • 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 ) (可选)将CUDA包含文件添加到搜索路径中,如果在.cpp文件中包含任何CUDA文件(而不是.cu文件),则需要这些文件(右键单击项目并选择“ 属性” ,然后在“ C / C ++”中 - >“常规”添加$(CUDA_INC_PATH)其他包含目录
  • Then just build your project and the .cu files will be compiled to .obj and added to the link automatically 然后只需构建您的项目,.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 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] 更改代码生成以使用静态加载的C运行时来匹配CUDA运行时,右键单击项目并选择Properties ,然后在C / C ++中 - >代码生成运行时库更改为/ MT(或/ MTd进行调试,其中你需要在CUDA构建规则 - >混合CUDA / C ++选项中镜像这个,参见注释[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 使用SDK附带的usertype.dat文件启用语法突出显示,请参阅<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支持(对于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"

Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. 顺便提一句,如果可能的话,我会提倡避免使用cutil ,而是自己检查。 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 (eg command line parsing). NVIDIA不支持Cutil,它仅用于尝试将SDK中的示例集中在实际程序和算法设计上,并避免在每个示例中重复相同的事情(例如命令行解析)。 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! 例如,如果函数失败, cutilSafeCall包装器调用exit() - 一个真正的应用程序(而不是样本)应该可以更优雅地处理失败!


NOTE 注意

  1. You can also use a Toolkit-version-specific rules fule eg NvCudaRuntimeApi.v3.2.rules . 您还可以使用Toolkit特定于版本的规则,例如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. 这意味着不是在%CUDA_PATH%中查找CUDA工具包,而是查看%CUDA_PATH_V3_2%,这反过来意味着您可以在系统上安装多个版本的CUDA Toolkit,并且不同的项目可以针对不同的版本。 See also note [3]. 另见注释[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. 规则文件不能修改C / C ++编译和链接器设置,因为它只是添加CUDA代码的编译设置。 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. 如果要稳定特定的CUDA Toolkit版本,则应将CUDA_PATH替换为CUDA_PATH_V3_2。 See also note 1 . 另见注1
  4. Having mismatched version of the C runtime can cause a variety of problems; 具有不匹配的C运行时版本可能会导致各种问题; in particular if you have any errors regarding LIBCMT (eg LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs ) or multiply defined symbols for standard library functions, then this should be your first suspect. 特别是如果你有任何关于LIBCMT的错误(例如LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs )或多个定义的符号用于标准库函数,那么这应该是你的第一个嫌疑人。

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. 对于那里的所有CUDA文档,我一直认为这是一个明显的遗漏...事实上,我很高兴我找到了这篇文章,因为在使用CUDA很长一段时间后,我仍然没有找到了一个正式的, 正确的方法让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. 当我需要启动一个新的CUDA程序时,我总是只复制并修改SDK目录中的“ 模板 ”示例。 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... 这可能不是你想要的,因为它没有重新开始,但它一个快速的方法来获得一个支持CUDA的项目在VS中使用所有正确的项目/文件名...

  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 从SDK中复制“ 模板 ”示例,并重命名目录 - 目录中唯一必需的内容是源代码和VS .sln和.vcproj文件
  2. Rename both .sln and .vcproj files 重命名.sln和.vcproj文件
  3. Open the .sln file in a text editor, and rename the Project variable and .vcproj filename in the 3rd line of the file 在文本编辑器中打开.sln文件,并在文件的第3行重命名Project变量和.vcproj文件名
  4. Open the .vcproj file in a text editor, and rename the Name and RootNamespace variables in the first few lines of the file 在文本编辑器中打开.vcproj文件,并在文件的前几行重命名NameRootNamespace变量
  5. Open the project with VS, and open the Property Pages (right click on the project name in the solution explorer pane, select "Properties") 使用VS打开项目,然后打开属性页(右键单击解决方案资源管理器窗格中的项目名称,选择“属性”)
  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 更改属性页中的输出文件名(在配置属性 - >链接器 - >常规下)...在更改文件名之前,我从配置下拉选择“所有配置” ,从平台拉出“x64” -因为我在64位系统上
  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. 更改属性页中的程序数据库文件名(在配置属性 - >链接器 - >调试下)...在更改文件名之前,我在下拉列表中选择“Debug”“x64”
  • Install CUDA VS wizard. 安装CUDA VS向导。 It will setup VS and add CUDA Project to the "new project" menu. 它将设置VS并将CUDA项目添加到“新项目”菜单中。
  • Make sure that you have x64 compiler installed (must be checked during VS install). 确保安装了x64编译器(必须在VS安装期间进行检查)。
  • Check if you have x64 libs, includes, nvcc dir and in the search path. 检查您是否有x64库,包括,nvcc目录和搜索路径。
  • Create new project using CUDA template. 使用CUDA模板创建新项目。
  • Change project type to x64 and CUDA setting to Native (if you have nv cuda-enabled card) or emulation otherwise. 将项目类型更改为x64,将CUDA设置更改为Native(如果您具有nv cuda-enabled卡)或其他模拟。
  • The template will create custom build rules that compile .cu files with nvcc and other files with default compiler. 该模板将创建自定义构建规则,使用nvcc和其他具有默认编译器的文件来编译.cu文件。
  • 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) if,vs正在尝试使用C / C ++编译器编译.cu文件,在解决方案资源管理器中单击该文件并禁用该文件的编译(文件图标上的红点)

Additional info about installing CUDA wizard on VS2008 can be found here and 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. 如果您不想使用向导,则必须手动设置CUDA lib / include / nvcc路径,并为每个新的CUDA程序添加自定义构建规则。 For additional info how to do it take a look at Tom's Answer. 有关其他信息,请参阅Tom's Answer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio 2010中启动CUDA应用程序? - How do I start a CUDA app in Visual Studio 2010? 如何将C ++ Visual Studio 2008项目降级到2005年 - How Do I Downgrade a C++ Visual Studio 2008 Project to 2005 如何在命令行上构建Visual Studio 2008 C ++项目的所有配置? - How do I build all configurations of a Visual Studio 2008 C++ project on the command line? 如何在Visual Studio 2017中启动cocos2d-x-3.17的新Android游戏项目? - How can i start new android game project of cocos2d-x-3.17 in visual studio 2017? 在Visual Studio 2008中调试时如何管道输出? - How do I pipe output when debugging in Visual Studio 2008? 如何在visual studio 2008中为我的应用程序设置图标? - How do I set the icon for my application in visual studio 2008? 如何在Visual Studio 2008中折叠选定的代码块? - How do I collapse selected chunks of code in Visual Studio 2008? 如何在Visual Studio 2008(C ++项目)中重建项目后运行构建规则的项目 - How do I just run a projects post build rules with out rebuilding in Visual Studio 2008 (C++ project) CUDA链接错误Visual Studio 2008 - CUDA Linking Error Visual Studio 2008 当我安装了多个Visual Studio版本时,如何使用Visual Studio 2008构建增强功能? - How do I build boost with Visual Studio 2008 when I have multiple Visual Studio versions installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM