-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi1.c
More file actions
56 lines (41 loc) · 1.07 KB
/
Copy pathpi1.c
File metadata and controls
56 lines (41 loc) · 1.07 KB
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
53
54
55
/*
This program computes the value of "pi".
Run as "time ./pi1".
Output should be something like:
Pi is 3.141593 <-- (output from pi1.c)
29.7u 0.0s 0:29 99% 0+0k 0+0io 0pf+0w <-- (output from time command)
^ ^ ^
| | (elapsed real runtime used by our program)
| (CPU-seconds used by system on behalf of user program)
(CPU-seconds used directly by user program)
*/
/* This version uses parallelization directives */
#include <stdio.h>
#include <omp.h>
const long num_intervals = 1000000000;
double f( double a )
{
return (4.0 / (1.0 + a*a));
}
main()
{
double h, sum, partial_sum, x, pi;
long i;
h = 1.0 / (double)num_intervals;
sum = 0.0;
#pragma omp parallel private(i,x,partial_sum) num_threads(8)
{
partial_sum = 0.0;
#pragma omp for schedule(dynamic)
for (i = 0; i < num_intervals; i++) {
x = h * ((double)i + 0.5);
partial_sum += f(x);
}
#pragma omp critical
{
sum += partial_sum;
}
}
pi = h * sum;
printf( "Pi is %f\n", pi );
}