Sonntag, 16. Oktober 2011

Mandelbrot - Qt and CUDA with OpenGL Visual Studio 2010

Do you like Mandelbrot Set? Do you like Animations? Wanna have all together right now? Here it comes. (It has been supposed to be an exciting announcement, nevermind...)

A small demonstration example for CUDA with OpenGL Pixelbuffer put together in a Qt4 Application.

Source Project Solution VS 2010 "qt4_mandelbrot"
16.04.12: This example is "deprecated". I have reworked and bugfixed it for the Linux tutorial, see here. I will release the VS2010 edition soon, but you can copy the new source from the Linux tutorial.
19.10.11: Bugfix: cudaGLSetGLDevice(0) must be called only in initGL()

(Requirements see previous tutorial on qt and cuda in vs2010)

In my previous tutorial on Qt, CUDA, VS2010 I have shown you how to integrate all the libs together. You have been able to run an empty CUDA kernel in a Qt Application. It already has an OpenGL Widget (yet black empty screen). Now you want to create a 2D Image with Pixel-Fun-Stuff processed right on the GPU. OpenGL is just used as Presenter.

Before I have started I had following questions:
  1. How to connect .cu source with .cpp source ?
    (i.e. running kernel functions by extern classes)
  2. How to calculate and paint the result on gpu side ?
    (without involving cpu cycles/host such as memcopy)
1. Our cuda source is compiled by nvcc as you know. On the other hand we have classes in C++ such as our OpenGL Widget or our custom class SimplePBO which is our "Pixelbuffer-CUDA-Manager". Since SimplePBO accesses the kernel function in, we have to declare the accessing function. This is done in globals.h.
extern "C" void launch_kernel(uchar4*, unsigned int, unsigned int, int);
(You cannot include your .cu files, since they are not simply C files. The implementation on cuda side will be linked after compilation, so launch_kernel() will find its definition here.)

2. I assume you know how to create and bind textures in OpenGL. You may heard of pixel buffer objects too. It's well explained on this site. We will allocate our image space on gpu side creating a pixelbuffer object. CUDA will use this object for pixel manipulation (of course on gpu side as well). Our image then will be bound to an OpenGL Quad as texture. I want to give you an encouraging quote from one of my references ([1]):
As we will see, CUDA and OpenGL interoperability is very fast!
The reason (aside from the speed of CUDA) is that CUDA maps OpenGL buffer(s) into the CUDA memory space with a call to cudaGLMapBufferObject(). On a single GPU system, no data movement is required! Once provided with a pointer, CUDA programmers are then free to exploit their knowledge of CUDA to write fast and efficient kernels that operate on the mapped OpenGL buffers. ([1])
But there is a little restriction you should know: The Pixelbuffer Access is exclusive. Only one can access the pixel buffer at the same time, either CUDA or OpenGL.

I also recommend presentation [3] about CUDA and OpenGL, especially the part starting on page 22 (Steps To Draw An Image From Cuda). You will see how to work with the pixel buffer in OpenGL and Cuda.

In our Demonstration Project we use Qt (QGLBuffer) for dealing with the Pixelbuffer, so we dont have to care for OpenGL extensions (maybe glew for proc adresses and so on). We create the pixel buffer object as follows (simplePBO.cpp::createPBO()):

pixelBuffer = new QGLBuffer(QGLBuffer::PixelUnpackBuffer);


HANDLE_ERROR( cudaGLRegisterBufferObject( pixelBuffer->bufferId() ) );

In simplePBO.cpp::initCuda() the first cuda device is choosen ( cudaGLSetGLDevice(0) ). You will have to change on your own, if it doesnt fit. You can check your cuda devices with this little exe I wrote from [2]: CUDA Device Checker (output on console). A more advanced GUI based CUDA Checker you can obtain here named as CUDA-Z.

Ok, I do not want to explain every method here, just catch the code and explore the comments and consider the references [1; 3].

Last thing I want to mention is the image size. Due to the thread dimensions (16 per block) image size has to be a multiple of 16. So dont get confused about it. You also could set a fixed image size of 512 or 528 or something like that (see simplePBO.cpp::initCuda()).

[1] - CUDA, Supercomputing for the Masses, from
[2] - CUDA By Example, An Introduction To General-Purpose GPU Programming. Book source codes you can download here
[3] - What Every CUDA Programmer Should Know About OpenGL, PDF Version

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)

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
    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)
    - right click on 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:
extern "C" void launch_kernel();

AppGLWidget.cpp in constructor method:

#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");

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.

Qt 4.7.4 and Visual Studio 2010 Integration

How to integrate Qt 4.7.4 in Visual Studio 2010 (32bit/64bit).

// i rewrote this howto for my mind but most of it comes from here (thanks)

My environment:
Assuming you have at least:
  • Visual Studio 2010 (not the express edition :P)
    (i will not cover alternative ways (mingw g++) here, but if you have tuts on it, let me know.)
  • on windows xp it should work but tell me if not (with error description of course :))
  • ServicePack 1 for Visual Studio 2010
    if link doesnt work, google for visual studio 2010 service pack and download the web installer
    if i recall download size was beyond 500MB
  • maybe this patch is still needed for vs2010 (only if u go for x64)

How to:
  1. extract qt source to dir (i'll use C:\Qt\4.7.4)

  2. setup path in system environment variables
    - add new variable QTDIR with path to source (C:\Qt\4.7.4)
    - edit variable PATH and insert "%QTDIR%\bin;"
    - add variable QMAKESPEC="win32-msvc2010" (works also for 64bit)

  3. start command prompt from visual studio tools:
    (it's a normal cmd with further environment vars for compiling)
    - there are several command prompt files:
    - 32bit: take the normal command prompt without any additions
    (I have "Visual Studio-Eingabeaufforderung (2010)" here :) )

    - 64bit: compiling take "...x64 Win64"
    ("Eingabeaufforderung von Visual Studio x64 Win64 (2010)" in german version)

    // if you do not really need 64bit i will recommend 32 bit
    // if you want both you perhaps have to create another dir such as Qt\4.7.4_64bit\

  4. type in that command console
    $ cd %QTDIR%
    $ configure -shared -debug-and-release -opensource -opengl desktop -platform win32-msvc2010 -no-qt3support -no-webkit -mp -no-phonon -no-phonon-backend
    $ nmake

    // -mp = multiple processors for compiling with MSVC (-MP) // so you dont need "jom"
    // -platform win32-msvc2010 works also for 64bit if you took VS2010 x64 console
    // Qt Libs are compiled as 64bit then

  5. compiling is done, now go for visual studio add-in (i have v1.1.9)
    - install it :)

  6. run vs2010 and set qt dir in Qt -> Qt Options (C:\Qt\4.7.4)

NOTE: configure for static libs
if you want static libs running configure -static ... you will get a very big output (30gigs and more) because examples and demos will compiled as static (bigger exes). unfortunately you cannot disable compiling examples and demo by configure argument as you may know from linux (-nomake examples ...). you could disable compiling by modifying pro files respectively or you dont care cuz you have enough space. you also can clean the examples and demos afterwards by moving into these directories and run "nmake clean."

Note: 64bit Qt and OpenGL ("libgles32.lib" linker error)
If you have compiled on x64 you will get a linker error in Qt OpenGL based projects because of a "bug". Linker additional dependencies point to a "libgles32.lib". Well, we are not on an embedded system so remove this and insert opengl32.lib instead of it.
(Project Properties and Linker and then Input and Additional Dependencies if i translated it correct.)

Note: 64bit Qt Libs
You cannot create 32bit qt projects of course. Linking against x64 compiled Qt Libs will fail.

Note: Enabling 64bit compiling in VS2010
Project Properties and Configurations Manager (top right button). Create a new configuration and choose x64.

Note: Qt GUI with forms and Console for debug output (qDebug, printf, etc)
Project Properties and Linker and System and change SubSystem to Console (GUI will work though).