Showing posts with label Visual Studio 2012. Show all posts
Showing posts with label Visual Studio 2012. Show all posts

Thursday, 6 June 2013

CUDA 5.0 grid size problem

Interesting thing. I was convinced that the maximum grid size of my machine (GeForce GTX 660M) can be [65535, 65535, 65535]. I checked the device parameters using the cudaGetDeviceProperties function and I couldn't believe the results. My GPU's limitation is actually _way_ higher than I expected and according to cudaGetDeviceProperties it's: [2147483647, 65535, 65535]. I looked my card up in the CUDA wiki and it turned out, that my device qery was not lying.

However, I had a serious problem testing the huge grid size I had unexpectedly discovered. My vector addition compiled successfully but returned errors and could not be profiled when the vector size was greater than 65535 (blocks) * 1024 (threads). I started two threads on NVIDIA dev forum and on stackoverflow and finally, this brilliant guy helped me.

What was the problem then? I didn't change the default Visual Studio setting which defines the arch and code parameters of nvcc. To change it to proper values for your card, please take a look at the CUDA wiki to locate your GPU and modify the following setting:

Now I can go beyond that old limit of 65535 and continue my research.

***I have updated my CUDA Hello World tutorial so you can update that setting as you create the project.***

Monday, 3 June 2013

CUDA 5.0 Hello World

Time for my first CUDA 'Hello World' program. Well, there won't be any GPU processing in it, but at least I'll prove that my environment is fully operational. Let's get to work!

I'll start by creating new Win32 Console Application (selecting 'Empty Project' option). Although it is a Visual C++ project, I will try to stick to C as I simply feel more confident using this language and don't want the OOP getting in my way.

First of all, since I use 64-bit windows, I changed my program to target the x64 platform.

Next, I selected CUDA 5.0 targets in my project's build customization options.

And I modify my project's Include and Library directories adding $(CUDA_INC_PATH) and $(CUDA_LIB_PATH) respectively (these settings can be located in project's properties menu -> Configuration Properties -> VC++ Directories).

Next thing is to change the nvcc parameters (by default they are set to compute_10,sm_10 which you probably don't want). To utilise full potential of your GPU, please adjust these parameters according to CUDA wiki. In my case it is compute_30,sm_30.

Last thing to do before I started writing code was to change CUDA/C++ target machine platform, which was still 32-bit.

And finally, my project nicely compiles (Intellisense complains about the angle brackets though, but I have yet to find an answer for this one).

#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <stdio.h>

#pragma comment(lib, "cudart") 

__global__ void mykernel(void)
{
}

int main(void)
{
 mykernel<<<1,1>>>();
 printf("Hello World!\n");
 getchar();
 
 return 0;
}

CUDA 5.0 and Visual Studio 2012 installation

I've been experimenting with OpenGL for a while and now I would like to take the next step and check out CUDA. People say amazing things about it and I figured it's about time for me to try it as well. However, the installation process is not as easy as I initially anticipated. After downloading CUDA Toolkit v5.0 and Visual Studio 2012 Express Edition, I found out that these two simply don't like each other and some extra effort had to be invested to get CUDA and VS up and running on my machine. Here's what I had to do:

  • I copied the content of this folder:
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\extras\visual_studio_integration\MSBuildExtensions
    to a safe location (let's say a folder on your desktop). There should be 4 files in total:
    • CUDA 5.0.props
    • CUDA 5.0.targets
    • CUDA 5.0.xml
    • Nvda.Build.CudaTasks.v5.0.dll
    Only first two will be modified but I copied them all to have everything in one place.
  • In my safe folder, I edited the CUDA 5.0.props file and added an extra CudaClVersion node. Was:
    <CudaClVersion Condition="'$(PlatformToolset)' == 'v90'">2008</CudaClVersion> <CudaClVersion Condition="'$(PlatformToolset)' == 'v100'">2010</CudaClVersion>
    Now:
    <CudaClVersion Condition="'$(PlatformToolset)' == 'v90'">2008</CudaClVersion> <CudaClVersion Condition="'$(PlatformToolset)' == 'v100'">2010</CudaClVersion> <CudaClVersion Condition="'$(PlatformToolset)' == 'v110'">2010</CudaClVersion>
  • Next, in the same folder, I modified the CUDA 5.0.targets file.
    • <CudaCleanDependsOn> node:
      Was:
      <CudaCleanDependsOn>
       AddCudaCompileMetadata;
       ValidateCudaBuild;
      </CudaCleanDependsOn>
      Now:
      <CudaCleanDependsOn>
       $(CudaCompileDependsOn);
       _SelectedFiles;
       CudaFilterSelectedFiles;
       AddCudaCompileMetadata;
       AddCudaLinkMetadata;
       AddCudaCompileDeps;
       AddCudaCompilePropsDeps;
       ValidateCudaBuild;
       ValidateCudaCodeGeneration;
       ComputeCudaCompileOutput;
       PrepareForCudaBuild
      </CudaCleanDependsOn>
    • <CudaCompile> node, GenerateRelocatableDeviceCode attribute:
      Was:
      GenerateRelocatableDeviceCode=""
      Now:
      GenerateRelocatableDeviceCode="%(CudaCompile.GenerateRelocatableDeviceCode)"
    • <CudaCompile> node, CodeGeneration attribute:
      Was:
      CodeGeneration=""
      Now:
      CodeGeneration="%(CudaCompile.CodeGenerationValues)"
    • <CudaCompile> node, CommandLineTemplate attribute:
      Was:
      CommandLineTemplate="&quot;$(CudaToolkitNvccPath)&quot; %(CudaCompile.ApiCommandLineTemplate) %(CudaCompile.CleanCommandLineTemplate)"
      Now:
      CommandLineTemplate="&quot;$(CudaToolkitNvccPath)&quot; %(CudaCompile.BuildCommandLineTemplate) %(CudaCompile.ApiCommandLineTemplate) %(CudaCompile.CleanCommandLineTemplate)"
  • Next, I copied all 4 files from my safe folder to the MSBuild folder, which I found here:
    C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations
  • Intellisense: to enable CUDA and GLSL code hightlighting I modified following Text Editor settings (can be found under Visual Studio's Tools -> Options menu):

  • I added .cu to the list of included extensions (Tools -> Options -> Projects and Solutions -> VC++ Project Settings)

  • Last step of my preparations was to locate and edit the host_config.h file. It can be found here:
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include
    Line 90 has to be changed from:
    #if _MSC_VER < 1400 || _MSC_VER > 1600
    to:
    #if _MSC_VER < 1400 || _MSC_VER > 1700
And that's it. CUDA 5.0 is integrated with Visual Studio 2012. I would like to thank Alan Tatourian for explaining how to do this, please find his blog here if you wish.