Freitag, 14. Oktober 2011

Visual Studio 2010 with Qt and CUDA and OpenGL

How to integrate CUDA in Visual Studio 2010 and how to write your Qt App with OpenGL using CUDA.

Source download
(tested on Win7 VS2010, Geforce 9800GT).
(for project/solution you still have to follow the howto :P)

Assuming you have at least:
  • Windows 7 32bit/64bit (XP?)
  • Visual Studio 2010 (not Express Edition)
  • Qt 4.7.4 (howto integrate in vs2010)
  • CUDA capable gpu (see nvidia)
  • CUDA Toolkit 4.0 (32bit or 64bit)
    - Developer Drivers
    - CUDA Toolkit
    - GPU Computing SDK
    - Parallel Nsight 2.0 (makes integration in vs2010)
Howto:

Setup cuda syntax highlighting:
  • copy usertype.dat to visual studio as following (Win7):
    C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\doc\syntax_highlighting\visual_studio_8\usertype.dat
    TO
    C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE
  • in your .cu files just add following headers for command recognition
    #include <cuda.h>
    #include <cuda_runtime.h>
    #include <device_launch_parameters.h>
Create Qt Project and connect .cu files with cuda.
  1. create a qt4 gui project (Qt4 Projects - Qt Application) in vs2010 (i will call it qt4_test2)
  2. follow the wizard and add OpenGL Library in the second step
    - (if you have 64bit consider the libgles32.lib linking bug, see notes in howto integrate in vs2010)
  3. right click on project in solution explorer and click "Build Customizations" ("Buildanpassungen")
    - select CUDA 4.0 ...
  4. go to Project Properties
    Add in Linker - Additional Dependencies
    - cudart.lib (cuda runtime library)
    Change Linker - System SubSystem to
    - Console (we want to see printf(), GUI will work though)
    In VC++ Directories
    - add $(CUDA_INC_PATH) to "Include Paths" (german "Includeverzeichnisse")
    Cutil Library
    If no cutil32.lib / cutil64.lib is there, just compile C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\cutil_vs2010.sln
    Then add to your own project properties - VC++ Directories:
    - add $(NVSDKCOMPUTE_ROOT)\C\common\inc (Include Directories)
    - add $(NVSDKCOMPUTE_ROOT)\C\common\lib\Win32\ (Library Directories) (or x64\)
    or just copy *.dll from ..lib\Win32\ to ..lib\bin\
  5. add a new qt class (not gui class) to be our opengl widget
    - Classname = AppGLWidget
    - BaseClass = QGLWidget
    - Constructor = QWidget *parent
  6. open the qt4_test2.ui (this is our QMainWindow) (will start the qt designer)
    - make the central widget as AppGLWidget (see screenshots)

  7. add to source a new cuda c/c++ file (.cu)
    - kernel.cu
    - right click on kernel.cu for properties and choose "CUDA C/C++" Compiler as type.
    (if there isnt anything you may have forgotten step 3)
  8. insert the following code to the certain files:
AppGLWidget.h:
extern "C" void launch_kernel();

AppGLWidget.cpp in constructor method:
launch_kernel();

kernel.cu:

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

__global__ void kernel()
{
 // ...
}

extern "C" void launch_kernel()
{
 printf("RUN CUDA KERNEL\n");
 kernel<<<1,1>>>();
}



Compile and go crazy.

Note: You will have to repeat project setting for Release configuration (assuming we've just been in Debug configuration).

Note: If you get asked at compiling that build must be finished ... you cant really get rid off it. it's an annoying bug of qt visual add-in. it's on the bug list (critical?). if you know more, just tell me.


For a more decent example in OpenGL using Pixelbuffer Objects for 2D procedural textures, see my next post: Qt4 Mandelbrot with CUDA.

5 Kommentare:

  1. thank you this works great

    AntwortenLöschen
  2. Thank you so much, this helped me a lot.

    AntwortenLöschen
  3. Thanks for comments, I am glad, that this helped you. I hope there will be time to update some stuff, maybe posting new tutorials. But life moves pretty fast, but some things also become more "easy" these days :)

    AntwortenLöschen
  4. Why don't need include cuda file in AppGLWidget.h?

    AntwortenLöschen
    Antworten
    1. Yeah, such kind of questions often arises, also because there are now two compilers involved. We have to understand that linking process.

      "launch_kernel" is implemented in kernel.cu (compiled by nvcc).
      nvcc creates a function address for "launch_kernel" when compiling kernel.cu.

      In AppGLWidget.h it is just declared, so the Linker has to look for the implementation. AppGLWidget is compiled by a c++ compiler.

      The Linker then takes all these generated object files, "merging" the function addresses and references into an executable.

      My code examples are not up to date according to the extern "C" code (I used outdated SDK examples as guidance where we still have examples using extern "C" code).

      Please see:
      http://stackoverflow.com/questions/2090974/how-to-separate-cuda-code-into-multiple-files

      It also answers the question, why you do not have to include cuda files.

      Löschen