dbdsdc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* dbdsdc.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__9 = 9;
  15. static integer c__0 = 0;
  16. static doublereal c_b15 = 1.;
  17. static integer c__1 = 1;
  18. static doublereal c_b29 = 0.;
  19. /* Subroutine */ int _starpu_dbdsdc_(char *uplo, char *compq, integer *n, doublereal *
  20. d__, doublereal *e, doublereal *u, integer *ldu, doublereal *vt,
  21. integer *ldvt, doublereal *q, integer *iq, doublereal *work, integer *
  22. iwork, integer *info)
  23. {
  24. /* System generated locals */
  25. integer u_dim1, u_offset, vt_dim1, vt_offset, i__1, i__2;
  26. doublereal d__1;
  27. /* Builtin functions */
  28. double d_sign(doublereal *, doublereal *), log(doublereal);
  29. /* Local variables */
  30. integer i__, j, k;
  31. doublereal p, r__;
  32. integer z__, ic, ii, kk;
  33. doublereal cs;
  34. integer is, iu;
  35. doublereal sn;
  36. integer nm1;
  37. doublereal eps;
  38. integer ivt, difl, difr, ierr, perm, mlvl, sqre;
  39. extern logical _starpu_lsame_(char *, char *);
  40. extern /* Subroutine */ int _starpu_dlasr_(char *, char *, char *, integer *,
  41. integer *, doublereal *, doublereal *, doublereal *, integer *), _starpu_dcopy_(integer *, doublereal *, integer *
  42. , doublereal *, integer *), _starpu_dswap_(integer *, doublereal *,
  43. integer *, doublereal *, integer *);
  44. integer poles, iuplo, nsize, start;
  45. extern /* Subroutine */ int _starpu_dlasd0_(integer *, integer *, doublereal *,
  46. doublereal *, doublereal *, integer *, doublereal *, integer *,
  47. integer *, integer *, doublereal *, integer *);
  48. extern doublereal _starpu_dlamch_(char *);
  49. extern /* Subroutine */ int _starpu_dlasda_(integer *, integer *, integer *,
  50. integer *, doublereal *, doublereal *, doublereal *, integer *,
  51. doublereal *, integer *, doublereal *, doublereal *, doublereal *,
  52. doublereal *, integer *, integer *, integer *, integer *,
  53. doublereal *, doublereal *, doublereal *, doublereal *, integer *,
  54. integer *), _starpu_dlascl_(char *, integer *, integer *, doublereal *,
  55. doublereal *, integer *, integer *, doublereal *, integer *,
  56. integer *), _starpu_dlasdq_(char *, integer *, integer *, integer
  57. *, integer *, integer *, doublereal *, doublereal *, doublereal *,
  58. integer *, doublereal *, integer *, doublereal *, integer *,
  59. doublereal *, integer *), _starpu_dlaset_(char *, integer *,
  60. integer *, doublereal *, doublereal *, doublereal *, integer *), _starpu_dlartg_(doublereal *, doublereal *, doublereal *,
  61. doublereal *, doublereal *);
  62. extern integer _starpu_ilaenv_(integer *, char *, char *, integer *, integer *,
  63. integer *, integer *);
  64. extern /* Subroutine */ int _starpu_xerbla_(char *, integer *);
  65. integer givcol;
  66. extern doublereal _starpu_dlanst_(char *, integer *, doublereal *, doublereal *);
  67. integer icompq;
  68. doublereal orgnrm;
  69. integer givnum, givptr, qstart, smlsiz, wstart, smlszp;
  70. /* -- LAPACK routine (version 3.2) -- */
  71. /* Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd.. */
  72. /* November 2006 */
  73. /* .. Scalar Arguments .. */
  74. /* .. */
  75. /* .. Array Arguments .. */
  76. /* .. */
  77. /* Purpose */
  78. /* ======= */
  79. /* DBDSDC computes the singular value decomposition (SVD) of a real */
  80. /* N-by-N (upper or lower) bidiagonal matrix B: B = U * S * VT, */
  81. /* using a divide and conquer method, where S is a diagonal matrix */
  82. /* with non-negative diagonal elements (the singular values of B), and */
  83. /* U and VT are orthogonal matrices of left and right singular vectors, */
  84. /* respectively. DBDSDC can be used to compute all singular values, */
  85. /* and optionally, singular vectors or singular vectors in compact form. */
  86. /* This code makes very mild assumptions about floating point */
  87. /* arithmetic. It will work on machines with a guard digit in */
  88. /* add/subtract, or on those binary machines without guard digits */
  89. /* which subtract like the Cray X-MP, Cray Y-MP, Cray C-90, or Cray-2. */
  90. /* It could conceivably fail on hexadecimal or decimal machines */
  91. /* without guard digits, but we know of none. See DLASD3 for details. */
  92. /* The code currently calls DLASDQ if singular values only are desired. */
  93. /* However, it can be slightly modified to compute singular values */
  94. /* using the divide and conquer method. */
  95. /* Arguments */
  96. /* ========= */
  97. /* UPLO (input) CHARACTER*1 */
  98. /* = 'U': B is upper bidiagonal. */
  99. /* = 'L': B is lower bidiagonal. */
  100. /* COMPQ (input) CHARACTER*1 */
  101. /* Specifies whether singular vectors are to be computed */
  102. /* as follows: */
  103. /* = 'N': Compute singular values only; */
  104. /* = 'P': Compute singular values and compute singular */
  105. /* vectors in compact form; */
  106. /* = 'I': Compute singular values and singular vectors. */
  107. /* N (input) INTEGER */
  108. /* The order of the matrix B. N >= 0. */
  109. /* D (input/output) DOUBLE PRECISION array, dimension (N) */
  110. /* On entry, the n diagonal elements of the bidiagonal matrix B. */
  111. /* On exit, if INFO=0, the singular values of B. */
  112. /* E (input/output) DOUBLE PRECISION array, dimension (N-1) */
  113. /* On entry, the elements of E contain the offdiagonal */
  114. /* elements of the bidiagonal matrix whose SVD is desired. */
  115. /* On exit, E has been destroyed. */
  116. /* U (output) DOUBLE PRECISION array, dimension (LDU,N) */
  117. /* If COMPQ = 'I', then: */
  118. /* On exit, if INFO = 0, U contains the left singular vectors */
  119. /* of the bidiagonal matrix. */
  120. /* For other values of COMPQ, U is not referenced. */
  121. /* LDU (input) INTEGER */
  122. /* The leading dimension of the array U. LDU >= 1. */
  123. /* If singular vectors are desired, then LDU >= max( 1, N ). */
  124. /* VT (output) DOUBLE PRECISION array, dimension (LDVT,N) */
  125. /* If COMPQ = 'I', then: */
  126. /* On exit, if INFO = 0, VT' contains the right singular */
  127. /* vectors of the bidiagonal matrix. */
  128. /* For other values of COMPQ, VT is not referenced. */
  129. /* LDVT (input) INTEGER */
  130. /* The leading dimension of the array VT. LDVT >= 1. */
  131. /* If singular vectors are desired, then LDVT >= max( 1, N ). */
  132. /* Q (output) DOUBLE PRECISION array, dimension (LDQ) */
  133. /* If COMPQ = 'P', then: */
  134. /* On exit, if INFO = 0, Q and IQ contain the left */
  135. /* and right singular vectors in a compact form, */
  136. /* requiring O(N log N) space instead of 2*N**2. */
  137. /* In particular, Q contains all the DOUBLE PRECISION data in */
  138. /* LDQ >= N*(11 + 2*SMLSIZ + 8*INT(LOG_2(N/(SMLSIZ+1)))) */
  139. /* words of memory, where SMLSIZ is returned by ILAENV and */
  140. /* is equal to the maximum size of the subproblems at the */
  141. /* bottom of the computation tree (usually about 25). */
  142. /* For other values of COMPQ, Q is not referenced. */
  143. /* IQ (output) INTEGER array, dimension (LDIQ) */
  144. /* If COMPQ = 'P', then: */
  145. /* On exit, if INFO = 0, Q and IQ contain the left */
  146. /* and right singular vectors in a compact form, */
  147. /* requiring O(N log N) space instead of 2*N**2. */
  148. /* In particular, IQ contains all INTEGER data in */
  149. /* LDIQ >= N*(3 + 3*INT(LOG_2(N/(SMLSIZ+1)))) */
  150. /* words of memory, where SMLSIZ is returned by ILAENV and */
  151. /* is equal to the maximum size of the subproblems at the */
  152. /* bottom of the computation tree (usually about 25). */
  153. /* For other values of COMPQ, IQ is not referenced. */
  154. /* WORK (workspace) DOUBLE PRECISION array, dimension (MAX(1,LWORK)) */
  155. /* If COMPQ = 'N' then LWORK >= (4 * N). */
  156. /* If COMPQ = 'P' then LWORK >= (6 * N). */
  157. /* If COMPQ = 'I' then LWORK >= (3 * N**2 + 4 * N). */
  158. /* IWORK (workspace) INTEGER array, dimension (8*N) */
  159. /* INFO (output) INTEGER */
  160. /* = 0: successful exit. */
  161. /* < 0: if INFO = -i, the i-th argument had an illegal value. */
  162. /* > 0: The algorithm failed to compute an singular value. */
  163. /* The update process of divide and conquer failed. */
  164. /* Further Details */
  165. /* =============== */
  166. /* Based on contributions by */
  167. /* Ming Gu and Huan Ren, Computer Science Division, University of */
  168. /* California at Berkeley, USA */
  169. /* ===================================================================== */
  170. /* Changed dimension statement in comment describing E from (N) to */
  171. /* (N-1). Sven, 17 Feb 05. */
  172. /* ===================================================================== */
  173. /* .. Parameters .. */
  174. /* .. */
  175. /* .. Local Scalars .. */
  176. /* .. */
  177. /* .. External Functions .. */
  178. /* .. */
  179. /* .. External Subroutines .. */
  180. /* .. */
  181. /* .. Intrinsic Functions .. */
  182. /* .. */
  183. /* .. Executable Statements .. */
  184. /* Test the input parameters. */
  185. /* Parameter adjustments */
  186. --d__;
  187. --e;
  188. u_dim1 = *ldu;
  189. u_offset = 1 + u_dim1;
  190. u -= u_offset;
  191. vt_dim1 = *ldvt;
  192. vt_offset = 1 + vt_dim1;
  193. vt -= vt_offset;
  194. --q;
  195. --iq;
  196. --work;
  197. --iwork;
  198. /* Function Body */
  199. *info = 0;
  200. iuplo = 0;
  201. if (_starpu_lsame_(uplo, "U")) {
  202. iuplo = 1;
  203. }
  204. if (_starpu_lsame_(uplo, "L")) {
  205. iuplo = 2;
  206. }
  207. if (_starpu_lsame_(compq, "N")) {
  208. icompq = 0;
  209. } else if (_starpu_lsame_(compq, "P")) {
  210. icompq = 1;
  211. } else if (_starpu_lsame_(compq, "I")) {
  212. icompq = 2;
  213. } else {
  214. icompq = -1;
  215. }
  216. if (iuplo == 0) {
  217. *info = -1;
  218. } else if (icompq < 0) {
  219. *info = -2;
  220. } else if (*n < 0) {
  221. *info = -3;
  222. } else if (*ldu < 1 || icompq == 2 && *ldu < *n) {
  223. *info = -7;
  224. } else if (*ldvt < 1 || icompq == 2 && *ldvt < *n) {
  225. *info = -9;
  226. }
  227. if (*info != 0) {
  228. i__1 = -(*info);
  229. _starpu_xerbla_("DBDSDC", &i__1);
  230. return 0;
  231. }
  232. /* Quick return if possible */
  233. if (*n == 0) {
  234. return 0;
  235. }
  236. smlsiz = _starpu_ilaenv_(&c__9, "DBDSDC", " ", &c__0, &c__0, &c__0, &c__0);
  237. if (*n == 1) {
  238. if (icompq == 1) {
  239. q[1] = d_sign(&c_b15, &d__[1]);
  240. q[smlsiz * *n + 1] = 1.;
  241. } else if (icompq == 2) {
  242. u[u_dim1 + 1] = d_sign(&c_b15, &d__[1]);
  243. vt[vt_dim1 + 1] = 1.;
  244. }
  245. d__[1] = abs(d__[1]);
  246. return 0;
  247. }
  248. nm1 = *n - 1;
  249. /* If matrix lower bidiagonal, rotate to be upper bidiagonal */
  250. /* by applying Givens rotations on the left */
  251. wstart = 1;
  252. qstart = 3;
  253. if (icompq == 1) {
  254. _starpu_dcopy_(n, &d__[1], &c__1, &q[1], &c__1);
  255. i__1 = *n - 1;
  256. _starpu_dcopy_(&i__1, &e[1], &c__1, &q[*n + 1], &c__1);
  257. }
  258. if (iuplo == 2) {
  259. qstart = 5;
  260. wstart = (*n << 1) - 1;
  261. i__1 = *n - 1;
  262. for (i__ = 1; i__ <= i__1; ++i__) {
  263. _starpu_dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
  264. d__[i__] = r__;
  265. e[i__] = sn * d__[i__ + 1];
  266. d__[i__ + 1] = cs * d__[i__ + 1];
  267. if (icompq == 1) {
  268. q[i__ + (*n << 1)] = cs;
  269. q[i__ + *n * 3] = sn;
  270. } else if (icompq == 2) {
  271. work[i__] = cs;
  272. work[nm1 + i__] = -sn;
  273. }
  274. /* L10: */
  275. }
  276. }
  277. /* If ICOMPQ = 0, use DLASDQ to compute the singular values. */
  278. if (icompq == 0) {
  279. _starpu_dlasdq_("U", &c__0, n, &c__0, &c__0, &c__0, &d__[1], &e[1], &vt[
  280. vt_offset], ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
  281. wstart], info);
  282. goto L40;
  283. }
  284. /* If N is smaller than the minimum divide size SMLSIZ, then solve */
  285. /* the problem with another solver. */
  286. if (*n <= smlsiz) {
  287. if (icompq == 2) {
  288. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
  289. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
  290. _starpu_dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &vt[vt_offset]
  291. , ldvt, &u[u_offset], ldu, &u[u_offset], ldu, &work[
  292. wstart], info);
  293. } else if (icompq == 1) {
  294. iu = 1;
  295. ivt = iu + *n;
  296. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &q[iu + (qstart - 1) * *n], n);
  297. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &q[ivt + (qstart - 1) * *n], n);
  298. _starpu_dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &q[ivt + (
  299. qstart - 1) * *n], n, &q[iu + (qstart - 1) * *n], n, &q[
  300. iu + (qstart - 1) * *n], n, &work[wstart], info);
  301. }
  302. goto L40;
  303. }
  304. if (icompq == 2) {
  305. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &u[u_offset], ldu);
  306. _starpu_dlaset_("A", n, n, &c_b29, &c_b15, &vt[vt_offset], ldvt);
  307. }
  308. /* Scale. */
  309. orgnrm = _starpu_dlanst_("M", n, &d__[1], &e[1]);
  310. if (orgnrm == 0.) {
  311. return 0;
  312. }
  313. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, n, &c__1, &d__[1], n, &ierr);
  314. _starpu_dlascl_("G", &c__0, &c__0, &orgnrm, &c_b15, &nm1, &c__1, &e[1], &nm1, &
  315. ierr);
  316. eps = _starpu_dlamch_("Epsilon");
  317. mlvl = (integer) (log((doublereal) (*n) / (doublereal) (smlsiz + 1)) /
  318. log(2.)) + 1;
  319. smlszp = smlsiz + 1;
  320. if (icompq == 1) {
  321. iu = 1;
  322. ivt = smlsiz + 1;
  323. difl = ivt + smlszp;
  324. difr = difl + mlvl;
  325. z__ = difr + (mlvl << 1);
  326. ic = z__ + mlvl;
  327. is = ic + 1;
  328. poles = is + 1;
  329. givnum = poles + (mlvl << 1);
  330. k = 1;
  331. givptr = 2;
  332. perm = 3;
  333. givcol = perm + mlvl;
  334. }
  335. i__1 = *n;
  336. for (i__ = 1; i__ <= i__1; ++i__) {
  337. if ((d__1 = d__[i__], abs(d__1)) < eps) {
  338. d__[i__] = d_sign(&eps, &d__[i__]);
  339. }
  340. /* L20: */
  341. }
  342. start = 1;
  343. sqre = 0;
  344. i__1 = nm1;
  345. for (i__ = 1; i__ <= i__1; ++i__) {
  346. if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
  347. /* Subproblem found. First determine its size and then */
  348. /* apply divide and conquer on it. */
  349. if (i__ < nm1) {
  350. /* A subproblem with E(I) small for I < NM1. */
  351. nsize = i__ - start + 1;
  352. } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
  353. /* A subproblem with E(NM1) not too small but I = NM1. */
  354. nsize = *n - start + 1;
  355. } else {
  356. /* A subproblem with E(NM1) small. This implies an */
  357. /* 1-by-1 subproblem at D(N). Solve this 1-by-1 problem */
  358. /* first. */
  359. nsize = i__ - start + 1;
  360. if (icompq == 2) {
  361. u[*n + *n * u_dim1] = d_sign(&c_b15, &d__[*n]);
  362. vt[*n + *n * vt_dim1] = 1.;
  363. } else if (icompq == 1) {
  364. q[*n + (qstart - 1) * *n] = d_sign(&c_b15, &d__[*n]);
  365. q[*n + (smlsiz + qstart - 1) * *n] = 1.;
  366. }
  367. d__[*n] = (d__1 = d__[*n], abs(d__1));
  368. }
  369. if (icompq == 2) {
  370. _starpu_dlasd0_(&nsize, &sqre, &d__[start], &e[start], &u[start +
  371. start * u_dim1], ldu, &vt[start + start * vt_dim1],
  372. ldvt, &smlsiz, &iwork[1], &work[wstart], info);
  373. } else {
  374. _starpu_dlasda_(&icompq, &smlsiz, &nsize, &sqre, &d__[start], &e[
  375. start], &q[start + (iu + qstart - 2) * *n], n, &q[
  376. start + (ivt + qstart - 2) * *n], &iq[start + k * *n],
  377. &q[start + (difl + qstart - 2) * *n], &q[start + (
  378. difr + qstart - 2) * *n], &q[start + (z__ + qstart -
  379. 2) * *n], &q[start + (poles + qstart - 2) * *n], &iq[
  380. start + givptr * *n], &iq[start + givcol * *n], n, &
  381. iq[start + perm * *n], &q[start + (givnum + qstart -
  382. 2) * *n], &q[start + (ic + qstart - 2) * *n], &q[
  383. start + (is + qstart - 2) * *n], &work[wstart], &
  384. iwork[1], info);
  385. if (*info != 0) {
  386. return 0;
  387. }
  388. }
  389. start = i__ + 1;
  390. }
  391. /* L30: */
  392. }
  393. /* Unscale */
  394. _starpu_dlascl_("G", &c__0, &c__0, &c_b15, &orgnrm, n, &c__1, &d__[1], n, &ierr);
  395. L40:
  396. /* Use Selection Sort to minimize swaps of singular vectors */
  397. i__1 = *n;
  398. for (ii = 2; ii <= i__1; ++ii) {
  399. i__ = ii - 1;
  400. kk = i__;
  401. p = d__[i__];
  402. i__2 = *n;
  403. for (j = ii; j <= i__2; ++j) {
  404. if (d__[j] > p) {
  405. kk = j;
  406. p = d__[j];
  407. }
  408. /* L50: */
  409. }
  410. if (kk != i__) {
  411. d__[kk] = d__[i__];
  412. d__[i__] = p;
  413. if (icompq == 1) {
  414. iq[i__] = kk;
  415. } else if (icompq == 2) {
  416. _starpu_dswap_(n, &u[i__ * u_dim1 + 1], &c__1, &u[kk * u_dim1 + 1], &
  417. c__1);
  418. _starpu_dswap_(n, &vt[i__ + vt_dim1], ldvt, &vt[kk + vt_dim1], ldvt);
  419. }
  420. } else if (icompq == 1) {
  421. iq[i__] = i__;
  422. }
  423. /* L60: */
  424. }
  425. /* If ICOMPQ = 1, use IQ(N,1) as the indicator for UPLO */
  426. if (icompq == 1) {
  427. if (iuplo == 1) {
  428. iq[*n] = 1;
  429. } else {
  430. iq[*n] = 0;
  431. }
  432. }
  433. /* If B is lower bidiagonal, update U by those Givens rotations */
  434. /* which rotated B to be upper bidiagonal */
  435. if (iuplo == 2 && icompq == 2) {
  436. _starpu_dlasr_("L", "V", "B", n, n, &work[1], &work[*n], &u[u_offset], ldu);
  437. }
  438. return 0;
  439. /* End of DBDSDC */
  440. } /* _starpu_dbdsdc_ */