spigmr.f 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. *DECK SPIGMR
  2. SUBROUTINE SPIGMR (N, R0, SR, SZ, JSCAL, MAXL, MAXLP1, KMP, NRSTS,
  3. + JPRE, MATVEC, MSOLVE, NMSL, Z, V, HES, Q, LGMR, RPAR, IPAR, WK,
  4. + DL, RHOL, NRMAX, B, BNRM, X, XL, ITOL, TOL, NELT, IA, JA, A,
  5. + ISYM, IUNIT, IFLAG, ERR)
  6. C***BEGIN PROLOGUE SPIGMR
  7. C***SUBSIDIARY
  8. C***PURPOSE Internal routine for SGMRES.
  9. C***LIBRARY SLATEC (SLAP)
  10. C***CATEGORY D2A4, D2B4
  11. C***TYPE SINGLE PRECISION (SPIGMR-S, DPIGMR-D)
  12. C***KEYWORDS GENERALIZED MINIMUM RESIDUAL, ITERATIVE PRECONDITION,
  13. C NON-SYMMETRIC LINEAR SYSTEM, SLAP, SPARSE
  14. C***AUTHOR Brown, Peter, (LLNL), pnbrown@llnl.gov
  15. C Hindmarsh, Alan, (LLNL), alanh@llnl.gov
  16. C Seager, Mark K., (LLNL), seager@llnl.gov
  17. C Lawrence Livermore National Laboratory
  18. C PO Box 808, L-60
  19. C Livermore, CA 94550 (510) 423-3141
  20. C***DESCRIPTION
  21. C This routine solves the linear system A * Z = R0 using a
  22. C scaled preconditioned version of the generalized minimum
  23. C residual method. An initial guess of Z = 0 is assumed.
  24. C
  25. C *Usage:
  26. C INTEGER N, JSCAL, MAXL, MAXLP1, KMP, NRSTS, JPRE, NMSL, LGMR
  27. C INTEGER IPAR(USER DEFINED), NRMAX, ITOL, NELT, IA(NELT), JA(NELT)
  28. C INTEGER ISYM, IUNIT, IFLAG
  29. C REAL R0(N), SR(N), SZ(N), Z(N), V(N,MAXLP1), HES(MAXLP1,MAXL),
  30. C $ Q(2*MAXL), RPAR(USER DEFINED), WK(N), DL(N), RHOL, B(N),
  31. C $ BNRM, X(N), XL(N), TOL, A(NELT), ERR
  32. C EXTERNAL MATVEC, MSOLVE
  33. C
  34. C CALL SPIGMR(N, R0, SR, SZ, JSCAL, MAXL, MAXLP1, KMP,
  35. C $ NRSTS, JPRE, MATVEC, MSOLVE, NMSL, Z, V, HES, Q, LGMR,
  36. C $ RPAR, IPAR, WK, DL, RHOL, NRMAX, B, BNRM, X, XL,
  37. C $ ITOL, TOL, NELT, IA, JA, A, ISYM, IUNIT, IFLAG, ERR)
  38. C
  39. C *Arguments:
  40. C N :IN Integer
  41. C The order of the matrix A, and the lengths
  42. C of the vectors SR, SZ, R0 and Z.
  43. C R0 :IN Real R0(N)
  44. C R0 = the right hand side of the system A*Z = R0.
  45. C R0 is also used as workspace when computing
  46. C the final approximation.
  47. C (R0 is the same as V(*,MAXL+1) in the call to SPIGMR.)
  48. C SR :IN Real SR(N)
  49. C SR is a vector of length N containing the non-zero
  50. C elements of the diagonal scaling matrix for R0.
  51. C SZ :IN Real SZ(N)
  52. C SZ is a vector of length N containing the non-zero
  53. C elements of the diagonal scaling matrix for Z.
  54. C JSCAL :IN Integer
  55. C A flag indicating whether arrays SR and SZ are used.
  56. C JSCAL=0 means SR and SZ are not used and the
  57. C algorithm will perform as if all
  58. C SR(i) = 1 and SZ(i) = 1.
  59. C JSCAL=1 means only SZ is used, and the algorithm
  60. C performs as if all SR(i) = 1.
  61. C JSCAL=2 means only SR is used, and the algorithm
  62. C performs as if all SZ(i) = 1.
  63. C JSCAL=3 means both SR and SZ are used.
  64. C MAXL :IN Integer
  65. C The maximum allowable order of the matrix H.
  66. C MAXLP1 :IN Integer
  67. C MAXPL1 = MAXL + 1, used for dynamic dimensioning of HES.
  68. C KMP :IN Integer
  69. C The number of previous vectors the new vector VNEW
  70. C must be made orthogonal to. (KMP .le. MAXL)
  71. C NRSTS :IN Integer
  72. C Counter for the number of restarts on the current
  73. C call to SGMRES. If NRSTS .gt. 0, then the residual
  74. C R0 is already scaled, and so scaling of it is
  75. C not necessary.
  76. C JPRE :IN Integer
  77. C Preconditioner type flag.
  78. C MATVEC :EXT External.
  79. C Name of a routine which performs the matrix vector multiply
  80. C Y = A*X given A and X. The name of the MATVEC routine must
  81. C be declared external in the calling program. The calling
  82. C sequence to MATVEC is:
  83. C CALL MATVEC(N, X, Y, NELT, IA, JA, A, ISYM)
  84. C where N is the number of unknowns, Y is the product A*X
  85. C upon return, X is an input vector, and NELT is the number of
  86. C non-zeros in the SLAP IA, JA, A storage for the matrix A.
  87. C ISYM is a flag which, if non-zero, denotes that A is
  88. C symmetric and only the lower or upper triangle is stored.
  89. C MSOLVE :EXT External.
  90. C Name of the routine which solves a linear system Mz = r for
  91. C z given r with the preconditioning matrix M (M is supplied via
  92. C RPAR and IPAR arrays. The name of the MSOLVE routine must
  93. C be declared external in the calling program. The calling
  94. C sequence to MSOLVE is:
  95. C CALL MSOLVE(N, R, Z, NELT, IA, JA, A, ISYM, RPAR, IPAR)
  96. C Where N is the number of unknowns, R is the right-hand side
  97. C vector and Z is the solution upon return. NELT, IA, JA, A and
  98. C ISYM are defined as below. RPAR is a real array that can be
  99. C used to pass necessary preconditioning information and/or
  100. C workspace to MSOLVE. IPAR is an integer work array for the
  101. C same purpose as RPAR.
  102. C NMSL :OUT Integer
  103. C The number of calls to MSOLVE.
  104. C Z :OUT Real Z(N)
  105. C The final computed approximation to the solution
  106. C of the system A*Z = R0.
  107. C V :OUT Real V(N,MAXLP1)
  108. C The N by (LGMR+1) array containing the LGMR
  109. C orthogonal vectors V(*,1) to V(*,LGMR).
  110. C HES :OUT Real HES(MAXLP1,MAXL)
  111. C The upper triangular factor of the QR decomposition
  112. C of the (LGMR+1) by LGMR upper Hessenberg matrix whose
  113. C entries are the scaled inner-products of A*V(*,I)
  114. C and V(*,K).
  115. C Q :OUT Real Q(2*MAXL)
  116. C A real array of length 2*MAXL containing the components
  117. C of the Givens rotations used in the QR decomposition
  118. C of HES. It is loaded in SHEQR and used in SHELS.
  119. C LGMR :OUT Integer
  120. C The number of iterations performed and
  121. C the current order of the upper Hessenberg
  122. C matrix HES.
  123. C RPAR :IN Real RPAR(USER DEFINED)
  124. C Real workspace passed directly to the MSOLVE routine.
  125. C IPAR :IN Integer IPAR(USER DEFINED)
  126. C Integer workspace passed directly to the MSOLVE routine.
  127. C WK :IN Real WK(N)
  128. C A real work array of length N used by routines MATVEC
  129. C and MSOLVE.
  130. C DL :INOUT Real DL(N)
  131. C On input, a real work array of length N used for calculation
  132. C of the residual norm RHO when the method is incomplete
  133. C (KMP.lt.MAXL), and/or when using restarting.
  134. C On output, the scaled residual vector RL. It is only loaded
  135. C when performing restarts of the Krylov iteration.
  136. C RHOL :OUT Real
  137. C A real scalar containing the norm of the final residual.
  138. C NRMAX :IN Integer
  139. C The maximum number of restarts of the Krylov iteration.
  140. C NRMAX .gt. 0 means restarting is active, while
  141. C NRMAX = 0 means restarting is not being used.
  142. C B :IN Real B(N)
  143. C The right hand side of the linear system A*X = b.
  144. C BNRM :IN Real
  145. C The scaled norm of b.
  146. C X :IN Real X(N)
  147. C The current approximate solution as of the last
  148. C restart.
  149. C XL :IN Real XL(N)
  150. C An array of length N used to hold the approximate
  151. C solution X(L) when ITOL=11.
  152. C ITOL :IN Integer
  153. C A flag to indicate the type of convergence criterion
  154. C used. See the driver for its description.
  155. C TOL :IN Real
  156. C The tolerance on residuals R0-A*Z in scaled norm.
  157. C NELT :IN Integer
  158. C The length of arrays IA, JA and A.
  159. C IA :IN Integer IA(NELT)
  160. C An integer array of length NELT containing matrix data.
  161. C It is passed directly to the MATVEC and MSOLVE routines.
  162. C JA :IN Integer JA(NELT)
  163. C An integer array of length NELT containing matrix data.
  164. C It is passed directly to the MATVEC and MSOLVE routines.
  165. C A :IN Real A(NELT)
  166. C A real array of length NELT containing matrix data.
  167. C It is passed directly to the MATVEC and MSOLVE routines.
  168. C ISYM :IN Integer
  169. C A flag to indicate symmetric matrix storage.
  170. C If ISYM=0, all non-zero entries of the matrix are
  171. C stored. If ISYM=1, the matrix is symmetric and
  172. C only the upper or lower triangular part is stored.
  173. C IUNIT :IN Integer
  174. C The i/o unit number for writing intermediate residual
  175. C norm values.
  176. C IFLAG :OUT Integer
  177. C An integer error flag..
  178. C 0 means convergence in LGMR iterations, LGMR.le.MAXL.
  179. C 1 means the convergence test did not pass in MAXL
  180. C iterations, but the residual norm is .lt. norm(R0),
  181. C and so Z is computed.
  182. C 2 means the convergence test did not pass in MAXL
  183. C iterations, residual .ge. norm(R0), and Z = 0.
  184. C ERR :OUT Real.
  185. C Error estimate of error in final approximate solution, as
  186. C defined by ITOL.
  187. C
  188. C *Cautions:
  189. C This routine will attempt to write to the Fortran logical output
  190. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  191. C this logical unit is attached to a file or terminal before calling
  192. C this routine with a non-zero value for IUNIT. This routine does
  193. C not check for the validity of a non-zero IUNIT unit number.
  194. C
  195. C***SEE ALSO SGMRES
  196. C***ROUTINES CALLED ISSGMR, SAXPY, SCOPY, SHELS, SHEQR, SNRM2, SORTH,
  197. C SRLCAL, SSCAL
  198. C***REVISION HISTORY (YYMMDD)
  199. C 871001 DATE WRITTEN
  200. C 881213 Previous REVISION DATE
  201. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  202. C 890922 Numerous changes to prologue to make closer to SLATEC
  203. C standard. (FNF)
  204. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  205. C 910411 Prologue converted to Version 4.0 format. (BAB)
  206. C 910502 Removed MATVEC and MSOLVE from ROUTINES CALLED list. (FNF)
  207. C 910506 Made subsidiary to SGMRES. (FNF)
  208. C 920511 Added complete declaration section. (WRB)
  209. C***END PROLOGUE SPIGMR
  210. C The following is for optimized compilation on LLNL/LTSS Crays.
  211. CLLL. OPTIMIZE
  212. C .. Scalar Arguments ..
  213. REAL BNRM, ERR, RHOL, TOL
  214. INTEGER IFLAG, ISYM, ITOL, IUNIT, JPRE, JSCAL, KMP, LGMR, MAXL,
  215. + MAXLP1, N, NELT, NMSL, NRMAX, NRSTS
  216. C .. Array Arguments ..
  217. REAL A(NELT), B(*), DL(*), HES(MAXLP1,*), Q(*), R0(*), RPAR(*),
  218. + SR(*), SZ(*), V(N,*), WK(*), X(*), XL(*), Z(*)
  219. INTEGER IA(NELT), IPAR(*), JA(NELT)
  220. C .. Subroutine Arguments ..
  221. EXTERNAL MATVEC, MSOLVE
  222. C .. Local Scalars ..
  223. REAL C, DLNRM, PROD, R0NRM, RHO, S, SNORMW, TEM
  224. INTEGER I, I2, INFO, IP1, ITER, ITMAX, J, K, LL, LLP1
  225. C .. External Functions ..
  226. REAL SNRM2
  227. INTEGER ISSGMR
  228. EXTERNAL SNRM2, ISSGMR
  229. C .. External Subroutines ..
  230. EXTERNAL SAXPY, SCOPY, SHELS, SHEQR, SORTH, SRLCAL, SSCAL
  231. C .. Intrinsic Functions ..
  232. INTRINSIC ABS
  233. C***FIRST EXECUTABLE STATEMENT SPIGMR
  234. C
  235. C Zero out the Z array.
  236. C
  237. DO 5 I = 1,N
  238. Z(I) = 0
  239. 5 CONTINUE
  240. C
  241. IFLAG = 0
  242. LGMR = 0
  243. NMSL = 0
  244. C Load ITMAX, the maximum number of iterations.
  245. ITMAX =(NRMAX+1)*MAXL
  246. C -------------------------------------------------------------------
  247. C The initial residual is the vector R0.
  248. C Apply left precon. if JPRE < 0 and this is not a restart.
  249. C Apply scaling to R0 if JSCAL = 2 or 3.
  250. C -------------------------------------------------------------------
  251. IF ((JPRE .LT. 0) .AND.(NRSTS .EQ. 0)) THEN
  252. CALL SCOPY(N, R0, 1, WK, 1)
  253. CALL MSOLVE(N, WK, R0, NELT, IA, JA, A, ISYM, RPAR, IPAR)
  254. NMSL = NMSL + 1
  255. ENDIF
  256. IF (((JSCAL.EQ.2) .OR.(JSCAL.EQ.3)) .AND.(NRSTS.EQ.0)) THEN
  257. DO 10 I = 1,N
  258. V(I,1) = R0(I)*SR(I)
  259. 10 CONTINUE
  260. ELSE
  261. DO 20 I = 1,N
  262. V(I,1) = R0(I)
  263. 20 CONTINUE
  264. ENDIF
  265. R0NRM = SNRM2(N, V, 1)
  266. ITER = NRSTS*MAXL
  267. C
  268. C Call stopping routine ISSGMR.
  269. C
  270. IF (ISSGMR(N, B, X, XL, NELT, IA, JA, A, ISYM, MSOLVE,
  271. $ NMSL, ITOL, TOL, ITMAX, ITER, ERR, IUNIT, V(1,1), Z, WK,
  272. $ RPAR, IPAR, R0NRM, BNRM, SR, SZ, JSCAL,
  273. $ KMP, LGMR, MAXL, MAXLP1, V, Q, SNORMW, PROD, R0NRM,
  274. $ HES, JPRE) .NE. 0) RETURN
  275. TEM = 1.0E0/R0NRM
  276. CALL SSCAL(N, TEM, V(1,1), 1)
  277. C
  278. C Zero out the HES array.
  279. C
  280. DO 50 J = 1,MAXL
  281. DO 40 I = 1,MAXLP1
  282. HES(I,J) = 0
  283. 40 CONTINUE
  284. 50 CONTINUE
  285. C -------------------------------------------------------------------
  286. C Main loop to compute the vectors V(*,2) to V(*,MAXL).
  287. C The running product PROD is needed for the convergence test.
  288. C -------------------------------------------------------------------
  289. PROD = 1
  290. DO 90 LL = 1,MAXL
  291. LGMR = LL
  292. C -------------------------------------------------------------------
  293. C Unscale the current V(LL) and store in WK. Call routine
  294. C MSOLVE to compute(M-inverse)*WK, where M is the
  295. C preconditioner matrix. Save the answer in Z. Call routine
  296. C MATVEC to compute VNEW = A*Z, where A is the the system
  297. C matrix. save the answer in V(LL+1). Scale V(LL+1). Call
  298. C routine SORTH to orthogonalize the new vector VNEW =
  299. C V(*,LL+1). Call routine SHEQR to update the factors of HES.
  300. C -------------------------------------------------------------------
  301. IF ((JSCAL .EQ. 1) .OR.(JSCAL .EQ. 3)) THEN
  302. DO 60 I = 1,N
  303. WK(I) = V(I,LL)/SZ(I)
  304. 60 CONTINUE
  305. ELSE
  306. CALL SCOPY(N, V(1,LL), 1, WK, 1)
  307. ENDIF
  308. IF (JPRE .GT. 0) THEN
  309. CALL MSOLVE(N, WK, Z, NELT, IA, JA, A, ISYM, RPAR, IPAR)
  310. NMSL = NMSL + 1
  311. CALL MATVEC(N, Z, V(1,LL+1), NELT, IA, JA, A, ISYM)
  312. ELSE
  313. CALL MATVEC(N, WK, V(1,LL+1), NELT, IA, JA, A, ISYM)
  314. ENDIF
  315. IF (JPRE .LT. 0) THEN
  316. CALL SCOPY(N, V(1,LL+1), 1, WK, 1)
  317. CALL MSOLVE(N,WK,V(1,LL+1),NELT,IA,JA,A,ISYM,RPAR,IPAR)
  318. NMSL = NMSL + 1
  319. ENDIF
  320. IF ((JSCAL .EQ. 2) .OR.(JSCAL .EQ. 3)) THEN
  321. DO 65 I = 1,N
  322. V(I,LL+1) = V(I,LL+1)*SR(I)
  323. 65 CONTINUE
  324. ENDIF
  325. CALL SORTH(V(1,LL+1), V, HES, N, LL, MAXLP1, KMP, SNORMW)
  326. HES(LL+1,LL) = SNORMW
  327. CALL SHEQR(HES, MAXLP1, LL, Q, INFO, LL)
  328. IF (INFO .EQ. LL) GO TO 120
  329. C -------------------------------------------------------------------
  330. C Update RHO, the estimate of the norm of the residual R0-A*ZL.
  331. C If KMP < MAXL, then the vectors V(*,1),...,V(*,LL+1) are not
  332. C necessarily orthogonal for LL > KMP. The vector DL must then
  333. C be computed, and its norm used in the calculation of RHO.
  334. C -------------------------------------------------------------------
  335. PROD = PROD*Q(2*LL)
  336. RHO = ABS(PROD*R0NRM)
  337. IF ((LL.GT.KMP) .AND.(KMP.LT.MAXL)) THEN
  338. IF (LL .EQ. KMP+1) THEN
  339. CALL SCOPY(N, V(1,1), 1, DL, 1)
  340. DO 75 I = 1,KMP
  341. IP1 = I + 1
  342. I2 = I*2
  343. S = Q(I2)
  344. C = Q(I2-1)
  345. DO 70 K = 1,N
  346. DL(K) = S*DL(K) + C*V(K,IP1)
  347. 70 CONTINUE
  348. 75 CONTINUE
  349. ENDIF
  350. S = Q(2*LL)
  351. C = Q(2*LL-1)/SNORMW
  352. LLP1 = LL + 1
  353. DO 80 K = 1,N
  354. DL(K) = S*DL(K) + C*V(K,LLP1)
  355. 80 CONTINUE
  356. DLNRM = SNRM2(N, DL, 1)
  357. RHO = RHO*DLNRM
  358. ENDIF
  359. RHOL = RHO
  360. C -------------------------------------------------------------------
  361. C Test for convergence. If passed, compute approximation ZL.
  362. C If failed and LL < MAXL, then continue iterating.
  363. C -------------------------------------------------------------------
  364. ITER = NRSTS*MAXL + LGMR
  365. IF (ISSGMR(N, B, X, XL, NELT, IA, JA, A, ISYM, MSOLVE,
  366. $ NMSL, ITOL, TOL, ITMAX, ITER, ERR, IUNIT, DL, Z, WK,
  367. $ RPAR, IPAR, RHOL, BNRM, SR, SZ, JSCAL,
  368. $ KMP, LGMR, MAXL, MAXLP1, V, Q, SNORMW, PROD, R0NRM,
  369. $ HES, JPRE) .NE. 0) GO TO 200
  370. IF (LL .EQ. MAXL) GO TO 100
  371. C -------------------------------------------------------------------
  372. C Rescale so that the norm of V(1,LL+1) is one.
  373. C -------------------------------------------------------------------
  374. TEM = 1.0E0/SNORMW
  375. CALL SSCAL(N, TEM, V(1,LL+1), 1)
  376. 90 CONTINUE
  377. 100 CONTINUE
  378. IF (RHO .LT. R0NRM) GO TO 150
  379. 120 CONTINUE
  380. IFLAG = 2
  381. C
  382. C Load approximate solution with zero.
  383. C
  384. DO 130 I = 1,N
  385. Z(I) = 0
  386. 130 CONTINUE
  387. RETURN
  388. 150 IFLAG = 1
  389. C
  390. C Tolerance not met, but residual norm reduced.
  391. C
  392. IF (NRMAX .GT. 0) THEN
  393. C
  394. C If performing restarting (NRMAX > 0) calculate the residual
  395. C vector RL and store it in the DL array. If the incomplete
  396. C version is being used (KMP < MAXL) then DL has already been
  397. C calculated up to a scaling factor. Use SRLCAL to calculate
  398. C the scaled residual vector.
  399. C
  400. CALL SRLCAL(N, KMP, MAXL, MAXL, V, Q, DL, SNORMW, PROD,
  401. $ R0NRM)
  402. ENDIF
  403. C -------------------------------------------------------------------
  404. C Compute the approximation ZL to the solution. Since the
  405. C vector Z was used as workspace, and the initial guess
  406. C of the linear iteration is zero, Z must be reset to zero.
  407. C -------------------------------------------------------------------
  408. 200 CONTINUE
  409. LL = LGMR
  410. LLP1 = LL + 1
  411. DO 210 K = 1,LLP1
  412. R0(K) = 0
  413. 210 CONTINUE
  414. R0(1) = R0NRM
  415. CALL SHELS(HES, MAXLP1, LL, Q, R0)
  416. DO 220 K = 1,N
  417. Z(K) = 0
  418. 220 CONTINUE
  419. DO 230 I = 1,LL
  420. CALL SAXPY(N, R0(I), V(1,I), 1, Z, 1)
  421. 230 CONTINUE
  422. IF ((JSCAL .EQ. 1) .OR.(JSCAL .EQ. 3)) THEN
  423. DO 240 I = 1,N
  424. Z(I) = Z(I)/SZ(I)
  425. 240 CONTINUE
  426. ENDIF
  427. IF (JPRE .GT. 0) THEN
  428. CALL SCOPY(N, Z, 1, WK, 1)
  429. CALL MSOLVE(N, WK, Z, NELT, IA, JA, A, ISYM, RPAR, IPAR)
  430. NMSL = NMSL + 1
  431. ENDIF
  432. RETURN
  433. C------------- LAST LINE OF SPIGMR FOLLOWS ----------------------------
  434. END