-
Notifications
You must be signed in to change notification settings - Fork 0
/
dPackgmres.f
1563 lines (1563 loc) · 47.9 KB
/
dPackgmres.f
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
C*
C* Copyright (C) CERFACS 1998
C*
C* SOFTWARE LICENSE AGREEMENT NOTICE - THIS SOFTWARE IS BEING PROVIDED TO
C* YOU BY CERFACS UNDER THE FOLLOWING LICENSE. BY DOWN-LOADING, INSTALLING
C* AND/OR USING THE SOFTWARE YOU AGREE THAT YOU HAVE READ, UNDERSTOOD AND
C* WILL COMPLY WITH THESE FOLLOWING TERMS AND CONDITIONS.
C*
C* 1 - This software program provided in source code format ("the " Source
C* Code ") and any associated documentation (the " Documentation ") are
C* licensed, not sold, to you.
C*
C* 2 - CERFACS grants you a personal, non-exclusive, non-transferable and
C* royalty-free right to use, copy or modify the Source Code and
C* Documentation, provided that you agree to comply with the terms and
C* restrictions of this agreement. You may modify the Source Code and
C* Documentation to make source code derivative works, object code
C* derivative works and/or documentation derivative Works (called "
C* Derivative Works "). The Source Code, Documentation and Derivative
C* Works (called " Licensed Software ") may be used by you for personal
C* and non-commercial use only. " non-commercial use " means uses that are
C* not or will not result in the sale, lease or rental of the Licensed
C* Software and/or the use of the Licensed Software in any commercial
C* product or service. CERFACS reserves all rights not expressly granted
C* to you. No other licenses are granted or implied.
C*
C* 3 - The Source Code and Documentation are and will remain the sole
C* property of CERFACS. The Source Code and Documentation are copyrighted
C* works. You agree to treat any modification or derivative work of the
C* Licensed Software as if it were part of the Licensed Software itself.
C* In return for this license, you grant CERFACS a non-exclusive perpetual
C* paid-up royalty-free license to make, sell, have made, copy, distribute
C* and make derivative works of any modification or derivative work you
C* make of the Licensed Software.
C*
C* 4- The licensee shall acknowledge the contribution of the Source Code
C* (using the reference [1]) in any publication of material dependent upon
C* upon the use of the Source Code. The licensee shall use reasonable
C* endeavours to notify the authors of the package of this publication.
C*
C* [1] V. Frayssé, L. Giraud, S. Gratton, and J. Langou, A set of GMRES
C* routines for real and complex arithmetics on high performance
C* computers, CERFACS Technical Report TR/PA/03/3, public domain software
C* available on www.cerfacs/algor/Softs, 2003
C*
C* 5- CERFACS has no obligation to support the Licensed Software it is
C* providing under this license.
C*
C* THE LICENSED SOFTWARE IS PROVIDED " AS IS " AND CERFACS MAKE NO
C* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
C* BUT NOT LIMITATION, CERFACS MAKE NO REPRESENTATIONS OR WARRANTIES OF
C* MERCHANTIBILY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
C* THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD
C* PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. CERFACS WILL NOT
C* BE LIABLE FOR ANY CONSEQUENTIAL, INCIDENTAL, OR SPECIAL DAMAGES, OR ANY
C* OTHER RELIEF, OR FOR ANY CLAIM BY ANY THIRD PARTY, ARISING FROM YOUR
C* USE OF THE LICENSED SOFTWARE.
C*
C* 6- For information regarding a commercial license for the Source Code
C* and Documentation, please contact Mrs Campassens ([email protected])
C*
C* 7- This license is effective until terminated. You may terminate this
C* license at any time by destroying the Licensed Software.
C*
C* I agree all the terms and conditions of the above license agreement
C*
*
subroutine drive_dgmres(n,nloc,m,lwork,work,
& irc,icntl,cntl,info,rinfo)
*
* Purpose
* =======
* drive_dgmres is the driver routine for solving the linear system
* Ax = b using the * Generalized Minimal Residual iterative method
* with preconditioning.
* This solver is implemented with a reverse communication scheme: control
* is returned to the user for computing the (preconditioned)
* matrix-vector product.
* See the User's Guide for an example of use.
*
*
* Written : June 1996
* Authors : Luc Giraud, Serge Gratton, V. Fraysse
* Parallel Algorithms - CERFACS
*
* Updated : April 1997
* Authors : Valerie Fraysse, Luc Giraud, Serge Gratton
* Parallel Algorithms - CERFACS
*
* Updated : June 1998
* Authors : Valerie Fraysse, Luc Giraud, Serge Gratton
* Parallel Algorithms - CERFACS
* Purpose : Make clear that the warning and error messages come from the
* dgmres modules.
*
* Updated : December 2002 - L. Giraud, J.Langou
* Purpose : Add the capability to avoid explicit residual calculation at restart
*
*
* Arguments
* =========
*
* n (input) INTEGER.
* On entry, the dimension of the problem.
* Unchanged on exit.
*
* nloc (input) INTEGER.
* On entry, the dimension of the local problem.
* In a parallel distributed envirionment, this corresponds
* to the size of the subset of entries of the right hand side
* and solution allocated to the calling process.
* Unchanged on exit.
*
*
* m (input) INTEGER
* Restart parameter, <= N. This parameter controls the amount
* of memory required for matrix H (see WORK and H).
* Unchanged on exit.
*
* lwork (input) INTEGER
* size of the workspace
* lwork >= m*m + m*(n+5) + 5*n+1, if icntl(8) = 1
* lwork >= m*m + m*(n+5) + 6*n+1, if icntl(8) = 0
*
* work (workspace) real*8/real*8 array, length lwork
* work contains the required vector and matrices stored in the
* following order :
* x (n,1) : computed solution.
* b (n,1) : right hand side.
* r0 (n,1) : vector workspace.
* w (n,1) : vector workspace.
* V (n,m) : Krylov basis.
* H (m+1,m+1) : Hessenberg matrix (full storage).
* yCurrent (m,1) : solution of the current LS
* xCurrent (n,1) : current iterate
* rotSin (m,1) : Sine of the Givens rotation
* rotCos (m,1) : Cosine of the Givens rotation
*
* irc (input/output) INTEGER array. length 5
* irc(1) : REVCOM used for reverse communication
* (type of external operation)
* irc(2) : COLX used for reverse communication
* irc(3) : COLY used for reverse communication
* irc(4) : COLZ used for reverse communication
* irc(5) : NBSCAL used for reverse communication
*
* icntl (input) INTEGER array. length 7
* icntl(1) : stdout for error messages
* icntl(2) : stdout for warnings
* icntl(3) : stdout for convergence history
* icntl(4) : 0 - no preconditioning
* 1 - left preconditioning
* 2 - right preconditioning
* 3 - double side preconditioning
* 4 - error, default set in Init
* icntl(5) : 0 - modified Gram-Schmidt
* 1 - iterative modified Gram-Schmidt
* 2 - classical Gram-Schmidt
* 3 - iterative classical Gram-Schmidt
* icntl(6) : 0 - default initial guess x_0 = 0 (to be set)
* 1 - user supplied initial guess
* icntl(7) : maximum number of iterations
* icntl(8) : 0 - use recurence formula at restart
* 1 - default compute the true residual at each restart
*
* cntl (input) real*8 array, length 5
* cntl(1) : tolerance for convergence
* cntl(2) : scaling factor for normwise perturbation on A
* cntl(3) : scaling factor for normwise perturbation on b
* cntl(4) : scaling factor for normwise perturbation on the
* preconditioned matrix
* cntl(5) : scaling factor for normwise perturbation on
* preconditioned right hand side
*
* info (output) INTEGER array, length 3
* info(1) : 0 - normal exit
* -1 - n < 1
* -2 - m < 1
* -3 - lwork too small
* -4 - convergence not achieved after icntl(7) iterations
* -5 - precondition type not set by user
* info(2) : if info(1)=0 - number of iteration to converge
* if info(1)=-3 - minimum workspace size necessary
* info(3) : optimal size for the workspace
*
* rinfo (output) real*8 array, length 2
* if info(1)=0
* rinfo(1) : backward error for the preconditioned system
* rinfo(2) : backward error for the unpreconditioned system
*
* Input variables
* ---------------
integer n, nloc, lwork, icntl(*)
real(8) :: cntl(*)
real(8) :: sA, sb, sPA, sPb
* Output variables
* ----------------
integer info(*)
real(8) :: rinfo(*)
* Input/Output variables
* ----------------------
integer m, irc(*)
real(8) :: work(*)
* Local variables
* ---------------
integer xptr, bptr, wptr, r0ptr, Vptr, Hptr
integer yCurrent,rotSin, rotCos, xCurrent
integer sizeWrk, newRestart
integer iwarn, ierr, ihist, compRsd
real rn
real(8) :: DZRO
parameter (DZRO = 0.0d0)
*
integer icheck
DATA icheck /0/
save icheck
*
integer ifix
intrinsic ifix
real float
intrinsic float
*
* Executable statements :
*
ierr = icntl(1)
iwarn = icntl(2)
ihist = icntl(3)
compRsd = icntl(8)
*
if (ierr.lt.0) ierr = 6
*
if (compRsd.eq.1) then
sizeWrk = m*m + m*(nloc+5) + 5*nloc+ 1
else
sizeWrk = m*m + m*(nloc+5) + 6*nloc+ 1
endif
*
if (icheck.eq.0) then
* Check the value of the arguments
if ((n.lt.1).or.(nloc.lt.1)) then
write(ierr,*)
write(ierr,*)' ERROR GMRES : '
write(ierr,*)' N < 1 '
write(ierr,*)
info(1) = -1
irc(1) = 0
return
endif
if (m.lt.1) then
write(ierr,*)
write(ierr,*)' ERROR GMRES :'
write(ierr,*)' M < 1 '
write(ierr,*)
info(1) = -2
irc(1) = 0
return
endif
if ((icntl(4).ne.0).and.(icntl(4).ne.1).and.
& (icntl(4).ne.2).and.(icntl(4).ne.3)) then
write(ierr,*)
write(ierr,*)' ERROR GMRES : '
write(ierr,*)' Undefined preconditioner '
write(ierr,*)
info(1) = -5
irc(1) = 0
return
endif
*
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' For M = ',m,' optimal value '
write(iwarn,*) ' for LWORK = ', sizeWrk
write(iwarn,*)
endif
*
if ((icntl(5).lt.0).or.(icntl(5).gt.3)) then
icntl(5) = 0
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' Undefined orthogonalisation '
write(iwarn,*) ' Default MGS '
write(iwarn,*)
endif
endif
if ((icntl(6).ne.0).and.(icntl(6).ne.1)) then
icntl(6) = 0
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' Undefined intial guess '
write(iwarn,*) ' Default x0 = 0 '
write(iwarn,*)
endif
endif
if (icntl(7).le.0) then
icntl(7) = n
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES :'
write(iwarn,*) ' Negative max number of iterations'
write(iwarn,*) ' Default N '
write(iwarn,*)
endif
endif
if ((icntl(8).ne.0).and.(icntl(8).ne.1)) then
icntl(8) = 1
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES :'
write(iwarn,*) ' Undefined strategy for the residual'
write(iwarn,*) ' at restart'
write(iwarn,*) ' Default 1 '
write(iwarn,*)
endif
* Check if the restart parameter is correct and if the size of the
* workspace is big enough for the restart.
* If not try to fix correctly the parameters
*
if ((m .gt. n).or.(lwork.lt.sizeWrk)) then
if (m .gt. n) then
m = n
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' Parameter M bigger than N'
write(iwarn,*) ' New value for M ',m
write(iwarn,*)
endif
if (compRsd.eq.1) then
sizeWrk = m*m + m*(nloc+5) + 5*nloc+1
else
sizeWrk = m*m + m*(nloc+5) + 6*nloc+1
endif
endif
if ((lwork.lt.sizeWrk).and.(n.eq.nloc)) then
* Compute the maximum size of the m according to the memory space
rn = float(n)
newRestart = ifix((-5.0-rn+sqrt((rn+5.0)**2-4.0*(5.0*rn
& +1.0-float(lwork))))/2.0)
if (compRsd.eq.0) then
newRestart = newRestart - 1
endif
if (newRestart.gt.0) then
m = newRestart
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*)' WARNING GMRES : '
write(iwarn,*)' Workspace too small for M'
write(iwarn,*)' New value for M ',m
write(iwarn,*)
endif
else
write(ierr,*)
write(ierr,*)' ERROR GMRES : '
write(ierr,*)' Not enough space for the problem'
write(ierr,*)' the space does not permit any m'
write(ierr,*)
info(1) = -3
irc(1) = 0
return
endif
endif
if ((lwork.lt.sizeWrk).and.(n.ne.nloc)) then
write(ierr,*)
write(ierr,*)' ERROR GMRES : '
write(ierr,*)' Not enough space for the problem'
write(ierr,*)
info(1) = -3
irc(1) = 0
return
endif
endif
*
info(3) = sizeWrk
icheck = 1
*
* save the parameters the the history file
*
if (ihist.ne.0) then
write(ihist,'(10x,A39)') 'CONVERGENCE HISTORY FOR GMRES'
write(ihist,*)
write(ihist,'(A30,I2)') 'Errors are displayed in unit: ',ierr
if (iwarn.eq.0) then
write(ihist,'(A27)') 'Warnings are not displayed:'
else
write(ihist,'(A32,I2)') 'Warnings are displayed in unit: ',
& iwarn
endif
write(ihist,'(A13,I7)') 'Matrix size: ',n
write(ihist,'(A19,I7)') 'Local matrix size: ',nloc
write(ihist,'(A9,I7)') 'Restart: ',m
if (icntl(4).eq.0) then
write(ihist,'(A18)') 'No preconditioning'
elseif (icntl(4).eq.1) then
write(ihist,'(A20)') 'Left preconditioning'
elseif (icntl(4).eq.2) then
write(ihist,'(A21)') 'Right preconditioning'
elseif (icntl(4).eq.3) then
write(ihist,'(A30)') 'Left and right preconditioning'
endif
if (icntl(5).eq.0) then
write(ihist,'(A21)') 'Modified Gram-Schmidt'
elseif (icntl(5).eq.1) then
write(ihist,'(A31)') 'Iterative modified Gram-Schmidt'
elseif (icntl(5).eq.2) then
write(ihist,'(A22)') 'Classical Gram-Schmidt'
else
write(ihist,'(A32)') 'Iterative classical Gram-Schmidt'
endif
if (icntl(6).eq.0) then
write(ihist,'(A29)') 'Default initial guess x_0 = 0'
else
write(ihist,'(A27)') 'User supplied initial guess'
endif
if (icntl(8).eq.1) then
write(ihist,'(A33)') 'True residual computed at restart'
else
write(ihist,'(A30)') 'Recurrence residual at restart'
endif
write(ihist,'(A30,I5)') 'Maximum number of iterations: ',
& icntl(7)
write(ihist,'(A27,E8.2)') 'Tolerance for convergence: ',
& cntl(1)
*
write(ihist,'(A53)')
& 'Backward error on the unpreconditioned system Ax = b:'
sA = cntl(2)
sb = cntl(3)
if ((sA.eq.DZRO).and.(sb.eq.DZRO)) then
write(ihist,'(A39)')
& ' the residual is normalised by ||b||'
else
write(ihist,'(A34)') ' the residual is normalised by '
write(ihist,'(A8,E8.2,$)') ' ', sA
write(ihist,'(A11,E8.2)') ' * ||x|| + ', sb
endif
sPA = cntl(4)
sPb = cntl(5)
write(ihist,'(A22,$)') 'Backward error on the '
write(ihist,'(A41)')
& 'preconditioned system (P1)A(P2)y = (P1)b:'
if ((sPA.eq.DZRO).and.(sPb.eq.DZRO)) then
write(ihist,'(A35,$)') ' the preconditioned residual is'
write(ihist,'(A24)') ' normalised by ||(P1)b||'
else
write(ihist,'(A35,$)') ' the preconditioned residual is'
write(ihist,'(A15)') ' normalised by '
write(ihist,'(A8,E8.2,A3,$)') ' ', sPA, ' * '
write(ihist,'(A12,E8.2)') '||(P2)y|| + ', sPb
endif
*
write(ihist,'(A31,I7)') 'Optimal size for the workspace:',
& info(3)
write(ihist,*)
write(ihist,'(A32,$)') 'Convergence history: b.e. on the'
write(ihist,'(A22)') ' preconditioned system'
write(ihist,'(A11,$)') ' Iteration '
write(ihist,'(A27)') ' Arnoldi b.e. True b.e.'
endif
*
endif
* setup some pointers on the workspace
xptr = 1
bptr = xptr + nloc
r0ptr = bptr + nloc
wptr = r0ptr + nloc
Vptr = wptr + nloc
if (compRsd.eq.1) then
Hptr = Vptr + m*nloc
else
Hptr = Vptr + (m+1)*nloc
endif
yCurrent = Hptr + (m+1)*(m+1)
xCurrent = yCurrent + m
rotSin = xCurrent + nloc
rotCos = rotSin + m
*
call dgmres(nloc,m,work(bptr),work(xptr),
& work(Hptr),work(wptr),work(r0ptr),work(Vptr),
& work(yCurrent),work(xCurrent),
& work(rotSin),work(rotCos),irc,icntl,cntl,info,rinfo)
*
if (irc(1).eq.0) then
icheck = 0
endif
*
return
end
*
subroutine dgmres(n,m,b,x,H,w,r0,V,yCurrent,xCurrent,rotSin,
& rotCos,irc,icntl,cntl,info,rinfo)
*
*
* Purpose
* =======
* dgmres solves the linear system Ax = b using the
* Generalized Minimal Residual iterative method
*
* When preconditioning is used we solve :
* M_1^{-1} A M_2^{-1} y = M_1^{-1} b
* x = M_2^{-1} y
*
* Convergence test based on the normwise backward error for
* the preconditioned system
*
* Written : June 1996
* Authors : Luc Giraud, Serge Gratton, V. Fraysse
* Parallel Algorithms - CERFACS
*
* Updated : April 1997
* Authors : Valerie Fraysse, Luc Giraud, Serge Gratton
* Parallel Algorithms - CERFACS
*
* Updated : March 1998
* Purpose : Pb with F90 on DEC ws
* cure : remove "ZDSCAL" when used to initialize vectors to zero
*
* Updated : May 1998
* Purpose : r0(1) <-- r0'r0 : pb when used with DGEMV for the dot product
* cure : w(1) <-- r0'r0
*
* Updated : June 1998
* Purpose : Make clear that the warning and error messages come from the
* dgmres modules.
*
* Updated : February 2001 - L. Giraud
* Purpose : In complex version, initializations to zero performed in complex
* arithmetic to avoid implicit conversion by the compiler.
*
* Updated : July 2001 - L. Giraud, J. Langou
* Purpose : Avoid to compute the approximate solution at each step of
* the Krylov space construction when spA is zero.
*
* Updated : November 2002 - S. Gratton
* Purpose : Use Givens rotations conform to the classical definition.
* No impact one the convergence history.
*
* Updated : November 2002 - L. Giraud
* Purpose : Properly handle the situation when the convergence is obtained
* exactly at the "IterMax" iteration
*
* Updated : December 2002 - L. Giraud, J.Langou
* Purpose : Add the capability to avoid explicit residual calculation at restart
*
* Updated : January 2003 - L. Giraud, S. Gratton
* Purpose : Use Givens rotations from BLAS.
*
* Updated : March 2003 - L. Giraud
* Purpose : Set back retlbl to zero, if initial guess is solution
* or right-hand side is zero
*
* Arguments
* =========
*
* n (input) INTEGER.
* On entry, the dimension of the problem.
* Unchanged on exit.
*
* m (input) INTEGER
* Restart parameter, <= N. This parameter controls the amount
* of memory required for matrix H (see WORK and H).
* Unchanged on exit.
*
* b (input) real*8/real*8
* Right hand side of the linear system.
*
* x (output) real*8/real*8
* Computed solution of the linear system.
*
* H (workspace) real*8/real*8
* Hessenberg matrix built within dgmres
*
* w (workspace) real*8/real*8
* Vector used as temporary storage
*
* r0 (workspace) real*8/real*8
* Vector used as temporary storage
*
* V (workspace) real*8/real*8
* Basis computed by the Arnoldi's procedure.
*
* yCurrent (workspace) real*8/real*8
* solution of the current LS
*
* xCurrent (workspace) real*8/real*8
* current iterate
*
* rotSin (workspace) real*8/real*8
* Sine of the Givens rotation
*
* rotCos (workspace) real*8
* Cosine of the Givens rotation
*
* irc (input/output) INTEGER array. length 3
* irc(1) : REVCOM used for reverse communication
* (type of external operation)
* irc(2) : COLX used for reverse communication
* irc(3) : COLY used for reverse communication
* irc(4) : COLZ used for reverse communication
* irc(5) : NBSCAL used for reverse communication
*
* icntl (input) INTEGER array. length 7
* icntl(1) : stdout for error messages
* icntl(2) : stdout for warnings
* icntl(3) : stdout for convergence history
* icntl(4) : 0 - no preconditioning
* 1 - left preconditioning
* 2 - right preconditioning
* 3 - double side preconditioning
* 4 - error, default set in Init
* icntl(5) : 0 - modified Gram-Schmidt
* 1 - iterative modified Gram-Schmidt
* 2 - classical Gram-Schmidt
* 3 - iterative classical Gram-Schmidt
* icntl(6) : 0 - default initial guess x_0 = 0 (to be set)
* 1 - user supplied initial guess
* icntl(7) : maximum number of iterations
* icntl(8) : 1 - default compute the true residual at each restart
* 0 - use recurence formula at restart
*
* cntl (input) real*8 array, length 5
* cntl(1) : tolerance for convergence
* cntl(2) : scaling factor for normwise perturbation on A
* cntl(3) : scaling factor for normwise perturbation on b
* cntl(4) : scaling factor for normwise perturbation on the
* preconditioned matrix
* cntl(5) : scaling factor for normwise perturbation on
* preconditioned right hand side
*
* info (output) INTEGER array, length 2
* info(1) : 0 - normal exit
* -1 - n < 1
* -2 - m < 1
* -3 - lwork too small
* -4 - convergence not achieved after icntl(7) iterations
* -5 - precondition type not set by user
* info(2) : if info(1)=0 - number of iteration to converge
* if info(1)=-3 - minimum workspace size necessary
* info(3) : optimal size for the workspace
*
* rinfo (output) real*8 array, length 2
* if info(1)=0
* rinfo(1) : backward error for the preconditioned system
* rinfo(2) : backward error for the unpreconditioned system
*
* Input variables
* ---------------
integer n, m, icntl(*)
real(8) :: b(*)
real(8) :: cntl(*)
*
* Output variables
* ----------------
integer info(*)
real(8) :: rinfo(*)
*
* Input/Output variables
* ----------------------
integer irc(*)
real(8) :: x(*), H(m+1,*), w(*), r0(*), V(n,*), yCurrent(*)
real(8) :: xCurrent(*), rotSin(*)
real(8) :: rotCos(*)
*
* Local variables
* ---------------
integer j, jH, iterOut, nOrtho, iterMax, initGuess, iOrthog
integer xptr, bptr, wptr, r0ptr, Vptr, Hptr, yptr, xcuptr
integer typePrec, leftPrec, rightPrec, dblePrec, noPrec
integer iwarn, ihist
integer compRsd
real(8) :: beta, bn, sA, sb, sPA, sPb, bea, be
real(8) :: dloo, dnormw, dnormx, dnormres, trueNormRes
real(8) :: dVi, temp, aux
real(8) :: auxHjj, auxHjp1j
*
parameter (noPrec = 0, leftPrec = 1)
parameter (rightPrec = 2, dblePrec = 3)
*
real(8) :: ZERO, ONE
parameter (ZERO = 0.0d0, ONE = 1.0d0)
real(8) :: DZRO,DONE
parameter (DZRO = 0.0d0, DONE = 1.0d0)
*
*
* External functions
* ------------------
real(8) :: dnrm2
external dnrm2
*
* Reverse communication variables
* -------------------------------
integer retlbl
DATA retlbl /0/
integer matvec, precondLeft, precondRight, prosca
parameter(matvec=1, precondLeft=2, precondRight=3, prosca=4)
*
* Saved variables
* ---------------
save iterOut, jH, beta, bn, dnormres, retlbl, j
save sA, sb, sPA, sPb, dnormx, trueNormRes, bea, be
save dloo, nOrtho, compRsd
*
* Intrinsic function
* ------------------
intrinsic abs, sqrt
*
* Executable statements
*
* setup some pointers on the workspace
xptr = 1
bptr = xptr + n
r0ptr = bptr + n
wptr = r0ptr + n
Vptr = wptr + n
if (icntl(8).eq.1) then
Hptr = Vptr + m*n
else
Hptr = Vptr + (m+1)*n
endif
yptr = Hptr + (m+1)*(m+1)
xcuptr = yptr + m
*
iwarn = icntl(2)
ihist = icntl(3)
typePrec = icntl(4)
iOrthog = icntl(5)
initGuess = icntl(6)
iterMax = icntl(7)
*
if (retlbl.eq.0) then
compRsd = icntl(8)
endif
*
if (retlbl.ne.0) then
if (retlbl.eq.5) then
goto 5
else if (retlbl.eq.6) then
goto 6
else if (retlbl.eq.8) then
goto 8
else if (retlbl.eq.11) then
goto 11
else if (retlbl.eq.16) then
goto 16
else if (retlbl.eq.18) then
goto 18
else if (retlbl.eq.21) then
goto 21
else if (retlbl.eq.26) then
goto 26
else if (retlbl.eq.31) then
goto 31
else if (retlbl.eq.32) then
goto 32
else if (retlbl.eq.33) then
goto 33
else if (retlbl.eq.34) then
goto 34
else if (retlbl.eq.36) then
goto 36
else if (retlbl.eq.37) then
goto 37
else if (retlbl.eq.38) then
goto 38
else if (retlbl.eq.41) then
goto 41
else if (retlbl.eq.43) then
goto 43
else if (retlbl.eq.46) then
goto 46
else if (retlbl.eq.48) then
goto 48
else if (retlbl.eq.51) then
goto 51
else if (retlbl.eq.52) then
goto 52
else if (retlbl.eq.61) then
goto 61
else if (retlbl.eq.66) then
goto 66
else if (retlbl.eq.68) then
goto 68
endif
endif
*
*
* intialization of various variables
*
iterOut = 0
beta = DZRO
*
if (initGuess.eq.0) then
do j=1,n
x(j) = ZERO
enddo
endif
*
* bn = dnrm2(n,b,1)
*
irc(1) = prosca
irc(2) = bptr
irc(3) = bptr
irc(4) = r0ptr
irc(5) = 1
retlbl = 5
return
5 continue
bn = sqrt((r0(1)))
*
if (bn.eq.DZRO) then
do j=1,n
x(j) = ZERO
enddo
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' Null right hand side'
write(iwarn,*) ' solution set to zero'
write(iwarn,*)
endif
info(1) = 0
info(2) = 0
rinfo(1) = DZRO
rinfo(2) = DZRO
irc(1) = 0
retlbl = 0
return
endif
*
* Compute the scaling factor for the backward error on the
* unpreconditioned sytem
*
sA = cntl(2)
sb = cntl(3)
if ((sA.eq.DZRO).and.(sb.eq.DZRO)) then
sb = bn
endif
* Compute the scaling factor for the backward error on the
* preconditioned sytem
*
sPA = cntl(4)
sPb = cntl(5)
if ((sPA.eq.DZRO).and.(sPb.eq.DZRO)) then
if ((typePrec.eq.noPrec).or.(typePrec.eq.rightPrec)) then
sPb = bn
else
irc(1) = precondLeft
irc(2) = bptr
irc(4) = r0ptr
retlbl = 6
return
endif
endif
6 continue
if ((sPA.eq.DZRO).and.(sPb.eq.DZRO)) then
if ((typePrec.eq.dblePrec).or.(typePrec.eq.leftPrec)) then
*
* sPb = dnrm2(n,r0,1)
*
irc(1) = prosca
irc(2) = r0ptr
irc(3) = r0ptr
irc(4) = wptr
irc(5) = 1
retlbl = 8
return
endif
endif
8 continue
if ((sPA.eq.DZRO).and.(sPb.eq.DZRO)) then
if ((typePrec.eq.dblePrec).or.(typePrec.eq.leftPrec)) then
sPb = sqrt((w(1)))
*
endif
endif
*
*
* Compute the first residual
* Y = AX : r0 <-- A x
*
* The residual is computed only if the initial guess is not zero
*
if (initGuess.ne.0) then
irc(1) = matvec
irc(2) = xptr
irc(4) = r0ptr
retlbl = 11
return
endif
11 continue
if (initGuess.ne.0) then
do j=1,n
r0(j) = b(j)-r0(j)
enddo
else
call dcopy(n,b,1,r0,1)
endif
*
* Compute the preconditioned residual if necessary
* M_1Y = X : w <-- M_1^{-1} r0
*
if ((typePrec.eq.noPrec).or.(typePrec.eq.rightPrec)) then
call dcopy(n,r0,1,w,1)
else
irc(1) = precondLeft
irc(2) = r0ptr
irc(4) = wptr
retlbl = 16
return
endif
16 continue
*
*
* beta = dnrm2(n,w,1)
*
*
irc(1) = prosca
irc(2) = wptr
irc(3) = wptr
irc(4) = r0ptr
irc(5) = 1
retlbl = 18
return
18 continue
beta = sqrt((r0(1)))
*
if (beta .eq. DZRO) then
* The residual is exactly zero : x is the exact solution
info(1) = 0
info(2) = 0
rinfo(1) = DZRO
rinfo(2) = DZRO
irc(1) = 0
retlbl = 0
if (iwarn.ne.0) then
write(iwarn,*)
write(iwarn,*) ' WARNING GMRES : '
write(iwarn,*) ' Intial residual is zero'
write(iwarn,*) ' initial guess is solution'
write(iwarn,*)
endif
return
endif
*
aux = ONE/beta
do j=1,n
V(j,1) = ZERO
enddo
call daxpy(n,aux,w,1,V(1,1),1)
*
* Most outer loop : dgmres iteration
*
* REPEAT
7 continue
*
*
H(1,m+1)=beta
do j=1,m
H(j+1,m+1) = ZERO
enddo
*
* Construction of the hessenberg matrix WORK and of the orthogonal
* basis V such that AV=VH
*
jH = 1
10 continue
* Remark : this do loop has been written with a while do
* because the
* " do jH=1,restart "
* fails with the reverse communication.
* do jH=1,restart
*
*
* Compute the preconditioned residual if necessary
*
if ((typePrec.eq.rightPrec).or.(typePrec.eq.dblePrec)) then
*
* Y = M_2^{-1}X : w <-- M_2^{-1} V(1,jH)
*
irc(1) = precondRight
irc(2) = vptr + (jH-1)*n
irc(4) = wptr
retlbl = 21
return
else
call dcopy(n,V(1,jH),1,w,1)
endif
21 continue
*
* Y = AX : r0 <-- A w
*
irc(1) = matvec
irc(2) = wptr
irc(4) = r0ptr
retlbl = 26
return
26 continue
*
* MY = X : w <-- M_1^{-1} r0