dlalsd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /* dlalsd.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_b6 = 0.;
  16. static integer c__0 = 0;
  17. static doublereal c_b11 = 1.;
  18. /* Subroutine */ int _starpu_dlalsd_(char *uplo, integer *smlsiz, integer *n, integer
  19. *nrhs, doublereal *d__, doublereal *e, doublereal *b, integer *ldb,
  20. doublereal *rcond, integer *rank, doublereal *work, integer *iwork,
  21. integer *info)
  22. {
  23. /* System generated locals */
  24. integer b_dim1, b_offset, i__1, i__2;
  25. doublereal d__1;
  26. /* Builtin functions */
  27. double log(doublereal), d_sign(doublereal *, doublereal *);
  28. /* Local variables */
  29. integer c__, i__, j, k;
  30. doublereal r__;
  31. integer s, u, z__;
  32. doublereal cs;
  33. integer bx;
  34. doublereal sn;
  35. integer st, vt, nm1, st1;
  36. doublereal eps;
  37. integer iwk;
  38. doublereal tol;
  39. integer difl, difr;
  40. doublereal rcnd;
  41. integer perm, nsub;
  42. extern /* Subroutine */ int _starpu_drot_(integer *, doublereal *, integer *,
  43. doublereal *, integer *, doublereal *, doublereal *);
  44. integer nlvl, sqre, bxst;
  45. extern /* Subroutine */ int _starpu_dgemm_(char *, char *, integer *, integer *,
  46. integer *, doublereal *, doublereal *, integer *, doublereal *,
  47. integer *, doublereal *, doublereal *, integer *),
  48. _starpu_dcopy_(integer *, doublereal *, integer *, doublereal *, integer
  49. *);
  50. integer poles, sizei, nsize, nwork, icmpq1, icmpq2;
  51. extern doublereal _starpu_dlamch_(char *);
  52. extern /* Subroutine */ int _starpu_dlasda_(integer *, integer *, integer *,
  53. integer *, doublereal *, doublereal *, doublereal *, integer *,
  54. doublereal *, integer *, doublereal *, doublereal *, doublereal *,
  55. doublereal *, integer *, integer *, integer *, integer *,
  56. doublereal *, doublereal *, doublereal *, doublereal *, integer *,
  57. integer *), _starpu_dlalsa_(integer *, integer *, integer *, integer *,
  58. doublereal *, integer *, doublereal *, integer *, doublereal *,
  59. integer *, doublereal *, integer *, doublereal *, doublereal *,
  60. doublereal *, doublereal *, integer *, integer *, integer *,
  61. integer *, doublereal *, doublereal *, doublereal *, doublereal *,
  62. integer *, integer *), _starpu_dlascl_(char *, integer *, integer *,
  63. doublereal *, doublereal *, integer *, integer *, doublereal *,
  64. integer *, integer *);
  65. extern integer _starpu_idamax_(integer *, doublereal *, integer *);
  66. extern /* Subroutine */ int _starpu_dlasdq_(char *, integer *, integer *, integer
  67. *, integer *, integer *, doublereal *, doublereal *, doublereal *,
  68. integer *, doublereal *, integer *, doublereal *, integer *,
  69. doublereal *, integer *), _starpu_dlacpy_(char *, integer *,
  70. integer *, doublereal *, integer *, doublereal *, integer *), _starpu_dlartg_(doublereal *, doublereal *, doublereal *,
  71. doublereal *, doublereal *), _starpu_dlaset_(char *, integer *, integer *,
  72. doublereal *, doublereal *, doublereal *, integer *),
  73. _starpu_xerbla_(char *, integer *);
  74. integer givcol;
  75. extern doublereal _starpu_dlanst_(char *, integer *, doublereal *, doublereal *);
  76. extern /* Subroutine */ int _starpu_dlasrt_(char *, integer *, doublereal *,
  77. integer *);
  78. doublereal orgnrm;
  79. integer givnum, givptr, smlszp;
  80. /* -- LAPACK routine (version 3.2) -- */
  81. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  82. /* November 2006 */
  83. /* .. Scalar Arguments .. */
  84. /* .. */
  85. /* .. Array Arguments .. */
  86. /* .. */
  87. /* Purpose */
  88. /* ======= */
  89. /* DLALSD uses the singular value decomposition of A to solve the least */
  90. /* squares problem of finding X to minimize the Euclidean norm of each */
  91. /* column of A*X-B, where A is N-by-N upper bidiagonal, and X and B */
  92. /* are N-by-NRHS. The solution X overwrites B. */
  93. /* The singular values of A smaller than RCOND times the largest */
  94. /* singular value are treated as zero in solving the least squares */
  95. /* problem; in this case a minimum norm solution is returned. */
  96. /* The actual singular values are returned in D in ascending order. */
  97. /* This code makes very mild assumptions about floating point */
  98. /* arithmetic. It will work on machines with a guard digit in */
  99. /* add/subtract, or on those binary machines without guard digits */
  100. /* which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
  101. /* It could conceivably fail on hexadecimal or decimal machines */
  102. /* without guard digits, but we know of none. */
  103. /* Arguments */
  104. /* ========= */
  105. /* UPLO (input) CHARACTER*1 */
  106. /* = 'U': D and E define an upper bidiagonal matrix. */
  107. /* = 'L': D and E define a lower bidiagonal matrix. */
  108. /* SMLSIZ (input) INTEGER */
  109. /* The maximum size of the subproblems at the bottom of the */
  110. /* computation tree. */
  111. /* N (input) INTEGER */
  112. /* The dimension of the bidiagonal matrix. N >= 0. */
  113. /* NRHS (input) INTEGER */
  114. /* The number of columns of B. NRHS must be at least 1. */
  115. /* D (input/output) DOUBLE PRECISION array, dimension (N) */
  116. /* On entry D contains the main diagonal of the bidiagonal */
  117. /* matrix. On exit, if INFO = 0, D contains its singular values. */
  118. /* E (input/output) DOUBLE PRECISION array, dimension (N-1) */
  119. /* Contains the super-diagonal entries of the bidiagonal matrix. */
  120. /* On exit, E has been destroyed. */
  121. /* B (input/output) DOUBLE PRECISION array, dimension (LDB,NRHS) */
  122. /* On input, B contains the right hand sides of the least */
  123. /* squares problem. On output, B contains the solution X. */
  124. /* LDB (input) INTEGER */
  125. /* The leading dimension of B in the calling subprogram. */
  126. /* LDB must be at least max(1,N). */
  127. /* RCOND (input) DOUBLE PRECISION */
  128. /* The singular values of A less than or equal to RCOND times */
  129. /* the largest singular value are treated as zero in solving */
  130. /* the least squares problem. If RCOND is negative, */
  131. /* machine precision is used instead. */
  132. /* For example, if diag(S)*X=B were the least squares problem, */
  133. /* where diag(S) is a diagonal matrix of singular values, the */
  134. /* solution would be X(i) = B(i) / S(i) if S(i) is greater than */
  135. /* RCOND*max(S), and X(i) = 0 if S(i) is less than or equal to */
  136. /* RCOND*max(S). */
  137. /* RANK (output) INTEGER */
  138. /* The number of singular values of A greater than RCOND times */
  139. /* the largest singular value. */
  140. /* WORK (workspace) DOUBLE PRECISION array, dimension at least */
  141. /* (9*N + 2*N*SMLSIZ + 8*N*NLVL + N*NRHS + (SMLSIZ+1)**2), */
  142. /* where NLVL = max(0, INT(log_2 (N/(SMLSIZ+1))) + 1). */
  143. /* IWORK (workspace) INTEGER array, dimension at least */
  144. /* (3*N*NLVL + 11*N) */
  145. /* INFO (output) INTEGER */
  146. /* = 0: successful exit. */
  147. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  148. /* > 0: The algorithm failed to compute an singular value while */
  149. /* working on the submatrix lying in rows and columns */
  150. /* INFO/(N+1) through MOD(INFO,N+1). */
  151. /* Further Details */
  152. /* =============== */
  153. /* Based on contributions by */
  154. /* Ming Gu and Ren-Cang Li, Computer Science Division, University of */
  155. /* California at Berkeley, USA */
  156. /* Osni Marques, LBNL/NERSC, USA */
  157. /* ===================================================================== */
  158. /* .. Parameters .. */
  159. /* .. */
  160. /* .. Local Scalars .. */
  161. /* .. */
  162. /* .. External Functions .. */
  163. /* .. */
  164. /* .. External Subroutines .. */
  165. /* .. */
  166. /* .. Intrinsic Functions .. */
  167. /* .. */
  168. /* .. Executable Statements .. */
  169. /* Test the input parameters. */
  170. /* Parameter adjustments */
  171. --d__;
  172. --e;
  173. b_dim1 = *ldb;
  174. b_offset = 1 + b_dim1;
  175. b -= b_offset;
  176. --work;
  177. --iwork;
  178. /* Function Body */
  179. *info = 0;
  180. if (*n < 0) {
  181. *info = -3;
  182. } else if (*nrhs < 1) {
  183. *info = -4;
  184. } else if (*ldb < 1 || *ldb < *n) {
  185. *info = -8;
  186. }
  187. if (*info != 0) {
  188. i__1 = -(*info);
  189. _starpu_xerbla_("DLALSD", &i__1);
  190. return 0;
  191. }
  192. eps = _starpu_dlamch_("Epsilon");
  193. /* Set up the tolerance. */
  194. if (*rcond <= 0. || *rcond >= 1.) {
  195. rcnd = eps;
  196. } else {
  197. rcnd = *rcond;
  198. }
  199. *rank = 0;
  200. /* Quick return if possible. */
  201. if (*n == 0) {
  202. return 0;
  203. } else if (*n == 1) {
  204. if (d__[1] == 0.) {
  205. _starpu_dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
  206. } else {
  207. *rank = 1;
  208. _starpu_dlascl_("G", &c__0, &c__0, &d__[1], &c_b11, &c__1, nrhs, &b[
  209. b_offset], ldb, info);
  210. d__[1] = abs(d__[1]);
  211. }
  212. return 0;
  213. }
  214. /* Rotate the matrix if it is lower bidiagonal. */
  215. if (*(unsigned char *)uplo == 'L') {
  216. i__1 = *n - 1;
  217. for (i__ = 1; i__ <= i__1; ++i__) {
  218. _starpu_dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
  219. d__[i__] = r__;
  220. e[i__] = sn * d__[i__ + 1];
  221. d__[i__ + 1] = cs * d__[i__ + 1];
  222. if (*nrhs == 1) {
  223. _starpu_drot_(&c__1, &b[i__ + b_dim1], &c__1, &b[i__ + 1 + b_dim1], &
  224. c__1, &cs, &sn);
  225. } else {
  226. work[(i__ << 1) - 1] = cs;
  227. work[i__ * 2] = sn;
  228. }
  229. /* L10: */
  230. }
  231. if (*nrhs > 1) {
  232. i__1 = *nrhs;
  233. for (i__ = 1; i__ <= i__1; ++i__) {
  234. i__2 = *n - 1;
  235. for (j = 1; j <= i__2; ++j) {
  236. cs = work[(j << 1) - 1];
  237. sn = work[j * 2];
  238. _starpu_drot_(&c__1, &b[j + i__ * b_dim1], &c__1, &b[j + 1 + i__ *
  239. b_dim1], &c__1, &cs, &sn);
  240. /* L20: */
  241. }
  242. /* L30: */
  243. }
  244. }
  245. }
  246. /* Scale. */
  247. nm1 = *n - 1;
  248. orgnrm = _starpu_dlanst_("M", n, &d__[1], &e[1]);
  249. if (orgnrm == 0.) {
  250. _starpu_dlaset_("A", n, nrhs, &c_b6, &c_b6, &b[b_offset], ldb);
  251. return 0;
  252. }
  253. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, &c__1, &d__[1], n, info);
  254. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, &nm1, &c__1, &e[1], &nm1,
  255. info);
  256. /* If N is smaller than the minimum divide size SMLSIZ, then solve */
  257. /* the problem with another solver. */
  258. if (*n <= *smlsiz) {
  259. nwork = *n * *n + 1;
  260. _starpu_dlaset_("A", n, n, &c_b6, &c_b11, &work[1], n);
  261. _starpu_dlasdq_("U", &c__0, n, n, &c__0, nrhs, &d__[1], &e[1], &work[1], n, &
  262. work[1], n, &b[b_offset], ldb, &work[nwork], info);
  263. if (*info != 0) {
  264. return 0;
  265. }
  266. tol = rcnd * (d__1 = d__[_starpu_idamax_(n, &d__[1], &c__1)], abs(d__1));
  267. i__1 = *n;
  268. for (i__ = 1; i__ <= i__1; ++i__) {
  269. if (d__[i__] <= tol) {
  270. _starpu_dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &b[i__ + b_dim1], ldb);
  271. } else {
  272. _starpu_dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &b[
  273. i__ + b_dim1], ldb, info);
  274. ++(*rank);
  275. }
  276. /* L40: */
  277. }
  278. _starpu_dgemm_("T", "N", n, nrhs, n, &c_b11, &work[1], n, &b[b_offset], ldb, &
  279. c_b6, &work[nwork], n);
  280. _starpu_dlacpy_("A", n, nrhs, &work[nwork], n, &b[b_offset], ldb);
  281. /* Unscale. */
  282. _starpu_dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n,
  283. info);
  284. _starpu_dlasrt_("D", n, &d__[1], info);
  285. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset],
  286. ldb, info);
  287. return 0;
  288. }
  289. /* Book-keeping and setting up some constants. */
  290. nlvl = (integer) (log((doublereal) (*n) / (doublereal) (*smlsiz + 1)) /
  291. log(2.)) + 1;
  292. smlszp = *smlsiz + 1;
  293. u = 1;
  294. vt = *smlsiz * *n + 1;
  295. difl = vt + smlszp * *n;
  296. difr = difl + nlvl * *n;
  297. z__ = difr + (nlvl * *n << 1);
  298. c__ = z__ + nlvl * *n;
  299. s = c__ + *n;
  300. poles = s + *n;
  301. givnum = poles + (nlvl << 1) * *n;
  302. bx = givnum + (nlvl << 1) * *n;
  303. nwork = bx + *n * *nrhs;
  304. sizei = *n + 1;
  305. k = sizei + *n;
  306. givptr = k + *n;
  307. perm = givptr + *n;
  308. givcol = perm + nlvl * *n;
  309. iwk = givcol + (nlvl * *n << 1);
  310. st = 1;
  311. sqre = 0;
  312. icmpq1 = 1;
  313. icmpq2 = 0;
  314. nsub = 0;
  315. i__1 = *n;
  316. for (i__ = 1; i__ <= i__1; ++i__) {
  317. if ((d__1 = d__[i__], abs(d__1)) < eps) {
  318. d__[i__] = d_sign(&eps, &d__[i__]);
  319. }
  320. /* L50: */
  321. }
  322. i__1 = nm1;
  323. for (i__ = 1; i__ <= i__1; ++i__) {
  324. if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
  325. ++nsub;
  326. iwork[nsub] = st;
  327. /* Subproblem found. First determine its size and then */
  328. /* apply divide and conquer on it. */
  329. if (i__ < nm1) {
  330. /* A subproblem with E(I) small for I < NM1. */
  331. nsize = i__ - st + 1;
  332. iwork[sizei + nsub - 1] = nsize;
  333. } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
  334. /* A subproblem with E(NM1) not too small but I = NM1. */
  335. nsize = *n - st + 1;
  336. iwork[sizei + nsub - 1] = nsize;
  337. } else {
  338. /* A subproblem with E(NM1) small. This implies an */
  339. /* 1-by-1 subproblem at D(N), which is not solved */
  340. /* explicitly. */
  341. nsize = i__ - st + 1;
  342. iwork[sizei + nsub - 1] = nsize;
  343. ++nsub;
  344. iwork[nsub] = *n;
  345. iwork[sizei + nsub - 1] = 1;
  346. _starpu_dcopy_(nrhs, &b[*n + b_dim1], ldb, &work[bx + nm1], n);
  347. }
  348. st1 = st - 1;
  349. if (nsize == 1) {
  350. /* This is a 1-by-1 subproblem and is not solved */
  351. /* explicitly. */
  352. _starpu_dcopy_(nrhs, &b[st + b_dim1], ldb, &work[bx + st1], n);
  353. } else if (nsize <= *smlsiz) {
  354. /* This is a small subproblem and is solved by DLASDQ. */
  355. _starpu_dlaset_("A", &nsize, &nsize, &c_b6, &c_b11, &work[vt + st1],
  356. n);
  357. _starpu_dlasdq_("U", &c__0, &nsize, &nsize, &c__0, nrhs, &d__[st], &e[
  358. st], &work[vt + st1], n, &work[nwork], n, &b[st +
  359. b_dim1], ldb, &work[nwork], info);
  360. if (*info != 0) {
  361. return 0;
  362. }
  363. _starpu_dlacpy_("A", &nsize, nrhs, &b[st + b_dim1], ldb, &work[bx +
  364. st1], n);
  365. } else {
  366. /* A large problem. Solve it using divide and conquer. */
  367. _starpu_dlasda_(&icmpq1, smlsiz, &nsize, &sqre, &d__[st], &e[st], &
  368. work[u + st1], n, &work[vt + st1], &iwork[k + st1], &
  369. work[difl + st1], &work[difr + st1], &work[z__ + st1],
  370. &work[poles + st1], &iwork[givptr + st1], &iwork[
  371. givcol + st1], n, &iwork[perm + st1], &work[givnum +
  372. st1], &work[c__ + st1], &work[s + st1], &work[nwork],
  373. &iwork[iwk], info);
  374. if (*info != 0) {
  375. return 0;
  376. }
  377. bxst = bx + st1;
  378. _starpu_dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &b[st + b_dim1], ldb, &
  379. work[bxst], n, &work[u + st1], n, &work[vt + st1], &
  380. iwork[k + st1], &work[difl + st1], &work[difr + st1],
  381. &work[z__ + st1], &work[poles + st1], &iwork[givptr +
  382. st1], &iwork[givcol + st1], n, &iwork[perm + st1], &
  383. work[givnum + st1], &work[c__ + st1], &work[s + st1],
  384. &work[nwork], &iwork[iwk], info);
  385. if (*info != 0) {
  386. return 0;
  387. }
  388. }
  389. st = i__ + 1;
  390. }
  391. /* L60: */
  392. }
  393. /* Apply the singular values and treat the tiny ones as zero. */
  394. tol = rcnd * (d__1 = d__[_starpu_idamax_(n, &d__[1], &c__1)], abs(d__1));
  395. i__1 = *n;
  396. for (i__ = 1; i__ <= i__1; ++i__) {
  397. /* Some of the elements in D can be negative because 1-by-1 */
  398. /* subproblems were not solved explicitly. */
  399. if ((d__1 = d__[i__], abs(d__1)) <= tol) {
  400. _starpu_dlaset_("A", &c__1, nrhs, &c_b6, &c_b6, &work[bx + i__ - 1], n);
  401. } else {
  402. ++(*rank);
  403. _starpu_dlascl_("G", &c__0, &c__0, &d__[i__], &c_b11, &c__1, nrhs, &work[
  404. bx + i__ - 1], n, info);
  405. }
  406. d__[i__] = (d__1 = d__[i__], abs(d__1));
  407. /* L70: */
  408. }
  409. /* Now apply back the right singular vectors. */
  410. icmpq2 = 1;
  411. i__1 = nsub;
  412. for (i__ = 1; i__ <= i__1; ++i__) {
  413. st = iwork[i__];
  414. st1 = st - 1;
  415. nsize = iwork[sizei + i__ - 1];
  416. bxst = bx + st1;
  417. if (nsize == 1) {
  418. _starpu_dcopy_(nrhs, &work[bxst], n, &b[st + b_dim1], ldb);
  419. } else if (nsize <= *smlsiz) {
  420. _starpu_dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b11, &work[vt + st1], n,
  421. &work[bxst], n, &c_b6, &b[st + b_dim1], ldb);
  422. } else {
  423. _starpu_dlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &work[bxst], n, &b[st +
  424. b_dim1], ldb, &work[u + st1], n, &work[vt + st1], &iwork[
  425. k + st1], &work[difl + st1], &work[difr + st1], &work[z__
  426. + st1], &work[poles + st1], &iwork[givptr + st1], &iwork[
  427. givcol + st1], n, &iwork[perm + st1], &work[givnum + st1],
  428. &work[c__ + st1], &work[s + st1], &work[nwork], &iwork[
  429. iwk], info);
  430. if (*info != 0) {
  431. return 0;
  432. }
  433. }
  434. /* L80: */
  435. }
  436. /* Unscale and sort the singular values. */
  437. _starpu_dlascl_("G", &c__0, &c__0, &c_b11, &orgnrm, n, &c__1, &d__[1], n, info);
  438. _starpu_dlasrt_("D", n, &d__[1], info);
  439. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b11, n, nrhs, &b[b_offset], ldb,
  440. info);
  441. return 0;
  442. /* End of DLALSD */
  443. } /* _starpu_dlalsd_ */