sbcg.f 17 KB

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