Results 1 to 10 of 10

Thread: Converting OpenMP sections to QThreads

  1. #1
    Join Date
    Nov 2011
    Posts
    9
    Thanks
    1
    Platforms
    Windows

    Default Converting OpenMP sections to QThreads

    I am using a code that's written by somebody else, where he used OpenMP for Multi-Threading.

    So I found out, that you can't use OpenMP threads and QThreads at the same time. That means I have to possible ways to make my GUI work:

    1. Write my own BackgroundWorker, so that my GUI still works, when executing this function after a button click.
    2. Rewrite my function, so that it is done in QThreads.

    My problem with rewriting to QThreads is, that the code was not written very well:

    It only has one class, with a lot of variables and functions, where the variables are changed inside the functions and across different sections.
    The threading does something like this.
    Qt Code:
    1. #pragma omp parallel sections
    2. {
    3. #pragma omp section
    4. { ....
    5. }
    6. #pragma omp section
    7. { ....
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    So my question is:
    What is easier, writing a BackgroundWorker or rewriting the function?
    Second, could you roughly outline a solution?

    Thanks very much
    schludy

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

    Default Re: Converting OpenMP sections to QThreads

    Could you explain why your GUI won't work with OpenMP? What do you want to use threads for in your GUI?
    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
    Nov 2011
    Posts
    9
    Thanks
    1
    Platforms
    Windows

    Default Re: Converting OpenMP sections to QThreads

    The function that I'm calling runs in a loop for about 100 seconds and controls a robot. I want to be able to stop the process with the GUI and maybe output and modify some parameters.
    I tried run the function as a QThread, but when the program gets to the #pragma command, I get a SIGSEGV Error. So I guessed that you can't create new OpenMP threads in a QThread.

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

    Default Re: Converting OpenMP sections to QThreads

    Maybe you don't need the loop? How does your loop look like?
    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
    Nov 2011
    Posts
    9
    Thanks
    1
    Platforms
    Windows

    Default Re: Converting OpenMP sections to QThreads

    I have 3 parallel sections:
    Image information from the robot are extractet and processed.
    The robot computes new signals.
    Communication with the robot.

    This is from implementation in VS, with a background worker.
    Qt Code:
    1. #pragma omp section
    2. {
    3. while ( (iFrame<=EndFrame) && (time<timeMax) )&& !worker->CancellationPending) {
    4. ...
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    The robot needs about 100 seconds to complete the task, so I think I can't get rid of the loop.
    The program is quite big and confidential, so I can't really post a lot.

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

    Default Re: Converting OpenMP sections to QThreads

    This while loop can safely be removed in favour of the Step-by-Step solution mentioned in this article: [wiki]Keeping the GUI Responsive[/wiki].
    Last edited by wysota; 17th January 2012 at 16:17.
    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
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Converting OpenMP sections to QThreads

    If you are running on windows and using mingw 4.4 (mingw coming with Qt distribution) this link
    http://gcc.gnu.org/bugzill/show_bug.cgi?id=42616
    shows an error report on mingw 4.4 crashing when using openmp from a pthread. Maybe that's your crash too.

  8. #8
    Join Date
    Nov 2011
    Posts
    9
    Thanks
    1
    Platforms
    Windows

    Default Re: Converting OpenMP sections to QThreads

    Ok, so it might just be a bug? I thought this is just not possible.

    I use 32-bit Windows 7, and for MinGW I just used the one from QtSDK, that I downloaded with QtCreator.
    I installed the newest version of MinGW and changed the System path to the new folder. Is that everything that I have to do, to use the newer MinGW. In Qt it still says MinGW4.4 ...

    Anyways, it still crashes, at the line with the first #pragma omp.

    I created a testcase, that is close to my program:

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void on_pushButton_clicked();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "worker.h"
    4. #include <QThread>
    5.  
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_pushButton_clicked()
    20. {
    21. worker* wThread = new worker(this);
    22. wThread->start();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef WORKER_H
    2. #define WORKER_H
    3.  
    4. #include <QtCore>
    5. #include <QThread>
    6.  
    7. class worker : public QThread
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit worker(QObject *parent = 0);
    13.  
    14. protected:
    15. void run();
    16.  
    17. };
    18.  
    19. #endif // WORKER_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "worker.h"
    2. #include "threads.h"
    3. #include <QtCore>
    4. #include <QThread>
    5.  
    6. worker::worker(QObject *parent) :
    7. QThread(parent)
    8. {
    9. }
    10.  
    11. void worker::run()
    12. {
    13. threads thr;
    14. thr.startThreads();
    15. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef THREADS_H
    2. #define THREADS_H
    3.  
    4. class threads
    5. {
    6. public:
    7. void startThreads();
    8. };
    9.  
    10. #endif // THREADS_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "threads.h"
    2. #include <QDebug>
    3. #include <omp.h>
    4. #include <iostream>
    5.  
    6. using namespace std;
    7.  
    8. void threads::startThreads()
    9. {
    10.  
    11. bool finished = false;
    12.  
    13. #pragma omp parallel sections
    14. {
    15. #pragma omp section
    16. {
    17. int a=0;
    18. char outBuffer[500];
    19. while(a<1000){
    20. sprintf(outBuffer,"a: %d \n", a);
    21. qDebug( outBuffer);
    22. a++;
    23. }
    24. finished = true;
    25.  
    26. }
    27. #pragma omp section
    28. {
    29. int b=0;
    30. char outBuffer[500];
    31. while( !finished ){
    32. sprintf(outBuffer,"b: %d \n", b);
    33. qDebug( outBuffer);
    34. b++;
    35. }
    36. }
    37. }
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Converting OpenMP sections to QThreads

    I tried your example on my machine (dual core, Windows 7 64 bit, TDM mingw 32/64, version 4.5.2, Qt version 4.7.3) and it worked without problems in a 64 bit build. Unfortunately, it crashed when running the 32 bit build.

    32 bit build also crashed when simplifying the StartThreads method to
    Qt Code:
    1. #pragma omp parallel
    2. {
    3. cout << "id = " << omp_get_thread_num() << endl;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Again, 64 bit build worked fine.

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

    Default Re: Converting OpenMP sections to QThreads

    Why don't you just get rid of this extra pthread?
    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.


Similar Threads

  1. OpenMP problems with Qt Creator
    By Coocos in forum Qt Tools
    Replies: 1
    Last Post: 29th November 2010, 14:12
  2. Qt + OpenMP missing pthreadGC2.dll
    By Sanuden in forum Installation and Deployment
    Replies: 1
    Last Post: 30th March 2010, 14:13
  3. Progress Bar set value inside OpenMP parallel for
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2010, 19:51
  4. QThread and OpenMP on Mac problem
    By Debilski in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2009, 17:41
  5. Replies: 4
    Last Post: 19th April 2007, 13:17

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.