-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegral.cpp
More file actions
36 lines (30 loc) · 789 Bytes
/
integral.cpp
File metadata and controls
36 lines (30 loc) · 789 Bytes
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
#include <iostream>
#include <omp.h>
using namespace std;
static long num_steps = 100000000;
double step;
#define NUM_THREADS 4
int main()
{
double before = omp_get_wtime();
double pi = 0.0;
step = 1.0 / (double)num_steps;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel
{
int id, nthrds;
double x, sum = 0.0;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
for (int i = id; i < num_steps; i = i + nthrds)
{
x = (i + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}
#pragma omp critical
pi += step * sum;
}
printf("\nValue of pi : %lf\n", pi);
double after = omp_get_wtime();
printf("Time elapsed(after parallelization): %lf\n", after - before);
}