-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathquad2d_openmp.cpp
142 lines (119 loc) · 2.96 KB
/
quad2d_openmp.cpp
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
# include <omp.h>
using namespace std;
int main ( int argc, char *argv[] );
double f ( double x, double y );
double cpu_time ( );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for QUAD2D_OPENMP.
//
{
double a;
double b;
double error;
double exact;
int i;
int j;
int n;
int nx;
int ny;
double pi;
double total;
double wtime;
double x;
double y;
a = 0.0;
b = 1.0;
nx = 32768;
ny = 32768;
n = nx * ny;
pi = 3.141592653589793;
exact = pi * pi / 6.0;
cout << "\n";
cout << "QUAD2D_OPENMP:\n";
cout << " C++/OpenMP version\n";
cout << " Estimate the integral of f(x,y) over [0,1]x[0,1].\n";
cout << " f(x,y) = 1 / ( 1 - x * y ).\n";
cout << "\n";
cout << " A = " << a << "\n";
cout << " B = " << b << "\n";
cout << " NX = " << nx << "\n";
cout << " NY = " << ny << "\n";
cout << " N = " << n << "\n";
cout << " Exact = " << setprecision(16) << setw(24) << exact << "\n";
wtime = omp_get_wtime ( );
total = 0.0;
# pragma omp parallel shared ( a, b, n ) \
private ( i, j, x, y )
# pragma omp for reduction ( + : total )
for ( i = 1; i <= nx; i++ )
{
x = ( ( 2 * nx - 2 * i + 1 ) * a + ( 2 * i - 1 ) * b ) / ( 2 * nx );
for ( j = 1; j <= ny; j++ )
{
y = ( ( 2 * ny - 2 * j + 1 ) * a + ( 2 * j - 1 ) * b ) / ( 2 * ny );
total = total + f ( x, y );
}
}
wtime = omp_get_wtime ( ) - wtime;
total = ( b - a ) * ( b - a ) * total / ( double ) ( nx ) / ( double ) ( ny );
error = fabs ( total - exact );
cout << "\n";
cout << " Estimate = " << setprecision(16) << setw(24) << total << "\n";
cout << " Error = " << error << "\n";
cout << " Time = " << wtime << "\n";
//
// Terminate.
//
cout << "\n";
cout << "QUAD_OPENMP:\n";
cout << " Normal end of execution.\n";
return 0;
}
//****************************************************************************80
double f ( double x, double y )
//****************************************************************************80
//
// Purpose:
//
// F evaluates the function.
//
{
double value;
value = 1.0 / ( 1.0 - x * y );
return value;
}
//****************************************************************************80
double cpu_time ( )
//****************************************************************************80
//
// Purpose:
//
// CPU_TIME reports the elapsed CPU time.
//
// Modified:
//
// 27 September 2005
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double CPU_TIME, the current total elapsed CPU time in second.
//
{
double value;
value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC;
return value;
}