Sunday, January 5, 2014

Building CUDA SDK Samples with Visual C++ 2010 Express

This post describes the steps needed to run samples (as 32 as well as 64 bit applications) from the Nvidia CUDA SDK, using Visual C++ 2010 Express.


Platform: Windows 7 64 bit.
CUDA: 5.5 (latest as of 04 January 2014).

For Steps 1 to 3, please refer to my previous OpenCV post,

http://faramblog.blogspot.in/2014/01/building-opencv-64-bit-applications.html


4

Download and install cuda_5.5.20_winvista_win7_win8_notebook_64.exe** (854 MB) from Nvidia website. This contains Cuda SDK including build tools, includes, pre-compiled libs (both x86 and x64) and samples.
The libs, includes, tools are installed into
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5
While the samples are installed into –
Source – Subfolders of C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.5
Folder for each sample contains VisualStudio solution files along with the source code.

Pre-compiled 64 bit - C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.5\Bin\win64\Release
Pre-compiled 32 bit - C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.5\Bin\win32\Release

The samples folder hierarchy is –

C:\ProgramData\NVIDIA Corporation\CUDA Samples\v5.5
               -Category folder
                              -Sample folder
                                             VisualStudio solution file
                                             Source code
               -Category folder
               …
               -Bin – contains compiled samples and some DLLs required by the samples
                              -win32
                                             -Debug
                                             -Release
                              -win64
                                             -Debug
                                             -Release
               -common – contains includes, libs, etc required by all samples

Using default VisualStudio solution files provided by Nvidia, the output file path is such that any samples built by user in Release mode will overwrite the pre-compiled samples provided by Nvidia. If this is not wanted, then either copy the samples folder hierarchy and work on the copy or change the output file path in IDE when building the sample.


** Download Cuda toolkit version suitable for your platform.


5

5.1

Choose a sample to build, e.g. oceanFFT by double clicking on its VS2010 solution file, e.g. oceanFFT_vs2010.sln. This will automatically open the sample in Visual C++ 2010 IDE.

5.2

To build a 64 bit binary, set the project Configuration to Debug and Platform to x64 as shown in figure below. If you want a 32 bit executable then leave the configuration to the default Win32.



5.3

Now set Windows 7 SDK as the toolset for the project. If this is not done then 64 bit libs won’t be found and you can get linker errors like
LINK : fatal error LNK1104: cannot open file 'kernel32.lib'

Right-click project -> Properties ->  Configuration Properties -> General -> Platform Toolset = Windows7.1SDK


Among other things, what it does is sets VC++ Directories -> Library Directories to first point to $(WindowsSdkDir)lib\x64 so that linker gets the proper libs.


5.4

References to the include and lib paths (project -> Properties -> VC++ Directories) have already been set by Nvidia provided solution file.

5.5

Required Cuda libs (Properties -> Linker -> Input -> Additional Dependencies) have already been specified by Nvidia provided solution file.

5.6

To get syntax highlighting for .cu files,
Copy file usertype.dat from
C:\Program Files\NVIDIA Corporation\Installer2\CUDASamples_5.5\doc\syntax_highlighting\visual_studio_7
To
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE
And
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE

From
C:\Program Files\NVIDIA Corporation\Installer2\CUDASamples_5.5\doc\syntax_highlighting\visual_studio_7
Double-click install_cuda_highlighting_vs7.reg to add required entries to registry.
In IDE, Tools -> Options -> Text Editor -> File Extensions -> Add = cu extension


Select the newly added extension and press Apply button.


Restart the IDE to get syntax highlighting for .cu files.




6

6.1


Now run (with debug) the project by pressing F5.  The sample should run in a new window.



6.2

The default Nvidia VS2010 solution file puts the compiled binary in a folder outside the sample’s project folder with path ../../bin/win64/$(Configuration)/ (win64 replaced by win32 for 32 bit output). The intermediate files, including object file, are placed in the usual Debug/Release sub folders of the project.
If you want to put the output binary in the usual Debug/Release sub folder of the project then change project Properties -> Configuration properties -> General -> Output Directory as required.




7 Build Bug

There is a bug when using Visual C++ 2010 Express (instead of full-fledged Visual Studio 2010). It is an inconvenience rather than a show stopper. When the project is run (by pressing F5 or Ctrl + F5), it builds the executable then runs it. But if you only want to build (Build/Rebuild/Clean) the project without running it, then the build fails with error
nvcc : fatal error : nvcc cannot find a supported version of Microsoft Visual Studio. Only the versions 2008, 2010, and 2012 are supported

I haven’t yet found a fix for this, but it seems that it might be due to the different build command lines that are run when a project is built directly and when it is built indirectly by running (F5) the project. The command lines for various scenarios are shown below –

x64, project built via project run (F5)
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" -gencode=arch=compute_30,code=\"sm_30,compute_30\" -gencode=arch=compute_35,code=\"sm_35,compute_35\" --use-local-env --cl-version 2010 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64"  -I../../common/inc -I./ -I../../common/inc -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile -cudart static -Xcompiler "/wd 4819"  -g   -DWIN32 -DWIN32 -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MTd  " -o x64/Debug/oceanFFT_kernel.cu.obj "D:\PROJECTS\VisualStudio2010\CudaSamples\samples\oceanFFT\oceanFFT_kernel.cu"

x64, project built directly using Build/Rebuild/Clean
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64"  -I../../common/inc -I./ -I../../common/inc -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"  -G   --keep-dir x64\Debug -maxrregcount=0  --machine 64 --compile  -Xcompiler "/wd 4819"  -g   -DWIN32 -DWIN32 -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MTd  " -o x64/Debug/oceanFFT_kernel.cu.obj "D:\PROJECTS\VisualStudio2010\CudaSamples\samples\oceanFFT\oceanFFT_kernel.cu" -clean

x86 project build via project run (F5)
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" -gencode=arch=compute_20,code=\"sm_20,compute_20\" -gencode=arch=compute_30,code=\"sm_30,compute_30\" -gencode=arch=compute_35,code=\"sm_35,compute_35\" --use-local-env --cl-version 2010 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin"  -I../../common/inc -I./ -I../../common/inc -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static -Xcompiler "/wd 4819"  -g   -DWIN32 -DWIN32 -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MTd  " -o Win32/Debug/oceanFFT_kernel.cu.obj "D:\PROJECTS\VisualStudio2010\CudaSamples\samples\oceanFFT\oceanFFT_kernel.cu"

x86, project built directly using Build/Rebuild/Clean
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\bin\nvcc.exe" -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin"  -I../../common/inc -I./ -I../../common/inc -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.5\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile  -Xcompiler "/wd 4819"  -g   -DWIN32 -DWIN32 -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MTd  " -o Win32/Debug/oceanFFT_kernel.cu.obj "D:\PROJECTS\VisualStudio2010\CudaSamples\samples\oceanFFT\oceanFFT_kernel.cu" –clean


References



See Also



No comments:

Post a Comment