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;
}
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:
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
source
share