dgbrfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* dgbrfs.f -- translated by f2c (version 20061008).
  2. You must link the resulting object file with libf2c:
  3. on Microsoft Windows system, link with libf2c.lib;
  4. on Linux or Unix systems, link with .../path/to/libf2c.a -lm
  5. or, if you install libf2c.a in a standard place, with -lf2c -lm
  6. -- in that order, at the end of the command line, as in
  7. cc *.o -lf2c -lm
  8. Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
  9. http://www.netlib.org/f2c/libf2c.zip
  10. */
  11. #include "f2c.h"
  12. #include "blaswrap.h"
  13. /* Table of constant values */
  14. static integer c__1 = 1;
  15. static doublereal c_b15 = -1.;
  16. static doublereal c_b17 = 1.;
  17. /* Subroutine */ int _starpu_dgbrfs_(char *trans, integer *n, integer *kl, integer *
  18. ku, integer *nrhs, doublereal *ab, integer *ldab, doublereal *afb,
  19. integer *ldafb, integer *ipiv, doublereal *b, integer *ldb,
  20. doublereal *x, integer *ldx, doublereal *ferr, doublereal *berr,
  21. doublereal *work, integer *iwork, integer *info)
  22. {
  23. /* System generated locals */
  24. integer ab_dim1, ab_offset, afb_dim1, afb_offset, b_dim1, b_offset,
  25. x_dim1, x_offset, i__1, i__2, i__3, i__4, i__5, i__6, i__7;
  26. doublereal d__1, d__2, d__3;
  27. /* Local variables */
  28. integer i__, j, k;
  29. doublereal s;
  30. integer kk;
  31. doublereal xk;
  32. integer nz;
  33. doublereal eps;
  34. integer kase;
  35. doublereal safe1, safe2;
  36. extern /* Subroutine */ int _starpu_dgbmv_(char *, integer *, integer *, integer *
  37. , integer *, doublereal *, doublereal *, integer *, doublereal *,
  38. integer *, doublereal *, doublereal *, integer *);
  39. extern logical _starpu_lsame_(char *, char *);
  40. integer isave[3];
  41. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  42. doublereal *, integer *), _starpu_daxpy_(integer *, doublereal *,
  43. doublereal *, integer *, doublereal *, integer *);
  44. integer count;
  45. extern /* Subroutine */ int _starpu_dlacn2_(integer *, doublereal *, doublereal *,
  46. integer *, doublereal *, integer *, integer *);
  47. extern doublereal _starpu_dlamch_(char *);
  48. doublereal safmin;
  49. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *), _starpu_dgbtrs_(
  50. char *, integer *, integer *, integer *, integer *, doublereal *,
  51. integer *, integer *, doublereal *, integer *, integer *);
  52. logical notran;
  53. char transt[1];
  54. doublereal lstres;
  55. /* -- LAPACK routine (version 3.2) -- */
  56. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  57. /* November 2006 */
  58. /* Modified to call DLACN2 in place of DLACON, 5 Feb 03, SJH. */
  59. /* .. Scalar Arguments .. */
  60. /* .. */
  61. /* .. Array Arguments .. */
  62. /* .. */
  63. /* Purpose */
  64. /* ======= */
  65. /* DGBRFS improves the computed solution to a system of linear */
  66. /* equations when the coefficient matrix is banded, and provides */
  67. /* error bounds and backward error estimates for the solution. */
  68. /* Arguments */
  69. /* ========= */
  70. /* TRANS (input) CHARACTER*1 */
  71. /* Specifies the form of the system of equations: */
  72. /* = 'N': A * X = B (No transpose) */
  73. /* = 'T': A**T * X = B (Transpose) */
  74. /* = 'C': A**H * X = B (Conjugate transpose = Transpose) */
  75. /* N (input) INTEGER */
  76. /* The order of the matrix A. N >= 0. */
  77. /* KL (input) INTEGER */
  78. /* The number of subdiagonals within the band of A. KL >= 0. */
  79. /* KU (input) INTEGER */
  80. /* The number of superdiagonals within the band of A. KU >= 0. */
  81. /* NRHS (input) INTEGER */
  82. /* The number of right hand sides, i.e., the number of columns */
  83. /* of the matrices B and X. NRHS >= 0. */
  84. /* AB (input) DOUBLE PRECISION array, dimension (LDAB,N) */
  85. /* The original band matrix A, stored in rows 1 to KL+KU+1. */
  86. /* The j-th column of A is stored in the j-th column of the */
  87. /* array AB as follows: */
  88. /* AB(ku+1+i-j,j) = A(i,j) for max(1,j-ku)<=i<=min(n,j+kl). */
  89. /* LDAB (input) INTEGER */
  90. /* The leading dimension of the array AB. LDAB >= KL+KU+1. */
  91. /* AFB (input) DOUBLE PRECISION array, dimension (LDAFB,N) */
  92. /* Details of the LU factorization of the band matrix A, as */
  93. /* computed by DGBTRF. U is stored as an upper triangular band */
  94. /* matrix with KL+KU superdiagonals in rows 1 to KL+KU+1, and */
  95. /* the multipliers used during the factorization are stored in */
  96. /* rows KL+KU+2 to 2*KL+KU+1. */
  97. /* LDAFB (input) INTEGER */
  98. /* The leading dimension of the array AFB. LDAFB >= 2*KL*KU+1. */
  99. /* IPIV (input) INTEGER array, dimension (N) */
  100. /* The pivot indices from DGBTRF; for 1<=i<=N, row i of the */
  101. /* matrix was interchanged with row IPIV(i). */
  102. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  103. /* The right hand side matrix B. */
  104. /* LDB (input) INTEGER */
  105. /* The leading dimension of the array B. LDB >= max(1,N). */
  106. /* X (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  107. /* On entry, the solution matrix X, as computed by DGBTRS. */
  108. /* On exit, the improved solution matrix X. */
  109. /* LDX (input) INTEGER */
  110. /* The leading dimension of the array X. LDX >= max(1,N). */
  111. /* FERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  112. /* The estimated forward error bound for each solution vector */
  113. /* X(j) (the j-th column of the solution matrix X). */
  114. /* If XTRUE is the true solution corresponding to X(j), FERR(j) */
  115. /* is an estimated upper bound for the magnitude of the largest */
  116. /* element in (X(j) - XTRUE) divided by the magnitude of the */
  117. /* largest element in X(j). The estimate is as reliable as */
  118. /* the estimate for RCOND, and is almost always a slight */
  119. /* overestimate of the true error. */
  120. /* BERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  121. /* The componentwise relative backward error of each solution */
  122. /* vector X(j) (i.e., the smallest relative change in */
  123. /* any element of A or B that makes X(j) an exact solution). */
  124. /* WORK (workspace) DOUBLE PRECISION array, dimension (3*N) */
  125. /* IWORK (workspace) INTEGER array, dimension (N) */
  126. /* INFO (output) INTEGER */
  127. /* = 0: successful exit */
  128. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  129. /* Internal Parameters */
  130. /* =================== */
  131. /* ITMAX is the maximum number of steps of iterative refinement. */
  132. /* ===================================================================== */
  133. /* .. Parameters .. */
  134. /* .. */
  135. /* .. Local Scalars .. */
  136. /* .. */
  137. /* .. Local Arrays .. */
  138. /* .. */
  139. /* .. External Subroutines .. */
  140. /* .. */
  141. /* .. Intrinsic Functions .. */
  142. /* .. */
  143. /* .. External Functions .. */
  144. /* .. */
  145. /* .. Executable Statements .. */
  146. /* Test the input parameters. */
  147. /* Parameter adjustments */
  148. ab_dim1 = *ldab;
  149. ab_offset = 1 + ab_dim1;
  150. ab -= ab_offset;
  151. afb_dim1 = *ldafb;
  152. afb_offset = 1 + afb_dim1;
  153. afb -= afb_offset;
  154. --ipiv;
  155. b_dim1 = *ldb;
  156. b_offset = 1 + b_dim1;
  157. b -= b_offset;
  158. x_dim1 = *ldx;
  159. x_offset = 1 + x_dim1;
  160. x -= x_offset;
  161. --ferr;
  162. --berr;
  163. --work;
  164. --iwork;
  165. /* Function Body */
  166. *info = 0;
  167. notran = _starpu_lsame_(trans, "N");
  168. if (! notran && ! _starpu_lsame_(trans, "T") && ! _starpu_lsame_(
  169. trans, "C")) {
  170. *info = -1;
  171. } else if (*n < 0) {
  172. *info = -2;
  173. } else if (*kl < 0) {
  174. *info = -3;
  175. } else if (*ku < 0) {
  176. *info = -4;
  177. } else if (*nrhs < 0) {
  178. *info = -5;
  179. } else if (*ldab < *kl + *ku + 1) {
  180. *info = -7;
  181. } else if (*ldafb < (*kl << 1) + *ku + 1) {
  182. *info = -9;
  183. } else if (*ldb < max(1,*n)) {
  184. *info = -12;
  185. } else if (*ldx < max(1,*n)) {
  186. *info = -14;
  187. }
  188. if (*info != 0) {
  189. i__1 = -(*info);
  190. _starpu_xerbla_("DGBRFS", &i__1);
  191. return 0;
  192. }
  193. /* Quick return if possible */
  194. if (*n == 0 || *nrhs == 0) {
  195. i__1 = *nrhs;
  196. for (j = 1; j <= i__1; ++j) {
  197. ferr[j] = 0.;
  198. berr[j] = 0.;
  199. /* L10: */
  200. }
  201. return 0;
  202. }
  203. if (notran) {
  204. *(unsigned char *)transt = 'T';
  205. } else {
  206. *(unsigned char *)transt = 'N';
  207. }
  208. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  209. /* Computing MIN */
  210. i__1 = *kl + *ku + 2, i__2 = *n + 1;
  211. nz = min(i__1,i__2);
  212. eps = _starpu_dlamch_("Epsilon");
  213. safmin = _starpu_dlamch_("Safe minimum");
  214. safe1 = nz * safmin;
  215. safe2 = safe1 / eps;
  216. /* Do for each right hand side */
  217. i__1 = *nrhs;
  218. for (j = 1; j <= i__1; ++j) {
  219. count = 1;
  220. lstres = 3.;
  221. L20:
  222. /* Loop until stopping criterion is satisfied. */
  223. /* Compute residual R = B - op(A) * X, */
  224. /* where op(A) = A, A**T, or A**H, depending on TRANS. */
  225. _starpu_dcopy_(n, &b[j * b_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  226. _starpu_dgbmv_(trans, n, n, kl, ku, &c_b15, &ab[ab_offset], ldab, &x[j *
  227. x_dim1 + 1], &c__1, &c_b17, &work[*n + 1], &c__1);
  228. /* Compute componentwise relative backward error from formula */
  229. /* max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) ) */
  230. /* where abs(Z) is the componentwise absolute value of the matrix */
  231. /* or vector Z. If the i-th component of the denominator is less */
  232. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  233. /* numerator and denominator before dividing. */
  234. i__2 = *n;
  235. for (i__ = 1; i__ <= i__2; ++i__) {
  236. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1));
  237. /* L30: */
  238. }
  239. /* Compute abs(op(A))*abs(X) + abs(B). */
  240. if (notran) {
  241. i__2 = *n;
  242. for (k = 1; k <= i__2; ++k) {
  243. kk = *ku + 1 - k;
  244. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  245. /* Computing MAX */
  246. i__3 = 1, i__4 = k - *ku;
  247. /* Computing MIN */
  248. i__6 = *n, i__7 = k + *kl;
  249. i__5 = min(i__6,i__7);
  250. for (i__ = max(i__3,i__4); i__ <= i__5; ++i__) {
  251. work[i__] += (d__1 = ab[kk + i__ + k * ab_dim1], abs(d__1)
  252. ) * xk;
  253. /* L40: */
  254. }
  255. /* L50: */
  256. }
  257. } else {
  258. i__2 = *n;
  259. for (k = 1; k <= i__2; ++k) {
  260. s = 0.;
  261. kk = *ku + 1 - k;
  262. /* Computing MAX */
  263. i__5 = 1, i__3 = k - *ku;
  264. /* Computing MIN */
  265. i__6 = *n, i__7 = k + *kl;
  266. i__4 = min(i__6,i__7);
  267. for (i__ = max(i__5,i__3); i__ <= i__4; ++i__) {
  268. s += (d__1 = ab[kk + i__ + k * ab_dim1], abs(d__1)) * (
  269. d__2 = x[i__ + j * x_dim1], abs(d__2));
  270. /* L60: */
  271. }
  272. work[k] += s;
  273. /* L70: */
  274. }
  275. }
  276. s = 0.;
  277. i__2 = *n;
  278. for (i__ = 1; i__ <= i__2; ++i__) {
  279. if (work[i__] > safe2) {
  280. /* Computing MAX */
  281. d__2 = s, d__3 = (d__1 = work[*n + i__], abs(d__1)) / work[
  282. i__];
  283. s = max(d__2,d__3);
  284. } else {
  285. /* Computing MAX */
  286. d__2 = s, d__3 = ((d__1 = work[*n + i__], abs(d__1)) + safe1)
  287. / (work[i__] + safe1);
  288. s = max(d__2,d__3);
  289. }
  290. /* L80: */
  291. }
  292. berr[j] = s;
  293. /* Test stopping criterion. Continue iterating if */
  294. /* 1) The residual BERR(J) is larger than machine epsilon, and */
  295. /* 2) BERR(J) decreased by at least a factor of 2 during the */
  296. /* last iteration, and */
  297. /* 3) At most ITMAX iterations tried. */
  298. if (berr[j] > eps && berr[j] * 2. <= lstres && count <= 5) {
  299. /* Update solution and try again. */
  300. _starpu_dgbtrs_(trans, n, kl, ku, &c__1, &afb[afb_offset], ldafb, &ipiv[1]
  301. , &work[*n + 1], n, info);
  302. _starpu_daxpy_(n, &c_b17, &work[*n + 1], &c__1, &x[j * x_dim1 + 1], &c__1)
  303. ;
  304. lstres = berr[j];
  305. ++count;
  306. goto L20;
  307. }
  308. /* Bound error from formula */
  309. /* norm(X - XTRUE) / norm(X) .le. FERR = */
  310. /* norm( abs(inv(op(A)))* */
  311. /* ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X) */
  312. /* where */
  313. /* norm(Z) is the magnitude of the largest component of Z */
  314. /* inv(op(A)) is the inverse of op(A) */
  315. /* abs(Z) is the componentwise absolute value of the matrix or */
  316. /* vector Z */
  317. /* NZ is the maximum number of nonzeros in any row of A, plus 1 */
  318. /* EPS is machine epsilon */
  319. /* The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B)) */
  320. /* is incremented by SAFE1 if the i-th component of */
  321. /* abs(op(A))*abs(X) + abs(B) is less than SAFE2. */
  322. /* Use DLACN2 to estimate the infinity-norm of the matrix */
  323. /* inv(op(A)) * diag(W), */
  324. /* where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) */
  325. i__2 = *n;
  326. for (i__ = 1; i__ <= i__2; ++i__) {
  327. if (work[i__] > safe2) {
  328. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  329. work[i__];
  330. } else {
  331. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  332. work[i__] + safe1;
  333. }
  334. /* L90: */
  335. }
  336. kase = 0;
  337. L100:
  338. _starpu_dlacn2_(n, &work[(*n << 1) + 1], &work[*n + 1], &iwork[1], &ferr[j], &
  339. kase, isave);
  340. if (kase != 0) {
  341. if (kase == 1) {
  342. /* Multiply by diag(W)*inv(op(A)**T). */
  343. _starpu_dgbtrs_(transt, n, kl, ku, &c__1, &afb[afb_offset], ldafb, &
  344. ipiv[1], &work[*n + 1], n, info);
  345. i__2 = *n;
  346. for (i__ = 1; i__ <= i__2; ++i__) {
  347. work[*n + i__] *= work[i__];
  348. /* L110: */
  349. }
  350. } else {
  351. /* Multiply by inv(op(A))*diag(W). */
  352. i__2 = *n;
  353. for (i__ = 1; i__ <= i__2; ++i__) {
  354. work[*n + i__] *= work[i__];
  355. /* L120: */
  356. }
  357. _starpu_dgbtrs_(trans, n, kl, ku, &c__1, &afb[afb_offset], ldafb, &
  358. ipiv[1], &work[*n + 1], n, info);
  359. }
  360. goto L100;
  361. }
  362. /* Normalize error. */
  363. lstres = 0.;
  364. i__2 = *n;
  365. for (i__ = 1; i__ <= i__2; ++i__) {
  366. /* Computing MAX */
  367. d__2 = lstres, d__3 = (d__1 = x[i__ + j * x_dim1], abs(d__1));
  368. lstres = max(d__2,d__3);
  369. /* L130: */
  370. }
  371. if (lstres != 0.) {
  372. ferr[j] /= lstres;
  373. }
  374. /* L140: */
  375. }
  376. return 0;
  377. /* End of DGBRFS */
  378. } /* _starpu_dgbrfs_ */