Created
January 2, 2015 17:11
-
-
Save richard1122/4ed5f28e5e68313b4570 to your computer and use it in GitHub Desktop.
OpenMP for PI calculation test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output on HP DV6 7002tx
I7 3612QM
Parallel 8 cores:4
Parallel 4 cores:8
Parallel 2 cores:17
Serial: 32