dsgesv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* dsgesv.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 doublereal c_b10 = -1.;
  15. static doublereal c_b11 = 1.;
  16. static integer c__1 = 1;
  17. /* Subroutine */ int _starpu__starpu_dsgesv_(integer *n, integer *nrhs, doublereal *a,
  18. integer *lda, integer *ipiv, doublereal *b, integer *ldb, doublereal *
  19. x, integer *ldx, doublereal *work, real *swork, integer *iter,
  20. integer *info)
  21. {
  22. /* System generated locals */
  23. integer a_dim1, a_offset, b_dim1, b_offset, work_dim1, work_offset,
  24. x_dim1, x_offset, i__1;
  25. doublereal d__1;
  26. /* Builtin functions */
  27. double sqrt(doublereal);
  28. /* Local variables */
  29. integer i__;
  30. doublereal cte, eps, anrm;
  31. integer ptsa;
  32. doublereal rnrm, xnrm;
  33. integer ptsx;
  34. extern /* Subroutine */ int _starpu_dgemm_(char *, char *, integer *, integer *,
  35. integer *, doublereal *, doublereal *, integer *, doublereal *,
  36. integer *, doublereal *, doublereal *, integer *);
  37. integer iiter;
  38. extern /* Subroutine */ int _starpu_daxpy_(integer *, doublereal *, doublereal *,
  39. integer *, doublereal *, integer *), _starpu_dlag2s_(integer *, integer *,
  40. doublereal *, integer *, real *, integer *, integer *), _starpu_slag2d_(
  41. integer *, integer *, real *, integer *, doublereal *, integer *,
  42. integer *);
  43. extern doublereal _starpu_dlamch_(char *), _starpu_dlange_(char *, integer *,
  44. integer *, doublereal *, integer *, doublereal *);
  45. extern integer _starpu_idamax_(integer *, doublereal *, integer *);
  46. extern /* Subroutine */ int _starpu_dlacpy_(char *, integer *, integer *,
  47. doublereal *, integer *, doublereal *, integer *),
  48. _starpu_xerbla_(char *, integer *), _starpu_dgetrf_(integer *, integer *,
  49. doublereal *, integer *, integer *, integer *), _starpu_dgetrs_(char *,
  50. integer *, integer *, doublereal *, integer *, integer *,
  51. doublereal *, integer *, integer *), _starpu_sgetrf_(integer *,
  52. integer *, real *, integer *, integer *, integer *), _starpu_sgetrs_(char
  53. *, integer *, integer *, real *, integer *, integer *, real *,
  54. integer *, integer *);
  55. /* -- LAPACK PROTOTYPE driver routine (version 3.2) -- */
  56. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  57. /* February 2007 */
  58. /* .. */
  59. /* .. Scalar Arguments .. */
  60. /* .. */
  61. /* .. Array Arguments .. */
  62. /* .. */
  63. /* Purpose */
  64. /* ======= */
  65. /* DSGESV computes the solution to a real system of linear equations */
  66. /* A * X = B, */
  67. /* where A is an N-by-N matrix and X and B are N-by-NRHS matrices. */
  68. /* DSGESV first attempts to factorize the matrix in SINGLE PRECISION */
  69. /* and use this factorization within an iterative refinement procedure */
  70. /* to produce a solution with DOUBLE PRECISION normwise backward error */
  71. /* quality (see below). If the approach fails the method switches to a */
  72. /* DOUBLE PRECISION factorization and solve. */
  73. /* The iterative refinement is not going to be a winning strategy if */
  74. /* the ratio SINGLE PRECISION performance over DOUBLE PRECISION */
  75. /* performance is too small. A reasonable strategy should take the */
  76. /* number of right-hand sides and the size of the matrix into account. */
  77. /* This might be done with a call to ILAENV in the future. Up to now, we */
  78. /* always try iterative refinement. */
  79. /* The iterative refinement process is stopped if */
  80. /* ITER > ITERMAX */
  81. /* or for all the RHS we have: */
  82. /* RNRM < SQRT(N)*XNRM*ANRM*EPS*BWDMAX */
  83. /* where */
  84. /* o ITER is the number of the current iteration in the iterative */
  85. /* refinement process */
  86. /* o RNRM is the infinity-norm of the residual */
  87. /* o XNRM is the infinity-norm of the solution */
  88. /* o ANRM is the infinity-operator-norm of the matrix A */
  89. /* o EPS is the machine epsilon returned by DLAMCH('Epsilon') */
  90. /* The value ITERMAX and BWDMAX are fixed to 30 and 1.0D+00 */
  91. /* respectively. */
  92. /* Arguments */
  93. /* ========= */
  94. /* N (input) INTEGER */
  95. /* The number of linear equations, i.e., the order of the */
  96. /* matrix A. N >= 0. */
  97. /* NRHS (input) INTEGER */
  98. /* The number of right hand sides, i.e., the number of columns */
  99. /* of the matrix B. NRHS >= 0. */
  100. /* A (input or input/ouptut) DOUBLE PRECISION array, */
  101. /* dimension (LDA,N) */
  102. /* On entry, the N-by-N coefficient matrix A. */
  103. /* On exit, if iterative refinement has been successfully used */
  104. /* (INFO.EQ.0 and ITER.GE.0, see description below), then A is */
  105. /* unchanged, if double precision factorization has been used */
  106. /* (INFO.EQ.0 and ITER.LT.0, see description below), then the */
  107. /* array A contains the factors L and U from the factorization */
  108. /* A = P*L*U; the unit diagonal elements of L are not stored. */
  109. /* LDA (input) INTEGER */
  110. /* The leading dimension of the array A. LDA >= max(1,N). */
  111. /* IPIV (output) INTEGER array, dimension (N) */
  112. /* The pivot indices that define the permutation matrix P; */
  113. /* row i of the matrix was interchanged with row IPIV(i). */
  114. /* Corresponds either to the single precision factorization */
  115. /* (if INFO.EQ.0 and ITER.GE.0) or the double precision */
  116. /* factorization (if INFO.EQ.0 and ITER.LT.0). */
  117. /* B (input) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  118. /* The N-by-NRHS right hand side matrix B. */
  119. /* LDB (input) INTEGER */
  120. /* The leading dimension of the array B. LDB >= max(1,N). */
  121. /* X (output) DOUBLE PRECISION array, dimension (LDX,NRHS) */
  122. /* If INFO = 0, the N-by-NRHS solution matrix X. */
  123. /* LDX (input) INTEGER */
  124. /* The leading dimension of the array X. LDX >= max(1,N). */
  125. /* WORK (workspace) DOUBLE PRECISION array, dimension (N*NRHS) */
  126. /* This array is used to hold the residual vectors. */
  127. /* SWORK (workspace) REAL array, dimension (N*(N+NRHS)) */
  128. /* This array is used to use the single precision matrix and the */
  129. /* right-hand sides or solutions in single precision. */
  130. /* ITER (output) INTEGER */
  131. /* < 0: iterative refinement has failed, double precision */
  132. /* factorization has been performed */
  133. /* -1 : the routine fell back to full precision for */
  134. /* implementation- or machine-specific reasons */
  135. /* -2 : narrowing the precision induced an overflow, */
  136. /* the routine fell back to full precision */
  137. /* -3 : failure of SGETRF */
  138. /* -31: stop the iterative refinement after the 30th */
  139. /* iterations */
  140. /* > 0: iterative refinement has been sucessfully used. */
  141. /* Returns the number of iterations */
  142. /* INFO (output) INTEGER */
  143. /* = 0: successful exit */
  144. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  145. /* > 0: if INFO = i, U(i,i) computed in DOUBLE PRECISION is */
  146. /* exactly zero. The factorization has been completed, */
  147. /* but the factor U is exactly singular, so the solution */
  148. /* could not be computed. */
  149. /* ========= */
  150. /* .. Parameters .. */
  151. /* .. Local Scalars .. */
  152. /* .. External Subroutines .. */
  153. /* .. */
  154. /* .. External Functions .. */
  155. /* .. */
  156. /* .. Intrinsic Functions .. */
  157. /* .. */
  158. /* .. Executable Statements .. */
  159. /* Parameter adjustments */
  160. work_dim1 = *n;
  161. work_offset = 1 + work_dim1;
  162. work -= work_offset;
  163. a_dim1 = *lda;
  164. a_offset = 1 + a_dim1;
  165. a -= a_offset;
  166. --ipiv;
  167. b_dim1 = *ldb;
  168. b_offset = 1 + b_dim1;
  169. b -= b_offset;
  170. x_dim1 = *ldx;
  171. x_offset = 1 + x_dim1;
  172. x -= x_offset;
  173. --swork;
  174. /* Function Body */
  175. *info = 0;
  176. *iter = 0;
  177. /* Test the input parameters. */
  178. if (*n < 0) {
  179. *info = -1;
  180. } else if (*nrhs < 0) {
  181. *info = -2;
  182. } else if (*lda < max(1,*n)) {
  183. *info = -4;
  184. } else if (*ldb < max(1,*n)) {
  185. *info = -7;
  186. } else if (*ldx < max(1,*n)) {
  187. *info = -9;
  188. }
  189. if (*info != 0) {
  190. i__1 = -(*info);
  191. _starpu_xerbla_("DSGESV", &i__1);
  192. return 0;
  193. }
  194. /* Quick return if (N.EQ.0). */
  195. if (*n == 0) {
  196. return 0;
  197. }
  198. /* Skip single precision iterative refinement if a priori slower */
  199. /* than double precision factorization. */
  200. if (FALSE_) {
  201. *iter = -1;
  202. goto L40;
  203. }
  204. /* Compute some constants. */
  205. anrm = _starpu_dlange_("I", n, n, &a[a_offset], lda, &work[work_offset]);
  206. eps = _starpu_dlamch_("Epsilon");
  207. cte = anrm * eps * sqrt((doublereal) (*n)) * 1.;
  208. /* Set the indices PTSA, PTSX for referencing SA and SX in SWORK. */
  209. ptsa = 1;
  210. ptsx = ptsa + *n * *n;
  211. /* Convert B from double precision to single precision and store the */
  212. /* result in SX. */
  213. _starpu_dlag2s_(n, nrhs, &b[b_offset], ldb, &swork[ptsx], n, info);
  214. if (*info != 0) {
  215. *iter = -2;
  216. goto L40;
  217. }
  218. /* Convert A from double precision to single precision and store the */
  219. /* result in SA. */
  220. _starpu_dlag2s_(n, n, &a[a_offset], lda, &swork[ptsa], n, info);
  221. if (*info != 0) {
  222. *iter = -2;
  223. goto L40;
  224. }
  225. /* Compute the LU factorization of SA. */
  226. _starpu_sgetrf_(n, n, &swork[ptsa], n, &ipiv[1], info);
  227. if (*info != 0) {
  228. *iter = -3;
  229. goto L40;
  230. }
  231. /* Solve the system SA*SX = SB. */
  232. _starpu_sgetrs_("No transpose", n, nrhs, &swork[ptsa], n, &ipiv[1], &swork[ptsx],
  233. n, info);
  234. /* Convert SX back to double precision */
  235. _starpu_slag2d_(n, nrhs, &swork[ptsx], n, &x[x_offset], ldx, info);
  236. /* Compute R = B - AX (R is WORK). */
  237. _starpu_dlacpy_("All", n, nrhs, &b[b_offset], ldb, &work[work_offset], n);
  238. _starpu_dgemm_("No Transpose", "No Transpose", n, nrhs, n, &c_b10, &a[a_offset],
  239. lda, &x[x_offset], ldx, &c_b11, &work[work_offset], n);
  240. /* Check whether the NRHS normwise backward errors satisfy the */
  241. /* stopping criterion. If yes, set ITER=0 and return. */
  242. i__1 = *nrhs;
  243. for (i__ = 1; i__ <= i__1; ++i__) {
  244. xnrm = (d__1 = x[_starpu_idamax_(n, &x[i__ * x_dim1 + 1], &c__1) + i__ *
  245. x_dim1], abs(d__1));
  246. rnrm = (d__1 = work[_starpu_idamax_(n, &work[i__ * work_dim1 + 1], &c__1) +
  247. i__ * work_dim1], abs(d__1));
  248. if (rnrm > xnrm * cte) {
  249. goto L10;
  250. }
  251. }
  252. /* If we are here, the NRHS normwise backward errors satisfy the */
  253. /* stopping criterion. We are good to exit. */
  254. *iter = 0;
  255. return 0;
  256. L10:
  257. for (iiter = 1; iiter <= 30; ++iiter) {
  258. /* Convert R (in WORK) from double precision to single precision */
  259. /* and store the result in SX. */
  260. _starpu_dlag2s_(n, nrhs, &work[work_offset], n, &swork[ptsx], n, info);
  261. if (*info != 0) {
  262. *iter = -2;
  263. goto L40;
  264. }
  265. /* Solve the system SA*SX = SR. */
  266. _starpu_sgetrs_("No transpose", n, nrhs, &swork[ptsa], n, &ipiv[1], &swork[
  267. ptsx], n, info);
  268. /* Convert SX back to double precision and update the current */
  269. /* iterate. */
  270. _starpu_slag2d_(n, nrhs, &swork[ptsx], n, &work[work_offset], n, info);
  271. i__1 = *nrhs;
  272. for (i__ = 1; i__ <= i__1; ++i__) {
  273. _starpu_daxpy_(n, &c_b11, &work[i__ * work_dim1 + 1], &c__1, &x[i__ *
  274. x_dim1 + 1], &c__1);
  275. }
  276. /* Compute R = B - AX (R is WORK). */
  277. _starpu_dlacpy_("All", n, nrhs, &b[b_offset], ldb, &work[work_offset], n);
  278. _starpu_dgemm_("No Transpose", "No Transpose", n, nrhs, n, &c_b10, &a[
  279. a_offset], lda, &x[x_offset], ldx, &c_b11, &work[work_offset],
  280. n);
  281. /* Check whether the NRHS normwise backward errors satisfy the */
  282. /* stopping criterion. If yes, set ITER=IITER>0 and return. */
  283. i__1 = *nrhs;
  284. for (i__ = 1; i__ <= i__1; ++i__) {
  285. xnrm = (d__1 = x[_starpu_idamax_(n, &x[i__ * x_dim1 + 1], &c__1) + i__ *
  286. x_dim1], abs(d__1));
  287. rnrm = (d__1 = work[_starpu_idamax_(n, &work[i__ * work_dim1 + 1], &c__1)
  288. + i__ * work_dim1], abs(d__1));
  289. if (rnrm > xnrm * cte) {
  290. goto L20;
  291. }
  292. }
  293. /* If we are here, the NRHS normwise backward errors satisfy the */
  294. /* stopping criterion, we are good to exit. */
  295. *iter = iiter;
  296. return 0;
  297. L20:
  298. /* L30: */
  299. ;
  300. }
  301. /* If we are at this place of the code, this is because we have */
  302. /* performed ITER=ITERMAX iterations and never satisified the */
  303. /* stopping criterion, set up the ITER flag accordingly and follow up */
  304. /* on double precision routine. */
  305. *iter = -31;
  306. L40:
  307. /* Single-precision iterative refinement failed to converge to a */
  308. /* satisfactory solution, so we resort to double precision. */
  309. _starpu_dgetrf_(n, n, &a[a_offset], lda, &ipiv[1], info);
  310. if (*info != 0) {
  311. return 0;
  312. }
  313. _starpu_dlacpy_("All", n, nrhs, &b[b_offset], ldb, &x[x_offset], ldx);
  314. _starpu_dgetrs_("No transpose", n, nrhs, &a[a_offset], lda, &ipiv[1], &x[x_offset]
  315. , ldx, info);
  316. return 0;
  317. /* End of DSGESV. */
  318. } /* _starpu__starpu_dsgesv_ */