Skip to content

Instantly share code, notes, and snippets.

@richard1122
Created January 2, 2015 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richard1122/4ed5f28e5e68313b4570 to your computer and use it in GitHub Desktop.
Save richard1122/4ed5f28e5e68313b4570 to your computer and use it in GitHub Desktop.
OpenMP for PI calculation test
#include <iostream>
#include <omp.h>
#include <ctime>
using namespace std;
#define N 3000000000L
int main() {
omp_set_num_threads(8);
auto c = time(NULL);
double pi = 0;
omp_set_num_threads(8);
#pragma omp parallel for reduction(+: pi)
for (long i = 0; i < N; ++i) {
pi += 1 / (double) (i * 2 + 1) * (i & 1 ? -1 : 1);
}
cout << "Parallel 8 cores:" << (time(NULL) - c) << endl;
pi = 0;
c = time(NULL);
omp_set_num_threads(4);
#pragma omp parallel for reduction(+: pi)
for (long i = 0; i < N; ++i) {
pi += 1 / (double) (i * 2 + 1) * (i & 1 ? -1 : 1);
}
cout << "Parallel 4 cores:" << (time(NULL) - c) << endl;
pi = 0;
c = time(NULL);
omp_set_num_threads(2);
#pragma omp parallel for reduction(+: pi)
for (long i = 0; i < N; ++i) {
pi += 1 / (double) (i * 2 + 1) * (i & 1 ? -1 : 1);
}
cout << "Parallel 2 cores:" << (time(NULL) - c) << endl;
pi = 0;
c = time(NULL);
for (long i = 0; i < N; ++i) {
pi += 1 / (double) (i * 2 + 1) * (i & 1 ? -1 : 1);
}
cout << "Serial: " << (time(NULL) - c) << endl;
return 0;
}
@richard1122
Copy link
Author

output on HP DV6 7002tx
I7 3612QM

Parallel 8 cores:4
Parallel 4 cores:8
Parallel 2 cores:17
Serial: 32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment