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 += cuda

Change CUDA_DIR to match your version

Add the .cu files with project files as

CUDA_SOURCES += cuda_helloworld.cu

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-gdb

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

Read More »Adding CUDA compiler to QtCreator