Debian Codegraph @

packages/seacas/libraries/chaco/eigen/lanczos_FO.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 <stdio.h>
13
14 /* Lanczos iteration with FULL orthogonalization.
15 Works in standard (version 1) or inverse operator (version 2) mode. */
16 /* Finds the lowest (zero) eigenvalue, but does not print it
17 or compute corresponding eigenvector. */
18 /* Does NOT find distinct eigenvectors corresponding to multiple
19 eigenvectors and hence will not lead to good partitioners
20 for symmetric graphs. Will be useful for graphs known
21 (believed) not to have multiple eigenvectors, e.g. graphs
22 in which a random set of edges have been added or perturbed. */
23 /* May fail on small graphs with high multiplicities (e.g. k5) if Ritz
24 pairs converge before there have been as many iterations as the number
25 of eigenvalues sought. This is rare and a different random number
26 seed will generally alleviate the problem. */
27 /* Convergence check uses Paige bji estimate over the whole
28 spectrum of T. This is a lot of work, but we are trying to be
29 extra safe. Since we are orthogonalizing fully, we assume the
30 bji estimates are very good and don't provide a contingency for
31 when they don't match the residuals. */
32 /* A lot of the time in this routine (say half) is spent in ql finding
33 the evals of T on each iteration. This could be reduced by only using
34 ql say every 10 steps. This might require that the orthogonalization
35 be done with Householder (rather than Gram-Scmidt as currently)
36 to avoid a problem with zero beta values causing subsequent breakdown.
37 But this routine is really intended to be used for smallish problems
38 where the Lanczos runs will be int, e.g. when we are using the inverse
39 operator method. In the inverse operator case, it's really important to
40 stop quickly to avoid additional back solves. If the ql work is really
41 a problem then we should be using a selective othogonalization algorithm.
42 This routine provides a convenient reference point for how well those
43 routines are functioning since it has the same basic structure but just
44 does more orthogonalizing. */
45 /* The algorithm orthogonalizes the starting vector and the residual vectors
46 against the vector of all ones since we know that is the null space of
47 the Laplacian. This generally saves net time because Lanczos tends to
48 converge faster. */
49 /* Replaced call to ql() with call to get_ritzvals(). This starts with ql
50 bisection, whichever is predicted to be faster based on a simple complexity
51 model. If that fails it switches to the other. This routine should be
52 safer and faster than straight ql (which does occasionally fail). */
53 /* NOTE: This routine indexes beta (and workj) from 1 to maxj+1, whereas
54 selective orthogonalization indexes them from 0 to maxj. */
55
56 /* Comments for Lanczos with inverted operator: */
57 /* Used Symmlq for the back solve since already maintaining that code for
58 the RQI/Symmlq multilevel method. Straight CG would only be marginally
59 faster. */
60 /* The orthogonalization against the vector of all ones in Symmlq is not
61 as efficient as possible - could in principle use orthog1 method, but
62 don't want to disrupt the RQI/Symmlq code. Also, probably would only
63 need to orthogonalize out a given mode periodically. But we want to be
64 extra robust numericlly. */
65
66 void lanczos_FO(struct vtx_data **A, /* graph data structure */
67 int n, /* number of rows/columns in matrix */
68 int d, /* problem dimension = # evecs to find */
69 double **y, /* columns of y are eigenvectors of A */
70 double *lambda, /* ritz approximation to eigenvals of A */
71 double *bound, /* on ritz pair approximations to eig pairs of A */
72 double eigtol, /* tolerance on eigenvectors */
73 double *vwsqrt, /* square root of vertex weights */
74 double maxdeg, /* maximum degree of graph */
75 int version /* 1 = standard mode, 2 = inverse operator mode */
76 )
77
78 {
79 extern FILE *Output_File; /* output file or NULL */
80 extern int DEBUG_EVECS; /* print debugging output? */
81 extern int DEBUG_TRACE; /* trace main execution path */
82 extern int WARNING_EVECS; /* print warning messages? */
83 extern int LANCZOS_MAXITNS; /* maximum Lanczos iterations allowed */
84 extern double BISECTION_SAFETY; /* safety factor for bisection algorithm */
85 extern double SRESTOL; /* resid tol for T evec comp */
86 extern double DOUBLE_MAX; /* Warning on inaccurate computation of evec of T */
87 extern double splarax_time; /* time matvecs */
88 extern double orthog_time; /* time orthogonalization work */
89 extern double tevec_time; /* time tridiagonal eigvec work */
90 extern double evec_time; /* time to generate eigenvectors */
91 extern double ql_time; /* time tridiagonal eigval work */
92 extern double blas_time; /* time for blas (not assembly coded) */
93 extern double init_time; /* time for allocating memory, etc. */
94 extern double scan_time; /* time for scanning bounds list */
95 extern double debug_time; /* time for debug computations and output */
96 int i, j; /* indices */
97 int maxj; /* maximum number of Lanczos iterations */
98 double *u, *r; /* Lanczos vectors */
99 double *Aq; /* sparse matrix-vector product vector */
100 double *alpha, *beta; /* the Lanczos scalars from each step */
101 double *ritz; /* copy of alpha for tqli */
102 double *workj; /* work vector (eg. for tqli) */
103 double *workn; /* work vector (eg. for checkeig) */
104 double *s; /* eigenvector of T */
105 double **q; /* columns of q = Lanczos basis vectors */
106 double *bj; /* beta(j)*(last element of evecs of T) */
107 double bis_safety; /* real safety factor for bisection algorithm */
108 double Sres; /* how well Tevec calculated eigvecs */
109 double Sres_max; /* Maximum value of Sres */
110 int inc_bis_safety; /* need to increase bisection safety */
111 double *Ares; /* how well Lanczos calculated each eigpair */
112 double *inv_lambda; /* eigenvalues of inverse operator */
113 int *index; /* the Ritz index of an eigenpair */
114 struct orthlink *orthlist = NULL; /* vectors to orthogonalize against in Lanczos */
115 struct orthlink *orthlist2 = NULL; /* vectors to orthogonalize against in Symmlq */
116 struct orthlink *temp; /* for expanding orthogonalization list */
117 double *ritzvec = NULL; /* ritz vector for current iteration */
118 double *zeros = NULL; /* vector of all zeros */
119 double *ones = NULL; /* vector of all ones */
120 struct scanlink *scanlist; /* list of fields for min ritz vals */
121 struct scanlink *curlnk; /* for traversing the scanlist */
122 double bji_tol; /* tol on bji estimate of A e-residual */
123 int converged; /* has the iteration converged? */
124 double time; /* current clock time */
125 double shift, rtol; /* symmlq input */
126 long precon, goodb, nout; /* symmlq input */
127 long checka, intlim; /* symmlq input */
128 double anorm, acond; /* symmlq output */
129 double rnorm, ynorm; /* symmlq output */
130 long istop, itn; /* symmlq output */
131 double macheps; /* machine precision calculated by symmlq */
132 double normxlim; /* a stopping criteria for symmlq */
133 long itnmin; /* enforce minimum number of iterations */
134 int symmlqitns = 0; /* # symmlq itns */
135 double *wv1 = NULL, *wv2 = NULL, *wv3 = NULL; /* Symmlq work space */
136 double *wv4 = NULL, *wv5 = NULL, *wv6 = NULL; /* Symmlq work space */
137 long long_n; /* long int copy of n for symmlq */
138 int ritzval_flag = 0; /* status flag for ql() */
139 double Anorm; /* Norm estimate of the Laplacian matrix */
140 int left, right; /* ranges on the search for ritzvals */
141 int memory_ok; /* TRUE as long as don't run out of memory */
142
143 if (DEBUG_TRACE > 0) {
144 printf("<Entering lanczos_FO>\n");
145 }
146
147 if (DEBUG_EVECS > 0) {
148 if (version == 1) {
149 printf("Full orthogonalization Lanczos, matrix size = %d\n", n);
150 }
151 else {
152 printf("Full orthogonalization Lanczos, inverted operator, matrix size = %d\n", n);
153 }
154 }
155
156 /* Initialize time. */
157 time = lanc_seconds();
158
159 if (n < d + 1) {
160 bail("ERROR: System too small for number of eigenvalues requested.", 1);
161 /* d+1 since don't use zero eigenvalue pair */
162 }
163
164 /* Allocate Lanczos space. */
165 maxj = LANCZOS_MAXITNS;
166 u = mkvec(1, n);
167 r = mkvec(1, n);
168 Aq = mkvec(1, n);
169 ritzvec = mkvec(1, n);
170 zeros = mkvec(1, n);
171 setvec(zeros, 1, n, 0.0);
172 workn = mkvec(1, n);
173 Ares = mkvec(1, d);
174 inv_lambda = mkvec(1, d);
175 index = smalloc((d + 1) * sizeof(int));
176 alpha = mkvec(1, maxj);
177 beta = mkvec(1, maxj + 1);
178 ritz = mkvec(1, maxj);
179 s = mkvec(1, maxj);
180 bj = mkvec(1, maxj);
181 workj = mkvec(1, maxj + 1);
182 q = smalloc((maxj + 1) * sizeof(double *));
183 scanlist = mkscanlist(d);
184
185 if (version == 2) {
186 /* Allocate Symmlq space all in one chunk. */
187 wv1 = smalloc(6 * (n + 1) * sizeof(double));
188 wv2 = &wv1[(n + 1)];
189 wv3 = &wv1[2 * (n + 1)];
190 wv4 = &wv1[3 * (n + 1)];
191 wv5 = &wv1[4 * (n + 1)];
192 wv6 = &wv1[5 * (n + 1)];
193
194 /* Set invariant symmlq parameters */
195 precon = FALSE; /* FALSE until we figure out a good way */
196 goodb = FALSE; /* should be FALSE for this application */
197 checka = FALSE; /* if don't know by now, too bad */
198 intlim = n; /* set to enforce a maximum number of Symmlq itns */
199 itnmin = 0; /* set to enforce a minimum number of Symmlq itns */
200 shift = 0.0; /* since just solving rather than doing RQI */
201 symmlqitns = 0; /* total number of Symmlq iterations */
202 nout = 0; /* Effectively disabled - see notes in symmlq.f */
203 rtol = 1.0e-5; /* requested residual tolerance */
204 normxlim = DOUBLE_MAX; /* Effectively disables ||x|| termination criterion */
205 long_n = n; /* copy to long for linting */
206 }
207
208 /* Initialize. */
209 vecran(r, 1, n);
210 if (vwsqrt == NULL) {
211 /* whack one's direction from initial vector */
212 orthog1(r, 1, n);
213
214 /* list the ones direction for later use in Symmlq */
215 if (version == 2) {
216 orthlist2 = makeorthlnk();
217 ones = mkvec(1, n);
218 setvec(ones, 1, n, 1.0);
219 orthlist2->vec = ones;
220 orthlist2->pntr = NULL;
221 }
222 }
223 else {
224 /* whack vwsqrt direction from initial vector */
225 orthogvec(r, 1, n, vwsqrt);
226
227 if (version == 2) {
228 /* list the vwsqrt direction for later use in Symmlq */
229 orthlist2 = makeorthlnk();
230 orthlist2->vec = vwsqrt;
231 orthlist2->pntr = NULL;
232 }
233 }
234 beta[1] = ch_norm(r, 1, n);
235 q[0] = zeros;
236 bji_tol = eigtol;
237 orthlist = NULL;
238 Sres_max = 0.0;
239 Anorm = 2 * maxdeg; /* Gershgorin estimate for ||A|| */
240 bis_safety = BISECTION_SAFETY;
241 inc_bis_safety = FALSE;
242 init_time += lanc_seconds() - time;
243
244 /* Main Lanczos loop. */
245 j = 1;
246 converged = FALSE;
247 memory_ok = TRUE;
248 while ((j <= maxj) && (converged == FALSE) && memory_ok) {
249 time = lanc_seconds();
250
251 /* Allocate next Lanczos vector. If fail, back up one step and compute approx. eigvec. */
252 q[j] = mkvec_ret(1, n);
253 if (q[j] == NULL) {
254 memory_ok = FALSE;
255 if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) {
256 strout("WARNING: Lanczos out of memory; computing best approximation available.\n");
257 }
258 if (j <= 2) {
259 bail("ERROR: Sorry, can't salvage Lanczos.", 1);
260 /* ... save yourselves, men. */
261 }
262 j--;
263 }
264
265 vecscale(q[j], 1, n, 1.0 / beta[j], r);
266 blas_time += lanc_seconds() - time;
267 time = lanc_seconds();
268 if (version == 1) {
269 splarax(Aq, A, n, q[j], vwsqrt, workn);
270 }
271 else if (version == 2) {
272 symmlq(&long_n, &(q[j][1]), &wv1[1], &wv2[1], &wv3[1], &wv4[1], &Aq[1], &wv5[1], &wv6[1],
273 &checka, &goodb, &precon, &shift, &nout, &intlim, &rtol, &istop, &itn, &anorm, &acond,
274 &rnorm, &ynorm, (double *)A, vwsqrt, (double *)orthlist2, &macheps, &normxlim,
275 &itnmin);
276 symmlqitns += itn;
277 if (DEBUG_EVECS > 2) {
278 printf("Symmlq report: rtol %g\n", rtol);
279 printf(" system norm %g, solution norm %g\n", anorm, ynorm);
280 printf(" system condition %g, residual %g\n", acond, rnorm);
281 printf(" termination condition %2ld, iterations %3ld\n", istop, itn);
282 }
283 }
284 splarax_time += lanc_seconds() - time;
285 time = lanc_seconds();
286 update(u, 1, n, Aq, -beta[j], q[j - 1]);
287 alpha[j] = dot(u, 1, n, q[j]);
288 update(r, 1, n, u, -alpha[j], q[j]);
289 blas_time += lanc_seconds() - time;
290 time = lanc_seconds();
291 if (vwsqrt == NULL) {
292 orthog1(r, 1, n);
293 }
294 else {
295 orthogvec(r, 1, n, vwsqrt);
296 }
297 orthogonalize(r, n, orthlist);
298 temp = orthlist;
299 orthlist = makeorthlnk();
300 orthlist->vec = q[j];
301 orthlist->pntr = temp;
302 beta[j + 1] = ch_norm(r, 1, n);
303 orthog_time += lanc_seconds() - time;
304
305 time = lanc_seconds();
306 left = j / 2;
307 right = j - left + 1;
308 if (inc_bis_safety) {
309 bis_safety *= 10;
310 inc_bis_safety = FALSE;
311 }
312 ritzval_flag = get_ritzvals(alpha, beta + 1, j, Anorm, workj + 1, ritz, d, left, right, eigtol,
313 bis_safety);
314 /* ... have to off-set beta and workj since full orthogonalization
315 indexes these from 1 to maxj+1 whereas selective orthog.
316 indexes them from 0 to maxj */
317
318 if (ritzval_flag != 0) {
319 bail("ERROR: Both Sturm bisection and QL failed.", 1);
320 /* ... give up. */
321 }
322 ql_time += lanc_seconds() - time;
323
324 /* Convergence check using Paige bji estimates. */
325 time = lanc_seconds();
326 for (i = 1; i <= j; i++) {
327 Sres = Tevec(alpha, beta, j, ritz[i], s);
328 if (Sres > Sres_max) {
329 Sres_max = Sres;
330 }
331 if (Sres > SRESTOL) {
332 inc_bis_safety = TRUE;
333 }
334 bj[i] = s[j] * beta[j + 1];
335 }
336 tevec_time += lanc_seconds() - time;
337
338 time = lanc_seconds();
339 if (version == 1) {
340 scanmin(ritz, 1, j, &scanlist);
341 }
342 else {
343 scanmax(ritz, 1, j, &scanlist);
344 }
345 converged = TRUE;
346 if (j < d) {
347 converged = FALSE;
348 }
349 else {
350 curlnk = scanlist;
351 while (curlnk != NULL) {
352 if (bj[curlnk->indx] > bji_tol) {
353 converged = FALSE;
354 }
355 curlnk = curlnk->pntr;
356 }
357 }
358 scan_time += lanc_seconds() - time;
359 j++;
360 }
361 j--;
362
363 /* Collect eigenvalue and bound information. */
364 time = lanc_seconds();
365 mkeigvecs(scanlist, lambda, bound, index, bj, d, &Sres_max, alpha, beta + 1, j, s, y, n, q);
366 evec_time += lanc_seconds() - time;
367
368 /* Analyze computation for and report additional problems */
369 time = lanc_seconds();
370 if (DEBUG_EVECS > 0 && version == 2) {
371 printf("\nTotal Symmlq iterations %3d\n", symmlqitns);
372 }
373 if (version == 2) {
374 for (i = 1; i <= d; i++) {
375 lambda[i] = 1.0 / lambda[i];
376 }
377 }
378 warnings(workn, A, y, n, lambda, vwsqrt, Ares, bound, index, d, j, maxj, Sres_max, eigtol, u,
379 Anorm, Output_File);
380 debug_time += lanc_seconds() - time;
381
382 /* Free any memory allocated in this routine. */
383 time = lanc_seconds();
384 frvec(u, 1);
385 frvec(r, 1);
386 frvec(Aq, 1);
387 frvec(ritzvec, 1);
388 frvec(zeros, 1);
389 if (vwsqrt == NULL && version == 2) {
390 frvec(ones, 1);
391 }
392 frvec(workn, 1);
393 frvec(Ares, 1);
394 frvec(inv_lambda, 1);
395 sfree(index);
396 frvec(alpha, 1);
397 frvec(beta, 1);
398 frvec(ritz, 1);
399 frvec(s, 1);
400 frvec(bj, 1);
401 frvec(workj, 1);
402 if (version == 2) {
403 frvec(wv1, 0);
404 }
405 while (scanlist != NULL) {
406 curlnk = scanlist->pntr;
407 sfree(scanlist);
408 scanlist = curlnk;
409 }
410 for (i = 1; i <= j; i++) {
411 frvec(q[i], 1);
412 }
413 while (orthlist != NULL) {
414 temp = orthlist->pntr;
415 sfree(orthlist);
416 orthlist = temp;
417 }
418 while (version == 2 && orthlist2 != NULL) {
419 temp = orthlist2->pntr;
420 sfree(orthlist2);
421 orthlist2 = temp;
422 }
423 sfree(q);
424 init_time += lanc_seconds() - time;
425 }