-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi_task.c
More file actions
33 lines (31 loc) · 847 Bytes
/
pi_task.c
File metadata and controls
33 lines (31 loc) · 847 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
#include <stdio.h>
#include <time.h>
#include <omp.h>
#define INTERVALS 1000000
int main(int argc, char **argv)
{
long int i, intervals = INTERVALS;
double x, dx, f, sum, pi;
sum = 0.0;
dx = 1.0 / (double) intervals;
#pragma omp parallel shared(sum, pi) private(x, f, i) firstprivate(dx)
{
#pragma omp single
{
#pragma omp taskgroup task_reduction(+: sum)
{
for (i = 1; i <= intervals; i++) {
#pragma omp task firstprivate(dx) private(x,f) in_reduction(+: sum)
{
x = dx * ((double) (i - 0.5));
f = 4.0 / (1.0 + x*x);
sum = sum + f;
}
}
}
pi = dx*sum;
printf("pi = %f \n", pi);
}
}
return 0;
}