-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjacobi_openmp.c
192 lines (167 loc) · 3.27 KB
/
jacobi_openmp.c
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
# include <math.h>
# include <stdio.h>
# include <stdlib.h>
int main ( );
/******************************************************************************/
int main ( )
/******************************************************************************/
/*
Purpose:
MAIN is the main program for JACOBI_OPENMP.
Discussion:
JACOBI_OPENMP carries out a Jacobi iteration with OpenMP.
Licensing:
This code is distributed under the GNU LGPL license.
Modified:
31 January 2017
Author:
John Burkardt
*/
{
double *b;
double d;
int i;
int it;
int m;
int n;
double r;
double t;
double *x;
double *xnew;
m = 5000;
n = 50000;
b = ( double * ) malloc ( n * sizeof ( double ) );
x = ( double * ) malloc ( n * sizeof ( double ) );
xnew = ( double * ) malloc ( n * sizeof ( double ) );
printf ( "\n" );
printf ( "JACOBI_OPENMP:\n" );
printf ( " C/OpenMP version\n" );
printf ( " Jacobi iteration to solve A*x=b.\n" );
printf ( "\n" );
printf ( " Number of variables N = %d\n", n );
printf ( " Number of iterations M = %d\n", m );
printf ( "\n" );
printf ( " IT l2(dX) l2(resid)\n" );
printf ( "\n" );
# pragma omp parallel private ( i )
{
/*
Set up the right hand side.
*/
# pragma omp for
for ( i = 0; i < n; i++ )
{
b[i] = 0.0;
}
b[n-1] = ( double ) ( n + 1 );
/*
Initialize the solution estimate to 0.
Exact solution is (1,2,3,...,N).
*/
# pragma omp for
for ( i = 0; i < n; i++ )
{
x[i] = 0.0;
}
}
/*
Iterate M times.
*/
for ( it = 0; it < m; it++ )
{
# pragma omp parallel private ( i, t )
{
/*
Jacobi update.
*/
# pragma omp for
for ( i = 0; i < n; i++ )
{
xnew[i] = b[i];
if ( 0 < i )
{
xnew[i] = xnew[i] + x[i-1];
}
if ( i < n - 1 )
{
xnew[i] = xnew[i] + x[i+1];
}
xnew[i] = xnew[i] / 2.0;
}
/*
Difference.
*/
d = 0.0;
# pragma omp for reduction ( + : d )
for ( i = 0; i < n; i++ )
{
d = d + pow ( x[i] - xnew[i], 2 );
}
/*
Overwrite old solution.
*/
# pragma omp for
for ( i = 0; i < n; i++ )
{
x[i] = xnew[i];
}
/*
Residual.
*/
r = 0.0;
# pragma omp for reduction ( + : r )
for ( i = 0; i < n; i++ )
{
t = b[i] - 2.0 * x[i];
if ( 0 < i )
{
t = t + x[i-1];
}
if ( i < n - 1 )
{
t = t + x[i+1];
}
r = r + t * t;
}
# pragma omp master
{
if ( it < 10 || m - 10 < it )
{
printf ( " %8d %14.6g %14.6g\n", it, sqrt ( d ), sqrt ( r ) );
}
if ( it == 9 )
{
printf ( " Omitting intermediate results.\n" );
}
}
}
}
/*
Write part of final estimate.
*/
printf ( "\n" );
printf ( " Part of final solution estimate:\n" );
printf ( "\n" );
for ( i = 0; i < 10; i++ )
{
printf ( " %8d %14.6g\n", i, x[i] );
}
printf ( "...\n" );
for ( i = n - 11; i < n; i++ )
{
printf ( " %8d %14.6g\n", i, x[i] );
}
/*
Free memory.
*/
free ( b );
free ( x );
free ( xnew );
/*
Terminate.
*/
printf ( "\n" );
printf ( "JACOBI_OPENMP:\n" );
printf ( " Normal end of execution.\n" );
return 0;
}