scgn.f 17 KB

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