Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: Use more CPU cores

  1. #1
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Use more CPU cores

    Hi there,
    is there a way to run a Qt application using all the cores of the CPU? I've a two-core CPU and there are two threads in my program. This application has to be as fast as possible, a smart thing will be assign a thread for each core. Is it possible?

    If yes, is there a way to determine the number of CPU cores of the machine where the application is running?

    Sorry for my english

    Thanks, Dario
    Last edited by satoshi; 20th April 2010 at 11:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    If there are two threads in your application then if your system policy allows it, two cores will be used.

  3. The following user says thank you to wysota for this useful post:

    satoshi (20th April 2010)

  4. #3
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Quote Originally Posted by wysota View Post
    If there are two threads in your application then if your system policy allows it, two cores will be used.
    Thanks, this is what I was expecting but it seems it's using only one core.

    I'm running Qt 4.5.3 under Windows XP. The effect is the same under Ubuntu.

    Any suggestion?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    Maybe you are not using two threads?

  6. The following user says thank you to wysota for this useful post:

    satoshi (20th April 2010)

  7. #5
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Quote Originally Posted by wysota View Post
    Maybe you are not using two threads?
    I see two threads when I debug...

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    Quote Originally Posted by satoshi View Post
    I see two threads when I debug...
    Which doesn't mean both are doing something. If you have bugs in your code concerning thread affinity, then one of the threads will be idle all the time and won't use practically any cpu power.

  9. The following user says thank you to wysota for this useful post:

    satoshi (20th April 2010)

  10. #7
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Quote Originally Posted by wysota View Post
    Which doesn't mean both are doing something. If you have bugs in your code concerning thread affinity, then one of the threads will be idle all the time and won't use practically any cpu power.
    Ok, thanks. I'll verify that.

    Dario

  11. #8
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Maybe I'm not using threads correctly.. Here's my code:
    Qt Code:
    1. for(int i = 0; i < numCores; i++)
    2. {
    3. new MyThread(...);
    4. }
    To copy to clipboard, switch view to plain text mode 

    The class MyThread is:
    Qt Code:
    1. //HEADER
    2. #ifndef MYTHREAD_H
    3. #define MYTHREAD_H
    4.  
    5. #include <QThread>
    6.  
    7. class MyThread : public QThread
    8. {
    9. public:
    10. MyThread(...);
    11.  
    12. protected:
    13. void run();
    14. };
    15.  
    16. #endif // MYTHREAD_H
    17.  
    18.  
    19. // BODY
    20. #include "mythread.h"
    21.  
    22. MyThread::MyThread(...)
    23. {
    24. start();
    25. }
    26.  
    27. void MyThread::run()
    28. {
    29. ...
    30. }
    To copy to clipboard, switch view to plain text mode 

    Is it right? Have I to call exec() in the run() method? If yes, where?

    Thanks,

    Dario

  12. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    start() should not be called from the constructor, that's for sure. But what is important is the contents of the run() method too.

  13. The following user says thank you to wysota for this useful post:

    satoshi (20th April 2010)

  14. #10
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Ok, I've removed the start() method in the constructor of MyThread, now the code is:
    Qt Code:
    1. MyThread *mt;
    2. for(int i = 0; i < numCores; i++)
    3. {
    4. mt = new MyThread(...);
    5. mt->start();
    6. }
    To copy to clipboard, switch view to plain text mode 

    The result is the same, it will only create one new thread (numCores = 2)...

    The content of run() isn't nothing special, say for(int i = 0; i < INT_MAX; i++){}

    Thanks for the help you're giving me.

    Dario

  15. #11
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    I think the problem is the debugger, since if i put numCores = 50, the debugger says that 10 threads has been created. This is too strange to be real, or not?

    Anyway, always one core is 100% busy, the second is free.

  16. #12
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Ok, now I'm sure that the threads are running correctly. With numCores=2, if I put a breakpoint in the execution of the run() method, the debugger will stop there sometimes with one thread (id=4) and sometimes with the other thread (id=5).

    Always one core is 100% busy, the second is free.

  17. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    Open the task manager, right click on your process and check its processor affinity. Both checkboxes should be checked.

  18. The following user says thank you to wysota for this useful post:

    satoshi (20th April 2010)

  19. #14
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    1. Use QThread::idealThreadCount to retrieve processors core count
    2. I think, that one of your thread doesn't give any time to another. Try to call
    Qt Code:
    1. msleep( 1 );
    To copy to clipboard, switch view to plain text mode 
    inside for in your run functions as following:
    Qt Code:
    1. for(int i = 0; i < INT_MAX; i++)
    2. {
    3. if ( ( i % 100 ) == 0 )
    4. {
    5. msleep( 1 );
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  20. The following user says thank you to borisbn for this useful post:

    satoshi (20th April 2010)

  21. #15
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use more CPU cores

    Quote Originally Posted by wysota View Post
    Open the task manager, right click on your process and check its processor affinity. Both checkboxes should be checked.
    Both checked

    Quote Originally Posted by borisbn View Post
    1. Use QThread::idealThreadCount to retrieve processors core count
    2. I think, that one of your thread doesn't give any time to another. Try to call
    Qt Code:
    1. msleep( 1 );
    To copy to clipboard, switch view to plain text mode 
    inside for in your run functions as following:
    Qt Code:
    1. for(int i = 0; i < INT_MAX; i++)
    2. {
    3. if ( ( i % 100 ) == 0 )
    4. {
    5. msleep( 1 );
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Thanks for that but sorry, I just discovered that the threads are running correctly...


    So, after starting this new 2 thread, I've 4 threads running in the application.

    The first thread (which created the second) is waiting for a timer which will timeout in 3 minutes. This is a QApplication.
    The second thread (which created the 2 new threads) is waiting for the termination of these new 2 threads (with QWaitCondition), which are calculating and need a lot of CPU.

    I'm wondering if the OS is stupid and put the first 2 threads in one core and the others 2 in the other thread...

  22. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    The affinity should be dynamically changing to balance the load between two cores. If one thread waits on a wait condition, it is not scheduled by the OS so it won't add to the overall load of the processing unit. Maybe it is your OS that reports usage incorrectly.

  23. #17
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use more CPU cores

    Quote Originally Posted by wysota View Post
    start() should not be called from the constructor, that's for sure.
    Why not? That way the user of the class cannot forget to call it.

  24. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    For one thing start() calls a method that calls a virtual method. It's true that the virtual method is called on behalf of another thread but I won't guarantee the object will be fully constructed by then. So it might work in 99% of the cases but it will fail when you need it the most (acc. to Murphy's Law).

  25. #19
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use more CPU cores

    Ok, fair enough. I can't see that start() calls any virtual methods in Qt 4.6, though.

    P.S. I'm not nitpicking, it's just that if it's not safe, I'll have too change a lot of code.

  26. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use more CPU cores

    Quote Originally Posted by spud View Post
    Ok, fair enough. I can't see that start() calls any virtual methods in Qt 4.6, though.
    It calls QThreadPrivate::start() which in turn calls QThread::run() (which is virtual).

    P.S. I'm not nitpicking, it's just that if it's not safe, I'll have too change a lot of code.
    As I said, in 99% of the cases it will work without problems. But if start subclassing and adding variables, something might explode.

Similar Threads

  1. Determining no. of CPU cores available
    By cnbp173 in forum Qt Programming
    Replies: 3
    Last Post: 21st April 2009, 17:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.