如何在Visual Studio 2008中启动一个新的CUDA项目? [英] How do I start a new CUDA project in Visual Studio 2008?

查看:162
本文介绍了如何在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.cpp
1>链接...
1> LIBCMT.lib(crt0.obj):error LNK2019 :未解析的外部符号main在函数__tmainCRTStartup中引用
1> D:\Stuff\Programming\Visual Studio 2008 \Projects\CUDASandbox\x64\Debug\CUDASandbox.exe:致命错误LNK1120: 1 unresolved externals
1>构建日志保存在file:// d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog .htm
1> CUDASandbox - 2错误,0警告
========== Build:0成功,1失败,0最新,0 skipped ==========


解决方案

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



strong>这些说明适用于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
  • 可选地将CUDA包含文件添加到搜索路径, .cpp文件中的任何CUDA文件(而不是.cu文件)(右键单击项目并选择属性,然后在 C / C ++ - >常规 code> $(CUDA_PATH)\include 附加包含目录),请参阅注释[3]



一些其他的提示:




  • 更改代码生成以使用静态加载的C运行时来匹配CUDA运行时;右键单击项目并选择属性,然后在 C / C ++ - >代码生成中将运行时库更改为/ MT在这种情况下,您需要在 Runtime API - >主机 - >运行时库中镜像),请参见注释[4]

  • 启用语法高亮使用SDK附带的usertype.dat文件,请参阅< sdk_install_dir> \C\doc\syntax_highlighting\visual_studio_8
  • 中的readme.txt


我还建议使用以下注册表项启用Intellisense支持(将VS2005的9.0替换为9.0,而不是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

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






CUDA TOOLKIT 3.1及更早版本



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



规则文件(在 C \\ SDK的常见目录)教导Visual Studio如何编译并将项目中的任何.cu文件链接到应用程序中。




  • 使用标准MS向导创建新项目(例如空控台项目)

  • 您的主机(序列)代码在.c或.cpp文件中

  • 在.cu文件中实现您的封装容器和内核

  • 添加 Cuda.rules (右键单击项目,自定义生成规则,浏览规则文件并确保已选中)

  • 添加CUDA运行时库(右键单击项目并选择属性,然后在链接器 - >常规添加 $(CUDA_LIB_PATH)添加到附加库目录链接器 - >输入中添加 cudart.lib em>附加依赖关系),请参见下面的注释[2]

  • 可选地将CUDA包含文件添加到搜索路径,如果您在.cpp文件中包含任何CUDA文件(与.cu文件相反)(右键单击项目并选择属性,然后在 C / C ++ - >常规添加 $(CUDA_INC_PATH )附加包含目录

  • 然后只需构建项目,.cu文件将被编译为.obj并添加



其他一些提示:




  • 更改代码生成以使用静态加载的C运行时来匹配CUDA运行时,右键单击项目并选择属性,然后在 C / C ++ - >代码生成运行时库更改为/ MT(或/ MTd用于调试,在这种情况下,您需要在 CUDA Build Rule - > Hybrid CUDA / C ++ Options ,请参阅注释[4]

  • 使用SDK中包含的usertype.dat文件启用语法高亮显示,请参阅< sdk_install_dir> \C中的readme.txt \doc\syntax_highlighting\visual_studio_8



我还建议启用Intellisense支持,注册表项(将vs2005而不是VS2008的9.0替换为9.0):

  [HKEY_CURRENT_USER \Software \Microsoft\VisualStudio \\ \\9.0 \Languages\Language\C/C ++] 
NCB Default C / C ++ Extensions=。cpp; .cxx; .c; .cc; .h; .hh; .hxx; .hpp; .inl; .tlh; .tli; .cu; .cuh; .cl

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

注意




  1. 您还可以使用Toolkit版本特定的规则 NvCudaRuntimeApi.v3.2.rules 。这意味着,不是在%CUDA_PATH%中查找CUDA工具包,它将在%CUDA_PATH_V3_2%中查找,这反过来意味着您可以在系统上安装多个版本的CUDA Toolkit,不同的项目可以定位不同的版本。另请参见注释[3]。

  2. 规则文件无法修改C / C ++编译和链接器设置,因为它只是添加CUDA代码的编译设置。因此,您需要手动执行此步骤。

  3. 如果要稳定特定的CUDA Toolkit版本,则应该使用CUDA_PATH_V3_2替换CUDA_PATH。另请参见 1 < a>。

  4. 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:\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. 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.

这篇关于如何在Visual Studio 2008中启动一个新的CUDA项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆