Results 1 to 11 of 11

Thread: QtConcurrent, i need advice

  1. #1
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Question QtConcurrent, i need advice

    I have a cycle what iterate 4294967295 times ((quint32)-1). And i want use QtConcurrent for use both cores. But i need use QProgressDialog who change progress each 1% (42949672,95). One thread calculate range 0-2147483647, secons thread 2147483648-4294967295. So i need create signal like step() what i can emit from each thread on each percernt operation. I need also have feature uses Cancel button on QProgressDialog what will be stop both threads. Can you get me some advice how i can do this? I think i must create QObject's, declare bool flag like isCanceled what check inside for(){} and using QtConcurrent::run(). I can't use QtConcurrent::map becouse 4294967295*4= ~17Gb RAM excluding container overhead. And i think i can't use QEventLoop inside thread like in QThread::run(){exec()} for respond on signals from main thread, becouse i use QtConcurrent instead QThread.

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

    Default Re: QtConcurrent, i need advice

    Have a look at QFutureWatcher api. I would use QtConcurrent::mapped() not ::run(). You can break your problem into subproblems to avoid problems with memory consumption.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: QtConcurrent, i need advice

    Quote Originally Posted by wysota View Post
    You can break your problem into subproblems to avoid problems with memory consumption.
    If i reserve ~20Mb this about 5242880 elements in each container and 819 calling QtConcurrent::mapped(). This is not overhead?
    ---
    I create class based on QObject and emit signal step() inside thread

    Qt Code:
    1. for (int i = 0, j = count/100*1; i < count; ++i, ++start) {
    2. if (!isCanceled()) {
    3. if (i % j) {
    4. emit step();
    5. }
    6. } else {
    7. break;
    8. }
    9. }
    10. deleteLater();
    11. return 0;
    To copy to clipboard, switch view to plain text mode 

    If connection QueuedConnection my QProgressDialog is freezed, if DirectConnection - all alright. Why?
    Last edited by SABROG; 28th December 2009 at 14:33.

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

    Default Re: QtConcurrent, i need advice

    Compared to what?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: QtConcurrent, i need advice

    Quote Originally Posted by wysota View Post
    Compared to what?
    Compared to solid cycle with signals.

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

    Default Re: QtConcurrent, i need advice

    There will be little overhead with memory footprint but you should get better (or comparable) overall performance.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: QtConcurrent, i need advice

    Anyway i'll do it with custom class based on QObject and my signals from thread working.

    Qt Code:
    1. for (int i = 0; i < idealThreadCount; ++i) {
    2. MyClass *myclass = new MyClass;
    3. connect(progress.data(), SIGNAL(canceled()), myclass, SLOT(cancel()));
    4. connect(myclass, SIGNAL(step()), this, SLOT(stepChanged()));
    5. QFuture <void> future = QtConcurrent::run(myclass, &MyClass::run, offset, blockSize, (quint32)-1);
    6. //there is need place QFuture to outside container
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qint32 MyClass::run(quint32 start, qint32 count, quint32 total)
    2. {
    3. for (int i = 0, j = (total/100)*1; i < count; ++i, ++start) {
    4. if (!isCanceled()) {
    5. if (i % j == 0) {
    6. emit step();
    7. qDebug() << j << i << start << count << QThread::currentThreadId() << thread();
    8. }
    9. } else {
    10. break;
    11. }
    12. }
    13. deleteLater();
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 

    There is another problem, how to distribute count of operation on cores if devide operation on cores don't get integer without remainder. So i need somehow distribute this remainder on operations.

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

    Default Re: QtConcurrent, i need advice

    It's better to have a queue filled with input data which the threads will read. Just don't place all the data into the queue at once, it won't fit there. You can implement it with an extra thread acting as a producer with wait conditions acting as guards over the data buffer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: QtConcurrent, i need advice

    I don't need only one thread, becouse i need do calculation on all cores synchronously. How i say before, one thread calculate 0-10 range, second thread 11-20 range. So i need two producers? For each thread?

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

    Default Re: QtConcurrent, i need advice

    I meant two calculating threads plus an extra thread acting as a producer for the queue feeding both calculators. The producer sleeps until the queue runs out of data and is signaled (using a wait condition) to produce more data before going to sleep again.

    A rough code for the producer:
    Qt Code:
    1. QQueue<int> buffer;
    2. QWaitCondition condition;
    3. QMutex mutex;
    4.  
    5. class Producer : public QThread {
    6. public:
    7. Producer() : QThread() { m_last = 0; }
    8. void run() {
    9. while(m_last<MAX_INT){
    10. mutex.lock();
    11. produce();
    12. condition.wait(&mutex);
    13. }
    14. }
    15. void produce() {
    16. for(int i=0;i<65535;i++) queue.enqueue(++m_last);
    17. }
    18. private:
    19. int m_last;
    20. };
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Apr 2008
    Location
    Russia, Moscow
    Posts
    86
    Thanks
    2
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4

    Default Re: QtConcurrent, i need advice

    I solved my task himself, with signals and slots and using QtConcurrent::run().

Similar Threads

  1. Replies: 11
    Last Post: 1st February 2018, 04:27
  2. Need advice for a best way to make this application
    By frenk_castle in forum Newbie
    Replies: 3
    Last Post: 25th September 2009, 18:55
  3. QtConcurrent and QPainter
    By AwDogsgo2Heaven in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 08:35
  4. Model / Threading Advice
    By tntcoda in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2008, 14:02
  5. QtConcurrent Performance
    By tomf in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2008, 15:41

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.