scgs.f 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. *DECK SCGS
  2. SUBROUTINE SCGS (N, B, X, NELT, IA, JA, A, ISYM, MATVEC, MSOLVE,
  3. + ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, R0, P, Q, U, V1,
  4. + V2, RWORK, IWORK)
  5. C***BEGIN PROLOGUE SCGS
  6. C***PURPOSE Preconditioned BiConjugate Gradient Squared Ax=b Solver.
  7. C Routine to solve a Non-Symmetric linear system Ax = b
  8. C using the Preconditioned BiConjugate Gradient Squared
  9. C method.
  10. C***LIBRARY SLATEC (SLAP)
  11. C***CATEGORY D2A4, D2B4
  12. C***TYPE SINGLE PRECISION (SCGS-S, DCGS-D)
  13. C***KEYWORDS BICONJUGATE GRADIENT, ITERATIVE PRECONDITION,
  14. C NON-SYMMETRIC LINEAR SYSTEM, SLAP, SPARSE
  15. C***AUTHOR Greenbaum, Anne, (Courant Institute)
  16. C Seager, Mark K., (LLNL)
  17. C Lawrence Livermore National Laboratory
  18. C PO BOX 808, L-60
  19. C Livermore, CA 94550 (510) 423-3141
  20. C seager@llnl.gov
  21. C***DESCRIPTION
  22. C
  23. C *Usage:
  24. C INTEGER N, NELT, IA(NELT), JA(NELT), ISYM, ITOL, ITMAX
  25. C INTEGER ITER, IERR, IUNIT, IWORK(USER DEFINED)
  26. C REAL B(N), X(N), A(NELT), TOL, ERR, R(N), R0(N), P(N)
  27. C REAL Q(N), U(N), V1(N), V2(N), RWORK(USER DEFINED)
  28. C EXTERNAL MATVEC, MSOLVE
  29. C
  30. C CALL SCGS(N, B, X, NELT, IA, JA, A, ISYM, MATVEC,
  31. C $ MSOLVE, ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT,
  32. C $ R, R0, P, Q, U, V1, V2, RWORK, IWORK)
  33. C
  34. C *Arguments:
  35. C N :IN Integer
  36. C Order of the Matrix.
  37. C B :IN Real B(N).
  38. C Right-hand side vector.
  39. C X :INOUT Real X(N).
  40. C On input X is your initial guess for solution vector.
  41. C On output X is the final approximate solution.
  42. C NELT :IN Integer.
  43. C Number of Non-Zeros stored in A.
  44. C IA :IN Integer IA(NELT).
  45. C JA :IN Integer JA(NELT).
  46. C A :IN Real A(NELT).
  47. C These arrays contain the matrix data structure for A.
  48. C It could take any form. See "Description", below,
  49. C for more details.
  50. C ISYM :IN Integer.
  51. C Flag to indicate symmetric storage format.
  52. C If ISYM=0, all non-zero entries of the matrix are stored.
  53. C If ISYM=1, the matrix is symmetric, and only the upper
  54. C or lower triangle of the matrix is stored.
  55. C MATVEC :EXT External.
  56. C Name of a routine which performs the matrix vector multiply
  57. C operation Y = A*X given A and X. The name of the MATVEC
  58. C routine must be declared external in the calling program.
  59. C The calling sequence of MATVEC is:
  60. C CALL MATVEC( N, X, Y, NELT, IA, JA, A, ISYM )
  61. C Where N is the number of unknowns, Y is the product A*X upon
  62. C return, X is an input vector. NELT, IA, JA, A and ISYM
  63. C define the SLAP matrix data structure: see Description,below.
  64. C MSOLVE :EXT External.
  65. C Name of a routine which solves a linear system MZ = R for Z
  66. C given R with the preconditioning matrix M (M is supplied via
  67. C RWORK and IWORK arrays). The name of the MSOLVE routine
  68. C must be declared external in the calling program. The
  69. C calling sequence of MSOLVE is:
  70. C CALL MSOLVE(N, R, Z, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  71. C Where N is the number of unknowns, R is the right-hand side
  72. C vector, and Z is the solution upon return. NELT, IA, JA, A
  73. C and ISYM define the SLAP matrix data structure: see
  74. C Description, below. RWORK is a real array that can be used
  75. C to pass necessary preconditioning information and/or
  76. C workspace to MSOLVE. IWORK is an integer work array for the
  77. C same purpose as RWORK.
  78. C ITOL :IN Integer.
  79. C Flag to indicate type of convergence criterion.
  80. C If ITOL=1, iteration stops when the 2-norm of the residual
  81. C divided by the 2-norm of the right-hand side is less than TOL.
  82. C This routine must calculate the residual from R = A*X - B.
  83. C This is unnatural and hence expensive for this type of iter-
  84. C ative method. ITOL=2 is *STRONGLY* recommended.
  85. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  86. C residual divided by the 2-norm of M-inv times the right hand
  87. C side is less than TOL, where M-inv time a vector is the pre-
  88. C conditioning step. This is the *NATURAL* stopping for this
  89. C iterative method and is *STRONGLY* recommended.
  90. C ITOL=11 is often useful for checking and comparing different
  91. C routines. For this case, the user must supply the "exact"
  92. C solution or a very accurate approximation (one with an error
  93. C much less than TOL) through a common block,
  94. C COMMON /SSLBLK/ SOLN( )
  95. C If ITOL=11, iteration stops when the 2-norm of the difference
  96. C between the iterative approximation and the user-supplied
  97. C solution divided by the 2-norm of the user-supplied solution
  98. C is less than TOL.
  99. C TOL :INOUT Real.
  100. C Convergence criterion, as described above. (Reset if IERR=4.)
  101. C ITMAX :IN Integer.
  102. C Maximum number of iterations.
  103. C ITER :OUT Integer.
  104. C Number of iterations required to reach convergence, or
  105. C ITMAX+1 if convergence criterion could not be achieved in
  106. C ITMAX iterations.
  107. C ERR :OUT Real.
  108. C Error estimate of error in final approximate solution, as
  109. C defined by ITOL.
  110. C IERR :OUT Integer.
  111. C Return error flag.
  112. C IERR = 0 => All went well.
  113. C IERR = 1 => Insufficient space allocated for WORK or IWORK.
  114. C IERR = 2 => Method failed to converge in ITMAX steps.
  115. C IERR = 3 => Error in user input.
  116. C Check input values of N, ITOL.
  117. C IERR = 4 => User error tolerance set too tight.
  118. C Reset to 500*R1MACH(3). Iteration proceeded.
  119. C IERR = 5 => Breakdown of the method detected.
  120. C (r0,r) approximately 0.
  121. C IERR = 6 => Stagnation of the method detected.
  122. C (r0,v) approximately 0.
  123. C IUNIT :IN Integer.
  124. C Unit number on which to write the error at each iteration,
  125. C if this is desired for monitoring convergence. If unit
  126. C number is 0, no writing will occur.
  127. C R :WORK Real R(N).
  128. C R0 :WORK Real R0(N).
  129. C P :WORK Real P(N).
  130. C Q :WORK Real Q(N).
  131. C U :WORK Real U(N).
  132. C V1 :WORK Real V1(N).
  133. C V2 :WORK Real V2(N).
  134. C Real arrays used for workspace.
  135. C RWORK :WORK Real RWORK(USER DEFINED).
  136. C Real array that can be used for workspace in MSOLVE.
  137. C IWORK :WORK Integer IWORK(USER DEFINED).
  138. C Integer array that can be used for workspace in MSOLVE.
  139. C
  140. C *Description
  141. C This routine does not care what matrix data structure is
  142. C used for A and M. It simply calls the MATVEC and MSOLVE
  143. C routines, with the arguments as described above. The user
  144. C could write any type of structure and the appropriate MATVEC
  145. C and MSOLVE routines. It is assumed that A is stored in the
  146. C IA, JA, A arrays in some fashion and that M (or INV(M)) is
  147. C stored in IWORK and RWORK in some fashion. The SLAP
  148. C routines SSDBCG and SSLUCS are examples of this procedure.
  149. C
  150. C Two examples of matrix data structures are the: 1) SLAP
  151. C Triad format and 2) SLAP Column format.
  152. C
  153. C =================== S L A P Triad format ===================
  154. C
  155. C In this format only the non-zeros are stored. They may
  156. C appear in *ANY* order. The user supplies three arrays of
  157. C length NELT, where NELT is the number of non-zeros in the
  158. C matrix: (IA(NELT), JA(NELT), A(NELT)). For each non-zero
  159. C the user puts the row and column index of that matrix
  160. C element in the IA and JA arrays. The value of the non-zero
  161. C matrix element is placed in the corresponding location of
  162. C the A array. This is an extremely easy data structure to
  163. C generate. On the other hand it is not too efficient on
  164. C vector computers for the iterative solution of linear
  165. C systems. Hence, SLAP changes this input data structure to
  166. C the SLAP Column format for the iteration (but does not
  167. C change it back).
  168. C
  169. C Here is an example of the SLAP Triad storage format for a
  170. C 5x5 Matrix. Recall that the entries may appear in any order.
  171. C
  172. C 5x5 Matrix SLAP Triad format for 5x5 matrix on left.
  173. C 1 2 3 4 5 6 7 8 9 10 11
  174. C |11 12 0 0 15| A: 51 12 11 33 15 53 55 22 35 44 21
  175. C |21 22 0 0 0| IA: 5 1 1 3 1 5 5 2 3 4 2
  176. C | 0 0 33 0 35| JA: 1 2 1 3 5 3 5 2 5 4 1
  177. C | 0 0 0 44 0|
  178. C |51 0 53 0 55|
  179. C
  180. C =================== S L A P Column format ==================
  181. C
  182. C In this format the non-zeros are stored counting down
  183. C columns (except for the diagonal entry, which must appear
  184. C first in each "column") and are stored in the real array A.
  185. C In other words, for each column in the matrix put the
  186. C diagonal entry in A. Then put in the other non-zero
  187. C elements going down the column (except the diagonal) in
  188. C order. The IA array holds the row index for each non-zero.
  189. C The JA array holds the offsets into the IA, A arrays for the
  190. C beginning of each column. That is, IA(JA(ICOL)),
  191. C A(JA(ICOL)) points to the beginning of the ICOL-th column in
  192. C IA and A. IA(JA(ICOL+1)-1), A(JA(ICOL+1)-1) points to the
  193. C end of the ICOL-th column. Note that we always have JA(N+1)
  194. C = NELT+1, where N is the number of columns in the matrix and
  195. C NELT is the number of non-zeros in the matrix.
  196. C
  197. C Here is an example of the SLAP Column storage format for a
  198. C 5x5 Matrix (in the A and IA arrays '|' denotes the end of a
  199. C column):
  200. C
  201. C 5x5 Matrix SLAP Column format for 5x5 matrix on left.
  202. C 1 2 3 4 5 6 7 8 9 10 11
  203. C |11 12 0 0 15| A: 11 21 51 | 22 12 | 33 53 | 44 | 55 15 35
  204. C |21 22 0 0 0| IA: 1 2 5 | 2 1 | 3 5 | 4 | 5 1 3
  205. C | 0 0 33 0 35| JA: 1 4 6 8 9 12
  206. C | 0 0 0 44 0|
  207. C |51 0 53 0 55|
  208. C
  209. C *Cautions:
  210. C This routine will attempt to write to the Fortran logical output
  211. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  212. C this logical unit is attached to a file or terminal before calling
  213. C this routine with a non-zero value for IUNIT. This routine does
  214. C not check for the validity of a non-zero IUNIT unit number.
  215. C
  216. C***SEE ALSO SSDCGS, SSLUCS
  217. C***REFERENCES 1. P. Sonneveld, CGS, a fast Lanczos-type solver
  218. C for nonsymmetric linear systems, Delft University
  219. C of Technology Report 84-16, Department of Mathe-
  220. C matics and Informatics, Delft, The Netherlands.
  221. C 2. E. F. Kaasschieter, The solution of non-symmetric
  222. C linear systems by biconjugate gradients or conjugate
  223. C gradients squared, Delft University of Technology
  224. C Report 86-21, Department of Mathematics and Informa-
  225. C tics, Delft, The Netherlands.
  226. C 3. Mark K. Seager, A SLAP for the Masses, in
  227. C G. F. Carey, Ed., Parallel Supercomputing: Methods,
  228. C Algorithms and Applications, Wiley, 1989, pp.135-155.
  229. C***ROUTINES CALLED ISSCGS, R1MACH, SAXPY, SDOT
  230. C***REVISION HISTORY (YYMMDD)
  231. C 871119 DATE WRITTEN
  232. C 881213 Previous REVISION DATE
  233. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  234. C 890921 Removed TeX from comments. (FNF)
  235. C 890922 Numerous changes to prologue to make closer to SLATEC
  236. C standard. (FNF)
  237. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  238. C 891004 Added new reference.
  239. C 910411 Prologue converted to Version 4.0 format. (BAB)
  240. C 910502 Removed MATVEC and MSOLVE from ROUTINES CALLED list. (FNF)
  241. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  242. C 920511 Added complete declaration section. (WRB)
  243. C 920929 Corrected format of references. (FNF)
  244. C 921019 Changed 500.0 to 500 to reduce SP/DP differences. (FNF)
  245. C 921113 Corrected C***CATEGORY line. (FNF)
  246. C***END PROLOGUE SCGS
  247. C .. Scalar Arguments ..
  248. REAL ERR, TOL
  249. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, N, NELT
  250. C .. Array Arguments ..
  251. REAL A(NELT), B(N), P(N), Q(N), R(N), R0(N), RWORK(*), U(N),
  252. + V1(N), V2(N), X(N)
  253. INTEGER IA(NELT), IWORK(*), JA(NELT)
  254. C .. Subroutine Arguments ..
  255. EXTERNAL MATVEC, MSOLVE
  256. C .. Local Scalars ..
  257. REAL AK, AKM, BK, BNRM, FUZZ, RHON, RHONM1, SIGMA, SOLNRM, TOLMIN
  258. INTEGER I, K
  259. C .. External Functions ..
  260. REAL R1MACH, SDOT
  261. INTEGER ISSCGS
  262. EXTERNAL R1MACH, SDOT, ISSCGS
  263. C .. External Subroutines ..
  264. EXTERNAL SAXPY
  265. C .. Intrinsic Functions ..
  266. INTRINSIC ABS
  267. C***FIRST EXECUTABLE STATEMENT SCGS
  268. C
  269. C Check some of the input data.
  270. C
  271. ITER = 0
  272. IERR = 0
  273. IF( N.LT.1 ) THEN
  274. IERR = 3
  275. RETURN
  276. ENDIF
  277. TOLMIN = 500*R1MACH(3)
  278. IF( TOL.LT.TOLMIN ) THEN
  279. TOL = TOLMIN
  280. IERR = 4
  281. ENDIF
  282. C
  283. C Calculate initial residual and pseudo-residual, and check
  284. C stopping criterion.
  285. CALL MATVEC(N, X, R, NELT, IA, JA, A, ISYM)
  286. DO 10 I = 1, N
  287. V1(I) = R(I) - B(I)
  288. 10 CONTINUE
  289. CALL MSOLVE(N, V1, R, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  290. C
  291. IF( ISSCGS(N, B, X, NELT, IA, JA, A, ISYM, MATVEC, MSOLVE,
  292. $ ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, R0, P, Q,
  293. $ U, V1, V2, RWORK, IWORK, AK, BK, BNRM, SOLNRM) .NE. 0 )
  294. $ GO TO 200
  295. IF( IERR.NE.0 ) RETURN
  296. C
  297. C Set initial values.
  298. C
  299. FUZZ = R1MACH(3)**2
  300. DO 20 I = 1, N
  301. R0(I) = R(I)
  302. 20 CONTINUE
  303. RHONM1 = 1
  304. C
  305. C ***** ITERATION LOOP *****
  306. C
  307. DO 100 K=1,ITMAX
  308. ITER = K
  309. C
  310. C Calculate coefficient BK and direction vectors U, V and P.
  311. RHON = SDOT(N, R0, 1, R, 1)
  312. IF( ABS(RHONM1).LT.FUZZ ) GOTO 998
  313. BK = RHON/RHONM1
  314. IF( ITER.EQ.1 ) THEN
  315. DO 30 I = 1, N
  316. U(I) = R(I)
  317. P(I) = R(I)
  318. 30 CONTINUE
  319. ELSE
  320. DO 40 I = 1, N
  321. U(I) = R(I) + BK*Q(I)
  322. V1(I) = Q(I) + BK*P(I)
  323. 40 CONTINUE
  324. DO 50 I = 1, N
  325. P(I) = U(I) + BK*V1(I)
  326. 50 CONTINUE
  327. ENDIF
  328. C
  329. C Calculate coefficient AK, new iterate X, Q
  330. CALL MATVEC(N, P, V2, NELT, IA, JA, A, ISYM)
  331. CALL MSOLVE(N, V2, V1, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  332. SIGMA = SDOT(N, R0, 1, V1, 1)
  333. IF( ABS(SIGMA).LT.FUZZ ) GOTO 999
  334. AK = RHON/SIGMA
  335. AKM = -AK
  336. DO 60 I = 1, N
  337. Q(I) = U(I) + AKM*V1(I)
  338. 60 CONTINUE
  339. DO 70 I = 1, N
  340. V1(I) = U(I) + Q(I)
  341. 70 CONTINUE
  342. C X = X - ak*V1.
  343. CALL SAXPY( N, AKM, V1, 1, X, 1 )
  344. C -1
  345. C R = R - ak*M *A*V1
  346. CALL MATVEC(N, V1, V2, NELT, IA, JA, A, ISYM)
  347. CALL MSOLVE(N, V2, V1, NELT, IA, JA, A, ISYM, RWORK, IWORK)
  348. CALL SAXPY( N, AKM, V1, 1, R, 1 )
  349. C
  350. C check stopping criterion.
  351. IF( ISSCGS(N, B, X, NELT, IA, JA, A, ISYM, MATVEC, MSOLVE,
  352. $ ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, R, R0, P, Q,
  353. $ U, V1, V2, RWORK, IWORK, AK, BK, BNRM, SOLNRM) .NE. 0 )
  354. $ GO TO 200
  355. C
  356. C Update RHO.
  357. RHONM1 = RHON
  358. 100 CONTINUE
  359. C
  360. C ***** end of loop *****
  361. C Stopping criterion not satisfied.
  362. ITER = ITMAX + 1
  363. IERR = 2
  364. 200 RETURN
  365. C
  366. C Breakdown of method detected.
  367. 998 IERR = 5
  368. RETURN
  369. C
  370. C Stagnation of method detected.
  371. 999 IERR = 6
  372. RETURN
  373. C------------- LAST LINE OF SCGS FOLLOWS ----------------------------
  374. END