-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenmp.cpp
32 lines (28 loc) · 1.02 KB
/
openmp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Simple example of OpenMP
#include <math.h>
#include <time.h>
#include <omp.h>
#include <iostream>
static const int sSineCount = 1024 * 1024 * 16;
static volatile double sManySines[sSineCount];
int main() {
// try different #s of threads
for(int threads = 1; threads <= 4; threads++) {
omp_set_num_threads(threads); // set thread count (thread creation happens here!)
for(int iters = 0; iters < 5; iters++) { // 5 iterations of each config
clock_t start = clock(); // check the start time
#pragma omp parallel for schedule(dynamic, 1024 * 16) // parallel!
for(int i = 0; i < sSineCount; i++) { // fill the array
double d = (double)i / (double)sSineCount;
sManySines[i] = sin(d);
}
clock_t diff = clock() - start; // calc elapsed time
std::cout << threads << " threads took " << (diff * 1000) / CLOCKS_PER_SEC << " ms";
if(iters == 0 && threads != 1)
std::cout << " (adding new thread)";
std::cout << "\n";
}
}
std::cout << "done!\n";
return 0;
}