Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong><em>NOTE</em></strong> With the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the <em>rules</em> 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.</p> <p><strong><em>NOTE</em></strong> These instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see <a href="https://stackoverflow.com/questions/3778799/how-do-i-start-a-cuda-app-in-visual-studio-2010/7285235#7285235">this answer</a>.</p> <hr> <p><strong><em>CUDA TOOLKIT 3.2 and later</em></strong></p> <p>I recommend using the <code>NvCudaRuntimeApi.rules</code> file (or <code>NvCudaDriverApi.rules</code> 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.</p> <p>The rules file (installed into the <code>Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults</code> directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. </p> <ul> <li>Create a new project using the standard MS wizards (e.g. an empty console project)</li> <li>Implement your host (serial) code in .c or .cpp files</li> <li>Implement your wrappers and kernels in .cu files</li> <li>Add the <code>NvCudaRuntimeApi.rules</code> (right click on the project, <em>Custom Build Rules</em>, tick the relevant box), see note <a href="https://stackoverflow.com/questions/3778799/how-do-i-start-a-cuda-app-in-visual-studio-2010/7285235#7285235">1</a></li> <li>Add the CUDA runtime library (right click on the project and choose <em>Properties</em>, then in <em>Linker -> General</em> add <code>$(CUDA_PATH)\lib\$(PlatformName)</code> to the <em>Additional Library Directories</em> and in <em>Linker -> Input</em> add <code>cudart.lib</code> to the <em>Additional Dependencies</em>), see notes [2] and [3]</li> <li>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 <em>Properties</em>, then in <em>C/C++ -> General</em> add <code>$(CUDA_PATH)\include</code> to the <em>Additional Include Directories</em>), see note [3]</li> <li>Then just build your project and the .cu files will be compiled to .obj and added to the link automatically</li> </ul> <p>Some other tips:</p> <ul> <li>Change the code generation to use statically loaded C runtime to match the CUDA runtime; right click on the project and choose <em>Properties</em>, then in <em>C/C++ -> Code Generation</em> change the <em>Runtime Library</em> to /MT (or /MTd for debug, in which case you will need to mirror this in <em>Runtime API -> Host -> Runtime Library</em>), see note [4]</li> <li>Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <code>&lt;sdk_install_dir&gt;\C\doc\syntax_highlighting\visual_studio_8</code></li> </ul> <p>I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):</p> <pre><code>[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" </code></pre> <p>Incidentally I would advocate <strong>avoiding cutil</strong> 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 <code>cutilSafeCall</code> wrapper calls <code>exit()</code> if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!</p> <hr> <p><strong><em>CUDA TOOLKIT 3.1 and earlier</em></strong></p> <p>I would use the <code>Cuda.rules</code> 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.</p> <p>The rules file (in the <em>C\common</em> directory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application. </p> <ul> <li>Create a new project using the standard MS wizards (e.g. an empty console project)</li> <li>Implement your host (serial) code in .c or .cpp files</li> <li>Implement your wrappers and kernels in .cu files</li> <li>Add the <code>Cuda.rules</code> (right click on the project, <em>Custom Build Rules</em>, browse for the rules file and ensure it is ticked)</li> <li>Add the CUDA runtime library (right click on the project and choose <em>Properties</em>, then in <em>Linker -> General</em> add <code>$(CUDA_LIB_PATH)</code> to the <em>Additional Library Directories</em> and in <em>Linker -> Input</em> add <code>cudart.lib</code> to the <em>Additional Dependencies</em>), see note [2] below</li> <li>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 <em>Properties</em>, then in <em>C/C++ -> General</em> add <code>$(CUDA_INC_PATH)</code> to the <em>Additional Include Directories</em>)</li> <li>Then just build your project and the .cu files will be compiled to .obj and added to the link automatically</li> </ul> <p>Some other tips:</p> <ul> <li>Change the code generation to use statically loaded C runtime to match the CUDA runtime, right click on the project and choose <em>Properties</em>, then in <em>C/C++ -> Code Generation</em> change the <em>Runtime Library</em> to /MT (or /MTd for debug, in which case you will need to mirror this in <em>CUDA Build Rule -> Hybrid CUDA/C++ Options</em>), see note [4]</li> <li>Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <code>&lt;sdk_install_dir&gt;\C\doc\syntax_highlighting\visual_studio_8</code></li> </ul> <p>I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):</p> <pre><code>[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" </code></pre> <p>Incidentally I would advocate <strong>avoiding cutil</strong> 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 <code>cutilSafeCall</code> wrapper calls <code>exit()</code> if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!</p> <hr> <p><strong><em>NOTE</em></strong></p> <ol> <li>You can also use a Toolkit-version-specific rules fule e.g. <code>NvCudaRuntimeApi.v3.2.rules</code>. 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].</li> <li>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!</li> <li>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 <a href="https://stackoverflow.com/questions/3778799/how-do-i-start-a-cuda-app-in-visual-studio-2010/7285235#7285235">1</a>.</li> <li>Having mismatched version of the C runtime can cause a variety of problems; in particular if you have any errors regarding LIBCMT (e.g. <code>LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs</code>) or multiply defined symbols for standard library functions, then this should be your first suspect.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload