How to Integrate CUDA with Visual C++

来源:百度文库 编辑:神马文学网 时间:2024/06/03 13:59:20
http://sarathc.wordpress.com/2008/09/26/how-to-integrate-cuda-with-visual-c/
How to Integrate CUDA with Visual C++
Posted on September 26, 2008 bySarath
If youwant to program CUDA, first you will have to install latest nVidia CUDA Driverfor your Graphics Hardware which supports desired CUDA version. Then you willhave to install the CUDA Toolkit which includes the CUDA Compiler, Include file,lib file and binary files to develop your CUDA application.
The binfolder under your CUDA installation location (most probably C:\CUDA), youcan see nvcc.exe which helps you to compile the CUDA program. If you give, nvccmy_cuda_filename.cu, the compiler will compile the source file and creates theexecutables (a.exe).
But inWindows world, most of the developers are much satisfied with the IDE VisualStudio. So may have to leave the world of command line compilation and sourceediting in favor of improving our productivity. If we can integrate the CUDAdevelopment to Visual Studio IDE, that’s pretty nice no? In one of my previous post,I said about enabling syntax highlighting for CUDAfiles under Visual Studio.
Now let’s check how we can support CUDA compilationunder Visual Studio. CUDA compiler has a dependency with C++ compiler. Itsupports either Visual C++ 7.1 or 8.0. Currently it doesn’t support Visual C++ 9(VS 2008).
Method1 – Install CUDA Build Rule for Visual Studio 2005
There’s a painless method by installing acustom build rule for Visual Studio 2005 developedby JaredHoberock. A cool installer isavailable to do the necessary settings. When you add the “.cu” files to yourapplication, it shows a dialog box to select the rule you installed. Press “OK”button.
Select CUDA Build Rule
You cancontrol the per-file (.cu file) configuration for your application byselecting the properties of the file from your solution explorer. See the figurebelow.
Detailed Build Configuration under Wizard
The installer also adds new Project Type to create yournew CUDA project which does the necessary settings for yourapplication.
Wizard to create new CUDA Project
Finally you can put the required CUDA libraries (e.gCUDA Runtime library - cudart.lib) inProject->Properties->Linker->Input->Additional Dependency.
Ithink the installer won’t work for Visual Studio 2003. I think you can manuallycopy the rule file from $:\Program Files\Microsoft Visual Studio8\VC\VCProjectDefaults\cuda.rules and specify this as the rule file forcompilation. Anyway try it yourself.
Method2 – Manually Configure by Custom Build Event
WithoutVisual Studio, we can use nvcc compiler for compiling the CUDA files withvarious command line parameters. So the same setup we can use with Visual Studioas well. One of the advantages is that, we can use various Visual StudioSpecific variables to identify various file locations.
Custom Build
1. Selectthe CUDA source file from the solution explorer and takeproperties.
2.  Select“Custom Build Setup” from the tree
3.  You canselect the active configuration from “Configuration” combobox.
4. Specifythe following options under “Command Line”
“$(CUDA_BIN_PATH)\nvcc.exe” -ccbin “$(VCInstallDir)bin”-c -D_DEBUG -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler/EHsc,/W3,/nologo,/Wp64,/Od,/Zi,/RTC1,/MTd -I”$(CUDA_INC_PATH)” -I./ -o$(ConfigurationName)\.obj .cu
Theabove configuration is to compile the source under debug configuration. For arelease configuration, you will have to remove the symbol definition, _DEBUG (-Dswitch is to define symbols). Also the above command line ask to link with thestatic-debug version of C-Runtime library by specifying /MTd option. In the caseof release build you will have to modify it to /MD or /MT. Please check thevariousC-Runtime versions from MSDN and apply accordingly. Thus asample command line for release version may look as follows.
“$(CUDA_BIN_PATH)\nvcc.exe” -ccbin “$(VCInstallDir)bin”-c -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler/EHsc,/W3,/nologo,/Wp64,/Od,/Zi,/RTC1,/MD -I”$(CUDA_INC_PATH)” -I./ -o$(ConfigurationName)\.obj .cu
If youneed to add Device Emulation option, you add -deviceemu switch in the commandline. Thus you the compiler will generate code for GPGPU Emulation library. Youcan understand more about each switches from MSDN itself. The information aboutthe other variables used has been specified at the end of thispost.
5. You canspecify your own description for the custom build event under “Description” textbox. (See the figure)
6. Finallyyou have to specify the output file name under “Outputs”
$(ConfigurationName)\.obj
7. If you  have included any CUDA files(probably will be having extension .cu or .cuh), you can specify it in the“Additional Dependencies option
In thecommand line string, we’ve used some visual studio specific build macros.You can see the entire list of Visual Studio BuildMacros in MSDN.
8.Finally build your project and now see the output of CUDA compilation in youroutput Window.
Addinga new build configuration
Sometimes you have to build your file with CUDAemulation for many debugging purpose.  Please check MSDN to know more about adding buildconfiguration.
I’ll explain how to call a CUDA Program from a C++ filein the next post. Thanks for your patience. I’m a newbie in CUDA. If you’vebetter ideas and suggestions, please share with me through your comments.