-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_parallel_pi_v3.c
More file actions
52 lines (45 loc) · 1.32 KB
/
2_parallel_pi_v3.c
File metadata and controls
52 lines (45 loc) · 1.32 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
/*
This program will numerically compute the integral of
4/(1+x*x)
from 0 to 1. The value of this integral is pi -- which
is great since it gives us an easy way to check the answer.
The is the original sequential program. It uses the timer
from the OpenMP runtime library
Use synchronization to prevent false sharing and also prevent use of padding
*/
#include <stdio.h>
#include <omp.h>
#define NUM_THREADS 12
static long num_steps = 1000000000;
double step;
int main()
{
int i, nthreads;
double pi, sum[NUM_THREADS];
double start_time, run_time;
step = 1.0 / (double)num_steps;
omp_set_num_threads(NUM_THREADS);
start_time = omp_get_wtime();
#pragma omp parallel
{
#pragma omp single
printf(" num_threads = %d", omp_get_num_threads());
int nthrds, i, id;
double x, sum;
id = omp_get_thread_num();
nthrds = omp_get_num_threads();
if (id == 0)
nthreads = nthrds;
for (i = id, sum = 0.0; i <= num_steps; i += nthrds)
{
x = (i + 0.5) * step;
sum += 4.0 / (1.0 + x * x);
}
#pragma omp critical
pi = pi + sum;
}
pi = pi * step;
run_time = omp_get_wtime() - start_time;
printf("\n pi with %ld steps is %lf in %lf seconds\n ", num_steps, pi, run_time);
return 0;
}