Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to get the amount of total and available GPU(Cuda) memory?(QOpenGLFunctions)
Forum Updated to NodeBB v4.3 + New Features

How to get the amount of total and available GPU(Cuda) memory?(QOpenGLFunctions)

Scheduled Pinned Locked Moved Solved Mobile and Embedded
18 Posts 2 Posters 5.6k 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #1

    I'm trying to write a program for monitoring the statistics of my laptop, and bumped into an issue. By googling I found that I can get the total and available GPU memory by executing such code snippet, but in result the application crashes on start. Can you give me a hint what am I doing wrong?? Thank you.
    Compiler: 5.6.2 MSVC 2013 (32bit)
    OS: W7
    Code Snippet:
    #include "mainwindow.h"
    #include <QApplication>
    #include <QDebug>
    #include <qopengl.h>
    #include <QOpenGLFunctions>
    #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
    #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QOpenGLFunctions func;
    GLint total_mem_kb = 0;
    func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
    GLint cur_avail_mem_kb = 0;
    func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
    qDebug()<<cur_avail_mem_kb;
    return a.exec();
    }

    Mandruk1331

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!

      QOpenGLFunctions func;

      You can't use func without an OpenGL context / until you've initialized it. Please consult the corresponding manual page.

      mandruk1331M 1 Reply Last reply
      3
      • ? A Former User

        Hi!

        QOpenGLFunctions func;

        You can't use func without an OpenGL context / until you've initialized it. Please consult the corresponding manual page.

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3

        @Wieland Like this? (but it crashes) The context is being created, I have checked that

        QOpenGLContext *_context = new QOpenGLContext(this);
          _context->create();
          _context->functions()->initializeOpenGLFunctions();
         QOpenGLFunctions func (QOpenGLContext::currentContext());
         GLint total_mem_kb = 0;
         func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
         GLint cur_avail_mem_kb = 0;
         func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
         qDebug()<<cur_avail_mem_kb;
        

        Mandruk1331

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4
          #include <QGuiApplication>
          #include <QOpenGLFunctions>
          #include <QOpenGLContext>
          #include <QOffscreenSurface>
          #include <QDebug>
          
          #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
          #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
          
          int main(int argc, char *argv[])
          {
              QGuiApplication a(argc, argv);
          
              QOpenGLContext context;
              context.create();
          
              QOffscreenSurface surface;
              surface.create();
          
              QOpenGLFunctions func;
              context.makeCurrent(&surface);
              func.initializeOpenGLFunctions();
          
              GLint total_mem_kb = 0;
              func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
              GLint cur_avail_mem_kb = 0;
              func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
              qDebug()<<cur_avail_mem_kb;
          
              return a.exec();
          }
          
          mandruk1331M 2 Replies Last reply
          3
          • ? A Former User
            #include <QGuiApplication>
            #include <QOpenGLFunctions>
            #include <QOpenGLContext>
            #include <QOffscreenSurface>
            #include <QDebug>
            
            #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
            #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
            
            int main(int argc, char *argv[])
            {
                QGuiApplication a(argc, argv);
            
                QOpenGLContext context;
                context.create();
            
                QOffscreenSurface surface;
                surface.create();
            
                QOpenGLFunctions func;
                context.makeCurrent(&surface);
                func.initializeOpenGLFunctions();
            
                GLint total_mem_kb = 0;
                func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
                GLint cur_avail_mem_kb = 0;
                func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
                qDebug()<<cur_avail_mem_kb;
            
                return a.exec();
            }
            
            mandruk1331M Offline
            mandruk1331M Offline
            mandruk1331
            wrote on last edited by
            #5

            @Wieland Thank you.

            Mandruk1331

            1 Reply Last reply
            0
            • ? A Former User
              #include <QGuiApplication>
              #include <QOpenGLFunctions>
              #include <QOpenGLContext>
              #include <QOffscreenSurface>
              #include <QDebug>
              
              #define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
              #define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
              
              int main(int argc, char *argv[])
              {
                  QGuiApplication a(argc, argv);
              
                  QOpenGLContext context;
                  context.create();
              
                  QOffscreenSurface surface;
                  surface.create();
              
                  QOpenGLFunctions func;
                  context.makeCurrent(&surface);
                  func.initializeOpenGLFunctions();
              
                  GLint total_mem_kb = 0;
                  func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
                  GLint cur_avail_mem_kb = 0;
                  func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
                  qDebug()<<cur_avail_mem_kb;
              
                  return a.exec();
              }
              
              mandruk1331M Offline
              mandruk1331M Offline
              mandruk1331
              wrote on last edited by
              #6

              @Wieland An issue occured, using the functions above don't give the expected result, they return 0, but using the function above tell that they will return memory usage by the GPU, what can be the issue??

              Mandruk1331

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                Huh? The function returns void, not 0: void glGetIntegerv(GLenum pname, GLint * data). The results you asked for are stored in total_mem_kb and cur_avail_mem_kb.

                mandruk1331M 1 Reply Last reply
                2
                • ? A Former User

                  Huh? The function returns void, not 0: void glGetIntegerv(GLenum pname, GLint * data). The results you asked for are stored in total_mem_kb and cur_avail_mem_kb.

                  mandruk1331M Offline
                  mandruk1331M Offline
                  mandruk1331
                  wrote on last edited by mandruk1331
                  #8

                  @Wieland Yes I know. I meant that this two variables are not being init in the function.

                      QOpenGLContext context;
                     context.create();
                  
                     QOffscreenSurface surface;
                     surface.create();
                  
                     QOpenGLFunctions func;
                     context.makeCurrent(&surface);
                     func.initializeOpenGLFunctions();
                  
                     qDebug()<<func.glGetString(GL_RENDER);
                  

                  qDebug prints 0x0 smth, is for sure wrong

                  Mandruk1331

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Hmm.. worked for me. Tested on Nvidia GTX 1080, 64 bit desktop Linux, with Nvidia's proprietary drivers. Don't have that machine at hand right now, can't tell for sure which driver version it has.

                    mandruk1331M 1 Reply Last reply
                    1
                    • ? A Former User

                      Hmm.. worked for me. Tested on Nvidia GTX 1080, 64 bit desktop Linux, with Nvidia's proprietary drivers. Don't have that machine at hand right now, can't tell for sure which driver version it has.

                      mandruk1331M Offline
                      mandruk1331M Offline
                      mandruk1331
                      wrote on last edited by
                      #10

                      @Wieland Have you tried it on Windows?? ran this code on W7 and W10 different laptops, the result was 0x0

                      Mandruk1331

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        Wait, I can check with Windows 7 laptop.

                        1 Reply Last reply
                        1
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on last edited by
                          #12

                          Okay, the code I posted above works here, too.

                          qDebug() << QString::fromLatin1((const char*)func.glGetString(GL_EXTENSIONS)); prints all the extentions,

                          qDebug() << QString::fromLatin1((const char*)func.glGetString(GL_RENDER)); prints an empty string.

                          mandruk1331M 1 Reply Last reply
                          2
                          • ? A Former User

                            Okay, the code I posted above works here, too.

                            qDebug() << QString::fromLatin1((const char*)func.glGetString(GL_EXTENSIONS)); prints all the extentions,

                            qDebug() << QString::fromLatin1((const char*)func.glGetString(GL_RENDER)); prints an empty string.

                            mandruk1331M Offline
                            mandruk1331M Offline
                            mandruk1331
                            wrote on last edited by mandruk1331
                            #13

                            @Wieland tried the above code and it gives a list of extensions. But the main thing I need is the available GPU memory.

                              QOpenGLContext context;
                              context.create();
                              QOffscreenSurface surface;
                              surface.create();
                              QOpenGLFunctions func;
                              context.makeCurrent(&surface);
                              func.initializeOpenGLFunctions();
                              GLint cur_avail_mem_kb = 0;
                              func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
                              qDebug()<<cur_avail_mem_kb;
                            

                            cur_avail_mem_kb - is equals to the number it was initialized, in this case it's zero. What can be the problem? Does this code executes on your computer?

                            Mandruk1331

                            1 Reply Last reply
                            0
                            • ? Offline
                              ? Offline
                              A Former User
                              wrote on last edited by
                              #14

                              Yes, the following prints the correct results:

                              GLint total_mem_kb = 0;
                              func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
                              qDebug()<<total_mem_kb;
                              
                              GLint cur_avail_mem_kb = 0;
                              func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
                              qDebug()<<cur_avail_mem_kb;
                              
                              mandruk1331M 1 Reply Last reply
                              1
                              • ? A Former User

                                Yes, the following prints the correct results:

                                GLint total_mem_kb = 0;
                                func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
                                qDebug()<<total_mem_kb;
                                
                                GLint cur_avail_mem_kb = 0;
                                func.glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,&cur_avail_mem_kb);
                                qDebug()<<cur_avail_mem_kb;
                                
                                mandruk1331M Offline
                                mandruk1331M Offline
                                mandruk1331
                                wrote on last edited by
                                #15

                                @Wieland A big thank you for the time you invested in solving the problem. It looks like then smth is wrong on my side, now I'm updating my drivers and after I'll give it a try again.

                                Mandruk1331

                                ? 1 Reply Last reply
                                1
                                • mandruk1331M mandruk1331

                                  @Wieland A big thank you for the time you invested in solving the problem. It looks like then smth is wrong on my side, now I'm updating my drivers and after I'll give it a try again.

                                  ? Offline
                                  ? Offline
                                  A Former User
                                  wrote on last edited by
                                  #16

                                  @mandruk1331 You're welcome. Good luck, hope you can solve it. It's almost always driver-related issues.

                                  mandruk1331M 1 Reply Last reply
                                  0
                                  • ? A Former User

                                    @mandruk1331 You're welcome. Good luck, hope you can solve it. It's almost always driver-related issues.

                                    mandruk1331M Offline
                                    mandruk1331M Offline
                                    mandruk1331
                                    wrote on last edited by
                                    #17

                                    @Wieland Ok so I updated everything I could, but that did not solve the problem, but by asking the same question on StackOverflow, I now have a solution. The code above works ok, the main problem was that my main graphic card was Intel, therefore I had to switch the video card setting so that the main video card would be Nvidia, by doing that the problem has been solved.

                                    Mandruk1331

                                    ? 1 Reply Last reply
                                    0
                                    • mandruk1331M mandruk1331

                                      @Wieland Ok so I updated everything I could, but that did not solve the problem, but by asking the same question on StackOverflow, I now have a solution. The code above works ok, the main problem was that my main graphic card was Intel, therefore I had to switch the video card setting so that the main video card would be Nvidia, by doing that the problem has been solved.

                                      ? Offline
                                      ? Offline
                                      A Former User
                                      wrote on last edited by
                                      #18

                                      @mandruk1331 Hey, glad you solved it. And thanks for sharing the solution :)

                                      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