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. Inherit QObject in a cuda class
QtWS25 Last Chance

Inherit QObject in a cuda class

Scheduled Pinned Locked Moved Solved General and Desktop
cudaqt creator c++
10 Posts 4 Posters 593 Views
  • 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.
  • S Offline
    S Offline
    skishore1998
    wrote on last edited by
    #1

    I am c++ beginner working on an app development that acquires data in C++, processes it in GPU, and plots it in GUI using Qcustom plot library. I am using signals and slots to share data pointers between classes. But how to inherit Qobject in Cuda class?

    I will give a sample code to represent my problem.

    cmake_minimum_required(VERSION 3.5)
    
    project(Trial04 VERSION 0.1 LANGUAGES CXX CUDA)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    set(CMAKE_CUDA_STANDARD 11)
    set(CMAKE_CUDA_STANDARD_REQUIRED ON)
    
    find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport)
    
    qt_standard_project_setup()
    
    include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
    
    add_executable(t04a 
        mainwindow.ui
        mainwindow.cpp
        mainwindow.h
        main.cpp
        CudaProcessing.cu
        CudaProcessing.h
    )
    
    target_link_libraries(t04 PRIVATE Qt6::Widgets)
    
    set_target_properties(t04 PROPERTIES
        WIN32_EXECUTABLE ON
        MACOSX_BUNDLE ON
    )
    set_target_properties(t04 PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
    
    
    //mainwindow.cpp
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        CudaProcessing *c1 = new CudaProcessing(this);
        c1->hello_world();
    }
    
    //CudaProcessing.cu
    #include "CudaProcessing.h"
    #include <stdio.h>
    
    using namespace std;
    
    __global__ void hello(){
        printf("hello from GPU");
    }
    
    void CudaProcessing::hello_world(){
        hello<<<1,1>>>();
    }
    
    //CudaProcessing.h
    #pragma once
    class CudaProcessing{
    public:
        CudaProcessing() {}
        void hello_world();
        ~CudaProcessing() {}
    };
    

    The above code without QObject inheritance works fine.

    #include <QObject>
    class CudaProcessing:public QObject {
        Q_OBJECT
    public:
        CudaProcessing(QObject *parent):QObject(parent){ }
        void hello_world();
        ~CudaProcessing() {}
    };
    

    When I inherit QObject in the above CUDA class I am getting an error

    error "Qt requires a C++17 compiler, and a suitable value for __cplusplus. On MSVC, you must pass the /Zc:__cplusplus option to the compiler."
    

    If I add the following

    if (MSVC)
        add_compile_options(/Zc:__cplusplus)
    endif()
    

    to cmake file it shows a new error

    error: Cannot specify link libraries for target "t04" which is not built by this project.
    

    Is my approach of signal and slot in CUDA class right? If not, any suggestion would be greatly appreciated. Thanks

    Pl45m4P jsulmJ 3 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #10

      Hi,

      As @Pl45m4 wrote, CUDA kernels are a different beast that you should keep separated from QObject.
      You won't be able to use signals and slots from CUDA code anyway.
      Make your QObject based classes use CUDA kernels and keep compilation units separate.

      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
      0
      • S skishore1998

        I am c++ beginner working on an app development that acquires data in C++, processes it in GPU, and plots it in GUI using Qcustom plot library. I am using signals and slots to share data pointers between classes. But how to inherit Qobject in Cuda class?

        I will give a sample code to represent my problem.

        cmake_minimum_required(VERSION 3.5)
        
        project(Trial04 VERSION 0.1 LANGUAGES CXX CUDA)
        
        set(CMAKE_CXX_STANDARD 17)
        set(CMAKE_CXX_STANDARD_REQUIRED ON)
        
        set(CMAKE_CUDA_STANDARD 11)
        set(CMAKE_CUDA_STANDARD_REQUIRED ON)
        
        find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport)
        
        qt_standard_project_setup()
        
        include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
        
        add_executable(t04a 
            mainwindow.ui
            mainwindow.cpp
            mainwindow.h
            main.cpp
            CudaProcessing.cu
            CudaProcessing.h
        )
        
        target_link_libraries(t04 PRIVATE Qt6::Widgets)
        
        set_target_properties(t04 PROPERTIES
            WIN32_EXECUTABLE ON
            MACOSX_BUNDLE ON
        )
        set_target_properties(t04 PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
        
        
        //mainwindow.cpp
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            CudaProcessing *c1 = new CudaProcessing(this);
            c1->hello_world();
        }
        
        //CudaProcessing.cu
        #include "CudaProcessing.h"
        #include <stdio.h>
        
        using namespace std;
        
        __global__ void hello(){
            printf("hello from GPU");
        }
        
        void CudaProcessing::hello_world(){
            hello<<<1,1>>>();
        }
        
        //CudaProcessing.h
        #pragma once
        class CudaProcessing{
        public:
            CudaProcessing() {}
            void hello_world();
            ~CudaProcessing() {}
        };
        

        The above code without QObject inheritance works fine.

        #include <QObject>
        class CudaProcessing:public QObject {
            Q_OBJECT
        public:
            CudaProcessing(QObject *parent):QObject(parent){ }
            void hello_world();
            ~CudaProcessing() {}
        };
        

        When I inherit QObject in the above CUDA class I am getting an error

        error "Qt requires a C++17 compiler, and a suitable value for __cplusplus. On MSVC, you must pass the /Zc:__cplusplus option to the compiler."
        

        If I add the following

        if (MSVC)
            add_compile_options(/Zc:__cplusplus)
        endif()
        

        to cmake file it shows a new error

        error: Cannot specify link libraries for target "t04" which is not built by this project.
        

        Is my approach of signal and slot in CUDA class right? If not, any suggestion would be greatly appreciated. Thanks

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by
        #2

        @skishore1998 said in Inherit QObject in a cuda class:

        I am using signals and slots to share data pointers between classes

        I don't see any Qt signal-slot connection in your code?!


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        S 1 Reply Last reply
        0
        • S skishore1998

          I am c++ beginner working on an app development that acquires data in C++, processes it in GPU, and plots it in GUI using Qcustom plot library. I am using signals and slots to share data pointers between classes. But how to inherit Qobject in Cuda class?

          I will give a sample code to represent my problem.

          cmake_minimum_required(VERSION 3.5)
          
          project(Trial04 VERSION 0.1 LANGUAGES CXX CUDA)
          
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          
          set(CMAKE_CUDA_STANDARD 11)
          set(CMAKE_CUDA_STANDARD_REQUIRED ON)
          
          find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport)
          
          qt_standard_project_setup()
          
          include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
          
          add_executable(t04a 
              mainwindow.ui
              mainwindow.cpp
              mainwindow.h
              main.cpp
              CudaProcessing.cu
              CudaProcessing.h
          )
          
          target_link_libraries(t04 PRIVATE Qt6::Widgets)
          
          set_target_properties(t04 PROPERTIES
              WIN32_EXECUTABLE ON
              MACOSX_BUNDLE ON
          )
          set_target_properties(t04 PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
          
          
          //mainwindow.cpp
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              CudaProcessing *c1 = new CudaProcessing(this);
              c1->hello_world();
          }
          
          //CudaProcessing.cu
          #include "CudaProcessing.h"
          #include <stdio.h>
          
          using namespace std;
          
          __global__ void hello(){
              printf("hello from GPU");
          }
          
          void CudaProcessing::hello_world(){
              hello<<<1,1>>>();
          }
          
          //CudaProcessing.h
          #pragma once
          class CudaProcessing{
          public:
              CudaProcessing() {}
              void hello_world();
              ~CudaProcessing() {}
          };
          

          The above code without QObject inheritance works fine.

          #include <QObject>
          class CudaProcessing:public QObject {
              Q_OBJECT
          public:
              CudaProcessing(QObject *parent):QObject(parent){ }
              void hello_world();
              ~CudaProcessing() {}
          };
          

          When I inherit QObject in the above CUDA class I am getting an error

          error "Qt requires a C++17 compiler, and a suitable value for __cplusplus. On MSVC, you must pass the /Zc:__cplusplus option to the compiler."
          

          If I add the following

          if (MSVC)
              add_compile_options(/Zc:__cplusplus)
          endif()
          

          to cmake file it shows a new error

          error: Cannot specify link libraries for target "t04" which is not built by this project.
          

          Is my approach of signal and slot in CUDA class right? If not, any suggestion would be greatly appreciated. Thanks

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @skishore1998 said in Inherit QObject in a cuda class:

          add_executable(t04a

          in other places you use t04 - why? Fix you CMakeLists.txt file.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          1
          • Pl45m4P Pl45m4

            @skishore1998 said in Inherit QObject in a cuda class:

            I am using signals and slots to share data pointers between classes

            I don't see any Qt signal-slot connection in your code?!

            S Offline
            S Offline
            skishore1998
            wrote on last edited by skishore1998
            #4

            @Pl45m4
            The purpose of inheriting Qobject is to use signals and slots. But I see the error immediately after inheriting Qobject. I have tried going ahead and implementing signals and slots in my main code but the error remains the same.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @skishore1998 said in Inherit QObject in a cuda class:

              add_executable(t04a

              in other places you use t04 - why? Fix you CMakeLists.txt file.

              S Offline
              S Offline
              skishore1998
              wrote on last edited by
              #5

              @jsulm Thanks, that was a typo. Now the error is

              :-1: error: ninja: build stopped: subcommand failed.
              
              jsulmJ 1 Reply Last reply
              0
              • S skishore1998

                @jsulm Thanks, that was a typo. Now the error is

                :-1: error: ninja: build stopped: subcommand failed.
                
                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @skishore1998 Please post the actual error

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @skishore1998 Please post the actual error

                  S Offline
                  S Offline
                  skishore1998
                  wrote on last edited by
                  #7

                  @jsulm Sorry, This is what I get at the compile output

                  18:32:43: Running steps for project Trial04...
                  18:32:43: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build D:/desktop_c/Kishore/qwt_testing/10_contour_ADQ_linked/build-Trial04-Desktop_Qt_6_6_1_MSVC2019_64bit-Debug --target all
                  [1/3 20.0/sec] Building CUDA object CMakeFiles\t04.dir\CudaProcessing.cu.obj
                  FAILED: CMakeFiles/t04.dir/CudaProcessing.cu.obj 
                  C:\PROGRA~1\NVIDIA~2\CUDA\v12.3\bin\nvcc.exe -forward-unknown-to-host-compiler -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DUNICODE -DWIN32 -DWIN64 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -D_UNICODE -D_WIN64 -ID:\desktop_c\Kishore\qwt_testing\10_contour_ADQ_linked\build-Trial04-Desktop_Qt_6_6_1_MSVC2019_64bit-Debug\t04_autogen\include -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\include" -isystem C:\Qt\6.6.1\msvc2019_64\include\QtWidgets -isystem C:\Qt\6.6.1\msvc2019_64\include -isystem C:\Qt\6.6.1\msvc2019_64\include\QtCore -isystem C:\Qt\6.6.1\msvc2019_64\mkspecs\win32-msvc -isystem C:\Qt\6.6.1\msvc2019_64\include\QtGui -D_WINDOWS -Xcompiler="/W3 /GR /EHsc" -Xcompiler="-MDd  -Zi -Ob0 -Od /RTC1" /Zc:__cplusplus -MD -MT CMakeFiles\t04.dir\CudaProcessing.cu.obj -MF CMakeFiles\t04.dir\CudaProcessing.cu.obj.d -x cu -rdc=true -c D:\desktop_c\Kishore\qwt_testing\10_contour_ADQ_linked\Trial04\CudaProcessing.cu -o CMakeFiles\t04.dir\CudaProcessing.cu.obj -Xcompiler=-FdCMakeFiles\t04.dir\,-FS
                  nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified
                  ninja: build stopped: subcommand failed.
                  18:32:44: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 1.
                  Error while building/deploying project Trial04 (kit: Desktop Qt 6.6.1 MSVC2019 64bit)
                  When executing step "Build"
                  18:32:44: Elapsed time: 00:00.
                  
                  1 Reply Last reply
                  0
                  • S skishore1998

                    I am c++ beginner working on an app development that acquires data in C++, processes it in GPU, and plots it in GUI using Qcustom plot library. I am using signals and slots to share data pointers between classes. But how to inherit Qobject in Cuda class?

                    I will give a sample code to represent my problem.

                    cmake_minimum_required(VERSION 3.5)
                    
                    project(Trial04 VERSION 0.1 LANGUAGES CXX CUDA)
                    
                    set(CMAKE_CXX_STANDARD 17)
                    set(CMAKE_CXX_STANDARD_REQUIRED ON)
                    
                    set(CMAKE_CUDA_STANDARD 11)
                    set(CMAKE_CUDA_STANDARD_REQUIRED ON)
                    
                    find_package(Qt6 REQUIRED COMPONENTS Widgets PrintSupport)
                    
                    qt_standard_project_setup()
                    
                    include_directories(${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
                    
                    add_executable(t04a 
                        mainwindow.ui
                        mainwindow.cpp
                        mainwindow.h
                        main.cpp
                        CudaProcessing.cu
                        CudaProcessing.h
                    )
                    
                    target_link_libraries(t04 PRIVATE Qt6::Widgets)
                    
                    set_target_properties(t04 PROPERTIES
                        WIN32_EXECUTABLE ON
                        MACOSX_BUNDLE ON
                    )
                    set_target_properties(t04 PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
                    
                    
                    //mainwindow.cpp
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                        CudaProcessing *c1 = new CudaProcessing(this);
                        c1->hello_world();
                    }
                    
                    //CudaProcessing.cu
                    #include "CudaProcessing.h"
                    #include <stdio.h>
                    
                    using namespace std;
                    
                    __global__ void hello(){
                        printf("hello from GPU");
                    }
                    
                    void CudaProcessing::hello_world(){
                        hello<<<1,1>>>();
                    }
                    
                    //CudaProcessing.h
                    #pragma once
                    class CudaProcessing{
                    public:
                        CudaProcessing() {}
                        void hello_world();
                        ~CudaProcessing() {}
                    };
                    

                    The above code without QObject inheritance works fine.

                    #include <QObject>
                    class CudaProcessing:public QObject {
                        Q_OBJECT
                    public:
                        CudaProcessing(QObject *parent):QObject(parent){ }
                        void hello_world();
                        ~CudaProcessing() {}
                    };
                    

                    When I inherit QObject in the above CUDA class I am getting an error

                    error "Qt requires a C++17 compiler, and a suitable value for __cplusplus. On MSVC, you must pass the /Zc:__cplusplus option to the compiler."
                    

                    If I add the following

                    if (MSVC)
                        add_compile_options(/Zc:__cplusplus)
                    endif()
                    

                    to cmake file it shows a new error

                    error: Cannot specify link libraries for target "t04" which is not built by this project.
                    

                    Is my approach of signal and slot in CUDA class right? If not, any suggestion would be greatly appreciated. Thanks

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #8

                    @skishore1998 said in Inherit QObject in a cuda class:

                    The above code without QObject inheritance works fine.

                    I think it's not a good idea to make your CUDA class (including the *.cu file) a QObject.
                    You need to compile your CUDA/cu file separately.

                    Here is an example
                    (it's somewhat old and it uses QMake, you have to adapt it to CMake)

                    • https://www.qt.io/blog/2015/03/03/qt-weekly-28-qt-and-cuda-on-the-jetson-tk1

                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    1
                    • jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      Something is wrong with the nvcc.exe call, but I'm not a CUDA expert.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                        Hi,

                        As @Pl45m4 wrote, CUDA kernels are a different beast that you should keep separated from QObject.
                        You won't be able to use signals and slots from CUDA code anyway.
                        Make your QObject based classes use CUDA kernels and keep compilation units separate.

                        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
                        0
                        • S skishore1998 has marked this topic as solved on

                        • Login

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