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. Why QThread cannot speedup my program on Intel core 2 duo Q6700

Why QThread cannot speedup my program on Intel core 2 duo Q6700

Scheduled Pinned Locked Moved General and Desktop
12 Posts 6 Posters 6.3k 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.
  • P Offline
    P Offline
    PigiRoN
    wrote on last edited by
    #1

    I wrote a simple program computing pi, which uses integration 4 / (1 + x * x) for x from 0 to 1.

    @// The serial code:
    double computePi(){
    int n = 2000000000;

    double result = 0.0;
    
    double temp;
    double step = (double)1.0 / n;
    
    for (int i = 0; i < n; i++){
        temp = step * i;
        result += 4 / (1 + temp * temp);
    }
    result *= step;
    return result;
    

    } // it cost 23.618s on Q6700

    // Multithread code:
    class ThreadPi : public QThread{

    Q_OBJECT

    public:
    void setData(int index, int p, int n);
    void addResult(double &adding);

    protected:
    void run();

    private:
    int index;
    int p;
    int n;

    double result;
    

    };

    void ThreadPi::setData(int index, int p, int n){
    this->index = index;
    this->p = p;
    this->n = n;
    }

    void ThreadPi::addResult(double &adding){
    adding += result;
    }

    void ThreadPi::run(){
    int i;
    result = 0;
    int interval = n / p;
    int startpoint = index * interval;
    double temp;
    double step = (double)1.0 / n;
    for (i = 0; i < interval; i++){
    temp = step * (i + startpoint);
    result += 4 / (1 + temp * temp);
    }
    result *= step;
    }
    class QMulThreadPi : public QObject{
    Q_OBJECT

    public:
    QMulThreadPi();
    ~QMulThreadPi();

    double Result();
    void Compute();
    

    private:
    int p;
    int n;
    ThreadPi *TP;
    double result;
    };
    QMulThreadPi::QMulThreadPi(){
    p = 4;
    n = 2000000000;

    TP = new ThreadPi[p];
    
    result = 0;
    

    }

    QMulThreadPi::~QMulThreadPi(){
    delete [] TP;
    }

    double QMulThreadPi::Result(){
    return result;
    }

    void QMulThreadPi::Compute(){
    for (int i = 0; i < p; i++){
    TP[i].setData(i, p, n);
    TP[i].start();
    }
    for (int i = 0; i < p; i++){
    TP[i].wait();
    TP[i].addResult(result);
    }
    }
    // it cost 32.324s on Q6700@
    However, on my laptop(Intel core 2 duo T9300) the serial code cost 16.532s, while the multithread code only cost 10.137s.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      Please update your code with code tags (@) so it becomes readable.

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PigiRoN
        wrote on last edited by
        #3

        [quote author="Franzk" date="1282279430"]Please update your code with code tags (@) so it becomes readable.[/quote]

        Thanks for your remind. it's my first time starting a discussion.

        i tried it in another i7 920 computer currently. It works well and four threads made up 2.82x speedup. So i think it's Q6700's problem.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          And what times are on Q6700? I can't believe that something that parallels ok at T9300 and i7 strangely breaks at Q6700.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            PigiRoN
            wrote on last edited by
            #5

            [quote author="Denis Kormalev" date="1282287379"]And what times are on Q6700? I can't believe that something that parallels ok at T9300 and i7 strangely breaks at Q6700.[/quote]

            Serial code cost 23s, while parallel code cost 32s. I tried it many times.

            But when i increase p from 4 to 32 or 64 on Q6700, it will speedup 3.7x...

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tobias.hunger
              wrote on last edited by
              #6

              How do you actually run the multithreaded code? Are you doing something stupid there that forces a serialization again?

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mario
                wrote on last edited by
                #7

                @PigiRoN: Little OT, but just out of curiosity...

                Would it be possible to use "QtConcurrent::mappedReduced":http://doc.qt.nokia.com/4.6/qtconcurrentmap.html#mappedReduced instead of almost all the code you have written?

                I think that the "QtConcurrent":http://doc.qt.nokia.com/4.6/threads-qtconcurrent.html module contains a lot of goodies for doing this kind of stuff.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tobias.hunger
                  wrote on last edited by
                  #8

                  mario: Yeap, that is what I would have tried, too. Let's try to help with the current solution though.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ixSci
                    wrote on last edited by
                    #9

                    There are can be really many causes for this behaviour. It should be investigated by yourself which one you have encountered. It can be bind with the cache related issues(missing, races etc.) and it can be just an overhead of using thread. The best answer you can get from the thorough profiling of your code.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DenisKormalev
                      wrote on last edited by
                      #10

                      [quote author="ixSci" date="1282324950"]There are can be really many causes for this behaviour. It should be investigated by yourself which one you have encountered. It can be bind with the cache related issues(missing, races etc.) and it can be just an overhead of using thread. The best answer you can get from the thorough profiling of your code.
                      [/quote]

                      More strange that it works ok for author at another multicore processors. So source architecture problems and possible race conditions are hardly the reason.

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        ixSci
                        wrote on last edited by
                        #11

                        Different processors have different cache size and that may be a problem source.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          PigiRoN
                          wrote on last edited by
                          #12

                          [quote author="Denis Kormalev" date="1282326556"][quote author="ixSci" date="1282324950"]There are can be really many causes for this behaviour. It should be investigated by yourself which one you have encountered. It can be bind with the cache related issues(missing, races etc.) and it can be just an overhead of using thread. The best answer you can get from the thorough profiling of your code.
                          [/quote]

                          More strange that it works ok for author at another multicore processors. So source architecture problems and possible race conditions are hardly the reason. [/quote]

                          As long as increasing number of threads to 64, it will speedup 1.98x on dual core cpu or 3.79x on quad core cpu stably.

                          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