Debian Codegraph @

packages/seacas/libraries/chaco/klspiff/coarsen_kl.c

raw
1 /*
2 * Copyright(C) 1999-2020, 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 <stdio.h>
14
15 int LIMIT_KL_EWGTS = FALSE; /* limit range of edge weights in multilevel-KL? */
16 double EWGT_RATIO_MAX = .25; /* if so, max allowed ewgt/nvtxs */
17
18 void coarsen_kl(
19 /* Coarsen until nvtxs < vmax, compute and uncoarsen. */
20 struct vtx_data **graph, /* array of vtx data for graph */
21 int nvtxs, /* number of vertices in graph */
22 int nedges, /* number of edges in graph */
23 int using_vwgts, /* are vertices weights being used? */
24 int using_ewgts, /* are edge weights being used? */
25 float *term_wgts[], /* weights for terminal propagation */
26 int igeom, /* dimension for geometric information */
27 float **coords, /* coordinates for vertices */
28 int vwgt_max, /* largest vertex weight */
29 int *assignment, /* processor each vertex gets assigned to */
30 double *goal, /* desired set sizes */
31 int architecture, /* 0 => hypercube, d => d-dimensional mesh */
32 int (*hops)[MAXSETS], /* cost of edge between sets */
33 int solver_flag, /* which eigensolver to use */
34 int ndims, /* number of eigenvectors to calculate */
35 int nsets, /* number of sets being divided into */
36 int vmax, /* largest subgraph to stop coarsening */
37 int mediantype, /* flag for different assignment strategies */
38 int mkconnected, /* make graph connected before eigensolver? */
39 double eigtol, /* tolerance in eigen calculation */
40 int nstep, /* number of coarsenings between RQI steps */
41 int step, /* current step number */
42 int **pbndy_list, /* pointer to returned boundary list */
43 double *weights, /* weights of vertices in each set */
44 int give_up /* has coarsening bogged down? */
45 )
46 {
47 extern FILE *Output_File; /* output file or null */
48 extern int DEBUG_TRACE; /* trace the execution of the code */
49 extern int DEBUG_COARSEN; /* debug flag for coarsening */
50 extern int DEBUG_CONNECTED; /* debug flag for connectivity checking */
51 extern double COARSEN_RATIO_MIN; /* vtx reduction demanded */
52 extern int COARSEN_VWGTS; /* use vertex weights while coarsening? */
53 extern int COARSEN_EWGTS; /* use edge weights while coarsening? */
54 extern int COARSE_KL_BOTTOM; /* force KL invocation at bottom level? */
55 extern int KL_ONLY_BNDY; /* only initialize boundary vertices? */
56 extern double KL_IMBALANCE; /* fractional imbalance allowed in KL */
57 extern int MAPPING_TYPE; /* how to get from eigenvectors to partition */
58 struct connect_data *cdata; /* data structure for enforcing connectivity */
59 struct vtx_data **cgraph; /* array of vtx data for coarsened graph */
60 double *yvecs[MAXDIMS + 1]; /* eigenvectors for subgraph */
61 double evals[MAXDIMS + 1]; /* eigenvalues returned */
62 double new_goal[MAXSETS]; /* new goal if not using vertex weights */
63 double *real_goal; /* chooses between goal and new_goal */
64 double goal_weight; /* total weight of vertices in goal */
65 double *vwsqrt = NULL; /* square root of vertex weights */
66 double maxdeg; /* maximum weighted degree of a vertex */
67 double temp_goal[2]; /* goal to simulate bisection while striping */
68 double *fake_goal; /* either goal or temp_goal */
69 float *cterm_wgts[MAXSETS]; /* terminal weights for coarse graph */
70 float *new_term_wgts[MAXSETS]; /* modified for Bui's method */
71 float **real_term_wgts; /* which of previous two to use */
72 float ewgt_max; /* largest edge weight in graph */
73 float *twptr; /* loops through term_wgts */
74 float *twptr_save; /* copy of twptr */
75 float *ctwptr; /* loops through cterm_wgts */
76 float **ccoords; /* coarse graph coordinates */
77 int *active; /* space for assign routine */
78 int *v2cv; /* mapping from fine to coarse vertices */
79 int *cv2v; /* mapping from coarse to fine vertices */
80 int *bndy_list; /* list of vertices on boundary */
81 int *cbndy_list; /* list of vertices of coarse graph on boundary */
82 int *mflag; /* flags indicating matching */
83 int *cassignment; /* set assignments for coarsened vertices */
84 int clist_length; /* length of coarse graph boundary vtx list */
85 int list_length; /* length of boundary vtx list */
86 int vtx; /* vertex in graph */
87 int cvtx; /* vertex in coarse graph */
88 int cnvtxs; /* number of vertices in coarsened graph */
89 int cnedges; /* number of edges in coarsened graph */
90 int cvwgt_max; /* largest vertex weight in coarsened graph */
91 int nextstep; /* next step in RQI test */
92 int max_dev; /* largest allowed deviation from balance */
93 int set; /* set a vertex is in */
94 int i, j; /* loop counters */
95
96 if (DEBUG_COARSEN > 0 || DEBUG_TRACE > 0) {
97 printf("<Entering coarsen_kl, step=%d, nvtxs=%d, nedges=%d, vmax=%d>\n", step, nvtxs, nedges,
98 vmax);
99 }
100
101 /* Is problem small enough to solve? */
102 if (nvtxs <= vmax || give_up) {
103 if (using_vwgts) {
104 vwsqrt = smalloc((nvtxs + 1) * sizeof(double));
105 makevwsqrt(vwsqrt, graph, nvtxs);
106 }
107 else {
108 vwsqrt = NULL;
109 }
110
111 /* Create space for subgraph yvecs. */
112 for (i = 1; i <= ndims; i++) {
113 yvecs[i] = smalloc((nvtxs + 1) * sizeof(double));
114 }
115
116 active = smalloc(nvtxs * sizeof(int));
117 if (mkconnected) {
118 make_connected(graph, nvtxs, &nedges, (int *)&(yvecs[1][0]), active, &cdata, using_ewgts);
119 if (DEBUG_CONNECTED > 0) {
120 printf("Enforcing connectivity on coarse graph\n");
121 print_connected(cdata);
122 }
123 }
124
125 /* If not coarsening ewgts, then need care with term_wgts. */
126 if (!using_ewgts && term_wgts[1] != NULL && step != 0) {
127 twptr = smalloc((nvtxs + 1) * (nsets - 1) * sizeof(float));
128 twptr_save = twptr;
129 for (j = 1; j < nsets; j++) {
130 new_term_wgts[j] = twptr;
131 twptr += nvtxs + 1;
132 }
133
134 for (j = 1; j < nsets; j++) {
135 twptr = term_wgts[j];
136 ctwptr = new_term_wgts[j];
137 for (i = 1; i <= nvtxs; i++) {
138 if (twptr[i] > .5) {
139 ctwptr[i] = 1;
140 }
141 else if (twptr[i] < -.5) {
142 ctwptr[i] = -1;
143 }
144 else {
145 ctwptr[i] = 0;
146 }
147 }
148 }
149 real_term_wgts = new_term_wgts;
150 }
151 else {
152 real_term_wgts = term_wgts;
153 new_term_wgts[1] = NULL;
154 }
155
156 if (!COARSEN_VWGTS && step != 0) { /* Construct new goal */
157 goal_weight = 0;
158 for (i = 0; i < nsets; i++) {
159 goal_weight += goal[i];
160 }
161 for (i = 0; i < nsets; i++) {
162 new_goal[i] = goal[i] * (nvtxs / goal_weight);
163 }
164 real_goal = new_goal;
165 }
166 else {
167 real_goal = goal;
168 }
169
170 if (ndims == 1 && nsets > 2) { /* Striping. */
171 mediantype = 4;
172 temp_goal[0] = temp_goal[1] = 0;
173 for (i = 0; 2 * i + 1 < nsets; i++) {
174 temp_goal[0] += real_goal[i];
175 temp_goal[1] += real_goal[nsets - 1 - i];
176 }
177 i = nsets / 2;
178 if (2 * i != nsets) {
179 temp_goal[0] += .5 * real_goal[i];
180 temp_goal[1] += .5 * real_goal[i];
181 }
182 fake_goal = temp_goal;
183 }
184 else {
185 mediantype = MAPPING_TYPE;
186 fake_goal = real_goal;
187 }
188
189 /* Partition coarse graph with spectral method */
190 maxdeg = find_maxdeg(graph, nvtxs, using_ewgts, &ewgt_max);
191 eigensolve(graph, nvtxs, nedges, maxdeg, vwgt_max, vwsqrt, using_vwgts, using_ewgts,
192 real_term_wgts, 0, (float **)NULL, yvecs, evals, architecture, assignment, fake_goal,
193 solver_flag, FALSE, 0, ndims, mediantype, eigtol);
194
195 if (mkconnected) {
196 make_unconnected(graph, &nedges, &cdata, using_ewgts);
197 }
198
199 assign(graph, yvecs, nvtxs, ndims, architecture, nsets, vwsqrt, assignment, active, mediantype,
200 real_goal, vwgt_max);
201 /*
202 simple_part(graph, nvtxs, assignment, nsets, 1, real_goal);
203 */
204 sfree(active);
205
206 count_weights(graph, nvtxs, assignment, nsets, weights, (vwgt_max != 1));
207
208 bndy_list = NULL;
209 if (COARSE_KL_BOTTOM || !(step % nstep)) {
210 if (LIMIT_KL_EWGTS) {
211 compress_ewgts(graph, nvtxs, nedges, ewgt_max, using_ewgts);
212 }
213
214 max_dev = (step == 0) ? vwgt_max : 5 * vwgt_max;
215 goal_weight = 0;
216 for (i = 0; i < nsets; i++) {
217 goal_weight += real_goal[i];
218 }
219 goal_weight *= KL_IMBALANCE / nsets;
220 if (goal_weight > max_dev) {
221 max_dev = goal_weight;
222 }
223
224 if (KL_ONLY_BNDY) {
225 /* On small graph, quickest to include everybody in list. */
226 bndy_list = smalloc((nvtxs + 1) * sizeof(int));
227 for (i = 0; i < nvtxs; i++) {
228 bndy_list[i] = i + 1;
229 }
230 bndy_list[nvtxs] = 0;
231 }
232 klspiff(graph, nvtxs, assignment, nsets, hops, real_goal, real_term_wgts, max_dev, maxdeg,
233 using_ewgts, &bndy_list, weights);
234 if (LIMIT_KL_EWGTS) {
235 restore_ewgts(graph, nvtxs);
236 }
237 }
238
239 else if (KL_ONLY_BNDY) { /* Find boundary vertices directly. */
240 bndy_list = smalloc((nvtxs + 1) * sizeof(int));
241 list_length = 0;
242 for (i = 1; i <= nvtxs; i++) {
243 set = assignment[i];
244 for (j = 1; j < graph[i]->nedges; j++) {
245 if (assignment[graph[i]->edges[j]] != set) {
246 bndy_list[list_length++] = i;
247 break;
248 }
249 }
250 }
251 bndy_list[list_length] = 0;
252 bndy_list = srealloc(bndy_list, (list_length + 1) * sizeof(int));
253 }
254 *pbndy_list = bndy_list;
255
256 if (real_term_wgts != term_wgts && new_term_wgts[1] != NULL) {
257 sfree(real_term_wgts[1]);
258 }
259 if (vwsqrt != NULL) {
260 sfree(vwsqrt);
261 }
262 for (i = ndims; i > 0; i--) {
263 sfree(yvecs[i]);
264 }
265 return;
266 }
267
268 /* Otherwise I have to coarsen. */
269 if (coords != NULL) {
270 ccoords = smalloc(igeom * sizeof(float *));
271 }
272 else {
273 ccoords = NULL;
274 }
275 coarsen1(graph, nvtxs, nedges, &cgraph, &cnvtxs, &cnedges, &v2cv, igeom, coords, ccoords,
276 using_ewgts);
277
278 if (term_wgts[1] != NULL) {
279 twptr = smalloc((cnvtxs + 1) * (nsets - 1) * sizeof(float));
280 twptr_save = twptr;
281 for (i = (cnvtxs + 1) * (nsets - 1); i; i--) {
282 *twptr++ = 0;
283 }
284 twptr = twptr_save;
285 for (j = 1; j < nsets; j++) {
286 cterm_wgts[j] = twptr;
287 twptr += cnvtxs + 1;
288 }
289 for (j = 1; j < nsets; j++) {
290 ctwptr = cterm_wgts[j];
291 twptr = term_wgts[j];
292 for (i = 1; i < nvtxs; i++) {
293 ctwptr[v2cv[i]] += twptr[i];
294 }
295 }
296 }
297 else {
298 cterm_wgts[1] = NULL;
299 }
300
301 /* If coarsening isn't working very well, give up and partition. */
302 give_up = FALSE;
303 if (nvtxs * COARSEN_RATIO_MIN < cnvtxs && cnvtxs > vmax) {
304 printf("WARNING: Coarsening not making enough progress, nvtxs = %d, cnvtxs = %d.\n", nvtxs,
305 cnvtxs);
306 printf(" Recursive coarsening being stopped prematurely.\n");
307 if (Output_File != NULL) {
308 fprintf(Output_File,
309 "WARNING: Coarsening not making enough progress, nvtxs = %d, cnvtxs = %d.\n", nvtxs,
310 cnvtxs);
311 fprintf(Output_File, " Recursive coarsening being stopped prematurely.\n");
312 }
313 give_up = TRUE;
314 }
315
316 /* Now recurse on coarse subgraph. */
317 if (COARSEN_VWGTS) {
318 cvwgt_max = 0;
319 for (i = 1; i <= cnvtxs; i++) {
320 if (cgraph[i]->vwgt > cvwgt_max) {
321 cvwgt_max = cgraph[i]->vwgt;
322 }
323 }
324 }
325 else {
326 cvwgt_max = 1;
327 }
328
329 cassignment = smalloc((cnvtxs + 1) * sizeof(int));
330 nextstep = step + 1;
331 coarsen_kl(cgraph, cnvtxs, cnedges, COARSEN_VWGTS, COARSEN_EWGTS, cterm_wgts, igeom, ccoords,
332 cvwgt_max, cassignment, goal, architecture, hops, solver_flag, ndims, nsets, vmax,
333 mediantype, mkconnected, eigtol, nstep, nextstep, &cbndy_list, weights, give_up);
334
335 /* Interpolate assignment back to fine graph. */
336 for (i = 1; i <= nvtxs; i++) {
337 assignment[i] = cassignment[v2cv[i]];
338 }
339
340 if (KL_ONLY_BNDY) {
341 /* Construct boundary list from coarse boundary list. */
342 cv2v = smalloc((cnvtxs + 1) * sizeof(int));
343 mflag = smalloc((nvtxs + 1) * sizeof(int));
344 for (i = 1; i <= cnvtxs; i++) {
345 cv2v[i] = 0;
346 }
347 for (i = 1; i <= nvtxs; i++) {
348 mflag[i] = 0;
349 cvtx = v2cv[i];
350 if (cv2v[cvtx] == 0) {
351 cv2v[cvtx] = i;
352 }
353 else {
354 mflag[cv2v[cvtx]] = i;
355 }
356 }
357
358 clist_length = 0;
359 while (cbndy_list[clist_length] != 0) {
360 clist_length++;
361 }
362 bndy_list = smalloc((2 * clist_length + 1) * sizeof(int));
363
364 list_length = 0;
365 for (i = 0; i < clist_length; i++) {
366 vtx = cv2v[cbndy_list[i]];
367 bndy_list[list_length++] = vtx;
368 if (mflag[vtx] != 0) {
369 bndy_list[list_length++] = mflag[vtx];
370 }
371 }
372 bndy_list[list_length] = 0;
373 bndy_list = srealloc(bndy_list, (list_length + 1) * sizeof(int));
374
375 sfree(mflag);
376 sfree(cv2v);
377 sfree(cbndy_list);
378 }
379 else {
380 bndy_list = NULL;
381 }
382
383 /* Free the space that was allocated. */
384 sfree(cassignment);
385 if (cterm_wgts[1] != NULL) {
386 sfree(cterm_wgts[1]);
387 }
388 free_graph(cgraph);
389 sfree(v2cv);
390
391 /* Smooth using KL every nstep steps. */
392 if (!(step % nstep)) {
393 if (!COARSEN_VWGTS && step != 0) { /* Construct new goal */
394 goal_weight = 0;
395 for (i = 0; i < nsets; i++) {
396 goal_weight += goal[i];
397 }
398 for (i = 0; i < nsets; i++) {
399 new_goal[i] = goal[i] * (nvtxs / goal_weight);
400 }
401 real_goal = new_goal;
402 }
403 else {
404 real_goal = goal;
405 }
406
407 maxdeg = find_maxdeg(graph, nvtxs, using_ewgts, &ewgt_max);
408 if (LIMIT_KL_EWGTS) {
409 compress_ewgts(graph, nvtxs, nedges, ewgt_max, using_ewgts);
410 }
411
412 /* If not coarsening ewgts, then need care with term_wgts. */
413 if (!using_ewgts && term_wgts[1] != NULL && step != 0) {
414 twptr = smalloc((nvtxs + 1) * (nsets - 1) * sizeof(float));
415 twptr_save = twptr;
416 for (j = 1; j < nsets; j++) {
417 new_term_wgts[j] = twptr;
418 twptr += nvtxs + 1;
419 }
420
421 for (j = 1; j < nsets; j++) {
422 twptr = term_wgts[j];
423 ctwptr = new_term_wgts[j];
424 for (i = 1; i <= nvtxs; i++) {
425 if (twptr[i] > .5) {
426 ctwptr[i] = 1;
427 }
428 else if (twptr[i] < -.5) {
429 ctwptr[i] = -1;
430 }
431 else {
432 ctwptr[i] = 0;
433 }
434 }
435 }
436 real_term_wgts = new_term_wgts;
437 }
438 else {
439 real_term_wgts = term_wgts;
440 new_term_wgts[1] = NULL;
441 }
442
443 max_dev = (step == 0) ? vwgt_max : 5 * vwgt_max;
444 goal_weight = 0;
445 for (i = 0; i < nsets; i++) {
446 goal_weight += real_goal[i];
447 }
448 goal_weight *= KL_IMBALANCE / nsets;
449 if (goal_weight > max_dev) {
450 max_dev = goal_weight;
451 }
452
453 if (!COARSEN_VWGTS) {
454 count_weights(graph, nvtxs, assignment, nsets, weights, (vwgt_max != 1));
455 }
456
457 klspiff(graph, nvtxs, assignment, nsets, hops, real_goal, real_term_wgts, max_dev, maxdeg,
458 using_ewgts, &bndy_list, weights);
459
460 if (real_term_wgts != term_wgts && new_term_wgts[1] != NULL) {
461 sfree(real_term_wgts[1]);
462 }
463
464 if (LIMIT_KL_EWGTS) {
465 restore_ewgts(graph, nvtxs);
466 }
467 }
468 *pbndy_list = bndy_list;
469
470 if (ccoords != NULL) {
471 for (i = 0; i < igeom; i++) {
472 sfree(ccoords[i]);
473 }
474 sfree(ccoords);
475 }
476
477 if (DEBUG_COARSEN > 0) {
478 printf(" Leaving coarsen_kl, step=%d\n", step);
479 }
480 }