-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathomp_trap1.c
More file actions
120 lines (101 loc) · 3.38 KB
/
Copy pathomp_trap1.c
File metadata and controls
120 lines (101 loc) · 3.38 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* File: omp_trap1.c
* Purpose: Estimate definite integral (or area under curve) using trapezoidal
* rule.
*
* Input: a, b, n
* Output: estimate of integral from a to b of f(x)
* using n trapezoids.
*
* Compile: gcc -g -Wall -fopenmp -o omp_trap1 omp_trap1.c
* Usage: ./omp_trap1 <number of threads>
*
* Notes:
* 1. The function f(x) is hardwired.
* 2. In this version, each thread explicitly computes the integral
* over its assigned subinterval, a critical directive is used
* for the global sum.
* 3. This version assumes that n is evenly divisible by the
* number of threads
*
* IPP: Section 5.2.1 (pp. 216 and ff.)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
void Usage(char* prog_name);
double f(double x); /* Function we're integrating */
double Trap(double a, double b, int n);
int main(int argc, char* argv[]) {
double global_result = 0.0; /* Store result in global_result */
double a, b; /* Left and right endpoints */
int n; /* Total number of trapezoids */
int thread_count;
double start, end;
if (argc != 5) Usage(argv[0]);
thread_count = strtol(argv[1], NULL, 10);
a = atof(argv[2]);
b = atof(argv[3]);
n = strtol(argv[4], NULL, 10);
if (n % thread_count != 0) Usage(argv[0]);
start = omp_get_wtime();
double tmp = Trap(a, b, n);
global_result += tmp;
end = omp_get_wtime();
printf("Time: %.5f\n", (end-start));
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 */
/*--------------------------------------------------------------------
* Function: Usage
* Purpose: Print command line for function and terminate
* In arg: prog_name
*/
void Usage(char* prog_name) {
fprintf(stderr, "usage: %s <#threads> <left> <right> <#trapezoid>\n", prog_name);
fprintf(stderr, " number of trapezoids must be evenly divisible by\n");
fprintf(stderr, " number of threads\n");
exit(0);
} /* Usage */
/*------------------------------------------------------------------
* Function: f
* Purpose: Compute value of function to be integrated
* Input arg: x
* Return val: f(x)
*/
double f(double x) {
double return_val;
return_val = x*x;
return return_val;
} /* f */
/*------------------------------------------------------------------
* Function: Trap
* Purpose: Use trapezoidal rule to estimate definite integral
* Input args:
* a: left endpoint
* b: right endpoint
* n: number of trapezoids
* Output arg:
* integral: estimate of integral from a to b of f(x)
*/
double Trap(double a, double b, int n) {
double h, x = 0.0, my_result;
double local_a, local_b;
int i=0, local_n;
int my_rank = omp_get_thread_num();
int thread_count = omp_get_num_threads();
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;
#pragma omp parallel reduction(+:my_result) default(none) shared(h, local_n, local_a, local_b) firstprivate(x,i)
for (i = 1; i <= local_n-1; i++) {
x = local_a + i*h;
my_result += f(x);
}
my_result = my_result*h;
return ( my_result);
} /* Trap */