Equal execution of time for working with 4 and 8 threads

I am testing code using OpenMP. There he is:

#include <chrono>
#include <iostream>
#include <omp.h>

#define NUM_THREADS 8
#define ARR_SIZE 10000

class A {
private: 
    int a[ARR_SIZE];
public:
    A() {
        for (int i = 0; i < ARR_SIZE; i++)
            a[i] = i;
    }
// <<-----------MAIN CODE HERE--------------->
    void fn(A &o1, A &o2) {
        int some = 0;
        #pragma omp parallel num_threads(NUM_THREADS)
        {
            #pragma omp for reduction(+:some)
            for (int i = 0; i < ARR_SIZE; i++) {
                for (int j = 0; j < ARR_SIZE; j++)
                    some += o1.a[i] * o2.a[j];
            }
        }
        std::cout << some <<std::endl;
    }
};

int main() {
    A a,b,c;
    auto start = std::chrono::high_resolution_clock::now();
    c.fn(a,b);
    auto end = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = end - start;
    std::cout << elapsed.count();
}

Lead time:

  • 1 thread: 0.233663 sec
  • 2 topics: 0.12449 sec
  • 4 threads: 0.0665889 sec
  • 8 threads: 0.0643735 sec

    As you can see, there is practically no difference between the execution of 4 and 8 threads. What could be causing this behavior? It would also be nice if you try this code on your computer;).

PS My processor:

Model:               Intel(R) Core(TM) i7-4710HQ CPU @ 2.50GHz 
CPU(s):              8
On-line CPU(s) list: 0-7
Thread(s) per core:  2
Core(s) per socket:  4
Socket(s):           1
+4
source share
1 answer

4 . hyperthreading , ​​ "" , ( , ). , , , . , , 2x, , . 0 0,3x, .

4 - , , . 8 , .

+4

Source: https://habr.com/ru/post/1695995/


All Articles