dsprfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* dsprfs.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_dsprfs_(char *uplo, integer *n, integer *nrhs,
  18. doublereal *ap, doublereal *afp, integer *ipiv, doublereal *b,
  19. integer *ldb, doublereal *x, integer *ldx, doublereal *ferr,
  20. doublereal *berr, doublereal *work, integer *iwork, integer *info)
  21. {
  22. /* System generated locals */
  23. integer b_dim1, b_offset, x_dim1, x_offset, i__1, i__2, i__3;
  24. doublereal d__1, d__2, d__3;
  25. /* Local variables */
  26. integer i__, j, k;
  27. doublereal s;
  28. integer ik, kk;
  29. doublereal 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. extern /* Subroutine */ int _starpu_dspmv_(char *, integer *, doublereal *,
  41. doublereal *, doublereal *, integer *, doublereal *, doublereal *,
  42. integer *);
  43. logical upper;
  44. extern /* Subroutine */ int _starpu_dlacn2_(integer *, doublereal *, doublereal *,
  45. integer *, doublereal *, integer *, integer *);
  46. extern doublereal _starpu_dlamch_(char *);
  47. doublereal safmin;
  48. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  49. doublereal lstres;
  50. extern /* Subroutine */ int _starpu_dsptrs_(char *, integer *, integer *,
  51. doublereal *, integer *, doublereal *, integer *, integer *);
  52. /* -- LAPACK routine (version 3.2) -- */
  53. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  54. /* November 2006 */
  55. /* Modified to call DLACN2 in place of DLACON, 5 Feb 03, SJH. */
  56. /* .. Scalar Arguments .. */
  57. /* .. */
  58. /* .. Array Arguments .. */
  59. /* .. */
  60. /* Purpose */
  61. /* ======= */
  62. /* DSPRFS improves the computed solution to a system of linear */
  63. /* equations when the coefficient matrix is symmetric indefinite */
  64. /* and packed, and provides error bounds and backward error estimates */
  65. /* for the solution. */
  66. /* Arguments */
  67. /* ========= */
  68. /* UPLO (input) CHARACTER*1 */
  69. /* = 'U': Upper triangle of A is stored; */
  70. /* = 'L': Lower triangle of A is stored. */
  71. /* N (input) INTEGER */
  72. /* The order of the matrix A. N >= 0. */
  73. /* NRHS (input) INTEGER */
  74. /* The number of right hand sides, i.e., the number of columns */
  75. /* of the matrices B and X. NRHS >= 0. */
  76. /* AP (input) DOUBLE PRECISION array, dimension (N*(N+1)/2) */
  77. /* The upper or lower triangle of the symmetric matrix A, packed */
  78. /* columnwise in a linear array. The j-th column of A is stored */
  79. /* in the array AP as follows: */
  80. /* if UPLO = 'U', AP(i + (j-1)*j/2) = A(i,j) for 1<=i<=j; */
  81. /* if UPLO = 'L', AP(i + (j-1)*(2*n-j)/2) = A(i,j) for j<=i<=n. */
  82. /* AFP (input) DOUBLE PRECISION array, dimension (N*(N+1)/2) */
  83. /* The factored form of the matrix A. AFP contains the block */
  84. /* diagonal matrix D and the multipliers used to obtain the */
  85. /* factor U or L from the factorization A = U*D*U**T or */
  86. /* A = L*D*L**T as computed by DSPTRF, stored as a packed */
  87. /* triangular matrix. */
  88. /* IPIV (input) INTEGER array, dimension (N) */
  89. /* Details of the interchanges and the block structure of D */
  90. /* as determined by DSPTRF. */
  91. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  92. /* The right hand side matrix B. */
  93. /* LDB (input) INTEGER */
  94. /* The leading dimension of the array B. LDB >= max(1,N). */
  95. /* X (input/output) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  96. /* On entry, the solution matrix X, as computed by DSPTRS. */
  97. /* On exit, the improved solution matrix X. */
  98. /* LDX (input) INTEGER */
  99. /* The leading dimension of the array X. LDX >= max(1,N). */
  100. /* FERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  101. /* The estimated forward error bound for each solution vector */
  102. /* X(j) (the j-th column of the solution matrix X). */
  103. /* If XTRUE is the true solution corresponding to X(j), FERR(j) */
  104. /* is an estimated upper bound for the magnitude of the largest */
  105. /* element in (X(j) - XTRUE) divided by the magnitude of the */
  106. /* largest element in X(j). The estimate is as reliable as */
  107. /* the estimate for RCOND, and is almost always a slight */
  108. /* overestimate of the true error. */
  109. /* BERR (output) DOUBLE PRECISION array, dimension (NRHS) */
  110. /* The componentwise relative backward error of each solution */
  111. /* vector X(j) (i.e., the smallest relative change in */
  112. /* any element of A or B that makes X(j) an exact solution). */
  113. /* WORK (workspace) DOUBLE PRECISION array, dimension (3*N) */
  114. /* IWORK (workspace) INTEGER array, dimension (N) */
  115. /* INFO (output) INTEGER */
  116. /* = 0: successful exit */
  117. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  118. /* Internal Parameters */
  119. /* =================== */
  120. /* ITMAX is the maximum number of steps of iterative refinement. */
  121. /* ===================================================================== */
  122. /* .. Parameters .. */
  123. /* .. */
  124. /* .. Local Scalars .. */
  125. /* .. */
  126. /* .. Local Arrays .. */
  127. /* .. */
  128. /* .. External Subroutines .. */
  129. /* .. */
  130. /* .. Intrinsic Functions .. */
  131. /* .. */
  132. /* .. External Functions .. */
  133. /* .. */
  134. /* .. Executable Statements .. */
  135. /* Test the input parameters. */
  136. /* Parameter adjustments */
  137. --ap;
  138. --afp;
  139. --ipiv;
  140. b_dim1 = *ldb;
  141. b_offset = 1 + b_dim1;
  142. b -= b_offset;
  143. x_dim1 = *ldx;
  144. x_offset = 1 + x_dim1;
  145. x -= x_offset;
  146. --ferr;
  147. --berr;
  148. --work;
  149. --iwork;
  150. /* Function Body */
  151. *info = 0;
  152. upper = _starpu_lsame_(uplo, "U");
  153. if (! upper && ! _starpu_lsame_(uplo, "L")) {
  154. *info = -1;
  155. } else if (*n < 0) {
  156. *info = -2;
  157. } else if (*nrhs < 0) {
  158. *info = -3;
  159. } else if (*ldb < max(1,*n)) {
  160. *info = -8;
  161. } else if (*ldx < max(1,*n)) {
  162. *info = -10;
  163. }
  164. if (*info != 0) {
  165. i__1 = -(*info);
  166. _starpu_xerbla_("DSPRFS", &i__1);
  167. return 0;
  168. }
  169. /* Quick return if possible */
  170. if (*n == 0 || *nrhs == 0) {
  171. i__1 = *nrhs;
  172. for (j = 1; j <= i__1; ++j) {
  173. ferr[j] = 0.;
  174. berr[j] = 0.;
  175. /* L10: */
  176. }
  177. return 0;
  178. }
  179. /* NZ = maximum number of nonzero elements in each row of A, plus 1 */
  180. nz = *n + 1;
  181. eps = _starpu_dlamch_("Epsilon");
  182. safmin = _starpu_dlamch_("Safe minimum");
  183. safe1 = nz * safmin;
  184. safe2 = safe1 / eps;
  185. /* Do for each right hand side */
  186. i__1 = *nrhs;
  187. for (j = 1; j <= i__1; ++j) {
  188. count = 1;
  189. lstres = 3.;
  190. L20:
  191. /* Loop until stopping criterion is satisfied. */
  192. /* Compute residual R = B - A * X */
  193. _starpu_dcopy_(n, &b[j * b_dim1 + 1], &c__1, &work[*n + 1], &c__1);
  194. _starpu_dspmv_(uplo, n, &c_b12, &ap[1], &x[j * x_dim1 + 1], &c__1, &c_b14, &
  195. work[*n + 1], &c__1);
  196. /* Compute componentwise relative backward error from formula */
  197. /* max(i) ( abs(R(i)) / ( abs(A)*abs(X) + abs(B) )(i) ) */
  198. /* where abs(Z) is the componentwise absolute value of the matrix */
  199. /* or vector Z. If the i-th component of the denominator is less */
  200. /* than SAFE2, then SAFE1 is added to the i-th components of the */
  201. /* numerator and denominator before dividing. */
  202. i__2 = *n;
  203. for (i__ = 1; i__ <= i__2; ++i__) {
  204. work[i__] = (d__1 = b[i__ + j * b_dim1], abs(d__1));
  205. /* L30: */
  206. }
  207. /* Compute abs(A)*abs(X) + abs(B). */
  208. kk = 1;
  209. if (upper) {
  210. i__2 = *n;
  211. for (k = 1; k <= i__2; ++k) {
  212. s = 0.;
  213. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  214. ik = kk;
  215. i__3 = k - 1;
  216. for (i__ = 1; i__ <= i__3; ++i__) {
  217. work[i__] += (d__1 = ap[ik], abs(d__1)) * xk;
  218. s += (d__1 = ap[ik], abs(d__1)) * (d__2 = x[i__ + j *
  219. x_dim1], abs(d__2));
  220. ++ik;
  221. /* L40: */
  222. }
  223. work[k] = work[k] + (d__1 = ap[kk + k - 1], abs(d__1)) * xk +
  224. s;
  225. kk += k;
  226. /* L50: */
  227. }
  228. } else {
  229. i__2 = *n;
  230. for (k = 1; k <= i__2; ++k) {
  231. s = 0.;
  232. xk = (d__1 = x[k + j * x_dim1], abs(d__1));
  233. work[k] += (d__1 = ap[kk], abs(d__1)) * xk;
  234. ik = kk + 1;
  235. i__3 = *n;
  236. for (i__ = k + 1; i__ <= i__3; ++i__) {
  237. work[i__] += (d__1 = ap[ik], abs(d__1)) * xk;
  238. s += (d__1 = ap[ik], abs(d__1)) * (d__2 = x[i__ + j *
  239. x_dim1], abs(d__2));
  240. ++ik;
  241. /* L60: */
  242. }
  243. work[k] += s;
  244. kk += *n - k + 1;
  245. /* L70: */
  246. }
  247. }
  248. s = 0.;
  249. i__2 = *n;
  250. for (i__ = 1; i__ <= i__2; ++i__) {
  251. if (work[i__] > safe2) {
  252. /* Computing MAX */
  253. d__2 = s, d__3 = (d__1 = work[*n + i__], abs(d__1)) / work[
  254. i__];
  255. s = max(d__2,d__3);
  256. } else {
  257. /* Computing MAX */
  258. d__2 = s, d__3 = ((d__1 = work[*n + i__], abs(d__1)) + safe1)
  259. / (work[i__] + safe1);
  260. s = max(d__2,d__3);
  261. }
  262. /* L80: */
  263. }
  264. berr[j] = s;
  265. /* Test stopping criterion. Continue iterating if */
  266. /* 1) The residual BERR(J) is larger than machine epsilon, and */
  267. /* 2) BERR(J) decreased by at least a factor of 2 during the */
  268. /* last iteration, and */
  269. /* 3) At most ITMAX iterations tried. */
  270. if (berr[j] > eps && berr[j] * 2. <= lstres && count <= 5) {
  271. /* Update solution and try again. */
  272. _starpu_dsptrs_(uplo, n, &c__1, &afp[1], &ipiv[1], &work[*n + 1], n, 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_dsptrs_(uplo, n, &c__1, &afp[1], &ipiv[1], &work[*n + 1], n,
  315. 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_dsptrs_(uplo, n, &c__1, &afp[1], &ipiv[1], &work[*n + 1], n,
  329. 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 DSPRFS */
  349. } /* _starpu_dsprfs_ */