-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdot_product.cpp
220 lines (186 loc) · 4.13 KB
/
dot_product.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <omp.h>
using namespace std;
int main ( int argc, char *argv[] );
double test01 ( int n, double x[], double y[] );
double test02 ( int n, double x[], double y[] );
double test03 ( int n, double x[], double y[] );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for DOT_PRODUCT.
//
// Discussion:
//
// This program illustrates how a vector dot product could be set up
// in a FORTRAN90 program using OpenMP.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 April 2009
//
// Author:
//
// John Burkardt
//
{
double factor;
int i;
int n;
double wtime;
double *x;
double xdoty;
double *y;
cout << "\n";
cout << "DOT_PRODUCT\n";
cout << " C++/OpenMP version\n";
cout << "\n";
cout << " A program which computes a vector dot product.\n";
cout << "\n";
cout << " Number of processors available = " << omp_get_num_procs ( ) << "\n";
cout << " Number of threads = " << omp_get_max_threads ( ) << "\n";
//
// Set up the vector data.
// N may be increased to get better timing data.
//
// The value FACTOR is chosen so that the correct value of the dot product
// of X and Y is N.
//
n = 100;
while ( n < 10000000 )
{
n = n * 10;
x = new double[n];
y = new double[n];
factor = ( double ) ( n );
factor = 1.0 / sqrt ( 2.0 * factor * factor + 3 * factor + 1.0 );
for ( i = 0; i < n; i++ )
{
x[i] = ( i + 1 ) * factor;
}
for ( i = 0; i < n; i++ )
{
y[i] = ( i + 1 ) * 6 * factor;
}
cout << "\n";
//
// Test #1
//
wtime = omp_get_wtime ( );
xdoty = test01 ( n, x, y );
wtime = omp_get_wtime ( ) - wtime;
cout << " Sequential"
<< " " << setw(8) << n
<< " " << setw(14) << xdoty
<< " " << setw(15) << wtime << "\n";
//
// Test #2
//
wtime = omp_get_wtime ( );
xdoty = test02 ( n, x, y );
wtime = omp_get_wtime ( ) - wtime;
cout << " Parallel "
<< " " << setw(8) << n
<< " " << setw(14) << xdoty
<< " " << setw(15) << wtime << "\n";
delete [] x;
delete [] y;
}
//
// Terminate.
//
cout << "\n";
cout << "DOT_PRODUCT\n";
cout << " Normal end of execution.\n";
return 0;
}
//****************************************************************************80
double test01 ( int n, double x[], double y[] )
//****************************************************************************80
//
// Purpose:
//
// TEST01 computes the dot product with no parallel processing directives.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 April 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the order of the vectors.
//
// Input, double X[N], Y[N], the vectors.
//
// Output, double TEST01, the dot product of X and Y.
//
{
int i;
double xdoty;
xdoty = 0.0;
for ( i = 0; i < n; i++ )
{
xdoty = xdoty + x[i] * y[i];
}
return xdoty;
}
//****************************************************************************80
double test02 ( int n, double x[], double y[] )
//****************************************************************************80
//
// Purpose:
//
// TEST02 computes the dot product with parallel processing directives.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 April 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the order of the vectors.
//
// Input, double X[N], Y[N], the vectors.
//
// Output, double TEST02, the dot product of X and Y.
//
{
int i;
double xdoty;
xdoty = 0.0;
# pragma omp parallel \
shared ( n, x, y ) \
private ( i )
# pragma omp for reduction ( + : xdoty )
for ( i = 0; i < n; i++ )
{
xdoty = xdoty + x[i] * y[i];
}
return xdoty;
}