| 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" // for FALSE, TRUE, min, max |
| 10 |
#include "smalloc.h" // for sfree, smalloc |
| 11 |
#include "structs.h" // for orthlink, scanlink |
| 12 |
#include <math.h> // for sqrt |
| 13 |
#include <stdio.h> // for printf, NULL, FILE |
| 14 |
|
| 15 |
/* These comments are for version 1 of selective orthogonalization: |
| 16 |
This version of selective orthogonalization largely follows that described in |
| 17 |
Pareltt and Scott, "The Lanczos Algorithm with Selective Orthogonalization", |
| 18 |
Math Comp v33 #145, 1979. Different heuristics are used to control loss of |
| 19 |
orthogonality. Specifically, the pauses to check for convergence of Ritz pairs |
| 20 |
at first come at a small but increasing interval as the computation builds up. |
| 21 |
Later they come at a regular, pre-set interval, e.g. every 10 steps. Hence this |
| 22 |
is similar to Grear's periodic reorthogonalization scheme, but with an adaptive |
| 23 |
period. A small number of ritz pairs at both ends of the spectrum are monitored |
| 24 |
for convergence at each pause. The number monitored gradually increases as more |
| 25 |
Ritz pairs converge so that the number monitored at each pause is always at least |
| 26 |
2 greater than the number that have previously converged. This is a fairly |
| 27 |
conservative strategy, at least in the context of the typical Laplacian graph |
| 28 |
matrices studied in connection with load balancing. But it may occasionally |
| 29 |
result in premature loss of orthogonality. If the loss of orthogonality is |
| 30 |
severe, Lanczos may fail to meet the set eigen tolerance or mis-converge to |
| 31 |
the wrong eigenpair. These conditions will usually be detected by the algorithm |
| 32 |
and a warning issued. If the graph is small, full orthogonalization is |
| 33 |
then a fall back option. If the graph is large, one of the multi-level schemes |
| 34 |
should be tried. The algorithm orthogonalizes the starting vector and each |
| 35 |
residual vector against the vector of all ones since we know that is an |
| 36 |
eigenvector of the Laplacian (which we don't want to see). This is |
| 37 |
accomplished through calls to orthog1. These can be removed, but then |
| 38 |
we must compute an extra eigenvalue and the vector of all ones shows up as |
| 39 |
a Ritz pair we need to orthogonalize against in order to maintain the basis. |
| 40 |
This is more expensive then simply taking it out of the residual on each step, |
| 41 |
and doing the latter also reduces the number of iterations. The Ritz values |
| 42 |
being monitored at each step are computed using the QL algorithm or bisection |
| 43 |
on the Sturm sequence, whichever is faster based on a simple complexity model. |
| 44 |
In practice the time spent computing Ritz values is only a small portion of |
| 45 |
the total Lanczos time, so more complex schemes based on interpolating the |
| 46 |
bottom pivot function represent only a very marginal savings in execution |
| 47 |
time at the expense of either some robustness or substantially increased |
| 48 |
code complexity. */ |
| 49 |
|
| 50 |
/* These comments are for version 2 of selective orthogonalization: |
| 51 |
These comments are cumulative from version 1. Modified the heuristics to |
| 52 |
make it faster. Now only checks left end of spectrum for converging Ritz pairs. |
| 53 |
Only checking the left end of the spectrum for converging Ritz pairs is in principle |
| 54 |
not as good as checking both ends, but in practice seems to be no worse and usually |
| 55 |
better than checking both ends. Since the distribution of eigenvalues for Laplacian |
| 56 |
graphs of interest seems to generally result in much more rapid convergence on the |
| 57 |
right end of the spectrum, ignoring the converging Ritz pairs there saves a lot of |
| 58 |
work. */ |
| 59 |
|
| 60 |
/* These comments are for version 3 of selective orthogonalization: |
| 61 |
This option hasn't performed that well, so I've rescinded it from the menu. |
| 62 |
Uses Paige suggestion of monitoring dot product of current Lanczos vector |
| 63 |
against first to sense loss of orthogonality. This can save time by avoiding |
| 64 |
calls to the QL or bisection routines for finding Ritz values. */ |
| 65 |
|
| 66 |
/* These comments explain some of indexing conventions used: |
| 67 |
Note that the indexing of the tridiagonal matrix is consistent with Parlett and |
| 68 |
Scott's Math Comp '79 paper "The Lanczos Algorithm With Selective Orthogonalization" |
| 69 |
and with Golub and Van Loan's (2nd ed.) treatment in Chapter 9. So on iteration j |
| 70 |
the tridiagonal matrix T has diagonal = alpha[1], alpha[2] ... alpha[j] and |
| 71 |
off-diagonal = beta[1], beta[2] ... beta[j-1]. beta[0] is the norm of the initial |
| 72 |
residual and beta[j] is used in monitoring convergence. */ |
| 73 |
|
| 74 |
/* These comments pertain to the calculation of the eigenvalues of T: |
| 75 |
There are several safety features in the code which computes the Ritz values. |
| 76 |
Computation is first attempted using either the QL algorithm or Sturm sequence |
| 77 |
bisection, whichever is predicted to be cheaper based on simple complexity analysis |
| 78 |
(normally bisection is cheaper in this context). The classic bisection algorithm |
| 79 |
can fail due to overflow, so a re-scaling heuristic is used to guard against this. |
| 80 |
The number of bisection and QL iterations is monitored and if a reasonable maximum |
| 81 |
is observed these routines abort. If either routine fails, the other is applied |
| 82 |
to the problem. If both routines fail, Lanczos is backed up to the last pause point |
| 83 |
and an approximate eigenvector is computed based on the available information. */ |
| 84 |
|
| 85 |
/* These comments pertain to the calculation of eigenvectors of T: |
| 86 |
In the first instance the eigenvectors are computed using a bidirectional |
| 87 |
recurrence which amounts to one fancy step of inverse iteration. If this |
| 88 |
fails to achieve the requested tolerance, a more expensive version of inverse |
| 89 |
iteration (Tinvit from Eispack) is tried. The better of the two answers is |
| 90 |
then used. See the routine Tevec for more comments and details. */ |
| 91 |
|
| 92 |
void lanczos_SO(struct vtx_data **A, /* sparse matrix in row linked list format */ |
| 93 |
int n, /* problem size */ |
| 94 |
int d, /* problem dimension = number of eigvecs to find */ |
| 95 |
double **y, /* columns of y are eigenvectors of A */ |
| 96 |
double *lambda, /* ritz approximation to eigenvals of A */ |
| 97 |
double *bound, /* on ritz pair approximations to eig pairs of A */ |
| 98 |
double eigtol, /* tolerance on eigenvectors */ |
| 99 |
double *vwsqrt, /* square roots of vertex weights */ |
| 100 |
double maxdeg, /* maximum degree of graph */ |
| 101 |
int version, /* flags which version of sel. orth. to use */ |
| 102 |
int cube_or_mesh, /* 0 => hypercube, d => d-dimensional mesh */ |
| 103 |
int nsets, /* number of sets to divide into */ |
| 104 |
int *assignment, /* set number of each vtx (length n+1) */ |
| 105 |
int *active, /* space for nvtxs integers */ |
| 106 |
int mediantype, /* which partitioning strategy to use */ |
| 107 |
double *goal, /* desired set sizes */ |
| 108 |
int vwgt_max /* largest vertex weight */ |
| 109 |
) |
| 110 |
{ |
| 111 |
extern FILE *Output_File; /* output file or null */ |
| 112 |
extern int LANCZOS_SO_INTERVAL; /* interval between orthogonalizations */ |
| 113 |
extern int LANCZOS_CONVERGENCE_MODE; /* type of Lanczos convergence test */ |
| 114 |
extern int LANCZOS_MAXITNS; /* maximum Lanczos iterations allowed */ |
| 115 |
extern int DEBUG_EVECS; /* print debugging output? */ |
| 116 |
extern int DEBUG_TRACE; /* trace main execution path */ |
| 117 |
extern int WARNING_EVECS; /* print warning messages? */ |
| 118 |
extern double BISECTION_SAFETY; /* safety factor for bisection alg. */ |
| 119 |
extern double SRESTOL; /* resid tol for T evec comp */ |
| 120 |
extern double DOUBLE_EPSILON; /* machine precision */ |
| 121 |
extern double DOUBLE_MAX; /* largest double value */ |
| 122 |
extern double splarax_time; /* time matvec */ |
| 123 |
extern double orthog_time; /* time orthogonalization work */ |
| 124 |
extern double evec_time; /* time to generate eigenvectors */ |
| 125 |
extern double ql_time; /* time tridiagonal eigenvalue work */ |
| 126 |
extern double blas_time; /* time for blas. linear algebra */ |
| 127 |
extern double init_time; /* time to allocate, initialize variables */ |
| 128 |
extern double scan_time; /* time for scanning eval and bound lists */ |
| 129 |
extern double debug_time; /* time for (some of) debug computations */ |
| 130 |
extern double ritz_time; /* time to generate ritz vectors */ |
| 131 |
extern double pause_time; /* time to compute whether to pause */ |
| 132 |
double bis_safety; /* real safety factor for bisection alg. */ |
| 133 |
int i, j, k; /* indices */ |
| 134 |
int maxj; /* maximum number of Lanczos iterations */ |
| 135 |
double *u, *r; /* Lanczos vectors */ |
| 136 |
double *alpha, *beta; /* the Lanczos scalars from each step */ |
| 137 |
double *ritz; /* copy of alpha for ql */ |
| 138 |
double *workj; /* work vector, e.g. copy of beta for ql */ |
| 139 |
double *workn; /* work vector, e.g. product Av for checkeig */ |
| 140 |
double *s; /* eigenvector of T */ |
| 141 |
double **q; /* columns of q are Lanczos basis vectors */ |
| 142 |
double *bj; /* beta(j)*(last el. of corr. eigvec s of T) */ |
| 143 |
double Sres; /* how well Tevec calculated eigvec s */ |
| 144 |
double Sres_max; /* Max value of Sres */ |
| 145 |
int inc_bis_safety; /* need to increase bisection safety */ |
| 146 |
double *Ares; /* how well Lanczos calc. eigpair lambda,y */ |
| 147 |
int *index; /* the Ritz index of an eigenpair */ |
| 148 |
struct orthlink **solist; /* vec. of structs with vecs. to orthog. against */ |
| 149 |
struct scanlink *scanlist; /* linked list of fields to do with min ritz vals */ |
| 150 |
struct scanlink *curlnk; /* for traversing the scanlist */ |
| 151 |
double bji_tol; /* tol on bji est. of eigen residual of A */ |
| 152 |
int converged; /* has the iteration converged? */ |
| 153 |
double goodtol; /* error tolerance for a good Ritz vector */ |
| 154 |
int ngood; /* total number of good Ritz pairs at current step */ |
| 155 |
int maxngood; /* biggest val of ngood through current step */ |
| 156 |
int left_ngood; /* number of good Ritz pairs on left end */ |
| 157 |
int right_ngood; /* number of good Ritz pairs on right end */ |
| 158 |
int lastpause; /* Most recent step with good ritz vecs */ |
| 159 |
int firstpause; /* Is this the first pause? */ |
| 160 |
int nopauses; /* Have there been any pauses? */ |
| 161 |
int interval; /* number of steps between pauses */ |
| 162 |
double time; /* Current clock time */ |
| 163 |
int left_goodlim; /* number of ritz pairs checked on left end */ |
| 164 |
int right_goodlim; /* number of ritz pairs checked on right end */ |
| 165 |
double Anorm; /* Norm estimate of the Laplacian matrix */ |
| 166 |
int pausemode; /* which Lanczos pausing criterion to use */ |
| 167 |
int pause; /* whether to pause */ |
| 168 |
int temp; /* used to prevent redundant index computations */ |
| 169 |
int *old_assignment = NULL; /* set # of each vtx on previous pause, length n+1 */ |
| 170 |
int *assgn_pntr; /* pntr to assignment vector */ |
| 171 |
int *old_assgn_pntr; /* pntr to previous assignment vector */ |
| 172 |
int assigndiff; /* # of differences between old and new assignment */ |
| 173 |
int assigntol; /* tolerance on convergence of assignment vector */ |
| 174 |
int ritzval_flag; /* status flag for get_ritzvals() */ |
| 175 |
int memory_ok; /* True until lanczos runs out of memory */ |
| 176 |
|
| 177 |
if (DEBUG_TRACE > 0) { |
| 178 |
printf("<Entering lanczos_so>\n"); |
| 179 |
} |
| 180 |
|
| 181 |
if (DEBUG_EVECS > 0) { |
| 182 |
printf("Selective orthogonalization Lanczos (v. %d), matrix size = %d.\n", version, n); |
| 183 |
} |
| 184 |
|
| 185 |
/* Initialize time. */ |
| 186 |
time = lanc_seconds(); |
| 187 |
|
| 188 |
if (n < d + 1) { |
| 189 |
bail("ERROR: System too small for number of eigenvalues requested.", 1); |
| 190 |
/* d+1 since don't use zero eigenvalue pair */ |
| 191 |
} |
| 192 |
|
| 193 |
/* Allocate space. */ |
| 194 |
maxj = LANCZOS_MAXITNS; |
| 195 |
u = mkvec(1, n); |
| 196 |
r = mkvec(1, n); |
| 197 |
workn = mkvec(1, n); |
| 198 |
Ares = mkvec(0, d); |
| 199 |
index = smalloc((d + 1) * sizeof(int)); |
| 200 |
alpha = mkvec(1, maxj); |
| 201 |
beta = mkvec(0, maxj); |
| 202 |
ritz = mkvec(1, maxj); |
| 203 |
s = mkvec(1, maxj); |
| 204 |
bj = mkvec(1, maxj); |
| 205 |
workj = mkvec(0, maxj); |
| 206 |
q = smalloc((maxj + 1) * sizeof(double *)); |
| 207 |
solist = smalloc((maxj + 1) * sizeof(struct orthlink *)); |
| 208 |
scanlist = mkscanlist(d); |
| 209 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 210 |
old_assignment = smalloc((n + 1) * sizeof(int)); |
| 211 |
} |
| 212 |
|
| 213 |
/* Set some constants governing the orthogonalization heuristic. */ |
| 214 |
ngood = 0; |
| 215 |
maxngood = 0; |
| 216 |
bji_tol = eigtol; |
| 217 |
assigntol = eigtol * n; |
| 218 |
Anorm = 2 * maxdeg; /* Gershgorin estimate for ||A|| */ |
| 219 |
goodtol = Anorm * sqrt(DOUBLE_EPSILON); /* Parlett & Scott's bound, p.224 */ |
| 220 |
interval = 2 + min(LANCZOS_SO_INTERVAL - 2, n / (2 * LANCZOS_SO_INTERVAL)); |
| 221 |
bis_safety = BISECTION_SAFETY; |
| 222 |
|
| 223 |
if (DEBUG_EVECS > 0) { |
| 224 |
printf(" eigtol %g\n", eigtol); |
| 225 |
printf(" maxdeg %g\n", maxdeg); |
| 226 |
printf(" goodtol %g\n", goodtol); |
| 227 |
printf(" interval %d\n", interval); |
| 228 |
printf(" maxj %d\n", maxj); |
| 229 |
printf(" srestol %g\n", SRESTOL); |
| 230 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 231 |
printf(" assigntol %d\n", assigntol); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
/* Initialize space. */ |
| 236 |
vecran(r, 1, n); |
| 237 |
if (vwsqrt == NULL) { |
| 238 |
orthog1(r, 1, n); |
| 239 |
} |
| 240 |
else { |
| 241 |
orthogvec(r, 1, n, vwsqrt); |
| 242 |
} |
| 243 |
beta[0] = ch_norm(r, 1, n); |
| 244 |
q[0] = mkvec(1, n); |
| 245 |
setvec(q[0], 1, n, 0.0); |
| 246 |
setvec(bj, 1, maxj, DOUBLE_MAX); |
| 247 |
|
| 248 |
/* Main Lanczos loop. */ |
| 249 |
j = 1; |
| 250 |
lastpause = 0; |
| 251 |
pausemode = 1; |
| 252 |
left_ngood = 0; |
| 253 |
right_ngood = 0; |
| 254 |
left_goodlim = 0; |
| 255 |
right_goodlim = 0; |
| 256 |
converged = FALSE; |
| 257 |
Sres_max = 0.0; |
| 258 |
inc_bis_safety = FALSE; |
| 259 |
ritzval_flag = 0; |
| 260 |
memory_ok = TRUE; |
| 261 |
firstpause = FALSE; |
| 262 |
nopauses = TRUE; |
| 263 |
init_time += lanc_seconds() - time; |
| 264 |
while ((j <= maxj) && (!converged) && (ritzval_flag == 0) && memory_ok) { |
| 265 |
time = lanc_seconds(); |
| 266 |
|
| 267 |
/* Allocate next Lanczos vector. If fail, back up to last pause. */ |
| 268 |
q[j] = mkvec_ret(1, n); |
| 269 |
if (q[j] == NULL) { |
| 270 |
memory_ok = FALSE; |
| 271 |
if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) { |
| 272 |
strout("WARNING: Lanczos out of memory; computing best approximation available.\n"); |
| 273 |
} |
| 274 |
if (nopauses) { |
| 275 |
bail("ERROR: Sorry, can't salvage Lanczos.", 1); |
| 276 |
/* ... save yourselves, men. */ |
| 277 |
} |
| 278 |
for (i = lastpause + 1; i <= j - 1; i++) { |
| 279 |
frvec(q[i], 1); |
| 280 |
} |
| 281 |
j = lastpause; |
| 282 |
} |
| 283 |
|
| 284 |
/* Basic Lanczos iteration */ |
| 285 |
vecscale(q[j], 1, n, 1.0 / beta[j - 1], r); |
| 286 |
blas_time += lanc_seconds() - time; |
| 287 |
time = lanc_seconds(); |
| 288 |
splarax(u, A, n, q[j], vwsqrt, workn); |
| 289 |
splarax_time += lanc_seconds() - time; |
| 290 |
time = lanc_seconds(); |
| 291 |
update(r, 1, n, u, -beta[j - 1], q[j - 1]); |
| 292 |
alpha[j] = dot(r, 1, n, q[j]); |
| 293 |
update(r, 1, n, r, -alpha[j], q[j]); |
| 294 |
blas_time += lanc_seconds() - time; |
| 295 |
|
| 296 |
/* Selective orthogonalization */ |
| 297 |
time = lanc_seconds(); |
| 298 |
if (vwsqrt == NULL) { |
| 299 |
orthog1(r, 1, n); |
| 300 |
} |
| 301 |
else { |
| 302 |
orthogvec(r, 1, n, vwsqrt); |
| 303 |
} |
| 304 |
if ((j == (lastpause + 1)) || (j == (lastpause + 2))) { |
| 305 |
sorthog(r, n, solist, ngood); |
| 306 |
} |
| 307 |
orthog_time += lanc_seconds() - time; |
| 308 |
beta[j] = ch_norm(r, 1, n); |
| 309 |
time = lanc_seconds(); |
| 310 |
pause = lanpause(j, lastpause, interval, q, n, &pausemode, version, beta[j]); |
| 311 |
pause_time += lanc_seconds() - time; |
| 312 |
if (pause) { |
| 313 |
nopauses = FALSE; |
| 314 |
if (lastpause == 0) { |
| 315 |
firstpause = TRUE; |
| 316 |
} |
| 317 |
else { |
| 318 |
firstpause = FALSE; |
| 319 |
} |
| 320 |
lastpause = j; |
| 321 |
|
| 322 |
/* Compute limits for checking Ritz pair convergence. */ |
| 323 |
if (version == 1) { |
| 324 |
if (left_ngood + 2 > left_goodlim) { |
| 325 |
left_goodlim = left_ngood + 2; |
| 326 |
} |
| 327 |
if (right_ngood + 3 > right_goodlim) { |
| 328 |
right_goodlim = right_ngood + 3; |
| 329 |
} |
| 330 |
} |
| 331 |
if (version == 2) { |
| 332 |
if (left_ngood + 2 > left_goodlim) { |
| 333 |
left_goodlim = left_ngood + 2; |
| 334 |
} |
| 335 |
right_goodlim = 0; |
| 336 |
} |
| 337 |
|
| 338 |
/* Special case: need at least d Ritz vals on left. */ |
| 339 |
left_goodlim = max(left_goodlim, d); |
| 340 |
|
| 341 |
/* Special case: can't find more than j total Ritz vals. */ |
| 342 |
if (left_goodlim + right_goodlim > j) { |
| 343 |
left_goodlim = min(left_goodlim, j); |
| 344 |
right_goodlim = j - left_goodlim; |
| 345 |
} |
| 346 |
|
| 347 |
/* Find Ritz vals using faster of Sturm bisection or QL. */ |
| 348 |
time = lanc_seconds(); |
| 349 |
blas_time += lanc_seconds() - time; |
| 350 |
time = lanc_seconds(); |
| 351 |
if (inc_bis_safety) { |
| 352 |
bis_safety *= 10; |
| 353 |
inc_bis_safety = FALSE; |
| 354 |
} |
| 355 |
ritzval_flag = get_ritzvals(alpha, beta, j, Anorm, workj, ritz, d, left_goodlim, |
| 356 |
right_goodlim, eigtol, bis_safety); |
| 357 |
ql_time += lanc_seconds() - time; |
| 358 |
|
| 359 |
/* If get_ritzvals() fails, back up to last pause point and exit main loop. */ |
| 360 |
if (ritzval_flag != 0) { |
| 361 |
if (DEBUG_EVECS > 0 || WARNING_EVECS > 0) { |
| 362 |
strout("ERROR: Lanczos failed in computing eigenvalues of T; computing"); |
| 363 |
strout(" best readily available approximation to eigenvector.\n"); |
| 364 |
} |
| 365 |
if (firstpause) { |
| 366 |
bail("ERROR: Sorry, can't salvage Lanczos.", 1); |
| 367 |
/* ... save yourselves, men. */ |
| 368 |
} |
| 369 |
for (i = lastpause + 1; i <= j; i++) { |
| 370 |
frvec(q[i], 1); |
| 371 |
} |
| 372 |
j = lastpause; |
| 373 |
get_ritzvals(alpha, beta, j, Anorm, workj, ritz, d, left_goodlim, right_goodlim, eigtol, |
| 374 |
bis_safety); |
| 375 |
} |
| 376 |
|
| 377 |
/* Scan for minimum evals of tridiagonal. */ |
| 378 |
time = lanc_seconds(); |
| 379 |
scanmin(ritz, 1, j, &scanlist); |
| 380 |
scan_time += lanc_seconds() - time; |
| 381 |
|
| 382 |
/* Compute Ritz pair bounds at left end. */ |
| 383 |
time = lanc_seconds(); |
| 384 |
setvec(bj, 1, j, 0.0); |
| 385 |
for (i = 1; i <= left_goodlim; i++) { |
| 386 |
Sres = Tevec(alpha, beta - 1, j, ritz[i], s); |
| 387 |
if (Sres > Sres_max) { |
| 388 |
Sres_max = Sres; |
| 389 |
} |
| 390 |
if (Sres > SRESTOL) { |
| 391 |
inc_bis_safety = TRUE; |
| 392 |
} |
| 393 |
bj[i] = s[j] * beta[j]; |
| 394 |
/* bj[i] = fabs(s[j] * beta[j]); if don't enforce in Tevec */ |
| 395 |
} |
| 396 |
|
| 397 |
/* Compute Ritz pair bounds at right end. */ |
| 398 |
for (i = j; i > j - right_goodlim; i--) { |
| 399 |
Sres = Tevec(alpha, beta - 1, j, ritz[i], s); |
| 400 |
if (Sres > Sres_max) { |
| 401 |
Sres_max = Sres; |
| 402 |
} |
| 403 |
if (Sres > SRESTOL) { |
| 404 |
inc_bis_safety = TRUE; |
| 405 |
} |
| 406 |
bj[i] = s[j] * beta[j]; |
| 407 |
/* bj[i] = fabs(s[j] * beta[j]); if don't enforce in Tevec */ |
| 408 |
} |
| 409 |
ritz_time += lanc_seconds() - time; |
| 410 |
|
| 411 |
/* Show the portion of the spectrum checked for convergence. */ |
| 412 |
if (DEBUG_EVECS > 2) { |
| 413 |
time = lanc_seconds(); |
| 414 |
printf("index Ritz vals bji bounds (j = %d)\n", j); |
| 415 |
for (i = 1; i <= left_goodlim; i++) { |
| 416 |
printf(" %3d", i); |
| 417 |
doubleout(ritz[i], 1); |
| 418 |
doubleout(bj[i], 1); |
| 419 |
printf("\n"); |
| 420 |
} |
| 421 |
printf("\n"); |
| 422 |
curlnk = scanlist; |
| 423 |
while (curlnk != NULL) { |
| 424 |
temp = curlnk->indx; |
| 425 |
if ((temp > left_goodlim) && (temp < j - right_goodlim)) { |
| 426 |
printf(" %3d", temp); |
| 427 |
doubleout(ritz[temp], 1); |
| 428 |
doubleout(bj[temp], 1); |
| 429 |
printf("\n"); |
| 430 |
} |
| 431 |
curlnk = curlnk->pntr; |
| 432 |
} |
| 433 |
printf("\n"); |
| 434 |
for (i = j - right_goodlim + 1; i <= j; i++) { |
| 435 |
printf(" %3d", i); |
| 436 |
doubleout(ritz[i], 1); |
| 437 |
doubleout(bj[i], 1); |
| 438 |
printf("\n"); |
| 439 |
} |
| 440 |
printf(" -------------------\n"); |
| 441 |
printf(" goodtol: %19.16f\n\n", goodtol); |
| 442 |
debug_time += lanc_seconds() - time; |
| 443 |
} |
| 444 |
|
| 445 |
/* Check for convergence. */ |
| 446 |
time = lanc_seconds(); |
| 447 |
if (LANCZOS_CONVERGENCE_MODE != 1 || d > 1) { |
| 448 |
/* check convergence of residual bound */ |
| 449 |
converged = TRUE; |
| 450 |
if (j < d) { |
| 451 |
converged = FALSE; |
| 452 |
} |
| 453 |
else { |
| 454 |
curlnk = scanlist; |
| 455 |
while (curlnk != NULL) { |
| 456 |
if (bj[curlnk->indx] > bji_tol) { |
| 457 |
converged = FALSE; |
| 458 |
} |
| 459 |
curlnk = curlnk->pntr; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
if (LANCZOS_CONVERGENCE_MODE == 1 && d == 1) { |
| 464 |
/* check change in partition */ |
| 465 |
if (firstpause) { |
| 466 |
converged = TRUE; |
| 467 |
if (j < d) { |
| 468 |
converged = FALSE; |
| 469 |
} |
| 470 |
else { |
| 471 |
curlnk = scanlist; |
| 472 |
while (curlnk != NULL) { |
| 473 |
if (bj[curlnk->indx] > bji_tol) { |
| 474 |
converged = FALSE; |
| 475 |
} |
| 476 |
curlnk = curlnk->pntr; |
| 477 |
} |
| 478 |
} |
| 479 |
if (!converged) { |
| 480 |
/* compute current approx. to eigenvectors */ |
| 481 |
mkeigvecs(scanlist, lambda, bound, index, bj, d, &Sres_max, alpha, beta, j, s, y, n, q); |
| 482 |
|
| 483 |
/* compute first assignment */ |
| 484 |
assign(A, y, n, d, cube_or_mesh, nsets, vwsqrt, assignment, active, mediantype, goal, |
| 485 |
vwgt_max); |
| 486 |
} |
| 487 |
} |
| 488 |
else { |
| 489 |
/* copy assignment to old_assignment */ |
| 490 |
assgn_pntr = assignment; |
| 491 |
old_assgn_pntr = old_assignment; |
| 492 |
for (i = n + 1; i; i--) { |
| 493 |
*old_assgn_pntr++ = *assgn_pntr++; |
| 494 |
} |
| 495 |
|
| 496 |
/* compute current approx. to eigenvectors */ |
| 497 |
mkeigvecs(scanlist, lambda, bound, index, bj, d, &Sres_max, alpha, beta, j, s, y, n, q); |
| 498 |
|
| 499 |
/* write new assignment */ |
| 500 |
assign(A, y, n, d, cube_or_mesh, nsets, vwsqrt, assignment, active, mediantype, goal, |
| 501 |
vwgt_max); |
| 502 |
|
| 503 |
assigndiff = 0; |
| 504 |
assgn_pntr = assignment; |
| 505 |
old_assgn_pntr = old_assignment; |
| 506 |
for (i = n + 1; i; i--) { |
| 507 |
if (*old_assgn_pntr++ != *assgn_pntr++) { |
| 508 |
assigndiff++; |
| 509 |
} |
| 510 |
} |
| 511 |
assigndiff = min(assigndiff, n - assigndiff); |
| 512 |
if (DEBUG_EVECS > 1) { |
| 513 |
printf(" j %d, change from last assignment %d\n\n", j, assigndiff); |
| 514 |
} |
| 515 |
|
| 516 |
if (assigndiff <= assigntol) { |
| 517 |
converged = TRUE; |
| 518 |
} |
| 519 |
else { |
| 520 |
converged = FALSE; |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
scan_time += lanc_seconds() - time; |
| 525 |
|
| 526 |
/* Show current estimates of evals and bounds (for help in tuning) */ |
| 527 |
if (DEBUG_EVECS > 2 && !converged) { |
| 528 |
time = lanc_seconds(); |
| 529 |
|
| 530 |
/* compute current approx. to eigenvectors */ |
| 531 |
mkeigvecs(scanlist, lambda, bound, index, bj, d, &Sres_max, alpha, beta, j, s, y, n, q); |
| 532 |
|
| 533 |
/* Compute residuals and display associated info. */ |
| 534 |
printf("j %4d; lambda Ares est. Ares index\n", j); |
| 535 |
for (i = 1; i <= d; i++) { |
| 536 |
Ares[i] = checkeig(workn, A, y[i], n, lambda[i], vwsqrt, u); |
| 537 |
printf("%2d.", i); |
| 538 |
doubleout(lambda[i], 1); |
| 539 |
doubleout(bound[i], 1); |
| 540 |
doubleout(Ares[i], 1); |
| 541 |
printf(" %3d\n", index[i]); |
| 542 |
} |
| 543 |
printf("\n"); |
| 544 |
debug_time += lanc_seconds() - time; |
| 545 |
} |
| 546 |
|
| 547 |
if (!converged) { |
| 548 |
ngood = 0; |
| 549 |
left_ngood = 0; /* for setting left_goodlim on next loop */ |
| 550 |
right_ngood = 0; /* for setting right_goodlim on next loop */ |
| 551 |
|
| 552 |
/* Compute converged Ritz pairs on left end */ |
| 553 |
time = lanc_seconds(); |
| 554 |
for (i = 1; i <= left_goodlim; i++) { |
| 555 |
if (bj[i] <= goodtol) { |
| 556 |
ngood += 1; |
| 557 |
left_ngood += 1; |
| 558 |
if (ngood > maxngood) { |
| 559 |
maxngood = ngood; |
| 560 |
solist[ngood] = makeorthlnk(); |
| 561 |
(solist[ngood])->vec = mkvec(1, n); |
| 562 |
} |
| 563 |
(solist[ngood])->index = i; |
| 564 |
Sres = Tevec(alpha, beta - 1, j, ritz[i], s); |
| 565 |
if (Sres > Sres_max) { |
| 566 |
Sres_max = Sres; |
| 567 |
} |
| 568 |
if (Sres > SRESTOL) { |
| 569 |
inc_bis_safety = TRUE; |
| 570 |
} |
| 571 |
setvec((solist[ngood])->vec, 1, n, 0.0); |
| 572 |
for (k = 1; k <= j; k++) { |
| 573 |
scadd((solist[ngood])->vec, 1, n, s[k], q[k]); |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
/* Compute converged Ritz pairs on right end */ |
| 579 |
for (i = j; i > j - right_goodlim; i--) { |
| 580 |
if (bj[i] <= goodtol) { |
| 581 |
ngood += 1; |
| 582 |
right_ngood += 1; |
| 583 |
if (ngood > maxngood) { |
| 584 |
maxngood = ngood; |
| 585 |
solist[ngood] = makeorthlnk(); |
| 586 |
(solist[ngood])->vec = mkvec(1, n); |
| 587 |
} |
| 588 |
(solist[ngood])->index = i; |
| 589 |
Sres = Tevec(alpha, beta - 1, j, ritz[i], s); |
| 590 |
if (Sres > Sres_max) { |
| 591 |
Sres_max = Sres; |
| 592 |
} |
| 593 |
if (Sres > SRESTOL) { |
| 594 |
inc_bis_safety = TRUE; |
| 595 |
} |
| 596 |
setvec((solist[ngood])->vec, 1, n, 0.0); |
| 597 |
for (k = 1; k <= j; k++) { |
| 598 |
scadd((solist[ngood])->vec, 1, n, s[k], q[k]); |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
ritz_time += lanc_seconds() - time; |
| 603 |
|
| 604 |
if (DEBUG_EVECS > 2) { |
| 605 |
time = lanc_seconds(); |
| 606 |
printf(" j %3d; goodlim lft %2d, rgt %2d; list ", j, left_goodlim, right_goodlim); |
| 607 |
solistout(solist, ngood, j); |
| 608 |
printf("---------------------end of iteration---------------------\n\n"); |
| 609 |
debug_time += lanc_seconds() - time; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
j++; |
| 614 |
} |
| 615 |
j--; |
| 616 |
|
| 617 |
/* Collect eigenvalue and bound information. */ |
| 618 |
time = lanc_seconds(); |
| 619 |
mkeigvecs(scanlist, lambda, bound, index, bj, d, &Sres_max, alpha, beta, j, s, y, n, q); |
| 620 |
evec_time += lanc_seconds() - time; |
| 621 |
|
| 622 |
/* Analyze computation for and report additional problems */ |
| 623 |
time = lanc_seconds(); |
| 624 |
warnings(workn, A, y, n, lambda, vwsqrt, Ares, bound, index, d, j, maxj, Sres_max, eigtol, u, |
| 625 |
Anorm, Output_File); |
| 626 |
debug_time += lanc_seconds() - time; |
| 627 |
|
| 628 |
/* free up memory */ |
| 629 |
time = lanc_seconds(); |
| 630 |
frvec(u, 1); |
| 631 |
frvec(r, 1); |
| 632 |
frvec(workn, 1); |
| 633 |
frvec(Ares, 0); |
| 634 |
sfree(index); |
| 635 |
frvec(alpha, 1); |
| 636 |
frvec(beta, 0); |
| 637 |
frvec(ritz, 1); |
| 638 |
frvec(s, 1); |
| 639 |
frvec(bj, 1); |
| 640 |
frvec(workj, 0); |
| 641 |
for (i = 0; i <= j; i++) { |
| 642 |
frvec(q[i], 1); |
| 643 |
} |
| 644 |
sfree(q); |
| 645 |
while (scanlist != NULL) { |
| 646 |
curlnk = scanlist->pntr; |
| 647 |
sfree(scanlist); |
| 648 |
scanlist = curlnk; |
| 649 |
} |
| 650 |
for (i = 1; i <= maxngood; i++) { |
| 651 |
frvec((solist[i])->vec, 1); |
| 652 |
sfree(solist[i]); |
| 653 |
} |
| 654 |
sfree(solist); |
| 655 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 656 |
sfree(old_assignment); |
| 657 |
} |
| 658 |
init_time += lanc_seconds() - time; |
| 659 |
} |