dgeqrf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* dgeqrf.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 integer c_n1 = -1;
  16. static integer c__3 = 3;
  17. static integer c__2 = 2;
  18. /* Subroutine */ int _starpu_dgeqrf_(integer *m, integer *n, doublereal *a, integer *
  19. lda, doublereal *tau, doublereal *work, integer *lwork, integer *info)
  20. {
  21. /* System generated locals */
  22. integer a_dim1, a_offset, i__1, i__2, i__3, i__4, i__5, i__6;
  23. real r__1;
  24. /* Local variables */
  25. integer i__, j, k, ib, nb, nt, nx, iws;
  26. extern doublereal _starpu_sceil_(real *);
  27. integer nbmin, iinfo;
  28. extern /* Subroutine */ int _starpu_dgeqr2_(integer *, integer *, doublereal *,
  29. integer *, doublereal *, doublereal *, integer *), _starpu_dlarfb_(char *,
  30. char *, char *, char *, integer *, integer *, integer *,
  31. doublereal *, integer *, doublereal *, integer *, doublereal *,
  32. integer *, doublereal *, integer *), _starpu_dlarft_(char *, char *, integer *, integer *, doublereal
  33. *, integer *, doublereal *, doublereal *, integer *), _starpu_xerbla_(char *, integer *);
  34. extern integer _starpu_ilaenv_(integer *, char *, char *, integer *, integer *,
  35. integer *, integer *);
  36. integer lbwork, llwork, lwkopt;
  37. logical lquery;
  38. /* -- LAPACK routine (version 3.1) -- */
  39. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  40. /* March 2008 */
  41. /* .. Scalar Arguments .. */
  42. /* .. */
  43. /* .. Array Arguments .. */
  44. /* .. */
  45. /* Purpose */
  46. /* ======= */
  47. /* DGEQRF computes a QR factorization of a real M-by-N matrix A: */
  48. /* A = Q * R. */
  49. /* This is the left-looking Level 3 BLAS version of the algorithm. */
  50. /* Arguments */
  51. /* ========= */
  52. /* M (input) INTEGER */
  53. /* The number of rows of the matrix A. M >= 0. */
  54. /* N (input) INTEGER */
  55. /* The number of columns of the matrix A. N >= 0. */
  56. /* A (input/output) DOUBLE PRECISION array, dimension (LDA,N) */
  57. /* On entry, the M-by-N matrix A. */
  58. /* On exit, the elements on and above the diagonal of the array */
  59. /* contain the min(M,N)-by-N upper trapezoidal matrix R (R is */
  60. /* upper triangular if m >= n); the elements below the diagonal, */
  61. /* with the array TAU, represent the orthogonal matrix Q as a */
  62. /* product of min(m,n) elementary reflectors (see Further */
  63. /* Details). */
  64. /* LDA (input) INTEGER */
  65. /* The leading dimension of the array A. LDA >= max(1,M). */
  66. /* TAU (output) DOUBLE PRECISION array, dimension (min(M,N)) */
  67. /* The scalar factors of the elementary reflectors (see Further */
  68. /* Details). */
  69. /* WORK (workspace/output) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  70. /* On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
  71. /* LWORK (input) INTEGER */
  72. /* The dimension of the array WORK. The dimension can be divided into three parts. */
  73. /* 1) The part for the triangular factor T. If the very last T is not bigger */
  74. /* than any of the rest, then this part is NB x ceiling(K/NB), otherwise, */
  75. /* NB x (K-NT), where K = min(M,N) and NT is the dimension of the very last T */
  76. /* 2) The part for the very last T when T is bigger than any of the rest T. */
  77. /* The size of this part is NT x NT, where NT = K - ceiling ((K-NX)/NB) x NB, */
  78. /* where K = min(M,N), NX is calculated by */
  79. /* NX = MAX( 0, ILAENV( 3, 'DGEQRF', ' ', M, N, -1, -1 ) ) */
  80. /* 3) The part for dlarfb is of size max((N-M)*K, (N-M)*NB, K*NB, NB*NB) */
  81. /* So LWORK = part1 + part2 + part3 */
  82. /* If LWORK = -1, then a workspace query is assumed; the routine */
  83. /* only calculates the optimal size of the WORK array, returns */
  84. /* this value as the first entry of the WORK array, and no error */
  85. /* message related to LWORK is issued by XERBLA. */
  86. /* INFO (output) INTEGER */
  87. /* = 0: successful exit */
  88. /* < 0: if INFO = -i, the i-th argument had an illegal value */
  89. /* Further Details */
  90. /* =============== */
  91. /* The matrix Q is represented as a product of elementary reflectors */
  92. /* Q = H(1) H(2) . . . H(k), where k = min(m,n). */
  93. /* Each H(i) has the form */
  94. /* H(i) = I - tau * v * v' */
  95. /* where tau is a real scalar, and v is a real vector with */
  96. /* v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in A(i+1:m,i), */
  97. /* and tau in TAU(i). */
  98. /* ===================================================================== */
  99. /* .. Local Scalars .. */
  100. /* .. */
  101. /* .. External Subroutines .. */
  102. /* .. */
  103. /* .. Intrinsic Functions .. */
  104. /* .. */
  105. /* .. External Functions .. */
  106. /* .. */
  107. /* .. Executable Statements .. */
  108. /* Parameter adjustments */
  109. a_dim1 = *lda;
  110. a_offset = 1 + a_dim1;
  111. a -= a_offset;
  112. --tau;
  113. --work;
  114. /* Function Body */
  115. *info = 0;
  116. nbmin = 2;
  117. nx = 0;
  118. iws = *n;
  119. k = min(*m,*n);
  120. nb = _starpu_ilaenv_(&c__1, "DGEQRF", " ", m, n, &c_n1, &c_n1);
  121. if (nb > 1 && nb < k) {
  122. /* Determine when to cross over from blocked to unblocked code. */
  123. /* Computing MAX */
  124. i__1 = 0, i__2 = _starpu_ilaenv_(&c__3, "DGEQRF", " ", m, n, &c_n1, &c_n1);
  125. nx = max(i__1,i__2);
  126. }
  127. /* Get NT, the size of the very last T, which is the left-over from in-between K-NX and K to K, eg.: */
  128. /* NB=3 2NB=6 K=10 */
  129. /* | | | */
  130. /* 1--2--3--4--5--6--7--8--9--10 */
  131. /* | \________/ */
  132. /* K-NX=5 NT=4 */
  133. /* So here 4 x 4 is the last T stored in the workspace */
  134. r__1 = (real) (k - nx) / (real) nb;
  135. nt = k - _starpu_sceil_(&r__1) * nb;
  136. /* optimal workspace = space for dlarfb + space for normal T's + space for the last T */
  137. /* Computing MAX */
  138. /* Computing MAX */
  139. i__3 = (*n - *m) * k, i__4 = (*n - *m) * nb;
  140. /* Computing MAX */
  141. i__5 = k * nb, i__6 = nb * nb;
  142. i__1 = max(i__3,i__4), i__2 = max(i__5,i__6);
  143. llwork = max(i__1,i__2);
  144. r__1 = (real) llwork / (real) nb;
  145. llwork = _starpu_sceil_(&r__1);
  146. if (nt > nb) {
  147. lbwork = k - nt;
  148. /* Optimal workspace for dlarfb = MAX(1,N)*NT */
  149. lwkopt = (lbwork + llwork) * nb;
  150. work[1] = (doublereal) (lwkopt + nt * nt);
  151. } else {
  152. r__1 = (real) k / (real) nb;
  153. lbwork = _starpu_sceil_(&r__1) * nb;
  154. lwkopt = (lbwork + llwork - nb) * nb;
  155. work[1] = (doublereal) lwkopt;
  156. }
  157. /* Test the input arguments */
  158. lquery = *lwork == -1;
  159. if (*m < 0) {
  160. *info = -1;
  161. } else if (*n < 0) {
  162. *info = -2;
  163. } else if (*lda < max(1,*m)) {
  164. *info = -4;
  165. } else if (*lwork < max(1,*n) && ! lquery) {
  166. *info = -7;
  167. }
  168. if (*info != 0) {
  169. i__1 = -(*info);
  170. _starpu_xerbla_("DGEQRF", &i__1);
  171. return 0;
  172. } else if (lquery) {
  173. return 0;
  174. }
  175. /* Quick return if possible */
  176. if (k == 0) {
  177. work[1] = 1.;
  178. return 0;
  179. }
  180. if (nb > 1 && nb < k) {
  181. if (nx < k) {
  182. /* Determine if workspace is large enough for blocked code. */
  183. if (nt <= nb) {
  184. iws = (lbwork + llwork - nb) * nb;
  185. } else {
  186. iws = (lbwork + llwork) * nb + nt * nt;
  187. }
  188. if (*lwork < iws) {
  189. /* Not enough workspace to use optimal NB: reduce NB and */
  190. /* determine the minimum value of NB. */
  191. if (nt <= nb) {
  192. nb = *lwork / (llwork + (lbwork - nb));
  193. } else {
  194. nb = (*lwork - nt * nt) / (lbwork + llwork);
  195. }
  196. /* Computing MAX */
  197. i__1 = 2, i__2 = _starpu_ilaenv_(&c__2, "DGEQRF", " ", m, n, &c_n1, &
  198. c_n1);
  199. nbmin = max(i__1,i__2);
  200. }
  201. }
  202. }
  203. if (nb >= nbmin && nb < k && nx < k) {
  204. /* Use blocked code initially */
  205. i__1 = k - nx;
  206. i__2 = nb;
  207. for (i__ = 1; i__2 < 0 ? i__ >= i__1 : i__ <= i__1; i__ += i__2) {
  208. /* Computing MIN */
  209. i__3 = k - i__ + 1;
  210. ib = min(i__3,nb);
  211. /* Update the current column using old T's */
  212. i__3 = i__ - nb;
  213. i__4 = nb;
  214. for (j = 1; i__4 < 0 ? j >= i__3 : j <= i__3; j += i__4) {
  215. /* Apply H' to A(J:M,I:I+IB-1) from the left */
  216. i__5 = *m - j + 1;
  217. _starpu_dlarfb_("Left", "Transpose", "Forward", "Columnwise", &i__5, &
  218. ib, &nb, &a[j + j * a_dim1], lda, &work[j], &lbwork, &
  219. a[j + i__ * a_dim1], lda, &work[lbwork * nb + nt * nt
  220. + 1], &ib);
  221. /* L20: */
  222. }
  223. /* Compute the QR factorization of the current block */
  224. /* A(I:M,I:I+IB-1) */
  225. i__4 = *m - i__ + 1;
  226. _starpu_dgeqr2_(&i__4, &ib, &a[i__ + i__ * a_dim1], lda, &tau[i__], &work[
  227. lbwork * nb + nt * nt + 1], &iinfo);
  228. if (i__ + ib <= *n) {
  229. /* Form the triangular factor of the block reflector */
  230. /* H = H(i) H(i+1) . . . H(i+ib-1) */
  231. i__4 = *m - i__ + 1;
  232. _starpu_dlarft_("Forward", "Columnwise", &i__4, &ib, &a[i__ + i__ *
  233. a_dim1], lda, &tau[i__], &work[i__], &lbwork);
  234. }
  235. /* L10: */
  236. }
  237. } else {
  238. i__ = 1;
  239. }
  240. /* Use unblocked code to factor the last or only block. */
  241. if (i__ <= k) {
  242. if (i__ != 1) {
  243. i__2 = i__ - nb;
  244. i__1 = nb;
  245. for (j = 1; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
  246. /* Apply H' to A(J:M,I:K) from the left */
  247. i__4 = *m - j + 1;
  248. i__3 = k - i__ + 1;
  249. i__5 = k - i__ + 1;
  250. _starpu_dlarfb_("Left", "Transpose", "Forward", "Columnwise", &i__4, &
  251. i__3, &nb, &a[j + j * a_dim1], lda, &work[j], &lbwork,
  252. &a[j + i__ * a_dim1], lda, &work[lbwork * nb + nt *
  253. nt + 1], &i__5);
  254. /* L30: */
  255. }
  256. i__1 = *m - i__ + 1;
  257. i__2 = k - i__ + 1;
  258. _starpu_dgeqr2_(&i__1, &i__2, &a[i__ + i__ * a_dim1], lda, &tau[i__], &
  259. work[lbwork * nb + nt * nt + 1], &iinfo);
  260. } else {
  261. /* Use unblocked code to factor the last or only block. */
  262. i__1 = *m - i__ + 1;
  263. i__2 = *n - i__ + 1;
  264. _starpu_dgeqr2_(&i__1, &i__2, &a[i__ + i__ * a_dim1], lda, &tau[i__], &
  265. work[1], &iinfo);
  266. }
  267. }
  268. /* Apply update to the column M+1:N when N > M */
  269. if (*m < *n && i__ != 1) {
  270. /* Form the last triangular factor of the block reflector */
  271. /* H = H(i) H(i+1) . . . H(i+ib-1) */
  272. if (nt <= nb) {
  273. i__1 = *m - i__ + 1;
  274. i__2 = k - i__ + 1;
  275. _starpu_dlarft_("Forward", "Columnwise", &i__1, &i__2, &a[i__ + i__ *
  276. a_dim1], lda, &tau[i__], &work[i__], &lbwork);
  277. } else {
  278. i__1 = *m - i__ + 1;
  279. i__2 = k - i__ + 1;
  280. _starpu_dlarft_("Forward", "Columnwise", &i__1, &i__2, &a[i__ + i__ *
  281. a_dim1], lda, &tau[i__], &work[lbwork * nb + 1], &nt);
  282. }
  283. /* Apply H' to A(1:M,M+1:N) from the left */
  284. i__1 = k - nx;
  285. i__2 = nb;
  286. for (j = 1; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
  287. /* Computing MIN */
  288. i__4 = k - j + 1;
  289. ib = min(i__4,nb);
  290. i__4 = *m - j + 1;
  291. i__3 = *n - *m;
  292. i__5 = *n - *m;
  293. _starpu_dlarfb_("Left", "Transpose", "Forward", "Columnwise", &i__4, &
  294. i__3, &ib, &a[j + j * a_dim1], lda, &work[j], &lbwork, &a[
  295. j + (*m + 1) * a_dim1], lda, &work[lbwork * nb + nt * nt
  296. + 1], &i__5);
  297. /* L40: */
  298. }
  299. if (nt <= nb) {
  300. i__2 = *m - j + 1;
  301. i__1 = *n - *m;
  302. i__4 = k - j + 1;
  303. i__3 = *n - *m;
  304. _starpu_dlarfb_("Left", "Transpose", "Forward", "Columnwise", &i__2, &
  305. i__1, &i__4, &a[j + j * a_dim1], lda, &work[j], &lbwork, &
  306. a[j + (*m + 1) * a_dim1], lda, &work[lbwork * nb + nt *
  307. nt + 1], &i__3);
  308. } else {
  309. i__2 = *m - j + 1;
  310. i__1 = *n - *m;
  311. i__4 = k - j + 1;
  312. i__3 = *n - *m;
  313. _starpu_dlarfb_("Left", "Transpose", "Forward", "Columnwise", &i__2, &
  314. i__1, &i__4, &a[j + j * a_dim1], lda, &work[lbwork * nb +
  315. 1], &nt, &a[j + (*m + 1) * a_dim1], lda, &work[lbwork *
  316. nb + nt * nt + 1], &i__3);
  317. }
  318. }
  319. work[1] = (doublereal) iws;
  320. return 0;
  321. /* End of DGEQRF */
  322. } /* _starpu_dgeqrf_ */