dgtrfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* dgtrfs.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_b18 = -1.;
  16. static doublereal c_b19 = 1.;
  17. /* Subroutine */ int _starpu_dgtrfs_(char *trans, integer *n, integer *nrhs,
  18. doublereal *dl, doublereal *d__, doublereal *du, doublereal *dlf,
  19. doublereal *df, doublereal *duf, doublereal *du2, integer *ipiv,
  20. doublereal *b, integer *ldb, doublereal *x, integer *ldx, doublereal *
  21. ferr, doublereal *berr, doublereal *work, integer *iwork, integer *
  22. info)
  23. {
  24. /* System generated locals */
  25. integer b_dim1, b_offset, x_dim1, x_offset, i__1, i__2;
  26. doublereal d__1, d__2, d__3, d__4;
  27. /* Local variables */
  28. integer i__, j;
  29. doublereal s;
  30. integer nz;
  31. doublereal eps;
  32. integer kase;
  33. doublereal safe1, safe2;
  34. extern logical _starpu_lsame_(char *, char *);
  35. integer isave[3];
  36. extern /* Subroutine */ int _starpu_dcopy_(integer *, doublereal *, integer *,
  37. doublereal *, integer *), _starpu_daxpy_(integer *, doublereal *,
  38. doublereal *, integer *, doublereal *, integer *);
  39. integer count;
  40. extern /* Subroutine */ int _starpu_dlacn2_(integer *, doublereal *, doublereal *,
  41. integer *, doublereal *, integer *, integer *);
  42. extern doublereal _starpu_dlamch_(char *);
  43. extern /* Subroutine */ int _starpu_dlagtm_(char *, integer *, integer *,
  44. doublereal *, doublereal *, doublereal *, doublereal *,
  45. doublereal *, integer *, doublereal *, doublereal *, integer *);
  46. doublereal safmin;
  47. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  48. logical notran;
  49. char transn[1];
  50. extern /* Subroutine */ int _starpu_dgttrs_(char *, integer *, integer *,
  51. doublereal *, doublereal *, doublereal *, doublereal *, integer *,
  52. doublereal *, integer *, integer *);
  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. /* DGTRFS improves the computed solution to a system of linear */
  66. /* equations when the coefficient matrix is tridiagonal, 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. /* NRHS (input) INTEGER */
  78. /* The number of right hand sides, i.e., the number of columns */
  79. /* of the matrix B. NRHS >= 0. */
  80. /* DL (input) DOUBLE PRECISION array, dimension (N-1) */
  81. /* The (n-1) subdiagonal elements of A. */
  82. /* D (input) DOUBLE PRECISION array, dimension (N) */
  83. /* The diagonal elements of A. */
  84. /* DU (input) DOUBLE PRECISION array, dimension (N-1) */
  85. /* The (n-1) superdiagonal elements of A. */
  86. /* DLF (input) DOUBLE PRECISION array, dimension (N-1) */
  87. /* The (n-1) multipliers that define the matrix L from the */
  88. /* LU factorization of A as computed by DGTTRF. */
  89. /* DF (input) DOUBLE PRECISION array, dimension (N) */
  90. /* The n diagonal elements of the upper triangular matrix U from */
  91. /* the LU factorization of A. */
  92. /* DUF (input) DOUBLE PRECISION array, dimension (N-1) */
  93. /* The (n-1) elements of the first superdiagonal of U. */
  94. /* DU2 (input) DOUBLE PRECISION array, dimension (N-2) */
  95. /* The (n-2) elements of the second superdiagonal of U. */
  96. /* IPIV (input) INTEGER array, dimension (N) */
  97. /* The pivot indices; for 1 <= i <= n, row i of the matrix was */
  98. /* interchanged with row IPIV(i). IPIV(i) will always be either */
  99. /* i or i+1; IPIV(i) = i indicates a row interchange was not */
  100. /* required. */
  101. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  102. /* The right hand side matrix B. */
  103. /* LDB (input) INTEGER */
  104. /* The leading dimension of the array B. LDB >= max(1,N). */
  105. /* X (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  106. /* On entry, the solution matrix X, as computed by DGTTRS. */
  107. /* On exit, the improved solution matrix X. */
  108. /* LDX (input) INTEGER */
  109. /* The leading dimension of the array X. LDX >= max(1,N). */
  110. /* FERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  111. /* The estimated forward error bound for each solution vector */
  112. /* X(j) (the j-th column of the solution matrix X). */
  113. /* If XTRUE is the true solution corresponding to X(j), FERR(j) */
  114. /* is an estimated upper bound for the magnitude of the largest */
  115. /* element in (X(j) - XTRUE) divided by the magnitude of the */
  116. /* largest element in X(j). The estimate is as reliable as */
  117. /* the estimate for RCOND, and is almost always a slight */
  118. /* overestimate of the true error. */
  119. /* BERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  120. /* The componentwise relative backward error of each solution */
  121. /* vector X(j) (i.e., the smallest relative change in */
  122. /* any element of A or B that makes X(j) an exact solution). */
  123. /* WORK (workspace) DOUBLE PRECISION array, dimension (3*N) */
  124. /* IWORK (workspace) INTEGER array, dimension (N) */
  125. /* INFO (output) INTEGER */
  126. /* = 0: successful exit */
  127. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  128. /* Internal Parameters */
  129. /* =================== */
  130. /* ITMAX is the maximum number of steps of iterative refinement. */
  131. /* ===================================================================== */
  132. /* .. Parameters .. */
  133. /* .. */
  134. /* .. Local Scalars .. */
  135. /* .. */
  136. /* .. Local Arrays .. */
  137. /* .. */
  138. /* .. External Subroutines .. */
  139. /* .. */
  140. /* .. Intrinsic Functions .. */
  141. /* .. */
  142. /* .. External Functions .. */
  143. /* .. */
  144. /* .. Executable Statements .. */
  145. /* Test the input parameters. */
  146. /* Parameter adjustments */
  147. --dl;
  148. --d__;
  149. --du;
  150. --dlf;
  151. --df;
  152. --duf;
  153. --du2;
  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 (*nrhs < 0) {
  174. *info = -3;
  175. } else if (*ldb < max(1,*n)) {
  176. *info = -13;
  177. } else if (*ldx < max(1,*n)) {
  178. *info = -15;
  179. }
  180. if (*info != 0) {
  181. i__1 = -(*info);
  182. _starpu_xerbla_("DGTRFS", &i__1);
  183. return 0;
  184. }
  185. /* Quick return if possible */
  186. if (*n == 0 || *nrhs == 0) {
  187. i__1 = *nrhs;
  188. for (j = 1; j <= i__1; ++j) {
  189. ferr[j] = 0.;
  190. berr[j] = 0.;
  191. /* L10: */
  192. }
  193. return 0;
  194. }
  195. if (notran) {
  196. *(unsigned char *)transn = 'N';
  197. *(unsigned char *)transt = 'T';
  198. } else {
  199. *(unsigned char *)transn = 'T';
  200. *(unsigned char *)transt = 'N';
  201. }
  202. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  203. nz = 4;
  204. eps = _starpu_dlamch_("Epsilon");
  205. safmin = _starpu_dlamch_("Safe minimum");
  206. safe1 = nz * safmin;
  207. safe2 = safe1 / eps;
  208. /* Do for each right hand side */
  209. i__1 = *nrhs;
  210. for (j = 1; j <= i__1; ++j) {
  211. count = 1;
  212. lstres = 3.;
  213. L20:
  214. /* Loop until stopping criterion is satisfied. */
  215. /* Compute residual R = B - op(A) * X, */
  216. /* where op(A) = A, A**T, or A**H, depending on TRANS. */
  217. _starpu_dcopy_(n, &b[j * b_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  218. _starpu_dlagtm_(trans, n, &c__1, &c_b18, &dl[1], &d__[1], &du[1], &x[j *
  219. x_dim1 + 1], ldx, &c_b19, &work[*n + 1], n);
  220. /* Compute abs(op(A))*abs(x) + abs(b) for use in the backward */
  221. /* error bound. */
  222. if (notran) {
  223. if (*n == 1) {
  224. work[1] = (d__1 = b[j * b_dim1 + 1], abs(d__1)) + (d__2 = d__[
  225. 1] * x[j * x_dim1 + 1], abs(d__2));
  226. } else {
  227. work[1] = (d__1 = b[j * b_dim1 + 1], abs(d__1)) + (d__2 = d__[
  228. 1] * x[j * x_dim1 + 1], abs(d__2)) + (d__3 = du[1] *
  229. x[j * x_dim1 + 2], abs(d__3));
  230. i__2 = *n - 1;
  231. for (i__ = 2; i__ <= i__2; ++i__) {
  232. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1)) + (
  233. d__2 = dl[i__ - 1] * x[i__ - 1 + j * x_dim1], abs(
  234. d__2)) + (d__3 = d__[i__] * x[i__ + j * x_dim1],
  235. abs(d__3)) + (d__4 = du[i__] * x[i__ + 1 + j *
  236. x_dim1], abs(d__4));
  237. /* L30: */
  238. }
  239. work[*n] = (d__1 = b[*n + j * b_dim1], abs(d__1)) + (d__2 =
  240. dl[*n - 1] * x[*n - 1 + j * x_dim1], abs(d__2)) + (
  241. d__3 = d__[*n] * x[*n + j * x_dim1], abs(d__3));
  242. }
  243. } else {
  244. if (*n == 1) {
  245. work[1] = (d__1 = b[j * b_dim1 + 1], abs(d__1)) + (d__2 = d__[
  246. 1] * x[j * x_dim1 + 1], abs(d__2));
  247. } else {
  248. work[1] = (d__1 = b[j * b_dim1 + 1], abs(d__1)) + (d__2 = d__[
  249. 1] * x[j * x_dim1 + 1], abs(d__2)) + (d__3 = dl[1] *
  250. x[j * x_dim1 + 2], abs(d__3));
  251. i__2 = *n - 1;
  252. for (i__ = 2; i__ <= i__2; ++i__) {
  253. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1)) + (
  254. d__2 = du[i__ - 1] * x[i__ - 1 + j * x_dim1], abs(
  255. d__2)) + (d__3 = d__[i__] * x[i__ + j * x_dim1],
  256. abs(d__3)) + (d__4 = dl[i__] * x[i__ + 1 + j *
  257. x_dim1], abs(d__4));
  258. /* L40: */
  259. }
  260. work[*n] = (d__1 = b[*n + j * b_dim1], abs(d__1)) + (d__2 =
  261. du[*n - 1] * x[*n - 1 + j * x_dim1], abs(d__2)) + (
  262. d__3 = d__[*n] * x[*n + j * x_dim1], abs(d__3));
  263. }
  264. }
  265. /* Compute componentwise relative backward error from formula */
  266. /* max(i) ( abs(R(i)) / ( abs(op(A))*abs(X) + abs(B) )(i) ) */
  267. /* where abs(Z) is the componentwise absolute value of the matrix */
  268. /* or vector Z. If the i-th component of the denominator is less */
  269. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  270. /* numerator and denominator before dividing. */
  271. s = 0.;
  272. i__2 = *n;
  273. for (i__ = 1; i__ <= i__2; ++i__) {
  274. if (work[i__] > safe2) {
  275. /* Computing MAX */
  276. d__2 = s, d__3 = (d__1 = work[*n + i__], abs(d__1)) / work[
  277. i__];
  278. s = max(d__2,d__3);
  279. } else {
  280. /* Computing MAX */
  281. d__2 = s, d__3 = ((d__1 = work[*n + i__], abs(d__1)) + safe1)
  282. / (work[i__] + safe1);
  283. s = max(d__2,d__3);
  284. }
  285. /* L50: */
  286. }
  287. berr[j] = s;
  288. /* Test stopping criterion. Continue iterating if */
  289. /* 1) The residual BERR(J) is larger than machine epsilon, and */
  290. /* 2) BERR(J) decreased by at least a factor of 2 during the */
  291. /* last iteration, and */
  292. /* 3) At most ITMAX iterations tried. */
  293. if (berr[j] > eps && berr[j] * 2. <= lstres && count <= 5) {
  294. /* Update solution and try again. */
  295. _starpu_dgttrs_(trans, n, &c__1, &dlf[1], &df[1], &duf[1], &du2[1], &ipiv[
  296. 1], &work[*n + 1], n, info);
  297. _starpu_daxpy_(n, &c_b19, &work[*n + 1], &c__1, &x[j * x_dim1 + 1], &c__1)
  298. ;
  299. lstres = berr[j];
  300. ++count;
  301. goto L20;
  302. }
  303. /* Bound error from formula */
  304. /* norm(X - XTRUE) / norm(X) .le. FERR = */
  305. /* norm( abs(inv(op(A)))* */
  306. /* ( abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) / norm(X) */
  307. /* where */
  308. /* norm(Z) is the magnitude of the largest component of Z */
  309. /* inv(op(A)) is the inverse of op(A) */
  310. /* abs(Z) is the componentwise absolute value of the matrix or */
  311. /* vector Z */
  312. /* NZ is the maximum number of nonzeros in any row of A, plus 1 */
  313. /* EPS is machine epsilon */
  314. /* The i-th component of abs(R)+NZ*EPS*(abs(op(A))*abs(X)+abs(B)) */
  315. /* is incremented by SAFE1 if the i-th component of */
  316. /* abs(op(A))*abs(X) + abs(B) is less than SAFE2. */
  317. /* Use DLACN2 to estimate the infinity-norm of the matrix */
  318. /* inv(op(A)) * diag(W), */
  319. /* where W = abs(R) + NZ*EPS*( abs(op(A))*abs(X)+abs(B) ))) */
  320. i__2 = *n;
  321. for (i__ = 1; i__ <= i__2; ++i__) {
  322. if (work[i__] > safe2) {
  323. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  324. work[i__];
  325. } else {
  326. work[i__] = (d__1 = work[*n + i__], abs(d__1)) + nz * eps *
  327. work[i__] + safe1;
  328. }
  329. /* L60: */
  330. }
  331. kase = 0;
  332. L70:
  333. _starpu_dlacn2_(n, &work[(*n << 1) + 1], &work[*n + 1], &iwork[1], &ferr[j], &
  334. kase, isave);
  335. if (kase != 0) {
  336. if (kase == 1) {
  337. /* Multiply by diag(W)*inv(op(A)**T). */
  338. _starpu_dgttrs_(transt, n, &c__1, &dlf[1], &df[1], &duf[1], &du2[1], &
  339. ipiv[1], &work[*n + 1], n, info);
  340. i__2 = *n;
  341. for (i__ = 1; i__ <= i__2; ++i__) {
  342. work[*n + i__] = work[i__] * work[*n + i__];
  343. /* L80: */
  344. }
  345. } else {
  346. /* Multiply by inv(op(A))*diag(W). */
  347. i__2 = *n;
  348. for (i__ = 1; i__ <= i__2; ++i__) {
  349. work[*n + i__] = work[i__] * work[*n + i__];
  350. /* L90: */
  351. }
  352. _starpu_dgttrs_(transn, n, &c__1, &dlf[1], &df[1], &duf[1], &du2[1], &
  353. ipiv[1], &work[*n + 1], n, info);
  354. }
  355. goto L70;
  356. }
  357. /* Normalize error. */
  358. lstres = 0.;
  359. i__2 = *n;
  360. for (i__ = 1; i__ <= i__2; ++i__) {
  361. /* Computing MAX */
  362. d__2 = lstres, d__3 = (d__1 = x[i__ + j * x_dim1], abs(d__1));
  363. lstres = max(d__2,d__3);
  364. /* L100: */
  365. }
  366. if (lstres != 0.) {
  367. ferr[j] /= lstres;
  368. }
  369. /* L110: */
  370. }
  371. return 0;
  372. /* End of DGTRFS */
  373. } /* _starpu_dgtrfs_ */