-
Notifications
You must be signed in to change notification settings - Fork 0
/
gmres.c
464 lines (373 loc) · 12.7 KB
/
gmres.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* GMRES.f -- translated by f2c (version of 20 August 1993 13:15:44).
You must link the resulting object file with the libraries:
-lf2c -lm (in that order)
*/
#include "f2c.h"
#include <stdio.h>
/* Table of constant values */
static integer c__1 = 1;
static doublereal c_b7 = -1.;
static doublereal c_b8 = 1.;
static doublereal c_b20 = 0.;
/* -- Iterative template routine --
* Univ. of Tennessee and Oak Ridge National Laboratory
* October 1, 1993
* Details of this algorithm are described in "Templates for the
* Solution of Linear Systems: Building Blocks for Iterative
* Methods", Barrett, Berry, Chan, Demmel, Donato, Dongarra,
* Eijkhout, Pozo, Romine, and van der Vorst, SIAM Publications,
* 1993. (ftp netlib2.cs.utk.edu; cd linalg; get templates.ps).
*
* Purpose
* =======
*
* GMRES solves the linear system Ax = b using the
* Generalized Minimal Residual iterative method with preconditioning.
*
* Convergence test: ( norm( b - A*x ) / norm( b ) ) < TOL.
* For other measures, see the above reference.
*
* Arguments
* =========
*
* N (input) INTEGER.
* On entry, the dimension of the matrix.
* Unchanged on exit.
*
* B (input) DOUBLE PRECISION array, dimension N.
* On entry, right hand side vector B.
* Unchanged on exit.
*
* X (input/output) DOUBLE PRECISION array, dimension N.
* On input, the initial guess; on exit, the iterated solution.
*
* RESTRT (input) INTEGER
* Restart parameter, <= N. This parameter controls the amount
* of memory required for matrix H (see WORK and H).
*
* WORK (workspace) DOUBLE PRECISION array, dimension (LDW,RESTRT+4).
*
* LDW (input) INTEGER
* The leading dimension of the array WORK. LDW >= max(1,N).
*
* H (workspace) DOUBLE PRECISION array, dimension (LDH,RESTRT+2).
* This workspace is used for constructing and storing the
* upper Hessenberg matrix. The two extra columns are used to
* store the Givens rotation matrices.
*
* LDH (input) INTEGER
* The leading dimension of the array H. LDH >= max(1,RESTRT+1).
*
* ITER (input/output) INTEGER
* On input, the maximum iterations to be performed.
* On output, actual number of iterations performed.
*
* RESID (input/output) DOUBLE PRECISION
* On input, the allowable convergence measure for
* norm( b - A*x ) / norm( b ).
* On output, the final value of this measure.
*
* MATVEC (external subroutine)
* The user must provide a subroutine to perform the
* matrix-vector product
*
* y := alpha*A*x + beta*y,
*
* where alpha and beta are scalars, x and y are vectors,
* and A is a matrix. Vector x must remain unchanged.
* The solution is over-written on vector y.
*
* The call is:
*
* CALL MATVEC( ALPHA, X, BETA, Y )
*
* The matrix is passed into the routine in a common block.
*
* PSOLVE (external subroutine)
* The user must provide a subroutine to perform the
* preconditioner solve routine for the linear system
*
* M*x = b,
*
* where x and b are vectors, and M a matrix. Vector b must
* remain unchanged.
* The solution is over-written on vector x.
*
* The call is:
*
* CALL PSOLVE( X, B )
*
* The preconditioner is passed into the routine in a common
* block.
*
* INFO (output) INTEGER
*
* = 0: Successful exit. Iterated approximate solution returned.
*
* > 0: Convergence to tolerance not achieved. This will be
* set to the number of iterations performed.
*
* < 0: Illegal input parameter.
*
* -1: matrix dimension N < 0
* -2: LDW < N
* -3: Maximum number of iterations ITER <= 0.
* -4: LDH < RESTRT
*
* BLAS CALLS: DAXPY, DCOPY, DDOT, DNRM2, DROT, DROTG, DSCAL
* ============================================================
*/
//*****************************************************************
int gmres_(n, b, x, restrt, work, ldw, h, ldh, iter, resid, matvec, psolve,
info)
integer *n, *restrt, *ldw, *ldh, *iter, *info;
doublereal *b, *x, *work, *h, *resid;
int (*matvec) (), (*psolve) ();
{
/* System generated locals */
integer work_dim1, work_offset, h_dim1, h_offset, i__1;
doublereal d__1;
/* Local variables */
// extern /* Subroutine */ int drot_();
static doublereal bnrm2;
extern doublereal dnrm2_();
static integer i, k, r, s, v, w, y;
extern /* Subroutine */ int dscal_(), basis_(), dcopy_(), drot_(), drotg_();
static integer maxit;
static doublereal rnorm, aa, bb;
static integer cs, av, sn;
extern /* Subroutine */ int update_();
extern /* Subroutine */ int basis_();
static doublereal tol;
/* Parameter adjustments */
printf("%d\t%f\t%f\t%d\t%d\t%d\t%d\t%f\n",*n,b[0],x[0],*restrt,*ldw,*ldh,*iter,*resid);
h_dim1 = *ldh;
h_offset = h_dim1 + 1;
h -= h_offset;
work_dim1 = *ldw;
work_offset = work_dim1 + 1;
work -= work_offset;
--x;
--b;
/* Executable Statements */
*info = 0;
/* Test the input parameters. */
if (*n < 0) {
*info = -1;
} else if (*ldw < max(1,*n)) {
*info = -2;
} else if (*iter <= 0) {
*info = -3;
} else if (*ldh < *restrt + 1) {
*info = -4;
}
if (*info != 0) {
return 0;
}
maxit = *iter;
tol = *resid;
/* Alias workspace columns. */
r = 1;
s = r + 1;
w = s + 1;
y = w;
av = y;
v = av + 1;
/* Store the Givens parameters in matrix H. */
cs = *restrt + 1;
sn = cs + 1;
/* Set initial residual (AV is temporary workspace here). */
//printf("llf%d\t%f\n",*n,b[1]);
//getchar();
dcopy_(n, &b[1], &c__1, &work[av * work_dim1 + 1], &c__1);
//for (i=0;i<10;i++) printf("lsf%e\t%e\n",work[av * work_dim1 + 1+i],work[av * work_dim1 + 1+i+work_dim1/2] );
//getchar();
if (dnrm2_(n, &x[1], &c__1) != 0.) {
printf("call matvec at line 207\n");
/* AV is temporary workspace here. */
dcopy_(n, &b[1], &c__1, &work[av * work_dim1 + 1], &c__1);
(*matvec)(&c_b7, &x[1], &c_b8, &work[av * work_dim1 + 1]);
// (*matvec)(&x[1],&work[av * work_dim1 + 1]);
}
(*psolve)(&work[r * work_dim1 + 1], &work[av * work_dim1 + 1]);
bnrm2 = dnrm2_(n, &b[1], &c__1);
if (bnrm2 == 0.) {
bnrm2 = 1.;
}
if (dnrm2_(n, &work[r * work_dim1 + 1], &c__1) / bnrm2 < tol) {
goto L70;
}
*iter = 0;
L10:
i = 0;
/* Construct the first column of V. */
dcopy_(n, &work[r * work_dim1 + 1], &c__1, &work[v * work_dim1 + 1], &
c__1);
rnorm = dnrm2_(n, &work[v * work_dim1 + 1], &c__1);
d__1 = 1. / rnorm;
dscal_(n, &d__1, &work[v * work_dim1 + 1], &c__1);
/* Initialize S to the elementary vector E1 scaled by RNORM. */
work[s * work_dim1 + 1] = rnorm;
i__1 = *n;
for (k = 2; k <= i__1; ++k) {
work[k + s * work_dim1] = 0.;
/* L20: */
}
L30:
++i;
++(*iter);
//int ii;
// for (ii=39000;ii<39010;ii++) printf("ggss %e\t%e\n",work[(v + i - 1) * work_dim1 + 1+ii],work[av * work_dim1 + 1+ii] );
//for(ii=0;ii<10;ii++) printf("work=%f, for i=%d\n",
// work[(v + i - 1) * work_dim1+1+ii],(v + i - 1) * work_dim1+1+ii);
//printf("call iteration at 253, index start at %d, last of work is %f\n",
// (v + i - 1) * work_dim1 + 1, work[(v + i - 1) * work_dim1 + 2*30000]);
//work[(v + i - 1) * work_dim1 + 1]=10;
(*matvec)(&c_b8, &work[(v + i - 1) * work_dim1 + 1], &c_b20, &work[av *
work_dim1 + 1]);
//(*matvec)(&work[(v + i - 1) * work_dim1 + 1], &work[av *work_dim1 + 1]);
//int ii;
// for (ii=39000;ii<39010;ii++) printf("llff %e\t%e\n",work[(v + i - 1) * work_dim1 + 1+ii],work[av * work_dim1 + 1+ii] );
//getchar();
(*psolve)(&work[w * work_dim1 + 1], &work[av * work_dim1 + 1]);
/* Construct I-th column of H orthnormal to the previous */
/* I-1 columns. */
basis_(&i, n, &h[i * h_dim1 + 1], &work[v * work_dim1 + 1], ldw, &work[w *
work_dim1 + 1]);
/* Apply Givens rotations to the I-th column of H. This */
/* "updating" of the QR factorization effectively reduces */
/* the Hessenberg matrix to upper triangular form during */
/* the RESTRT iterations. */
i__1 = i - 1;
for (k = 1; k <= i__1; ++k) {
drot_(&c__1, &h[k + i * h_dim1], ldh, &h[k + 1 + i * h_dim1], ldh, &h[
k + cs * h_dim1], &h[k + sn * h_dim1]);
/* L40: */
}
/* Construct the I-th rotation matrix, and apply it to H so that
*/
/* H(I+1,I) = 0. */
aa = h[i + i * h_dim1];
bb = h[i + 1 + i * h_dim1];
drotg_(&aa, &bb, &h[i + cs * h_dim1], &h[i + sn * h_dim1]);
drot_(&c__1, &h[i + i * h_dim1], ldh, &h[i + 1 + i * h_dim1], ldh, &h[i +
cs * h_dim1], &h[i + sn * h_dim1]);
/* Apply the I-th rotation matrix to [ S(I), S(I+1) ]'. This */
/* gives an approximation of the residual norm. If less than */
/* tolerance, update the approximation vector X and quit. */
drot_(&c__1, &work[i + s * work_dim1], ldw, &work[i + 1 + s * work_dim1],
ldw, &h[i + cs * h_dim1], &h[i + sn * h_dim1]);
*resid = (d__1 = work[i + 1 + s * work_dim1], abs(d__1)) / bnrm2;
printf("iteration no.=%d, error=%e\n",*iter,*resid);
if (*resid <= tol) {
update_(&i, n, &x[1], &h[h_offset], ldh, &work[y * work_dim1 + 1], &
work[s * work_dim1 + 1], &work[v * work_dim1 + 1], ldw);
goto L70;
}
if (*iter == maxit) {
goto L50;
}
if (i < *restrt) {
goto L30;
}
L50:
/* Compute current solution vector X. */
update_(restrt, n, &x[1], &h[h_offset], ldh, &work[y * work_dim1 + 1], &
work[s * work_dim1 + 1], &work[v * work_dim1 + 1], ldw);
/* Compute residual vector R, find norm, then check for tolerance.
*/
/* (AV is temporary workspace here.) */
dcopy_(n, &b[1], &c__1, &work[av * work_dim1 + 1], &c__1);
//printf("call matvec at line 327\n");
(*matvec)(&c_b7, &x[1], &c_b8, &work[av * work_dim1 + 1]);
// (*matvec)(&x[1], &work[av * work_dim1 + 1]);
(*psolve)(&work[r * work_dim1 + 1], &work[av * work_dim1 + 1]);
work[i + 1 + s * work_dim1] = dnrm2_(n, &work[r * work_dim1 + 1], &c__1);
*resid = work[i + 1 + s * work_dim1] / bnrm2;
if (*resid <= tol) {
goto L70;
}
if (*iter == maxit) {
goto L60;
}
/* Restart. */
goto L10;
L60:
/* Iteration fails. */
*info = 1;
return 0;
L70:
/* Iteration successful; return. */
return 0;
/* End of GMRES */
} /* gmres_ */
/* =============================================================== */
/* Subroutine */ int update_(i, n, x, h, ldh, y, s, v, ldv)
integer *i, *n;
doublereal *x, *h;
integer *ldh;
doublereal *y, *s, *v;
integer *ldv;
{
/* System generated locals */
integer h_dim1, h_offset, v_dim1, v_offset;
/* Local variables */
extern /* Subroutine */ int dgemv_(), dcopy_(), dtrsv_();
/* This routine updates the GMRES iterated solution approximation. */
/* .. Executable Statements .. */
/* Solve H*Y = S for upper triangualar H. */
/* Parameter adjustments */
v_dim1 = *ldv;
v_offset = v_dim1 + 1;
v -= v_offset;
--s;
--y;
h_dim1 = *ldh;
h_offset = h_dim1 + 1;
h -= h_offset;
--x;
/* Function Body */
dcopy_(i, &s[1], &c__1, &y[1], &c__1);
dtrsv_("UPPER", "NOTRANS", "NONUNIT", i, &h[h_offset], ldh, &y[1], &c__1,
5L, 7L, 7L);
/* Compute current solution vector X = X + V*Y. */
dgemv_("NOTRANS", n, i, &c_b8, &v[v_offset], ldv, &y[1], &c__1, &c_b8, &x[
1], &c__1, 7L);
return 0;
} /* update_ */
/* ========================================================= */
/* Subroutine */ int basis_(i, n, h, v, ldv, w)
integer *i, *n;
doublereal *h, *v;
integer *ldv;
doublereal *w;
{
/* System generated locals */
integer v_dim1, v_offset, i__1;
doublereal d__1;
/* Local variables */
extern doublereal ddot_(), dnrm2_();
static integer k;
extern /* Subroutine */ int dscal_(), dcopy_(), daxpy_();
/* Construct the I-th column of the upper Hessenberg matrix H */
/* using the Gram-Schmidt process on V and W. */
/* Parameter adjustments */
--w;
v_dim1 = *ldv;
v_offset = v_dim1 + 1;
v -= v_offset;
--h;
/* Function Body */
i__1 = *i;
for (k = 1; k <= i__1; ++k) {
h[k] = ddot_(n, &w[1], &c__1, &v[k * v_dim1 + 1], &c__1);
d__1 = -h[k];
daxpy_(n, &d__1, &v[k * v_dim1 + 1], &c__1, &w[1], &c__1);
/* L10: */
}
h[*i + 1] = dnrm2_(n, &w[1], &c__1);
dcopy_(n, &w[1], &c__1, &v[(*i + 1) * v_dim1 + 1], &c__1);
d__1 = 1. / h[*i + 1];
dscal_(n, &d__1, &v[(*i + 1) * v_dim1 + 1], &c__1);
return 0;
}