問題描述
直接問題:如何在 Visual Studio 2010 中創(chuàng)建一個簡單的 hello world CUDA 項目?
背景:我寫過 CUDA 內(nèi)核.我非常熟悉 Visual Studio 2005 中的 .vcproj 文件——手動調(diào)整了幾個.在 VS 2005 中,如果我想構(gòu)建一個 CUDA 內(nèi)核,我會添加一個自定義構(gòu)建規(guī)則,然后顯式定義 nvcc 調(diào)用來構(gòu)建文件.
我已遷移到 Win 7 和 VS 2010,因為我真的很想試用 nSight.我安裝了 nSight 1.5.但這就是我完全迷失的地方.如果我像以前一樣繼續(xù),nvcc 報告它只支持 msvc 8.0 &9.0.但該網(wǎng)站明確表示支持VS 2010.
我在其他地方讀到我需要安裝 VS 2008 (msvc 9.0)——我的話.現(xiàn)在就這樣做.
但我猜我的問題至少有一部分源于本土的定制構(gòu)建工具規(guī)范.幾個 網(wǎng)站談添加*.rules文件到構(gòu)建中,但我收集到這僅適用于 VS 2008.在構(gòu)建自定義"下,我看到 CUDA 3.1 和 3.2,但是當(dāng)我向項目添加內(nèi)核時,它們并沒有構(gòu)建.另一個網(wǎng)站宣稱關(guān)鍵是三個文件:Cuda.props Cuda.xml Cuda.targets,但它沒有說明如何或在哪里添加這些文件——或者更確切地說,我會賭我只是不明白網(wǎng)站上引用的注釋.
所以 有誰知道如何在 VS 2010 中創(chuàng)建一個簡單的項目來構(gòu)建 CUDA 內(nèi)核——使用 nSight 1.5 設(shè)置或隨附的 NvCudaRuntimeApi.v3.2.rules 文件CUDA 3.2 RC?
提前致謝!我會提供賞金,但我總共只有 65 分.
CUDA TOOLKIT 4.0 及更高版本
構(gòu)建自定義文件(安裝到 Program FilesMSBuildMicrosoft.Cppv4.0BuildCustomizations
目錄中)教"Visual Studio 如何編譯和鏈接您的任何 .cu 文件項目到您的應(yīng)用程序.如果您選擇跳過安裝自定義,或者如果您在 CUDA 之后安裝了 VS2010,您可以稍后按照 Program FilesNVIDIA GPU Computing ToolkitCUDAv4.0extrasvisual_studio_integration
.
- 使用標(biāo)準(zhǔn) MS 向?qū)?chuàng)建一個新項目(例如一個空的控制臺項目)
- 在 .c 或 .cpp 文件中實現(xiàn)您的主機(串行)代碼
- 添加 NVIDIA 構(gòu)建自定義(右鍵單擊項目,構(gòu)建自定義,勾選相關(guān)的 CUDA 框)
- 如果使用 CUDA 4.0,請參閱注釋 1
- 在 .cu 文件中實現(xiàn)包裝器和內(nèi)核
- 如果您在構(gòu)建自定義之前添加了
.cu
文件,那么您需要將.cu
文件的類型設(shè)置為CUDA C/C++
(右擊文件,Properties,設(shè)置Item Type) - 添加 CUDA 運行時庫(右鍵單擊項目并選擇 Properties,然后在 Linker -> Input 中添加
cudart.lib
到附加依賴項) - 然后只需構(gòu)建您的項目,.cu 文件將被編譯為 .obj 并自動添加到鏈接中
順便說一句,如果可能的話,我會提倡避免cutil,而是自己進(jìn)行檢查.Cutil 不受 NVIDIA 支持,它只是用來嘗試使 SDK 中的示例專注于實際程序和算法設(shè)計,并避免在每個示例中重復(fù)相同的事情(例如命令行解析).如果您自己編寫,那么您將有更好的控制權(quán),并且會知道發(fā)生了什么.例如,如果函數(shù)失敗,cutilSafeCall
包裝器會調(diào)用 exit()
- 真正的應(yīng)用程序(而不是示例)可能應(yīng)該更優(yōu)雅地處理失敗!
注意
- 僅對于 CUDA 4.0,您可能需要將此修復(fù)應(yīng)用到建立自定義.此補丁修復(fù)了以下消息:
<塊引用>
計算元素中AssemblyFile"屬性的值$(CudaBuildTasksPath)"的結(jié)果"無效
Direct Question: How do I create a simple hello world CUDA project within visual studio 2010?
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.
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.
I read somewhere else that I need to have VS 2008 (msvc 9.0) also installed -- my word. Doing so now.
But I'm guessing that at least part of my problems stem from the homegrown custom build tool specifications. Several websites talk 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 website proclaims 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.
So does 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?
Thanks in advance! I'd offer a bounty, but I only have 65 points total.
CUDA TOOLKIT 4.0 and later
The build customisations file (installed into the Program FilesMSBuildMicrosoft.Cppv4.0BuildCustomizations
directory) "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 FilesNVIDIA GPU Computing ToolkitCUDAv4.0extrasvisual_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
.cu
files before the build customisations, then you'll need to set the type of the.cu
files toCUDA 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 -> Input add
cudart.lib
to the Additional Dependencies) - Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
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
- For CUDA 4.0 only you may need to apply this fix to the build customisations. This patch fixes the following message:
The result "" of evaluating the value "$(CudaBuildTasksPath)" of the "AssemblyFile" attribute in the element is not valid
這篇關(guān)于如何在 Visual Studio 2010 中啟動 CUDA 應(yīng)用程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!