-
Notifications
You must be signed in to change notification settings - Fork 3
/
omp_parallel_pi.C
52 lines (42 loc) · 1.45 KB
/
omp_parallel_pi.C
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
//---------------------------------------------------------------------
// This program is made to compute pi numerical value.
// It will numerically compute the integral of
//
// 4/(1+x*x)
//
// from 0 to 1 using variable number of steps.
// The value of this integral is pi. The knowing of pi is quite
// precise and the result can be compared to the "real" value.
//
// This parallel program uses omp parallel
// It uses a timer class defined in a separate header file.
//
// History:
// Written by Gabriele Gaetano Fronzé, 01/18.
// Based on Tim Mattson implementation, 11/99.
//---------------------------------------------------------------------
#include "StopWatch.h"
#include <omp.h>
#include <iostream>
#include "common.h"
int main()
{
StopWatch stopWatch;
double pi, sum = 0.0;
double step = 1.0/(double) common::num_steps; //x-step
int n_threads=1;
#pragma omp parallel
{
n_threads = omp_get_num_threads();
// Some changes have to be made:
// at the moment each thread performs the same operations, computing pi n_threads times
// TIP: work on the for loop ranges using the thread IDs
for (unsigned long long i=1; i<=common::num_steps; i++) {
double x = (i - 0.5) * step; //computing the x value
sum += 4.0 / (1.0 + x * x); //adding to the cumulus
}
}
pi = step * sum;
common::print_results(pi,n_threads);
return 0;
}