dgerfs.c 12 KB

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