dcg.f 15 KB

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