-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreaded_pi_calc_openmp.cpp
52 lines (40 loc) · 1.46 KB
/
threaded_pi_calc_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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <omp.h>
#include <fstream>
#include <vector>
#include <chrono>
const long num_steps = 100000000;
const int block_size = 1308080;
double step;
int main()
{
// Different number of threads to test
std::vector<int> threads = {1, 2, 4, 8, 12, 16, 32, 64};
std::ofstream resultsFile("Threaded_Pi_Calc_OpenMP_results.txt");
resultsFile << "Threads, Time taken (s)" << std::endl;
for (int numThreads : threads)
{
double pi, sum = 0.0;
step = 1.0 / (double)num_steps;
// Set the number of threads
omp_set_num_threads(numThreads);
// Start timing
std::cout << "Starting calculation with " << numThreads << " threads...\n";
auto startTime = std::chrono::high_resolution_clock::now();
// Perform the computation
#pragma omp parallel for schedule(dynamic, block_size) reduction(+:sum)
for (long i = 0; i < num_steps; i++)
{
double x = (i + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}
// End timing
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = endTime - startTime;
pi = step * sum;
std::cout << "Threads: " << numThreads << ", Time taken: " << duration.count() << " s, Calculated Pi: " << pi << "\n";
resultsFile << numThreads << ", " << duration.count() << std::endl;
}
resultsFile.close();
return 0;
}