dporfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* dporfs.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_b12 = -1.;
  16. static doublereal c_b14 = 1.;
  17. /* Subroutine */ int _starpu_dporfs_(char *uplo, integer *n, integer *nrhs,
  18. doublereal *a, integer *lda, doublereal *af, integer *ldaf,
  19. doublereal *b, integer *ldb, doublereal *x, integer *ldx, doublereal *
  20. ferr, doublereal *berr, doublereal *work, integer *iwork, integer *
  21. 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. 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. logical upper;
  41. extern /* Subroutine */ int _starpu_dsymv_(char *, integer *, doublereal *,
  42. doublereal *, integer *, doublereal *, integer *, doublereal *,
  43. doublereal *, integer *), _starpu_dlacn2_(integer *, doublereal *,
  44. doublereal *, integer *, doublereal *, integer *, integer *);
  45. extern doublereal _starpu_dlamch_(char *);
  46. doublereal safmin;
  47. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *), _starpu_dpotrs_(
  48. char *, integer *, integer *, doublereal *, integer *, doublereal
  49. *, integer *, integer *);
  50. doublereal lstres;
  51. /* -- LAPACK routine (version 3.2) -- */
  52. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  53. /* November 2006 */
  54. /* Modified to call DLACN2 in place of DLACON, 5 Feb 03, SJH. */
  55. /* .. Scalar Arguments .. */
  56. /* .. */
  57. /* .. Array Arguments .. */
  58. /* .. */
  59. /* Purpose */
  60. /* ======= */
  61. /* DPORFS improves the computed solution to a system of linear */
  62. /* equations when the coefficient matrix is symmetric positive definite, */
  63. /* and provides error bounds and backward error estimates for the */
  64. /* solution. */
  65. /* Arguments */
  66. /* ========= */
  67. /* UPLO (input) CHARACTER*1 */
  68. /* = 'U': Upper triangle of A is stored; */
  69. /* = 'L': Lower triangle of A is stored. */
  70. /* N (input) INTEGER */
  71. /* The order of the matrix A. N >= 0. */
  72. /* NRHS (input) INTEGER */
  73. /* The number of right hand sides, i.e., the number of columns */
  74. /* of the matrices B and X. NRHS >= 0. */
  75. /* A (input) DOUBLE PRECISION array, dimension (LDA,N) */
  76. /* The symmetric matrix A. If UPLO = 'U', the leading N-by-N */
  77. /* upper triangular part of A contains the upper triangular part */
  78. /* of the matrix A, and the strictly lower triangular part of A */
  79. /* is not referenced. If UPLO = 'L', the leading N-by-N lower */
  80. /* triangular part of A contains the lower triangular part of */
  81. /* the matrix A, and the strictly upper triangular part of A is */
  82. /* not referenced. */
  83. /* LDA (input) INTEGER */
  84. /* The leading dimension of the array A. LDA >= max(1,N). */
  85. /* AF (input) DOUBLE PRECISION array, dimension (LDAF,N) */
  86. /* The triangular factor U or L from the Cholesky factorization */
  87. /* A = U**T*U or A = L*L**T, as computed by DPOTRF. */
  88. /* LDAF (input) INTEGER */
  89. /* The leading dimension of the array AF. LDAF >= max(1,N). */
  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 DPOTRS. */
  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. b_dim1 = *ldb;
  143. b_offset = 1 + b_dim1;
  144. b -= b_offset;
  145. x_dim1 = *ldx;
  146. x_offset = 1 + x_dim1;
  147. x -= x_offset;
  148. --ferr;
  149. --berr;
  150. --work;
  151. --iwork;
  152. /* Function Body */
  153. *info = 0;
  154. upper = _starpu_lsame_(uplo, "U");
  155. if (! upper && ! _starpu_lsame_(uplo, "L")) {
  156. *info = -1;
  157. } else if (*n < 0) {
  158. *info = -2;
  159. } else if (*nrhs < 0) {
  160. *info = -3;
  161. } else if (*lda < max(1,*n)) {
  162. *info = -5;
  163. } else if (*ldaf < max(1,*n)) {
  164. *info = -7;
  165. } else if (*ldb < max(1,*n)) {
  166. *info = -9;
  167. } else if (*ldx < max(1,*n)) {
  168. *info = -11;
  169. }
  170. if (*info != 0) {
  171. i__1 = -(*info);
  172. _starpu_xerbla_("DPORFS", &i__1);
  173. return 0;
  174. }
  175. /* Quick return if possible */
  176. if (*n == 0 || *nrhs == 0) {
  177. i__1 = *nrhs;
  178. for (j = 1; j <= i__1; ++j) {
  179. ferr[j] = 0.;
  180. berr[j] = 0.;
  181. /* L10: */
  182. }
  183. return 0;
  184. }
  185. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  186. nz = *n + 1;
  187. eps = _starpu_dlamch_("Epsilon");
  188. safmin = _starpu_dlamch_("Safe minimum");
  189. safe1 = nz * safmin;
  190. safe2 = safe1 / eps;
  191. /* Do for each right hand side */
  192. i__1 = *nrhs;
  193. for (j = 1; j <= i__1; ++j) {
  194. count = 1;
  195. lstres = 3.;
  196. L20:
  197. /* Loop until stopping criterion is satisfied. */
  198. /* Compute residual R = B - A * X */
  199. _starpu_dcopy_(n, &b[j * b_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  200. _starpu_dsymv_(uplo, n, &c_b12, &a[a_offset], lda, &x[j * x_dim1 + 1], &c__1,
  201. &c_b14, &work[*n + 1], &c__1);
  202. /* Compute componentwise relative backward error from formula */
  203. /* max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) ) */
  204. /* where abs(Z) is the componentwise absolute value of the matrix */
  205. /* or vector Z. If the i-th component of the denominator is less */
  206. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  207. /* numerator and denominator before dividing. */
  208. i__2 = *n;
  209. for (i__ = 1; i__ <= i__2; ++i__) {
  210. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1));
  211. /* L30: */
  212. }
  213. /* Compute abs(A)*abs(X) + abs(B). */
  214. if (upper) {
  215. i__2 = *n;
  216. for (k = 1; k <= i__2; ++k) {
  217. s = 0.;
  218. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  219. i__3 = k - 1;
  220. for (i__ = 1; i__ <= i__3; ++i__) {
  221. work[i__] += (d__1 = a[i__ + k * a_dim1], abs(d__1)) * xk;
  222. s += (d__1 = a[i__ + k * a_dim1], abs(d__1)) * (d__2 = x[
  223. i__ + j * x_dim1], abs(d__2));
  224. /* L40: */
  225. }
  226. work[k] = work[k] + (d__1 = a[k + k * a_dim1], abs(d__1)) *
  227. xk + s;
  228. /* L50: */
  229. }
  230. } else {
  231. i__2 = *n;
  232. for (k = 1; k <= i__2; ++k) {
  233. s = 0.;
  234. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  235. work[k] += (d__1 = a[k + k * a_dim1], abs(d__1)) * xk;
  236. i__3 = *n;
  237. for (i__ = k + 1; i__ <= i__3; ++i__) {
  238. work[i__] += (d__1 = a[i__ + k * a_dim1], abs(d__1)) * xk;
  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_dpotrs_(uplo, n, &c__1, &af[af_offset], ldaf, &work[*n + 1], n,
  272. info);
  273. _starpu_daxpy_(n, &c_b14, &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(A))* */
  282. /* ( abs(R) + NZ*EPS*( abs(A)*abs(X)+abs(B) ))) / norm(X) */
  283. /* where */
  284. /* norm(Z) is the magnitude of the largest component of Z */
  285. /* inv(A) is the inverse of 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(A)*abs(X)+abs(B)) */
  291. /* is incremented by SAFE1 if the i-th component of */
  292. /* abs(A)*abs(X) + abs(B) is less than SAFE2. */
  293. /* Use DLACN2 to estimate the infinity-norm of the matrix */
  294. /* inv(A) * diag(W), */
  295. /* where W = abs(R) + NZ*EPS*( abs(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(A'). */
  314. _starpu_dpotrs_(uplo, n, &c__1, &af[af_offset], ldaf, &work[*n + 1],
  315. 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 if (kase == 2) {
  322. /* Multiply by inv(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_dpotrs_(uplo, n, &c__1, &af[af_offset], ldaf, &work[*n + 1],
  329. 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 DPORFS */
  349. } /* _starpu_dporfs_ */