ssiccg.f 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. *DECK SSICCG
  2. SUBROUTINE SSICCG (N, B, X, NELT, IA, JA, A, ISYM, ITOL, TOL,
  3. + ITMAX, ITER, ERR, IERR, IUNIT, RWORK, LENW, IWORK, LENIW)
  4. C***BEGIN PROLOGUE SSICCG
  5. C***PURPOSE Incomplete Cholesky Conjugate Gradient Sparse Ax=b Solver.
  6. C Routine to solve a symmetric positive definite linear
  7. C system Ax = b using the incomplete Cholesky
  8. C Preconditioned Conjugate Gradient method.
  9. C***LIBRARY SLATEC (SLAP)
  10. C***CATEGORY D2B4
  11. C***TYPE SINGLE PRECISION (SSICCG-S, DSICCG-D)
  12. C***KEYWORDS INCOMPLETE CHOLESKY, ITERATIVE PRECONDITION, SLAP, SPARSE,
  13. C SYMMETRIC LINEAR SYSTEM
  14. C***AUTHOR Greenbaum, Anne, (Courant Institute)
  15. C Seager, Mark K., (LLNL)
  16. C Lawrence Livermore National Laboratory
  17. C PO BOX 808, L-60
  18. C Livermore, CA 94550 (510) 423-3141
  19. C seager@llnl.gov
  20. C***DESCRIPTION
  21. C
  22. C *Usage:
  23. C INTEGER N, NELT, IA(NELT), JA(NELT), ISYM, ITOL, ITMAX
  24. C INTEGER ITER, IERR, IUNIT, LENW, IWORK(NL+2*N+1), LENIW
  25. C REAL B(N), X(N), A(NELT), TOL, ERR, RWORK(NL+5*N)
  26. C
  27. C CALL SSICCG(N, B, X, NELT, IA, JA, A, ISYM, ITOL, TOL,
  28. C $ ITMAX, ITER, ERR, IERR, IUNIT, RWORK, LENW, IWORK, LENIW )
  29. C
  30. C *Arguments:
  31. C N :IN Integer.
  32. C Order of the Matrix.
  33. C B :IN Real B(N).
  34. C Right-hand side vector.
  35. C X :INOUT Real X(N).
  36. C On input X is your initial guess for solution vector.
  37. C On output X is the final approximate solution.
  38. C NELT :IN Integer.
  39. C Number of Non-Zeros stored in A.
  40. C IA :INOUT Integer IA(NELT).
  41. C JA :INOUT Integer JA(NELT).
  42. C A :INOUT Real A(NELT).
  43. C These arrays should hold the matrix A in either the SLAP
  44. C Triad format or the SLAP Column format. See "Description",
  45. C below. If the SLAP Triad format is chosen it is changed
  46. C internally to the SLAP Column format.
  47. C ISYM :IN Integer.
  48. C Flag to indicate symmetric storage format.
  49. C If ISYM=0, all non-zero entries of the matrix are stored.
  50. C If ISYM=1, the matrix is symmetric, and only the upper
  51. C or lower triangle of the matrix is stored.
  52. C ITOL :IN Integer.
  53. C Flag to indicate type of convergence criterion.
  54. C If ITOL=1, iteration stops when the 2-norm of the residual
  55. C divided by the 2-norm of the right-hand side is less than TOL.
  56. C If ITOL=2, iteration stops when the 2-norm of M-inv times the
  57. C residual divided by the 2-norm of M-inv times the right hand
  58. C side is less than TOL, where M-inv is the inverse of the
  59. C diagonal of A.
  60. C ITOL=11 is often useful for checking and comparing different
  61. C routines. For this case, the user must supply the "exact"
  62. C solution or a very accurate approximation (one with an error
  63. C much less than TOL) through a common block,
  64. C COMMON /SSLBLK/ SOLN( )
  65. C If ITOL=11, iteration stops when the 2-norm of the difference
  66. C between the iterative approximation and the user-supplied
  67. C solution divided by the 2-norm of the user-supplied solution
  68. C is less than TOL. Note that this requires the user to set up
  69. C the "COMMON /SSLBLK/ SOLN(LENGTH)" in the calling routine.
  70. C The routine with this declaration should be loaded before the
  71. C stop test so that the correct length is used by the loader.
  72. C This procedure is not standard Fortran and may not work
  73. C correctly on your system (although it has worked on every
  74. C system the authors have tried). If ITOL is not 11 then this
  75. C common block is indeed standard Fortran.
  76. C TOL :INOUT Real.
  77. C Convergence criterion, as described above. (Reset if IERR=4.)
  78. C ITMAX :IN Integer.
  79. C Maximum number of iterations.
  80. C ITER :OUT Integer.
  81. C Number of iterations required to reach convergence, or
  82. C ITMAX+1 if convergence criterion could not be achieved in
  83. C ITMAX iterations.
  84. C ERR :OUT Real.
  85. C Error estimate of error in final approximate solution, as
  86. C defined by ITOL.
  87. C IERR :OUT Integer.
  88. C Return error flag.
  89. C IERR = 0 => All went well.
  90. C IERR = 1 => Insufficient space allocated for WORK or IWORK.
  91. C IERR = 2 => Method failed to converge in ITMAX steps.
  92. C IERR = 3 => Error in user input.
  93. C Check input values of N, ITOL.
  94. C IERR = 4 => User error tolerance set too tight.
  95. C Reset to 500*R1MACH(3). Iteration proceeded.
  96. C IERR = 5 => Preconditioning matrix, M, is not positive
  97. C definite. (r,z) < 0.
  98. C IERR = 6 => Matrix A is not positive definite. (p,Ap) < 0.
  99. C IERR = 7 => Incomplete factorization broke down and was
  100. C fudged. Resulting preconditioning may be less
  101. C than the best.
  102. C IUNIT :IN Integer.
  103. C Unit number on which to write the error at each iteration,
  104. C if this is desired for monitoring convergence. If unit
  105. C number is 0, no writing will occur.
  106. C RWORK :WORK Real RWORK(LENW).
  107. C Real array used for workspace.
  108. C LENW :IN Integer.
  109. C Length of the real workspace, RWORK. LENW >= NL+5*N.
  110. C NL is the number of non-zeros in the lower triangle of the
  111. C matrix (including the diagonal).
  112. C IWORK :WORK Integer IWORK(LENIW).
  113. C Integer array used for workspace.
  114. C Upon return the following locations of IWORK hold information
  115. C which may be of use to the user:
  116. C IWORK(9) Amount of Integer workspace actually used.
  117. C IWORK(10) Amount of Real workspace actually used.
  118. C LENIW :IN Integer.
  119. C Length of the integer workspace, IWORK. LENIW >= NL+N+11.
  120. C NL is the number of non-zeros in the lower triangle of the
  121. C matrix (including the diagonal).
  122. C
  123. C *Description:
  124. C This routine performs preconditioned conjugate gradient
  125. C method on the symmetric positive definite linear system
  126. C Ax=b. The preconditioner is the incomplete Cholesky (IC)
  127. C factorization of the matrix A. See SSICS for details about
  128. C the incomplete factorization algorithm. One should note
  129. C here however, that the IC factorization is a slow process
  130. C and that one should save factorizations for reuse, if
  131. C possible. The MSOLVE operation (handled in SSLLTI) does
  132. C vectorize on machines with hardware gather/scatter and is
  133. C quite fast.
  134. C
  135. C The Sparse Linear Algebra Package (SLAP) utilizes two matrix
  136. C data structures: 1) the SLAP Triad format or 2) the SLAP
  137. C Column format. The user can hand this routine either of the
  138. C of these data structures and SLAP will figure out which on
  139. C is being used and act accordingly.
  140. C
  141. C =================== S L A P Triad format ===================
  142. C
  143. C This routine requires that the matrix A be stored in the
  144. C SLAP Triad format. In this format only the non-zeros are
  145. C stored. They may appear in *ANY* order. The user supplies
  146. C three arrays of length NELT, where NELT is the number of
  147. C non-zeros in the matrix: (IA(NELT), JA(NELT), A(NELT)). For
  148. C each non-zero the user puts the row and column index of that
  149. C matrix element in the IA and JA arrays. The value of the
  150. C non-zero matrix element is placed in the corresponding
  151. C location of the A array. This is an extremely easy data
  152. C structure to generate. On the other hand it is not too
  153. C efficient on vector computers for the iterative solution of
  154. C linear systems. Hence, SLAP changes this input data
  155. C structure to the SLAP Column format for the iteration (but
  156. C does not change it back).
  157. C
  158. C Here is an example of the SLAP Triad storage format for a
  159. C 5x5 Matrix. Recall that the entries may appear in any order.
  160. C
  161. C 5x5 Matrix SLAP Triad format for 5x5 matrix on left.
  162. C 1 2 3 4 5 6 7 8 9 10 11
  163. C |11 12 0 0 15| A: 51 12 11 33 15 53 55 22 35 44 21
  164. C |21 22 0 0 0| IA: 5 1 1 3 1 5 5 2 3 4 2
  165. C | 0 0 33 0 35| JA: 1 2 1 3 5 3 5 2 5 4 1
  166. C | 0 0 0 44 0|
  167. C |51 0 53 0 55|
  168. C
  169. C =================== S L A P Column format ==================
  170. C
  171. C This routine requires that the matrix A be stored in the
  172. C SLAP Column format. In this format the non-zeros are stored
  173. C counting down columns (except for the diagonal entry, which
  174. C must appear first in each "column") and are stored in the
  175. C real array A. In other words, for each column in the matrix
  176. C put the diagonal entry in A. Then put in the other non-zero
  177. C elements going down the column (except the diagonal) in
  178. C order. The IA array holds the row index for each non-zero.
  179. C The JA array holds the offsets into the IA, A arrays for the
  180. C beginning of each column. That is, IA(JA(ICOL)),
  181. C A(JA(ICOL)) points to the beginning of the ICOL-th column in
  182. C IA and A. IA(JA(ICOL+1)-1), A(JA(ICOL+1)-1) points to the
  183. C end of the ICOL-th column. Note that we always have
  184. C JA(N+1) = NELT+1, where N is the number of columns in the
  185. C matrix and NELT is the number of non-zeros in the matrix.
  186. C
  187. C Here is an example of the SLAP Column storage format for a
  188. C 5x5 Matrix (in the A and IA arrays '|' denotes the end of a
  189. C column):
  190. C
  191. C 5x5 Matrix SLAP Column format for 5x5 matrix on left.
  192. C 1 2 3 4 5 6 7 8 9 10 11
  193. C |11 12 0 0 15| A: 11 21 51 | 22 12 | 33 53 | 44 | 55 15 35
  194. C |21 22 0 0 0| IA: 1 2 5 | 2 1 | 3 5 | 4 | 5 1 3
  195. C | 0 0 33 0 35| JA: 1 4 6 8 9 12
  196. C | 0 0 0 44 0|
  197. C |51 0 53 0 55|
  198. C
  199. C *Side Effects:
  200. C The SLAP Triad format (IA, JA, A) is modified internally to be
  201. C the SLAP Column format. See above.
  202. C
  203. C *Cautions:
  204. C This routine will attempt to write to the Fortran logical output
  205. C unit IUNIT, if IUNIT .ne. 0. Thus, the user must make sure that
  206. C this logical unit is attached to a file or terminal before calling
  207. C this routine with a non-zero value for IUNIT. This routine does
  208. C not check for the validity of a non-zero IUNIT unit number.
  209. C
  210. C***SEE ALSO SCG, SSLLTI
  211. C***REFERENCES 1. Louis Hageman and David Young, Applied Iterative
  212. C Methods, Academic Press, New York, 1981.
  213. C 2. Concus, Golub and O'Leary, A Generalized Conjugate
  214. C Gradient Method for the Numerical Solution of
  215. C Elliptic Partial Differential Equations, in Sparse
  216. C Matrix Computations, Bunch and Rose, Eds., Academic
  217. C Press, New York, 1979.
  218. C***ROUTINES CALLED SCG, SCHKW, SS2Y, SSICS, SSLLTI, SSMV, XERMSG
  219. C***REVISION HISTORY (YYMMDD)
  220. C 871119 DATE WRITTEN
  221. C 881213 Previous REVISION DATE
  222. C 890915 Made changes requested at July 1989 CML Meeting. (MKS)
  223. C 890921 Removed TeX from comments. (FNF)
  224. C 890922 Numerous changes to prologue to make closer to SLATEC
  225. C standard. (FNF)
  226. C 890929 Numerous changes to reduce SP/DP differences. (FNF)
  227. C 900805 Changed XERRWV calls to calls to XERMSG. (RWC)
  228. C 910411 Prologue converted to Version 4.0 format. (BAB)
  229. C 920407 COMMON BLOCK renamed SSLBLK. (WRB)
  230. C 920511 Added complete declaration section. (WRB)
  231. C 920929 Corrected format of references. (FNF)
  232. C 921019 Corrected NEL to NL. (FNF)
  233. C***END PROLOGUE SSICCG
  234. C .. Parameters ..
  235. INTEGER LOCRB, LOCIB
  236. PARAMETER (LOCRB=1, LOCIB=11)
  237. C .. Scalar Arguments ..
  238. REAL ERR, TOL
  239. INTEGER IERR, ISYM, ITER, ITMAX, ITOL, IUNIT, LENIW, LENW, N, NELT
  240. C .. Array Arguments ..
  241. REAL A(NELT), B(N), RWORK(LENW), X(N)
  242. INTEGER IA(NELT), IWORK(LENIW), JA(NELT)
  243. C .. Local Scalars ..
  244. INTEGER LOCDIN, LOCDZ, LOCEL, LOCIEL, LOCIW, LOCJEL, LOCP, LOCR,
  245. + LOCW, LOCZ, NL
  246. CHARACTER XERN1*8
  247. C .. External Subroutines ..
  248. EXTERNAL SCG, SCHKW, SS2Y, SSICS, SSLLTI, SSMV, XERMSG
  249. C***FIRST EXECUTABLE STATEMENT SSICCG
  250. C
  251. IERR = 0
  252. IF( N.LT.1 .OR. NELT.LT.1 ) THEN
  253. IERR = 3
  254. RETURN
  255. ENDIF
  256. C
  257. C Change the SLAP input matrix IA, JA, A to SLAP-Column format.
  258. CALL SS2Y( N, NELT, IA, JA, A, ISYM )
  259. C
  260. C Count number of elements in lower triangle of the matrix.
  261. C Then set up the work arrays.
  262. IF( ISYM.EQ.0 ) THEN
  263. NL = (NELT + N)/2
  264. ELSE
  265. NL = NELT
  266. ENDIF
  267. C
  268. LOCJEL = LOCIB
  269. LOCIEL = LOCJEL + NL
  270. LOCIW = LOCIEL + N + 1
  271. C
  272. LOCEL = LOCRB
  273. LOCDIN = LOCEL + NL
  274. LOCR = LOCDIN + N
  275. LOCZ = LOCR + N
  276. LOCP = LOCZ + N
  277. LOCDZ = LOCP + N
  278. LOCW = LOCDZ + N
  279. C
  280. C Check the workspace allocations.
  281. CALL SCHKW( 'SSICCG', LOCIW, LENIW, LOCW, LENW, IERR, ITER, ERR )
  282. IF( IERR.NE.0 ) RETURN
  283. C
  284. IWORK(1) = NL
  285. IWORK(2) = LOCJEL
  286. IWORK(3) = LOCIEL
  287. IWORK(4) = LOCEL
  288. IWORK(5) = LOCDIN
  289. IWORK(9) = LOCIW
  290. IWORK(10) = LOCW
  291. C
  292. C Compute the Incomplete Cholesky decomposition.
  293. C
  294. CALL SSICS(N, NELT, IA, JA, A, ISYM, NL, IWORK(LOCIEL),
  295. $ IWORK(LOCJEL), RWORK(LOCEL), RWORK(LOCDIN),
  296. $ RWORK(LOCR), IERR )
  297. IF( IERR.NE.0 ) THEN
  298. WRITE (XERN1, '(I8)') IERR
  299. CALL XERMSG ('SLATEC', 'SSICCG',
  300. $ 'IC factorization broke down on step ' // XERN1 //
  301. $ '. Diagonal was set to unity and factorization proceeded.',
  302. $ 1, 1)
  303. IERR = 7
  304. ENDIF
  305. C
  306. C Do the Preconditioned Conjugate Gradient.
  307. CALL SCG(N, B, X, NELT, IA, JA, A, ISYM, SSMV, SSLLTI,
  308. $ ITOL, TOL, ITMAX, ITER, ERR, IERR, IUNIT, RWORK(LOCR),
  309. $ RWORK(LOCZ), RWORK(LOCP), RWORK(LOCDZ), RWORK(1),
  310. $ IWORK(1))
  311. RETURN
  312. C------------- LAST LINE OF SSICCG FOLLOWS ----------------------------
  313. END