Adding CUDA compiler to QtCreator

Project setup

I have CUDA 9.2 installed at /usr/local/cuda-9.2/.

Create a QtCreator console project

In the project file add:

CUDA_DIR = /usr/local/cuda-9.2
CUDA_LIBS = -lcudart -lcuda

INCLUDEPATH  += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib64
LIBS += $$CUDA_LIBS

#####################################################################
#                   CUDA compiler configuration                     #
#####################################################################

# GPU architecture
SYSTEM_TYPE = 64
CUDA_ARCH = sm_30
NVCCOPTIONS = -use_fast_math -O2

# Mandatory flags for stepping through the code
debug {
    NVCCOPTIONS += -g -G
}

# Prepare the extra compiler configuration (taken from the nvidia forum - i'm not an expert in this part)
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')

cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o
cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCCOPTIONS $$CUDA_INC $$CUDA_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME} 2>&1 | sed -r \"s/\\(([0-9]+)\\)/:\\1/g\" 1>&2
cuda.dependency_type = TYPE_C

QMAKE_EXTRA_COMPILERS += cudaCode language: Bash (bash)

Change CUDA_DIR to match your version

Add the .cu files with project files as

CUDA_SOURCES += cuda_helloworld.cuCode language: Bash (bash)

Debugger setup

Next step is to configure QtCreator to use cuda-gdb instead of gdb

Check where cuda-gdb is located

$ which cuda-gdb
/usr/local/cuda-9.2/bin/cuda-gdbCode language: Bash (bash)

Go to QtCreator > Option > Kit > Debuggers > Add

Give it a name and the cuda gdb path

Then go to QtCreator > Options > Kits > Kits > Select the Desktop kit you’d like to use. For me Desktop Qt 5.13.0 GCC 64bit.

In the Debugger combo box select the cuda debugger you’d like to use.

Now you can add break points to native code as well as CUDA kernels. If after all these steps you are not able to get cuda-gdb working, make sure your GPU isn’t too old. To be able to debug with X server on, you need a graphics card with compute capability of >= 3.2. I had for example a GTX 650 on one PC and I was not able to debug or run the application at all. See https://en.wikipedia.org/wiki/CUDA

helloworld sample project

cudaTest.pro

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

CUDA_SOURCES += cuda_helloworld.cu

CUDA_DIR = /usr/local/cuda-9.2
CUDA_LIBS = -lcudart -lcuda

INCLUDEPATH  += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib64
LIBS += $$CUDA_LIBS

#####################################################################
#                   CUDA compiler configuration                     #
#####################################################################

# GPU architecture
SYSTEM_TYPE = 64
CUDA_ARCH = sm_30
NVCCOPTIONS = -use_fast_math -O2

# Mandatory flags for stepping through the code
debug {
    NVCCOPTIONS += -g -G
}

# Prepare the extra compiler configuration (taken from the nvidia forum - i'm not an expert in this part)
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')

cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o
cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCCOPTIONS $$CUDA_INC $$CUDA_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME} 2>&1 | sed -r \"s/\\(([0-9]+)\\)/:\\1/g\" 1>&2
cuda.dependency_type = TYPE_C

QMAKE_EXTRA_COMPILERS += cudaCode language: Bash (bash)

main.cpp

#include <QCoreApplication>
#include <QDebug>

#include <cuda_runtime.h>
extern "C" cudaError_t cuda_helloWorld();

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

    cudaError_t cuerr = cuda_helloWorld();

    if (cuerr == cudaError::cudaSuccess)
    {
        qDebug() << "CUDA success: " << cudaGetErrorString(cuerr);
    }
    else
    {
        qDebug() << "CUDA Error: " << cudaGetErrorString(cuerr);
    }

    return a.exec();
}Code language: C++ (cpp)

cuda_helloworld.cu

extern "C" cudaError_t cuda_helloWorld()
{
    return cudaGetLastError();
}
Code language: C++ (cpp)

Leave a Reply

Your email address will not be published. Required fields are marked *