Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. CUDA and QT create library
Forum Updated to NodeBB v4.3 + New Features

CUDA and QT create library

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kyll
    wrote on last edited by
    #1

    Hello i try to use CUDA with QT and get some errors.
    This example work fine:
    main.cpp

    extern "C" void runKernel();
    #include <cstdio>
    int main(int argc, char **argv)
    {
    	runKernel();
    	printf("Kernel passed \n");
    	return 0;
    }
    

    MWE.cu

    #include <cstdio>
    
    __global__ void testKernel()
    {
    }
    
    extern "C" void runKernel()
    {
    	testKernel<<<1,1>>>();
    	printf("\n Kernel pass \n");
    }
    

    Build instructions

        nvcc -ccbin g++ -m64 -arch=sm_50 -dc -o MWE.o -c MWE.cu
        nvcc -ccbin g++ -m64 -arch=sm_50 -dlink -o MWE.dlink.o MWE.o
        nvcc -ccbin g++ -m64 -arch=sm_50 -lib -o MWE.a MWE.o MWE.dlink.o
        g++ -m64 -o main.o -c main.cpp
        g++ -m64 main.o MWE.a -o test -L/usr/local/cuda/lib64 -lcudart
    

    So okay I get library MWE.a and can compile with main.cpp using g++, I try to create new library with previous one MWE.a
    KL.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-03-18T19:09:38
    #
    #-------------------------------------------------
    TEMPLATE = lib
    CONFIG -= qt
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG += c++11
    INCLUDEPATH += home/kyll/Documents/CUDATEST/LAST
    LIBS += -L/home/kyll/Documents/CUDATEST/LAST/MWE.a
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as 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 you use 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 += \
            kl.cpp
    
    HEADERS += \
        kl.h
    
    CONFIG(debug, debug|release) {
        # debug
        unix: {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp
        } else {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -fopenmp
        }
    } else {
        # release
        unix: {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp
        } else {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp
        }
    }
    
    CONFIG (debug, debug|release) {
        OBJECTS_DIR = debug
    } else {
        OBJECTS_DIR = release
    }
    

    kl.h

    #ifndef KL_H
    #define KL_H
    
    extern "C" void runKernel();
    extern "C" void f();
    
    #endif // KL_H
    

    kl.cpp

    #include <cstdio>
    #include "kl.h"
    void runKernel();
    void f()
    {
        runKernel();
        printf("Test \n");
    }
    

    MinGW generating library .so , .so.1 , .so.1.0, .so.1.0.0, I use .so in my app and get errors:
    :-1: error: /tmp/ccCGR1vb.ltrans0.ltrans.o: in function `main':
    :-1: error: collect2: error: ld returned 1 exit status

    LM.pro

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-03-18T19:09:38
    #
    #-------------------------------------------------
    TEMPLATE = app
    CONFIG -= qt
    CONFIG += console
    CONFIG -= app_bundle
    CONFIG += c++11
    INCLUDEPATH += /home/kyll/Documents/CUDATEST/LAST/build-KL-Desktop_Qt_5_10_0_GCC_64bit-Debug
    LIBS += -L/home/kyll/Documents/CUDATEST/LAST/build-KL-Desktop_Qt_5_10_0_GCC_64bit-Debug/libKL.so
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as 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 you use 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 += \
            lm.cpp
    HEADERS += \
        lm.h
    
    CONFIG(debug, debug|release) {
        # debug
        unix: {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp
        } else {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -fopenmp
        }
    } else {
        # release
        unix: {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp
        } else {
            QMAKE_LFLAGS = -fopenmp
            QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp
        }
    }
    
    CONFIG (debug, debug|release) {
        OBJECTS_DIR = debug
    } else {
        OBJECTS_DIR = release
    }
    

    lm.h

    #ifndef LM_H
    #define LM_H
    
    extern "C" void runKernel();
    extern "C" void f();
    
    #endif // LM_H
    

    lm.cpp

    #include <cstdio>
    #include "lm.h"
    int main(){
        runKernel();
        //f();
        return 0;
    }
    

    What I misunderstand? Where error?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      -L is for giving an additional search path to the linker.

      You need to use -l (lowercase L) to give an additional library to use to the linker.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • K Offline
        K Offline
        Kyll
        wrote on last edited by
        #3

        I change few lines in KL.pro:

        INCLUDEPATH += home/kyll/Documents/CUDATEST/LAST
        LIBS += -L/home/kyll/Documents/CUDATEST/LAST -lMWE
        

        add -l and name of library and get errors
        :-1: error: cannot find -lMWE
        :-1: error: collect2: error: ld returned 1 exit status

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kyll
          wrote on last edited by Kyll
          #4

          Add external library using project -> add library (recompile with new name lib*.a)

          unix:!macx: LIBS += -L$$PWD/../ -lMWE
          
          INCLUDEPATH += $$PWD/../
          DEPENDPATH += $$PWD/../
          
          unix:!macx: PRE_TARGETDEPS += $$PWD/../libMWE.a
          
          

          Now get new errors:
          :-1: error: /home/kyll/Documents/CUDATEST/LAST/KL/..//libMWE.a(MWE.o): relocation R_X86_64_PC32 against symbol `_Z10testKernelv' can not be used when making a shared object; recompile with -fPIC
          :-1: error: final link failed: bad value
          :-1: error: collect2: error: ld returned 1 exit status

          Recompile with -fPIC all work.(Can build library in library)
          But try to use this library in LM.pro I get errors
          :-1: error: /tmp/ccB1sXku.ltrans0.ltrans.o: in function `main':
          :-1: error: collect2: error: ld returned 1 exit status

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Are you using a self-built Qt version ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            0
            • SGaistS SGaist

              Are you using a self-built Qt version ?

              K Offline
              K Offline
              Kyll
              wrote on last edited by
              #6

              @SGaist I install qt (opensource version) from maintenance tool.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                How did you add -fPIC to your library build ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                K 1 Reply Last reply
                0
                • SGaistS SGaist

                  How did you add -fPIC to your library build ?

                  K Offline
                  K Offline
                  Kyll
                  wrote on last edited by
                  #8

                  @SGaist I added -fPIC in KL.pro in QMAKE_LFLAGS and QMAKE_CXXFLAGS in #release -> else.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    Kyll
                    wrote on last edited by
                    #9

                    Okay I can make this:
                    main.cpp

                    extern "C" void runKernel();
                    #include <cstdio>
                    int main(int argc, char **argv)
                    {
                    	runKernel();
                    	printf("Kernel passed\n");
                    	return 0;
                    }
                    

                    MWE.cu

                    #include <cstdio>
                    
                    __global__ void testKernel()
                    {
                    }
                    
                    extern "C" void runKernel()
                    {
                    	testKernel<<<1,1>>>();
                    	printf("\nKernel pass \n");
                    }
                    

                    Build

                    nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -dc -o MWE.o -c MWE.cu
                    nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -dlink -o MWE.dlink.o MWE.o
                    nvcc -ccbin g++ -m64 -arch=sm_50 --compiler-options '-fPIC' -lib -o libMWE.a MWE.o MWE.dlink.o
                    

                    KL.pro

                    TEMPLATE = lib
                    CONFIG -= qt
                    CONFIG += console
                    CONFIG -= app_bundle
                    CONFIG += c++11
                    DEFINES += QT_DEPRECATED_WARNINGS
                    CUDA_DIR      = /usr/local/cuda-10.1
                    INCLUDEPATH  += $$CUDA_DIR/include
                    QMAKE_LIBDIR += $$CUDA_DIR/lib64
                    LIBS += -L$$CUDA_DIR/lib64 -lcudart -lcuda
                    
                    SOURCES += \
                            kl.cpp
                    
                    CONFIG(debug, debug|release) {
                        # debug
                        unix: {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp
                        } else {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -fopenmp
                        }
                    } else {
                        # release
                        unix: {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp
                        } else {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp
                        }
                    }
                    
                    CONFIG (debug, debug|release) {
                        OBJECTS_DIR = debug
                    } else {
                        OBJECTS_DIR = release
                    }
                    
                    unix:!macx: LIBS += -L$$PWD/../ -lMWE
                    
                    INCLUDEPATH += $$PWD/../
                    DEPENDPATH += $$PWD/../
                    
                    unix:!macx: PRE_TARGETDEPS += $$PWD/../libMWE.a
                    

                    KL.cpp

                    #include <cstdio>
                    extern "C" void runKernel();
                    extern "C" void f()
                    {
                        runKernel();
                        printf("Test \n");
                    }
                    

                    LM.pro

                    TEMPLATE = app
                    CONFIG -= qt
                    CONFIG += console
                    CONFIG -= app_bundle
                    CONFIG += c++11
                    DEFINES += QT_DEPRECATED_WARNINGS
                    CUDA_DIR      = /usr/local/cuda-10.1
                    INCLUDEPATH  += $$CUDA_DIR/include
                    QMAKE_LIBDIR += $$CUDA_DIR/lib64
                    LIBS += -L$$CUDA_DIR/lib64 -lcudart -lcuda
                    SOURCES += \
                            lm.cpp
                    
                    CONFIG(debug, debug|release) {
                        # debug
                        unix: {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -m64 -flto -march=native -funroll-loops -fopenmp
                        } else {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -fopenmp
                        }
                    } else {
                        # release
                        unix: {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -Ofast -m64 -flto -march=native -funroll-loops -fopenmp
                        } else {
                            QMAKE_LFLAGS = -fopenmp
                            QMAKE_CXXFLAGS += -Ofast -flto -march=native -funroll-loops -fopenmp
                        }
                    }
                    
                    CONFIG (debug, debug|release) {
                        OBJECTS_DIR = debug
                    } else {
                        OBJECTS_DIR = release
                    }
                    
                    unix:!macx: LIBS += -L$$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release/ -lKL
                    INCLUDEPATH += $$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release
                    DEPENDPATH += $$PWD/../build-KL-Desktop_Qt_5_10_0_GCC_64bit-Release
                    

                    LM.cpp

                    #include <cstdio>
                    extern "C" void runKernel();
                    extern "C" void f();
                    int main(){
                        runKernel();
                        f();
                        return 0;
                    }
                    
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved