| 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 "params.h" |
| 11 |
#include "smalloc.h" |
| 12 |
#include "structs.h" |
| 13 |
#include <math.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
/* Invoke the eigenvector calculation */ |
| 17 |
void eigensolve(struct vtx_data **graph, /* graph data structure */ |
| 18 |
int nvtxs, /* number of vertices in graph */ |
| 19 |
int nedges, /* number of edges in graph */ |
| 20 |
double maxdeg, /* largest (weighted) degree of a vertex */ |
| 21 |
int vwgt_max, /* largest vertex weight */ |
| 22 |
double *vwsqrt, /* sqrt of vertex weights (length nvtxs+1) */ |
| 23 |
int using_vwgts, /* are vertex weights being used? */ |
| 24 |
int using_ewgts, /* are edge weights being used? */ |
| 25 |
float *term_wgts[], /* terminal propagation weight vector */ |
| 26 |
int igeom, /* geometric dimensionality if given coords */ |
| 27 |
float **coords, /* coordinates of vertices */ |
| 28 |
double **yvecs, /* space for pointing to eigenvectors */ |
| 29 |
double *evals, /* eigenvalues associated with eigenvectors */ |
| 30 |
int architecture, /* 0 => hypercube, d => d-dimensional mesh */ |
| 31 |
int *assignment, /* set number of each vtx (length n+1) */ |
| 32 |
double *goal, /* desired set sizes */ |
| 33 |
int solver_flag, /* flag indicating which solver to use */ |
| 34 |
int rqi_flag, /* use multi-level techniques? */ |
| 35 |
int vmax, /* if so, how many vtxs to coarsen down to? */ |
| 36 |
int ndims, /* number of eigenvectors (2^d sets) */ |
| 37 |
int mediantype, /* which partitioning strategy to use */ |
| 38 |
double eigtol /* tolerance on eigenvectors */ |
| 39 |
) |
| 40 |
{ |
| 41 |
extern int DEBUG_TRACE; /* trace the execution of the code */ |
| 42 |
extern int DEBUG_EVECS; /* debug flag for eigenvector generation */ |
| 43 |
extern int DEBUG_PERTURB; /* debug flag for matrix perturbation */ |
| 44 |
extern int PERTURB; /* randomly perturb to break symmetry? */ |
| 45 |
extern int NPERTURB; /* number of edges to perturb */ |
| 46 |
extern double PERTURB_MAX; /* maximum size of perturbation */ |
| 47 |
extern int COARSE_NLEVEL_RQI; /* do RQI this often in uncoarsening */ |
| 48 |
extern int LANCZOS_CONVERGENCE_MODE; /* how to stop Lanczos? */ |
| 49 |
extern double lanczos_time; /* time spent in Lanczos algorithm */ |
| 50 |
extern double rqi_symmlq_time; /* time spent in RQI/Symmlq method */ |
| 51 |
extern int WARNING_EVECS; /* print warning messages? */ |
| 52 |
extern int LANCZOS_SO_PRECISION; /* controls precision in eigen calc. */ |
| 53 |
extern double SRESTOL; /* resid tol for T evec computation */ |
| 54 |
extern int LANCZOS_MAXITNS; /* maximum Lanczos iterations allowed */ |
| 55 |
extern int EXPERT; /* user type */ |
| 56 |
double bound[MAXDIMS + 1]; /* ritz approx bounds to eigenpairs */ |
| 57 |
double time; /* time marker */ |
| 58 |
float *dummy_twgt[2]; /* turns off terminal propagation */ |
| 59 |
float *twptr; /* terminal propagation weight vector */ |
| 60 |
int *active; /* space for nvtxs values */ |
| 61 |
int step; /* current step in RQI counting */ |
| 62 |
int nstep; /* number of uncoarsening levels between RQIs */ |
| 63 |
int version; /* which version of sel. orth. to use */ |
| 64 |
int nsets = 0; /* number of sets to divide into */ |
| 65 |
double *g; /* rhs n-vector in the extended eigenproblem */ |
| 66 |
double *ptr; /* loops through yvec */ |
| 67 |
double w1, w2; /* desired weights of two sets */ |
| 68 |
double term_tot; /* sum of terminal weights */ |
| 69 |
double sigma; /* norm constraint on extended eigenvector */ |
| 70 |
int i, j; /* loop counter */ |
| 71 |
int normal; /* use normal or extended eigensolver? */ |
| 72 |
int autoset_maxitns; /* set LANCZOS_MAXITNS automatically? */ |
| 73 |
int prev_maxitns = 0; /* LANCZOS_MAXITNS value above this routine */ |
| 74 |
int autoset_srestol; /* set SRESTOL automatically? */ |
| 75 |
double prev_srestol = 0; /* SRESTOL value above this routine */ |
| 76 |
|
| 77 |
if (DEBUG_TRACE > 0) { |
| 78 |
printf("<Entering eigensolve, nvtxs = %d, nedges = %d>\n", nvtxs, nedges); |
| 79 |
} |
| 80 |
|
| 81 |
if (nvtxs <= ndims) { /* Pathological special case. */ |
| 82 |
for (i = 1; i <= ndims; i++) { |
| 83 |
for (j = 1; j <= nvtxs; j++) { |
| 84 |
yvecs[i][j] = 0; |
| 85 |
} |
| 86 |
} |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
active = NULL; |
| 91 |
|
| 92 |
/* Autoset (if necessary) some parameters for the eigen calculation */ |
| 93 |
autoset_maxitns = FALSE; |
| 94 |
autoset_srestol = FALSE; |
| 95 |
if (LANCZOS_MAXITNS < 0) { |
| 96 |
autoset_maxitns = TRUE; |
| 97 |
prev_maxitns = LANCZOS_MAXITNS; |
| 98 |
LANCZOS_MAXITNS = 2 * nvtxs; |
| 99 |
} |
| 100 |
if (SRESTOL < 0) { |
| 101 |
autoset_srestol = TRUE; |
| 102 |
prev_srestol = SRESTOL; |
| 103 |
SRESTOL = eigtol * eigtol; |
| 104 |
} |
| 105 |
|
| 106 |
/* Note: When (if ever) rqi_ext is done, change precedence of eigensolvers. */ |
| 107 |
|
| 108 |
if (term_wgts[1] != NULL && ndims == 1) { /* then use lanczos_ext */ |
| 109 |
if (PERTURB) { |
| 110 |
if (NPERTURB > 0 && PERTURB_MAX > 0.0) { |
| 111 |
perturb_init(nvtxs); |
| 112 |
if (DEBUG_PERTURB > 0) { |
| 113 |
printf("Matrix being perturbed with scale %e\n", PERTURB_MAX); |
| 114 |
} |
| 115 |
} |
| 116 |
else if (DEBUG_PERTURB > 0) { |
| 117 |
printf("Matrix not being perturbed\n"); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
version = 2; |
| 122 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 123 |
active = smalloc(nvtxs * sizeof(int)); |
| 124 |
} |
| 125 |
|
| 126 |
w1 = goal[0]; |
| 127 |
w2 = goal[1]; |
| 128 |
sigma = sqrt(4 * w1 * w2 / (w1 + w2)); |
| 129 |
g = smalloc((nvtxs + 1) * sizeof(double)); |
| 130 |
|
| 131 |
twptr = term_wgts[1]; |
| 132 |
term_tot = 0; |
| 133 |
for (i = 1; i <= nvtxs; i++) { |
| 134 |
term_tot += twptr[i]; |
| 135 |
} |
| 136 |
term_tot /= (w1 + w2); |
| 137 |
if (using_vwgts) { |
| 138 |
for (i = 1; i <= nvtxs; i++) { |
| 139 |
g[i] = twptr[i] / graph[i]->vwgt - term_tot; |
| 140 |
} |
| 141 |
} |
| 142 |
else { |
| 143 |
for (i = 1; i <= nvtxs; i++) { |
| 144 |
g[i] = twptr[i] - term_tot; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
time = seconds(); |
| 149 |
|
| 150 |
if (LANCZOS_SO_PRECISION == 2) { /* double precision */ |
| 151 |
normal = lanczos_ext(graph, nvtxs, ndims, yvecs, eigtol, vwsqrt, maxdeg, version, g, sigma); |
| 152 |
} |
| 153 |
else { /* single precision */ |
| 154 |
normal = |
| 155 |
lanczos_ext_float(graph, nvtxs, ndims, yvecs, eigtol, vwsqrt, maxdeg, version, g, sigma); |
| 156 |
} |
| 157 |
|
| 158 |
sfree(g); |
| 159 |
if (active != NULL) { |
| 160 |
sfree(active); |
| 161 |
} |
| 162 |
active = NULL; |
| 163 |
|
| 164 |
if (normal) { |
| 165 |
if (WARNING_EVECS > 2) { |
| 166 |
strout("WARNING: Not an extended eigenproblem; switching to standard eigensolver.\n"); |
| 167 |
} |
| 168 |
} |
| 169 |
else { |
| 170 |
if (w2 != w1) { |
| 171 |
if (using_vwgts) { |
| 172 |
y2x(yvecs, ndims, nvtxs, vwsqrt); |
| 173 |
} |
| 174 |
sigma = (w2 - w1) / (w2 + w1); |
| 175 |
ptr = yvecs[1]; |
| 176 |
for (i = nvtxs; i; i--) { |
| 177 |
*(++ptr) += sigma; |
| 178 |
} |
| 179 |
/* Note: if assign() could skip scaling, next call unnecessary. */ |
| 180 |
if (using_vwgts) { |
| 181 |
x2y(yvecs, ndims, nvtxs, vwsqrt); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) { |
| 187 |
perturb_clear(); |
| 188 |
} |
| 189 |
|
| 190 |
lanczos_time += seconds() - time; |
| 191 |
} |
| 192 |
else { |
| 193 |
normal = TRUE; |
| 194 |
} |
| 195 |
|
| 196 |
if (normal) { |
| 197 |
if (rqi_flag) { |
| 198 |
/* Solve using multi-level scheme RQI/Symmlq. */ |
| 199 |
time = seconds(); |
| 200 |
nstep = COARSE_NLEVEL_RQI; |
| 201 |
step = 0; |
| 202 |
dummy_twgt[1] = NULL; |
| 203 |
coarsen(graph, nvtxs, nedges, using_vwgts, using_ewgts, dummy_twgt, igeom, coords, yvecs, |
| 204 |
ndims, solver_flag, vmax, eigtol, nstep, step, FALSE); |
| 205 |
|
| 206 |
rqi_symmlq_time += seconds() - time; |
| 207 |
} |
| 208 |
|
| 209 |
else { /* Use standard Lanczos. */ |
| 210 |
if (PERTURB) { |
| 211 |
if (NPERTURB > 0 && PERTURB_MAX > 0.0) { |
| 212 |
perturb_init(nvtxs); |
| 213 |
if (DEBUG_PERTURB > 0) { |
| 214 |
printf("Matrix being perturbed with scale %e\n", PERTURB_MAX); |
| 215 |
} |
| 216 |
} |
| 217 |
else if (DEBUG_PERTURB > 0) { |
| 218 |
printf("Matrix not being perturbed\n"); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (solver_flag == 1) { |
| 223 |
time = seconds(); |
| 224 |
version = 1; |
| 225 |
lanczos_FO(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, version); |
| 226 |
lanczos_time += seconds() - time; |
| 227 |
} |
| 228 |
if (solver_flag == 2) { |
| 229 |
time = seconds(); |
| 230 |
version = 2; |
| 231 |
lanczos_FO(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, version); |
| 232 |
lanczos_time += seconds() - time; |
| 233 |
} |
| 234 |
else if (solver_flag == 3) { |
| 235 |
version = 2; /* orthog. against left end only */ |
| 236 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 237 |
active = smalloc(nvtxs * sizeof(int)); |
| 238 |
} |
| 239 |
nsets = 1 << ndims; |
| 240 |
time = seconds(); |
| 241 |
if (LANCZOS_SO_PRECISION == 2) { /* double precision */ |
| 242 |
lanczos_SO(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, version, |
| 243 |
architecture, nsets, assignment, active, mediantype, goal, vwgt_max); |
| 244 |
} |
| 245 |
else { /* single precision */ |
| 246 |
lanczos_SO_float(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, |
| 247 |
version, architecture, nsets, assignment, active, mediantype, goal, |
| 248 |
vwgt_max); |
| 249 |
} |
| 250 |
lanczos_time += seconds() - time; |
| 251 |
} |
| 252 |
else if (solver_flag == 4) { |
| 253 |
if (EXPERT) { |
| 254 |
version = 1; /* orthog. against both ends */ |
| 255 |
} |
| 256 |
else { |
| 257 |
/* this should have been caught earlier ... */ |
| 258 |
version = 2; |
| 259 |
} |
| 260 |
if (LANCZOS_CONVERGENCE_MODE == 1) { |
| 261 |
active = smalloc(nvtxs * sizeof(int)); |
| 262 |
} |
| 263 |
nsets = 1 << ndims; |
| 264 |
time = seconds(); |
| 265 |
if (LANCZOS_SO_PRECISION == 1) { /* Single precision */ |
| 266 |
lanczos_SO_float(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, |
| 267 |
version, architecture, nsets, assignment, active, mediantype, goal, |
| 268 |
vwgt_max); |
| 269 |
} |
| 270 |
else { /* Double precision */ |
| 271 |
lanczos_SO(graph, nvtxs, ndims, yvecs, evals, bound, eigtol, vwsqrt, maxdeg, version, |
| 272 |
architecture, nsets, assignment, active, mediantype, goal, vwgt_max); |
| 273 |
} |
| 274 |
lanczos_time += seconds() - time; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/* |
| 279 |
file = fopen("CHACO.EVECS", "w"); |
| 280 |
for (i = 1; i <= nvtxs; i++) { |
| 281 |
for (j = 1; j <= ndims; j++) { |
| 282 |
fprintf(file, "%g ", (yvecs[j])[i]); |
| 283 |
} |
| 284 |
fprintf(file, "\n"); |
| 285 |
} |
| 286 |
fclose(file); |
| 287 |
*/ |
| 288 |
|
| 289 |
if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) { |
| 290 |
perturb_clear(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/* This is an attempt to reduce some machine-to-machine |
| 295 |
* variance. If the first value in the eigenvector is negative, |
| 296 |
* reflect the eigenvector... This may not be needed following |
| 297 |
* the addition of the standard random number generator in util/random.c |
| 298 |
*/ |
| 299 |
for (nstep = 1; nstep <= ndims; nstep++) { |
| 300 |
vecnorm(yvecs[nstep], 1, nvtxs); |
| 301 |
} |
| 302 |
|
| 303 |
if (DEBUG_EVECS > 4) { |
| 304 |
for (nstep = 1; nstep <= ndims; nstep++) { |
| 305 |
vecout(yvecs[nstep], 1, nvtxs, "Eigenvector", NULL); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* Auto-reset (if necessary) some parameters for the eigen calculation */ |
| 310 |
if (autoset_maxitns) { |
| 311 |
LANCZOS_MAXITNS = prev_maxitns; |
| 312 |
} |
| 313 |
if (autoset_srestol) { |
| 314 |
SRESTOL = prev_srestol; |
| 315 |
} |
| 316 |
|
| 317 |
if (active != NULL) { |
| 318 |
sfree(active); |
| 319 |
} |
| 320 |
|
| 321 |
if (DEBUG_TRACE > 1) { |
| 322 |
printf("<Leaving eigensolve>\n"); |
| 323 |
} |
| 324 |
} |