1

I am trying to get CUDA code to work with Qt on Ubuntu 12.04

My cuda_interface.cu

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


extern "C"
void runCudaPart();

// Main cuda function

void runCudaPart() {

// all your cuda code here *smile*

}

My main.cpp #include

extern "C"
void runCudaPart();

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

My .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-17T10:50:37
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = QtCuda
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp


# This makes the .cu files appear in your project
OTHER_FILES +=  ./cuda_interface.cu

# CUDA settings <-- may change depending on your system
CUDA_SOURCES += ./cuda_interface.cu
CUDA_SDK = "/usr/local/cuda-5.0/"   # Path to cuda SDK install
CUDA_DIR = "/usr/local/cuda-5.0/"            # Path to cuda toolkit install
SYSTEM_NAME = unix         # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 32            # '32' or '64', depending on your system
CUDA_ARCH = sm_21           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS = --use_fast_math


# include paths
INCLUDEPATH += $$CUDA_DIR/include

# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib/

CUDA_OBJECTS_DIR = ./


# The following library conflicts with something in Cuda
#QMAKE_LFLAGS_RELEASE = /NODEFAULTLIB:msvcrt.lib
#QMAKE_LFLAGS_DEBUG   = /NODEFAULTLIB:msvcrtd.lib

# Add the necessary libraries
CUDA_LIBS = libcuda libcudart
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
NVCC_LIBS = $$join(CUDA_LIBS,' -l','-l', '')
LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')

# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
    # Debug mode
    cuda_d.input = CUDA_SOURCES
    cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda_d.commands = $$CUDA_DIR/bin/nvcc -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
    # Release mode
    cuda.input = CUDA_SOURCES
    cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}

I am trying to adopt this .pro file from Compiling Cuda code in Qt Creator on Windows Which is a similar question but seeks a solution for windows.

At the moment the compiler shows the following errors :

make: Entering directory `/home/swaroop/Work/ai-junkies/cuda/uc_davis/opencv2.x/QtCuda'
g++ -Wl,-O1 -o QtCuda cuda_interface_cuda.o main.o    -L/usr/local/cuda-5.0//lib/ -L/usr/lib/i386-linux-gnu libcuda.so libcudart.so -lQtCore -lpthread 
g++: error: libcuda.so: No such file or directory
g++: error: libcudart.so: No such file or directory
make: *** [QtCuda] Error 1

Please help me fix these problems.

Community
  • 1
  • 1
mkuse
  • 2,250
  • 4
  • 32
  • 61

3 Answers3

9

I can finally run CUDA code with Qt Creator on Ubuntu 12.04. I assume that you can run cuda independently on your system. Here is an excellent quide to setup cuda on ubuntu 12.04 http://sn0v.wordpress.com/2012/05/11/installing-cuda-on-ubuntu-12-04/

I started off an a Qt console application from Qt-Creator. Here is my main.cpp

#include <QtCore/QCoreApplication>

extern "C"
void runCudaPart();

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

Here is cuda_interface.cu

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

#include <cuda_runtime.h>

    #include <stdio.h>

    extern "C"
//Adds two arrays
    void runCudaPart();


    __global__ void addAry( int * ary1, int * ary2 )
    {
    int indx = threadIdx.x;
    ary1[ indx ] += ary2[ indx ];
}


// Main cuda function

void runCudaPart() {

    int ary1[32];
    int ary2[32];
    int res[32];

    for( int i=0 ; i<32 ; i++ )
    {
        ary1[i] = i;
        ary2[i] = 2*i;
        res[i]=0;
    }

    int * d_ary1, *d_ary2;
    cudaMalloc((void**)&d_ary1, 32*sizeof(int));
    cudaMalloc((void**)&d_ary2, 32*sizeof(int));


    cudaMemcpy((void*)d_ary1, (void*)ary1, 32*sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy((void*)d_ary2, (void*)ary2, 32*sizeof(int), cudaMemcpyHostToDevice);


    addAry<<<1,32>>>(d_ary1,d_ary2);

    cudaMemcpy((void*)res, (void*)d_ary1, 32*sizeof(int), cudaMemcpyDeviceToHost);
    for( int i=0 ; i<32 ; i++ )
        printf( "result[%d] = %d\n", i, res[i]);


    cudaFree(d_ary1);
    cudaFree(d_ary2);
}

Here is my .pro file.

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-17T16:30:33
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = QtCuda
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp


# This makes the .cu files appear in your project
OTHER_FILES +=  ./cuda_interface.cu

# CUDA settings <-- may change depending on your system
CUDA_SOURCES += ./cuda_interface.cu
CUDA_SDK = "/usr/local/cuda-5.0/"   # Path to cuda SDK install
CUDA_DIR = "/usr/local/cuda-5.0/"            # Path to cuda toolkit install

# DO NOT EDIT BEYOND THIS UNLESS YOU KNOW WHAT YOU ARE DOING....

SYSTEM_NAME = unix         # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 32            # '32' or '64', depending on your system
CUDA_ARCH = sm_21           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS = --use_fast_math


# include paths
INCLUDEPATH += $$CUDA_DIR/include

# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib/

CUDA_OBJECTS_DIR = ./


# Add the necessary libraries
CUDA_LIBS = -lcuda -lcudart

# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
#LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')
LIBS += $$CUDA_LIBS

# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
    # Debug mode
    cuda_d.input = CUDA_SOURCES
    cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda_d.commands = $$CUDA_DIR/bin/nvcc -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
    # Release mode
    cuda.input = CUDA_SOURCES
    cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda
}
mkuse
  • 2,250
  • 4
  • 32
  • 61
2

libcuda.so and libcudart.so are missing the -l flag in front of them in the g++ call. You have an appropriate join command to add them for the NVCC, so use the same logic for g++:

CUDA_LIBS = $$join(CUDA_LIBS,' -l','-l', '') 
LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')

Or just change to this:

CUDA_LIBS = -llibcuda -llibcudart

And get rid of the NVCC_LIBS variable.

cmannett85
  • 21,725
  • 8
  • 76
  • 119
  • 16:34:46: Starting: "/usr/bin/make" -w make: Entering directory `/home/swaroop/Work/ai-junkies/cuda/uc_davis/opencv2.x/QtCuda2' g++ -Wl,-O1 -o QtCuda cuda_interface_cuda.o main.o -L/usr/local/cuda-5.0//lib/ -L/usr/lib/i386-linux-gnu -llibcuda.so -llibcudart.so -lQtCore -lpthread /usr/bin/ld: cannot find -llibcuda.so /usr/bin/ld: cannot find -llibcudart.so – mkuse Apr 17 '13 at 11:06
  • `-L/usr/local/cuda-5.0//lib/` has two slashes before lib/. Is that a typo ? – pQB Apr 17 '13 at 11:19
  • Well actually does not matter with 2 slashes. With a little trial and error I can now run cuda code with Qt. I am putting up details as an answer to this question. – mkuse Apr 17 '13 at 11:32
  • @mkuse In `g++` you do not need the 'lib' or '.so' parts, so change the line to `-lcuda`, `-lcudart`. I didn't think it was that militant about it, but perhaps I'm wrong. – cmannett85 Apr 17 '13 at 11:33
  • @cmannett85 exactly...! I did just that. For completeness I am posting the modified .pro file again as an answer to this question. – mkuse Apr 17 '13 at 11:38
0

I can't comment in the mkuse's answer but I wanted to add that...

I had to add -L/usr/local/cuda-6.5/lib64 to the CUDA_LIBS:

# Add the necessary libraries
CUDA_LIBS = -lcuda -lcudart -L/usr/local/cuda-6.5/lib64

Otherwise I get the error "cannot find -lcudart", even when I can run cuda independently. Just in case.

EDIT: I realized that this is not necessary, I just had to check the path for QMAKE_LIBDIR since I have a 64 bit system.

user3484623
  • 111
  • 1
  • 4
  • Hey, I guess you can just edit my answer to include this extra step. – mkuse Jan 07 '15 at 03:52
  • I realized that this is not necessary, I just had to check the path for QMAKE_LIBDIR since I have a 64 bit system. I just added the comment to your answer, thanks. – user3484623 Jan 07 '15 at 17:20