Qt Creator + CUDA + Linux

This post has a review in this other post
Qt Creator + CUDA + Linux – Review.

I’ve been googling for a while looking for info about Qt Creator and CUDA and there is not too much information about this topic, at least useful. The topic ‘QMake build script for CUDA‘ in the nvidia forum pointed me the way. Basically we need to tell our qt project file how to generate the Makefile to include the CUDA stuff. A basic .pro file to run the HelloQtCuda World program looks like this:

# Basic .pro configuration
SOURCES += main.cpp
# Cuda sources
CUDA_SOURCES += cuda_interface.cu
# Project dir and outputs
PROJECT_DIR = $$system(pwd)
OBJECTS_DIR = $$PROJECT_DIR/Obj
DESTDIR = ../bin

Great! We have added the files that we are going to use in our Qt project. Next the CUDA configuration

# Path to cuda SDK install
CUDA_SDK = /pathto/NVIDIA_GPU_Computing_SDK/C (note i'm using a linux machine)
# Path to cuda toolkit install
CUDA_DIR = /usr/local/cuda
# GPU architecture
CUDA_ARCH = sm_11 (yep, i've a really old cuda capable graphic card)
# nvcc flags (ptxas option verbose is always useful)
NVCCFLAGS = --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v
# include paths
INCLUDEPATH += $$CUDA_DIR/include
INCLUDEPATH += $$CUDA_SDK/common/inc/
INCLUDEPATH += $$CUDA_SDK/../shared/inc/
# lib dirs
QMAKE_LIBDIR += $$CUDA_DIR/lib64
QMAKE_LIBDIR += $$CUDA_SDK/lib
QMAKE_LIBDIR += $$CUDA_SDK/common/lib
# libs - note than i'm using a x_86_64 machine
LIBS += -lcudart -lcutil_x86_64
# join the includes in a line
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')

Finally, we can customize our Makefile output (more info about this topic)

# Prepare the extra compiler configuration (taken from the nvidia forum - i'm not an expert in this part)
cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o

cuda.commands = $$CUDA_DIR/bin/nvcc -m64 -g -G -arch=$$CUDA_ARCH -c $$NVCCFLAGS $$CUDA_INC $$LIBS  ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}

cuda.dependency_type = TYPE_C # there was a typo here. Thanks workmate!
cuda.depend_command = $$CUDA_DIR/bin/nvcc -g -G -M $$CUDA_INC $$NVCCFLAGS   ${QMAKE_FILE_NAME}
# Tell Qt that we want add more stuff to the Makefile
QMAKE_EXTRA_UNIX_COMPILERS += cuda

That’s all. Now you can add your only c++ code in the main.cpp (i.e. check parameters, load an image from file…):


#include <QtCore/QCoreApplication>

// This is the 'elder trick of the...' - Tell the compiler this function is defined in other place
extern "C"
void runCudaPart();

int main(int argc, char *argv[]) {

QCoreApplication a(argc, argv);

runCudaPart();

return a.exec();

}

And the cuda functions and kernels in the cuda_interface.cu:


// CUDA-C includes
#include <cuda.h>
#include <cutil_inline.h>

extern "C"
void runCudaPart();

// Main cuda function

void runCudaPart() {

// all your cuda code here *smile*

}

So, what do i get with all this work?. Of course, this is a matter of taste! I’ve been using Eclipse IDE for years, programming in C/C++ and also CUDA but it has been a long since i wanted give a chance to the Qt Creator IDE. First, it is a light and clean IDE. Second, i got a template for future projects :). I can also auto-complete code and the best and most important i can easily debug all the cpp and cu code! – but not the kernels. Anyway, debug cpp and cu files into the IDE is a plus compared to Eclipse, at least, i wasn’t able to do it with eclipse.

Best Regards!

Advertisements

16 thoughts on “Qt Creator + CUDA + Linux

    1. Hello,

      i’ve not tried it on windows and i’ve not a windows machine at the moment but it’s supposed to be the same. Just try to fix the directories with the CUDA includes and libs. Oh! and be aware about the x86_64 flags if you have a 32bits machine.

      Hope this help and best regards!

  1. Hi, thanks for the tutorial. Since you write that you are using qtcreator, how is it possible to add cu-files to the qtcreator project such that they are not added to SOURCES automatically but to CUDA_SOURCES.

    Best regards
    -Flo-

    1. Hello Flo, welcome.

      You can use a ‘old trick’ for that purpose. First, add the .cu files to the SOURCES:

      SOURCES += main.cpp \
      cuda_interface.cu

      At this point, the Qt Designer will add the cuda_interface.cu file to the project tree. Below those lines remove the .cu files from the SOURCES:

      SOURCES -= cuda_interface.cu

      The Qt Designer will not remove the file from the project tree and the SOURCES variable will hold only the C++ files. This trick also work with the HEADERS.

      Hope this help and best regards!

  2. Hello,
    Thanks for the post. I am trying to compile a project with Qt on Mac OS and I am getting the following error:

    ld: warning: ignoring file ../qt4_mandelbrot/obj/kernelPBO_cuda.o, file was built for i386 which is not the architecture being linked (x86_64)

    After googling, I found that setting CONFIG+=x86_64 in .pro file would fix this. But that is not working with me. How to set a particular architecture for Qt? Any ideas?

    I also get another error:
    ld: symbol(s) not found for architecture x86_64

    What does this mean?

    Thanks in advance.

    Bharath

    1. Hello Bharath,

      Did you prepare the project from the scratch? or started from another project? Some users have reported that this tutorial has worked on Mac OS, but with a few issues with the CUDA SDK. Did you compile and execute the SDK examples successfully?

      The ‘symbol(s) not found for architecture x86_64’ sounds like you are not linking against the library correctly. Maybe it’s missing?.

      Maybe, you could try this review: https://cudaspace.wordpress.com/2012/07/05/qt-creator-cuda-linux-review/

      Regards,
      Pablo.

      1. Hello Pablo,
        I am still getting the same error. But I have few doubts:

        When I am running qmake, following is the compiler output:

        18:40:32: Running steps for project test…
        18:40:32: Starting: “/usr/local/Trolltech/Qt-4.8.2/bin/qmake” /Users/apple/Documents/Qt_apps/test/test/test.pro -r -spec macx-g++ CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug
        18:40:33: The process “/usr/local/Trolltech/Qt-4.8.2/bin/qmake” exited normally

        Why is there the flag ‘CONFIG+=x86_64′? I am using Mac OS. So, I need to compile with a 32-bit architecture.

        When I build the project, this is the compiler output:

        g++ -headerpad_max_install_names -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -o ../bin/QtCuda ../test/Obj/cuda_interface_cuda.o ../test/Obj/main.o -F/usr/local/Trolltech/Qt-4.8.2/lib -L/usr/local/cuda/lib -L/Developer/Qt_CUDA_SDK/C/lib -L/Developer/Qt_CUDA_SDK/C/common/lib -L/usr/local/Trolltech/Qt-4.8.2/lib -lcudart -framework QtCore -L/usr/local/Trolltech/Qt-4.8.2/lib
        ld: warning: ignoring file ../test/Obj/cuda_interface_cuda.o, file was built for i386 which is not the architecture being linked (x86_64)
        Undefined symbols for architecture x86_64:
        “_runCudaPart”, referenced from:
        _main in main.o
        ld: symbol(s) not found for architecture x86_64
        collect2: ld returned 1 exit status
        make: *** [../bin/QtCuda] Error 1
        make: Leaving directory `/Users/apple/Documents/Qt_apps/test/test-build-desktop-Qt_4_8_2__Qt-4_8_2__Debug’
        18:42:38: The process “/usr/bin/make” exited with code 2.
        Error while building/deploying project test (target: Desktop)
        When executing step ‘Make’

        Why is it trying to compile with x86_64 flag? In the properties under ‘Build & Run’ options I chose ‘GCC (x86 32bit)’. So, even then why is it compiling with 64 bit flag? Am I missing any other flag anywhere?

        Thank you

        Bharath

      2. Try to remove line 22. Change the cutil lib with the 32bit name (note the comment). And remove the flag -m64 from the line 33 (or change it for -m32).

        Note the line numbers are related to the .pro file off the post:)

        Run qmake and let me know.

        Regards and good luck.

  3. Hey, this seems interesting, I’ll start out with a CUDA project this fall and looking for a usable IDE in linux, I’m not familiar with qt creator.

    Not sure if the part about QCoreAppliaction is “separate”, I was wondering if it’s necessary to add the qt includes and QCoreApplication etc. for a pure c++/CUDA project to be developed in qt-creator, it would be inconvenient if it ended up IDE dependent (at least without editing the project). I suppose that can be removed for such purposes?

    1. Hello Andreas,

      It’s not necessary add the qt includes. I added them because I’m using some Qt file handler and I’d like to make a UI in the future.

      Maybe you are familiar with Eclipse IDE which has support for Parallel Nsight in CUDA 5.0. The Nsight can be used for debugging the CUDA kernels, which is obviously a plus.

      Right now, I prefer Qt creator IDE because is lighter and cleaner and because I’ve not tried CUDA 5.0 and I’ve not seen the Nsight working. Maybe I’ll jump to Eclipse in the future.

      Regards,
      Pablo.

      1. Hello,

        I was googling an appropriate IDE for CUDA programming on linux, and found this nice post. I am wondering which IDE you are using now, Eclipse or Qt Creator?

        Best,
        Yaozong

  4. Hi Pablo,

    After switching to Nsight Eclipse Edition, do you still use Qt libraries for your UI needs?

    A feature that I like in Qt Creator is the .pro file, seems to give more control about the compilation. I’m new in the world of linux/cuda development, and I’m currently using a Jetson TK1 board.

    Regards,

    Edson

  5. Dear Edson, currently I’m not using Qt. I’m using a custom Makefile within the Nsight projects and adding different flags and variables to manage my applications. It’s more convenient when I want to test the program in different machines which have different GPUs.
    Best,
    Pablo.

    1. Dear Pablo, thanks for your reply! It’s very important to me receive best practices from more experienced developers. In this sense, if you’d have to develop a project with complex GUI (windows, menus, tree explorer, dialogs, etc…), what UI toolkit/framework would be your preferred choice to work with? I mean…GTK-based libraries..Qt-based (although you’ve just said above that doesn’t use Qt anymore, but maybe for GUI related needs can be a choice..)…

      Best,

      Edson

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s