-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomp_trapezoidal_v2.cpp
More file actions
60 lines (53 loc) · 1.34 KB
/
omp_trapezoidal_v2.cpp
File metadata and controls
60 lines (53 loc) · 1.34 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
56
57
58
59
60
#include <iostream>
#include <cstdlib>
#ifdef _OPENMP
#include <omp.h>
#endif
double Local_trap(double a, double b, int n);
double f(double const x);
int main(int argc, char* argv[])
{
double global_result = 0.0;
double a, b;
int n;
int thread_count;
thread_count = strtol(argv[1], NULL, 10);
printf("Enter a, b, and n\n");
scanf("%lf %lf %d", &a, &b, &n);
#pragma omp parallel num_threads(thread_count) \
reduction(+: global_result)
global_result += Local_trap(a, b, n);
printf("With n = %d trapezoids, our estimate\n", n);
printf("of the integral from %f to %f = %.14e\n",
a, b, global_result);
return 0;
} /* main */
double Local_trap(double a, double b, int n)
{
double h, x, my_result;
double locaL_a, locaL_b;
int i, local_n;
#ifdef _OPENMP
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
#else
int my_rank = 0;
int thread_count = 1;
#endif
h = (b-a)/n;
local_n = n/thread_count;
locaL_a = a + my_rank * local_n * h;
locaL_b = locaL_a + local_n * h;
my_result = (f(locaL_a) + f(locaL_b))/2.0;
for (i = 1; i <= local_n-1; ++i)
{
x = locaL_a + i*h;
my_result += f(x);
}
return my_result * h;
} /* Trap */
// function f: f(x) = x;
double f(double const x)
{
return x;
} /* f */