dptrfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* dptrfs.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_b11 = 1.;
  16. /* Subroutine */ int _starpu_dptrfs_(integer *n, integer *nrhs, doublereal *d__,
  17. doublereal *e, doublereal *df, doublereal *ef, doublereal *b, integer
  18. *ldb, doublereal *x, integer *ldx, doublereal *ferr, doublereal *berr,
  19. doublereal *work, integer *info)
  20. {
  21. /* System generated locals */
  22. integer b_dim1, b_offset, x_dim1, x_offset, i__1, i__2;
  23. doublereal d__1, d__2, d__3;
  24. /* Local variables */
  25. integer i__, j;
  26. doublereal s, bi, cx, dx, ex;
  27. integer ix, nz;
  28. doublereal eps, safe1, safe2;
  29. extern /* Subroutine */ int _starpu_daxpy_(integer *, doublereal *, doublereal *,
  30. integer *, doublereal *, integer *);
  31. integer count;
  32. extern doublereal _starpu_dlamch_(char *);
  33. extern integer _starpu_idamax_(integer *, doublereal *, integer *);
  34. doublereal safmin;
  35. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  36. doublereal lstres;
  37. extern /* Subroutine */ int _starpu_dpttrs_(integer *, integer *, doublereal *,
  38. doublereal *, doublereal *, integer *, integer *);
  39. /* -- LAPACK routine (version 3.2) -- */
  40. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  41. /* November 2006 */
  42. /* .. Scalar Arguments .. */
  43. /* .. */
  44. /* .. Array Arguments .. */
  45. /* .. */
  46. /* Purpose */
  47. /* ======= */
  48. /* DPTRFS improves the computed solution to a system of linear */
  49. /* equations when the coefficient matrix is symmetric positive definite */
  50. /* and tridiagonal, and provides error bounds and backward error */
  51. /* estimates for the solution. */
  52. /* Arguments */
  53. /* ========= */
  54. /* N (input) INTEGER */
  55. /* The order of the matrix A. N >= 0. */
  56. /* NRHS (input) INTEGER */
  57. /* The number of right hand sides, i.e., the number of columns */
  58. /* of the matrix B. NRHS >= 0. */
  59. /* D (input) DOUBLE PRECISION array, dimension (N) */
  60. /* The n diagonal elements of the tridiagonal matrix A. */
  61. /* E (input) DOUBLE PRECISION array, dimension (N-1) */
  62. /* The (n-1) subdiagonal elements of the tridiagonal matrix A. */
  63. /* DF (input) DOUBLE PRECISION array, dimension (N) */
  64. /* The n diagonal elements of the diagonal matrix D from the */
  65. /* factorization computed by DPTTRF. */
  66. /* EF (input) DOUBLE PRECISION array, dimension (N-1) */
  67. /* The (n-1) subdiagonal elements of the unit bidiagonal factor */
  68. /* L from the factorization computed by DPTTRF. */
  69. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  70. /* The right hand side matrix B. */
  71. /* LDB (input) INTEGER */
  72. /* The leading dimension of the array B. LDB >= max(1,N). */
  73. /* X (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  74. /* On entry, the solution matrix X, as computed by DPTTRS. */
  75. /* On exit, the improved solution matrix X. */
  76. /* LDX (input) INTEGER */
  77. /* The leading dimension of the array X. LDX >= max(1,N). */
  78. /* FERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  79. /* The forward error bound for each solution vector */
  80. /* X(j) (the j-th column of the solution matrix X). */
  81. /* If XTRUE is the true solution corresponding to X(j), FERR(j) */
  82. /* is an estimated upper bound for the magnitude of the largest */
  83. /* element in (X(j) - XTRUE) divided by the magnitude of the */
  84. /* largest element in X(j). */
  85. /* BERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  86. /* The componentwise relative backward error of each solution */
  87. /* vector X(j) (i.e., the smallest relative change in */
  88. /* any element of A or B that makes X(j) an exact solution). */
  89. /* WORK (workspace) DOUBLE PRECISION array, dimension (2*N) */
  90. /* INFO (output) INTEGER */
  91. /* = 0: successful exit */
  92. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  93. /* Internal Parameters */
  94. /* =================== */
  95. /* ITMAX is the maximum number of steps of iterative refinement. */
  96. /* ===================================================================== */
  97. /* .. Parameters .. */
  98. /* .. */
  99. /* .. Local Scalars .. */
  100. /* .. */
  101. /* .. External Subroutines .. */
  102. /* .. */
  103. /* .. Intrinsic Functions .. */
  104. /* .. */
  105. /* .. External Functions .. */
  106. /* .. */
  107. /* .. Executable Statements .. */
  108. /* Test the input parameters. */
  109. /* Parameter adjustments */
  110. --d__;
  111. --e;
  112. --df;
  113. --ef;
  114. b_dim1 = *ldb;
  115. b_offset = 1 + b_dim1;
  116. b -= b_offset;
  117. x_dim1 = *ldx;
  118. x_offset = 1 + x_dim1;
  119. x -= x_offset;
  120. --ferr;
  121. --berr;
  122. --work;
  123. /* Function Body */
  124. *info = 0;
  125. if (*n < 0) {
  126. *info = -1;
  127. } else if (*nrhs < 0) {
  128. *info = -2;
  129. } else if (*ldb < max(1,*n)) {
  130. *info = -8;
  131. } else if (*ldx < max(1,*n)) {
  132. *info = -10;
  133. }
  134. if (*info != 0) {
  135. i__1 = -(*info);
  136. _starpu_xerbla_("DPTRFS", &i__1);
  137. return 0;
  138. }
  139. /* Quick return if possible */
  140. if (*n == 0 || *nrhs == 0) {
  141. i__1 = *nrhs;
  142. for (j = 1; j <= i__1; ++j) {
  143. ferr[j] = 0.;
  144. berr[j] = 0.;
  145. /* L10: */
  146. }
  147. return 0;
  148. }
  149. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  150. nz = 4;
  151. eps = _starpu_dlamch_("Epsilon");
  152. safmin = _starpu_dlamch_("Safe minimum");
  153. safe1 = nz * safmin;
  154. safe2 = safe1 / eps;
  155. /* Do for each right hand side */
  156. i__1 = *nrhs;
  157. for (j = 1; j <= i__1; ++j) {
  158. count = 1;
  159. lstres = 3.;
  160. L20:
  161. /* Loop until stopping criterion is satisfied. */
  162. /* Compute residual R = B - A * X. Also compute */
  163. /* abs(A)*abs(x) + abs(b) for use in the backward error bound. */
  164. if (*n == 1) {
  165. bi = b[j * b_dim1 + 1];
  166. dx = d__[1] * x[j * x_dim1 + 1];
  167. work[*n + 1] = bi - dx;
  168. work[1] = abs(bi) + abs(dx);
  169. } else {
  170. bi = b[j * b_dim1 + 1];
  171. dx = d__[1] * x[j * x_dim1 + 1];
  172. ex = e[1] * x[j * x_dim1 + 2];
  173. work[*n + 1] = bi - dx - ex;
  174. work[1] = abs(bi) + abs(dx) + abs(ex);
  175. i__2 = *n - 1;
  176. for (i__ = 2; i__ <= i__2; ++i__) {
  177. bi = b[i__ + j * b_dim1];
  178. cx = e[i__ - 1] * x[i__ - 1 + j * x_dim1];
  179. dx = d__[i__] * x[i__ + j * x_dim1];
  180. ex = e[i__] * x[i__ + 1 + j * x_dim1];
  181. work[*n + i__] = bi - cx - dx - ex;
  182. work[i__] = abs(bi) + abs(cx) + abs(dx) + abs(ex);
  183. /* L30: */
  184. }
  185. bi = b[*n + j * b_dim1];
  186. cx = e[*n - 1] * x[*n - 1 + j * x_dim1];
  187. dx = d__[*n] * x[*n + j * x_dim1];
  188. work[*n + *n] = bi - cx - dx;
  189. work[*n] = abs(bi) + abs(cx) + abs(dx);
  190. }
  191. /* Compute componentwise relative backward error from formula */
  192. /* max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) ) */
  193. /* where abs(Z) is the componentwise absolute value of the matrix */
  194. /* or vector Z. If the i-th component of the denominator is less */
  195. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  196. /* numerator and denominator before dividing. */
  197. s = 0.;
  198. i__2 = *n;
  199. for (i__ = 1; i__ <= i__2; ++i__) {
  200. if (work[i__] > safe2) {
  201. /* Computing MAX */
  202. d__2 = s, d__3 = (d__1 = work[*n + i__], abs(d__1)) / work[
  203. i__];
  204. s = max(d__2,d__3);
  205. } else {
  206. /* Computing MAX */
  207. d__2 = s, d__3 = ((d__1 = work[*n + i__], abs(d__1)) + safe1)
  208. / (work[i__] + safe1);
  209. s = max(d__2,d__3);
  210. }
  211. /* L40: */
  212. }
  213. berr[j] = s;
  214. /* Test stopping criterion. Continue iterating if */
  215. /* 1) The residual BERR(J) is larger than machine epsilon, and */
  216. /* 2) BERR(J) decreased by at least a factor of 2 during the */
  217. /* last iteration, and */
  218. /* 3) At most ITMAX iterations tried. */
  219. if (berr[j] > eps && berr[j] * 2. <= lstres && count <= 5) {
  220. /* Update solution and try again. */
  221. _starpu_dpttrs_(n, &c__1, &df[1], &ef[1], &work[*n + 1], n, info);
  222. _starpu_daxpy_(n, &c_b11, &work[*n + 1], &c__1, &x[j * x_dim1 + 1], &c__1)
  223. ;
  224. lstres = berr[j];
  225. ++count;
  226. goto L20;
  227. }
  228. /* Bound error from formula */
  229. /* norm(X - XTRUE) / norm(X) .le. FERR = */
  230. /* norm( abs(inv(A))* */
  231. /* ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X) */
  232. /* where */
  233. /* norm(Z) is the magnitude of the largest component of Z */
  234. /* inv(A) is the inverse of A */
  235. /* abs(Z) is the componentwise absolute value of the matrix or */
  236. /* vector Z */
  237. /* NZ is the maximum number of nonzeros in any row of A, plus 1 */
  238. /* EPS is machine epsilon */
  239. /* The i-th component of abs(R)+NZ*EPS*(abs(A)*abs(X)+abs(B)) */
  240. /* is incremented by SAFE1 if the i-th component of */
  241. /* abs(A)*abs(X) + abs(B) is less than SAFE2. */
  242. i__2 = *n;
  243. for (i__ = 1; i__ <= i__2; ++i__) {
  244. if (work[i__] > safe2) {
  245. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  246. work[i__];
  247. } else {
  248. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  249. work[i__] + safe1;
  250. }
  251. /* L50: */
  252. }
  253. ix = _starpu_idamax_(n, &work[1], &c__1);
  254. ferr[j] = work[ix];
  255. /* Estimate the norm of inv(A). */
  256. /* Solve M(A) * x = e, where M(A) = (m(i,j)) is given by */
  257. /* m(i,j) = abs(A(i,j)), i = j, */
  258. /* m(i,j) = -abs(A(i,j)), i .ne. j, */
  259. /* and e = [ 1, 1, ..., 1 ]'. Note M(A) = M(L)*D*M(L)'. */
  260. /* Solve M(L) * x = e. */
  261. work[1] = 1.;
  262. i__2 = *n;
  263. for (i__ = 2; i__ <= i__2; ++i__) {
  264. work[i__] = work[i__ - 1] * (d__1 = ef[i__ - 1], abs(d__1)) + 1.;
  265. /* L60: */
  266. }
  267. /* Solve D * M(L)' * x = b. */
  268. work[*n] /= df[*n];
  269. for (i__ = *n - 1; i__ >= 1; --i__) {
  270. work[i__] = work[i__] / df[i__] + work[i__ + 1] * (d__1 = ef[i__],
  271. abs(d__1));
  272. /* L70: */
  273. }
  274. /* Compute norm(inv(A)) = max(x(i)), 1<=i<=n. */
  275. ix = _starpu_idamax_(n, &work[1], &c__1);
  276. ferr[j] *= (d__1 = work[ix], abs(d__1));
  277. /* Normalize error. */
  278. lstres = 0.;
  279. i__2 = *n;
  280. for (i__ = 1; i__ <= i__2; ++i__) {
  281. /* Computing MAX */
  282. d__2 = lstres, d__3 = (d__1 = x[i__ + j * x_dim1], abs(d__1));
  283. lstres = max(d__2,d__3);
  284. /* L80: */
  285. }
  286. if (lstres != 0.) {
  287. ferr[j] /= lstres;
  288. }
  289. /* L90: */
  290. }
  291. return 0;
  292. /* End of DPTRFS */
  293. } /* _starpu_dptrfs_ */