Debian Codegraph @

packages/seacas/libraries/chaco/eigen/lanczos_ext_float.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "defs.h"
10 #include "smalloc.h"
11 #include "structs.h"
12 #include <math.h>
13 #include <stdio.h>
14
15 /* See comments in lanczos_ext(). */
16
17 int lanczos_ext_float(struct vtx_data **A, /* sparse matrix in row linked list format */
18 int n, /* problem size */
19 int d, /* problem dimension = number of eigvecs to find */
20 double **y, /* columns of y are eigenvectors of A */
21 double eigtol, /* tolerance on eigenvectors */
22 double *vwsqrt, /* square roots of vertex weights */
23 double maxdeg, /* maximum degree of graph */
24 int version, /* flags which version of sel. orth. to use */
25 double *gvec, /* the rhs n-vector in the extended eigen problem */
26 double sigma /* specifies the norm constraint on extended
27 eigenvector */
28 )
29 {
30 extern FILE *Output_File; /* output file or null */
31 extern int LANCZOS_SO_INTERVAL; /* interval between orthogonalizations */
32 extern int LANCZOS_MAXITNS; /* maximum Lanczos iterations allowed */
33 extern int DEBUG_EVECS; /* print debugging output? */
34 extern int DEBUG_TRACE; /* trace main execution path */
35 extern int WARNING_EVECS; /* print warning messages? */
36 extern double BISECTION_SAFETY; /* safety factor for T bisection */
37 extern double SRESTOL; /* resid tol for T evec comp */
38 extern double DOUBLE_EPSILON; /* machine precision */
39 extern double DOUBLE_MAX; /* largest double value */
40 extern double splarax_time; /* time matvec */
41 extern double orthog_time; /* time orthogonalization work */
42 extern double evec_time; /* time to generate eigenvectors */
43 extern double ql_time; /* time tridiagonal eigenvalue work */
44 extern double blas_time; /* time for blas. linear algebra */
45 extern double init_time; /* time to allocate, initialize variables */
46 extern double scan_time; /* time for scanning eval and bound lists */
47 extern double debug_time; /* time for (some of) debug computations */
48 extern double ritz_time; /* time to generate ritz vectors */
49 extern double pause_time; /* time to compute whether to pause */
50 int i, j, k; /* indices */
51 int maxj; /* maximum number of Lanczos iterations */
52 float *u, *r; /* Lanczos vectors */
53 double *u_double; /* double version of u */
54 double *alpha, *beta; /* the Lanczos scalars from each step */
55 double *ritz; /* copy of alpha for ql */
56 double *workj; /* work vector, e.g. copy of beta for ql */
57 float *workn; /* work vector, e.g. product Av for checkeig */
58 double *workn_double; /* work vector, e.g. product Av for checkeig */
59 double *s; /* eigenvector of T */
60 float **q; /* columns of q are Lanczos basis vectors */
61 double *bj; /* beta(j)*(last el. of corr. eigvec s of T) */
62 double bis_safety; /* real safety factor for T bisection */
63 double Sres; /* how well Tevec calculated eigvec s */
64 double Sres_max; /* Max value of Sres */
65 int inc_bis_safety; /* need to increase bisection safety */
66 double *Ares; /* how well Lanczos calc. eigpair lambda,y */
67 int *index; /* the Ritz index of an eigenpair */
68 struct orthlink_float **solist; /* vec. of structs with vecs. to orthog. against */
69 struct scanlink *scanlist; /* linked list of fields to do with min ritz vals */
70 struct scanlink *curlnk; /* for traversing the scanlist */
71 double bji_tol; /* tol on bji est. of eigen residual of A */
72 int converged; /* has the iteration converged? */
73 double goodtol; /* error tolerance for a good Ritz vector */
74 int ngood; /* total number of good Ritz pairs at current step */
75 int maxngood; /* biggest val of ngood through current step */
76 int left_ngood; /* number of good Ritz pairs on left end */
77 int lastpause; /* Most recent step with good ritz vecs */
78 int nopauses; /* Have there been any pauses? */
79 int interval; /* number of steps between pauses */
80 double time; /* Current clock time */
81 int left_goodlim; /* number of ritz pairs checked on left end */
82 double Anorm; /* Norm estimate of the Laplacian matrix */
83 int pausemode; /* which Lanczos pausing criterion to use */
84 int pause; /* whether to pause */
85 int temp; /* used to prevent redundant index computations */
86 double *extvec; /* n-vector solving the extended A eigenproblem */
87 double *v; /* j-vector solving the extended T eigenproblem */
88 double extval = 0.0; /* computed extended eigenvalue (of both A and T) */
89 double *work1, *work2; /* work vectors */
90 double check; /* to check an orthogonality condition */
91 double numerical_zero; /* used for zero in presence of round-off */
92 int ritzval_flag; /* status flag for get_ritzvals() */
93 double resid; /* residual */
94 int memory_ok; /* TRUE until memory runs out */
95 float *vwsqrt_float = NULL; /* float version of vwsqrt */
96
97 if (DEBUG_TRACE > 0) {
98 printf("<Entering lanczos_ext_float>\n");
99 }
100
101 if (DEBUG_EVECS > 0) {
102 printf("Selective orthogonalization Lanczos for extended eigenproblem, matrix size = %d.\n", n);
103 }
104
105 /* Initialize time. */
106 time = lanc_seconds();
107
108 if (d != 1) {
109 bail("ERROR: Extended Lanczos only available for bisection.", 1);
110 /* ... something must be wrong upstream. */
111 }
112
113 if (n < d + 1) {
114 bail("ERROR: System too small for number of eigenvalues requested.", 1);
115 /* ... d+1 since don't use zero eigenvalue pair */
116 }
117
118 /* Allocate space. */
119 maxj = LANCZOS_MAXITNS;
120 u = mkvec_float(1, n);
121 u_double = mkvec(1, n);
122 r = mkvec_float(1, n);
123 workn = mkvec_float(1, n);
124 workn_double = mkvec(1, n);
125 Ares = mkvec(0, d);
126 index = smalloc((d + 1) * sizeof(int));
127 alpha = mkvec(1, maxj);
128 beta = mkvec(0, maxj);
129 ritz = mkvec(1, maxj);
130 s = mkvec(1, maxj);
131 bj = mkvec(1, maxj);
132 workj = mkvec(0, maxj);
133 q = smalloc((maxj + 1) * sizeof(float *));
134 solist = smalloc((maxj + 1) * sizeof(struct orthlink_float *));
135 scanlist = mkscanlist(d);
136 extvec = mkvec(1, n);
137 v = mkvec(1, maxj);
138 work1 = mkvec(1, maxj);
139 work2 = mkvec(1, maxj);
140
141 /* Set some constants governing orthogonalization */
142 ngood = 0;
143 maxngood = 0;
144 bji_tol = eigtol;
145 Anorm = 2 * maxdeg; /* Gershgorin estimate for ||A|| */
146 goodtol = Anorm * sqrt(DOUBLE_EPSILON); /* Parlett & Scott's bound, p.224 */
147 interval = 2 + min(LANCZOS_SO_INTERVAL - 2, n / (2 * LANCZOS_SO_INTERVAL));
148 bis_safety = BISECTION_SAFETY;
149 numerical_zero = 1.0e-6;
150
151 if (DEBUG_EVECS > 0) {
152 printf(" maxdeg %g\n", maxdeg);
153 printf(" goodtol %g\n", goodtol);
154 printf(" interval %d\n", interval);
155 printf(" maxj %d\n", maxj);
156 }
157
158 /* Make a float copy of vwsqrt */
159 if (vwsqrt != NULL) {
160 vwsqrt_float = mkvec_float(0, n);
161 double_to_float(vwsqrt_float, 1, n, vwsqrt);
162 }
163
164 /* Initialize space. */
165 double_to_float(r, 1, n, gvec);
166 if (vwsqrt_float != NULL) {
167 scale_diag_float(r, 1, n, vwsqrt_float);
168 }
169 check = norm_float(r, 1, n);
170 if (vwsqrt_float == NULL) {
171 orthog1_float(r, 1, n);
172 }
173 else {
174 orthogvec_float(r, 1, n, vwsqrt_float);
175 }
176 check = fabs(check - norm_float(r, 1, n));
177 if (check > 10 * numerical_zero && WARNING_EVECS > 0) {
178 strout("WARNING: In terminal propagation, rhs should have no component in the");
179 printf(" nullspace of the Laplacian, so check val %g should be zero.\n", check);
180 if (Output_File != NULL) {
181 fprintf(Output_File, " nullspace of the Laplacian, so check val %g should be zero.\n",
182 check);
183 }
184 }
185 beta[0] = norm_float(r, 1, n);
186 q[0] = mkvec_float(1, n);
187 setvec_float(q[0], 1, n, 0.0);
188 setvec(bj, 1, maxj, DOUBLE_MAX);
189
190 if (beta[0] < numerical_zero) {
191 /* The rhs vector, Dg, of the transformed problem is numerically zero or is
192 in the null space of the Laplacian, so this is not a well posed extended
193 eigenproblem. Set maxj to zero to force a quick exit but still clean-up
194 memory and return(1) to indicate to eigensolve that it should call the
195 default eigensolver routine for the standard eigenproblem. */
196 maxj = 0;
197 }
198
199 /* Main Lanczos loop. */
200 j = 1;
201 lastpause = 0;
202 pausemode = 1;
203 left_ngood = 0;
204 left_goodlim = 0;
205 converged = FALSE;
206 Sres_max = 0.0;
207 inc_bis_safety = FALSE;
208 nopauses = TRUE;
209 memory_ok = TRUE;
210 init_time += lanc_seconds() - time;
211 while ((j <= maxj) && (!converged) && memory_ok) {
212 time = lanc_seconds();
213
214 /* Allocate next Lanczos vector. If fail, back up to last pause. */
215 q[j] = mkvec_ret_float(1, n);
216 if (q[j] == NULL) {
217 memory_ok = FALSE;
218 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
219 strout("WARNING: Lanczos_ext out of memory; computing best approximation available.\n");
220 }
221 if (nopauses) {
222 bail("ERROR: Sorry, can't salvage Lanczos_ext.", 1);
223 /* ... save yourselves, men. */
224 }
225 for (i = lastpause + 1; i <= j - 1; i++) {
226 frvec_float(q[i], 1);
227 }
228 j = lastpause;
229 }
230
231 /* Basic Lanczos iteration */
232 vecscale_float(q[j], 1, n, (float)(1.0 / beta[j - 1]), r);
233 blas_time += lanc_seconds() - time;
234 time = lanc_seconds();
235 splarax_float(u, A, n, q[j], vwsqrt_float, workn);
236 splarax_time += lanc_seconds() - time;
237 time = lanc_seconds();
238 update_float(r, 1, n, u, (float)(-beta[j - 1]), q[j - 1]);
239 alpha[j] = dot_float(r, 1, n, q[j]);
240 update_float(r, 1, n, r, (float)(-alpha[j]), q[j]);
241 blas_time += lanc_seconds() - time;
242
243 /* Selective orthogonalization */
244 time = lanc_seconds();
245 if (vwsqrt_float == NULL) {
246 orthog1_float(r, 1, n);
247 }
248 else {
249 orthogvec_float(r, 1, n, vwsqrt_float);
250 }
251 if ((j == (lastpause + 1)) || (j == (lastpause + 2))) {
252 sorthog_float(r, n, solist, ngood);
253 }
254 orthog_time += lanc_seconds() - time;
255 beta[j] = norm_float(r, 1, n);
256 time = lanc_seconds();
257 pause = lanpause_float(j, lastpause, interval, q, n, &pausemode, version, beta[j]);
258 pause_time += lanc_seconds() - time;
259 if (pause) {
260 nopauses = FALSE;
261 lastpause = j;
262
263 /* Compute limits for checking Ritz pair convergence. */
264 if (version == 2) {
265 if (left_ngood + 2 > left_goodlim) {
266 left_goodlim = left_ngood + 2;
267 }
268 }
269
270 /* Special case: need at least d Ritz vals on left. */
271 left_goodlim = max(left_goodlim, d);
272
273 /* Special case: can't find more than j total Ritz vals. */
274 if (left_goodlim > j) {
275 left_goodlim = min(left_goodlim, j);
276 }
277
278 /* Find Ritz vals using faster of Sturm bisection or ql. */
279 time = lanc_seconds();
280 if (inc_bis_safety) {
281 bis_safety *= 10;
282 inc_bis_safety = FALSE;
283 }
284 ritzval_flag =
285 get_ritzvals(alpha, beta, j, Anorm, workj, ritz, d, left_goodlim, 0, eigtol, bis_safety);
286 ql_time += lanc_seconds() - time;
287
288 if (ritzval_flag != 0) {
289 bail("ERROR: Lanczos_ext failed in computing eigenvalues of T.", 1);
290 /* ... we recover from this in lanczos_SO, but don't worry here. */
291 }
292
293 /* Scan for minimum evals of tridiagonal. */
294 time = lanc_seconds();
295 scanmin(ritz, 1, j, &scanlist);
296 scan_time += lanc_seconds() - time;
297
298 /* Compute Ritz pair bounds at left end. */
299 time = lanc_seconds();
300 setvec(bj, 1, j, 0.0);
301 for (i = 1; i <= left_goodlim; i++) {
302 Sres = Tevec(alpha, beta - 1, j, ritz[i], s);
303 if (Sres > Sres_max) {
304 Sres_max = Sres;
305 }
306 if (Sres > SRESTOL) {
307 inc_bis_safety = TRUE;
308 }
309 bj[i] = s[j] * beta[j];
310 }
311 ritz_time += lanc_seconds() - time;
312
313 /* Show the portion of the spectrum checked for convergence. */
314 if (DEBUG_EVECS > 2) {
315 time = lanc_seconds();
316 printf("\nindex Ritz vals bji bounds\n");
317 for (i = 1; i <= left_goodlim; i++) {
318 printf(" %3d", i);
319 doubleout(ritz[i], 1);
320 doubleout(bj[i], 1);
321 printf("\n");
322 }
323 printf("\n");
324 curlnk = scanlist;
325 while (curlnk != NULL) {
326 temp = curlnk->indx;
327 if ((temp > left_goodlim) && (temp < j)) {
328 printf(" %3d", temp);
329 doubleout(ritz[temp], 1);
330 doubleout(bj[temp], 1);
331 printf("\n");
332 }
333 curlnk = curlnk->pntr;
334 }
335 printf(" -------------------\n");
336 printf(" goodtol: %19.16f\n\n", goodtol);
337 debug_time += lanc_seconds() - time;
338 }
339
340 get_extval(alpha, beta, j, ritz[1], s, eigtol, beta[0], sigma, &extval, v, work1, work2);
341
342 /* check convergence of Ritz pairs */
343 time = lanc_seconds();
344 converged = TRUE;
345 if (j < d) {
346 converged = FALSE;
347 }
348 else {
349 curlnk = scanlist;
350 while (curlnk != NULL) {
351 if (bj[curlnk->indx] > bji_tol) {
352 converged = FALSE;
353 }
354 curlnk = curlnk->pntr;
355 }
356 }
357 scan_time += lanc_seconds() - time;
358
359 if (!converged) {
360 ngood = 0;
361 left_ngood = 0; /* for setting left_goodlim on next loop */
362
363 /* Compute converged Ritz pairs on left end */
364 time = lanc_seconds();
365 for (i = 1; i <= left_goodlim; i++) {
366 if (bj[i] <= goodtol) {
367 ngood += 1;
368 left_ngood += 1;
369 if (ngood > maxngood) {
370 maxngood = ngood;
371 solist[ngood] = makeorthlnk_float();
372 (solist[ngood])->vec = mkvec_float(1, n);
373 }
374 (solist[ngood])->index = i;
375 Sres = Tevec(alpha, beta - 1, j, ritz[i], s);
376 if (Sres > Sres_max) {
377 Sres_max = Sres;
378 }
379 if (Sres > SRESTOL) {
380 inc_bis_safety = TRUE;
381 }
382 setvec_float((solist[ngood])->vec, 1, n, 0.0);
383 for (k = 1; k <= j; k++) {
384 scadd_float((solist[ngood])->vec, 1, n, s[k], q[k]);
385 }
386 }
387 }
388 ritz_time += lanc_seconds() - time;
389
390 if (DEBUG_EVECS > 2) {
391 time = lanc_seconds();
392 printf(" j %3d; goodlim lft %2d, rgt %2d; list ", j, left_goodlim, 0);
393 solistout_float(solist, ngood, j);
394 printf("---------------------end of iteration---------------------\n\n");
395 debug_time += lanc_seconds() - time;
396 }
397 }
398 }
399 j++;
400 }
401 j--;
402
403 if (DEBUG_EVECS > 0) {
404 time = lanc_seconds();
405 if (maxj == 0) {
406 printf("Not extended eigenproblem -- calling ordinary eigensolver.\n");
407 }
408 else {
409 printf(" Lanczos_ext itns: %d\n", j);
410 printf(" eigenvalue: %g\n", ritz[1]);
411 printf(" extended eigenvalue: %g\n", extval);
412 }
413 debug_time += lanc_seconds() - time;
414 }
415
416 if (maxj != 0) {
417 /* Compute (scaled) extended eigenvector. */
418 time = lanc_seconds();
419 setvec(y[1], 1, n, 0.0);
420 for (k = 1; k <= j; k++) {
421 scadd_mixed(y[1], 1, n, v[k], q[k]);
422 }
423 evec_time += lanc_seconds() - time;
424 /* Note: assign() will scale this y vector back to x (since y = Dx) */
425
426 /* Compute and check residual directly. Use the Ay = extval*y + Dg version of
427 the problem for convenience. Note that u and v are used here as workspace */
428 time = lanc_seconds();
429 splarax(workn_double, A, n, y[1], vwsqrt, u_double);
430 scadd(workn_double, 1, n, -extval, y[1]);
431 scale_diag(gvec, 1, n, vwsqrt);
432 scadd(workn_double, 1, n, -1.0, gvec);
433 resid = ch_norm(workn_double, 1, n);
434 if (DEBUG_EVECS > 0) {
435 printf(" extended residual: %g\n", resid);
436 if (Output_File != NULL) {
437 fprintf(Output_File, " extended residual: %g\n", resid);
438 }
439 }
440 if (WARNING_EVECS > 0 && resid > eigtol) {
441 printf("WARNING: Extended residual (%g) greater than tolerance (%g).\n", resid, eigtol);
442 if (Output_File != NULL) {
443 fprintf(Output_File, "WARNING: Extended residual (%g) greater than tolerance (%g).\n",
444 resid, eigtol);
445 }
446 }
447 debug_time += lanc_seconds() - time;
448 }
449
450 /* free up memory */
451 time = lanc_seconds();
452 frvec_float(u, 1);
453 frvec(u_double, 1);
454 frvec_float(r, 1);
455 frvec_float(workn, 1);
456 frvec(workn_double, 1);
457 frvec(Ares, 0);
458 sfree(index);
459 frvec(alpha, 1);
460 frvec(beta, 0);
461 frvec(ritz, 1);
462 frvec(s, 1);
463 frvec(bj, 1);
464 frvec(workj, 0);
465 for (i = 0; i <= j; i++) {
466 frvec_float(q[i], 1);
467 }
468 sfree(q);
469 while (scanlist != NULL) {
470 curlnk = scanlist->pntr;
471 sfree(scanlist);
472 scanlist = curlnk;
473 }
474 for (i = 1; i <= maxngood; i++) {
475 frvec_float((solist[i])->vec, 1);
476 sfree(solist[i]);
477 }
478 sfree(solist);
479 frvec(extvec, 1);
480 frvec(v, 1);
481 frvec(work1, 1);
482 frvec(work2, 1);
483 if (vwsqrt != NULL) {
484 frvec_float(vwsqrt_float, 1);
485 }
486
487 init_time += lanc_seconds() - time;
488
489 if (maxj == 0) {
490 return (1); /* see note on beta[0] and maxj above */
491 }
492
493 return (0);
494 }