Debian Codegraph @

packages/seacas/libraries/chaco/submain/divide.c

raw
1 /*
2 * Copyright(C) 1999-2021, 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 void chaco_divide(struct vtx_data **graph, /* graph data structure */
16 int nvtxs, /* number of vertices in graph */
17 int nedges, /* number of edges in graph */
18 int using_vwgts, /* are vertex weights being used? */
19 int using_ewgts, /* are edge weights being used? */
20 double *vwsqrt, /* sqrt of vertex weights (length nvtxs+1) */
21 int igeom, /* geometry dimension for inertial method */
22 float **coords, /* coordinates for inertial method */
23 int *assignment, /* set number of each vtx (length n) */
24 double *goal, /* desired set sizes */
25 int architecture, /* 0 => hypercube, d => d-dimensional mesh */
26 float *term_wgts[], /* weights for terminal propagation */
27 int global_method, /* global partitioning algorithm */
28 int local_method, /* local partitioning algorithm */
29 int rqi_flag, /* should I use multilevel eigensolver? */
30 int vmax, /* if so, # vertices to coarsen down to */
31 int ndims, /* number of eigenvectors */
32 double eigtol, /* tolerance on eigenvectors */
33 int (*hop_mtx)[MAXSETS], /* between-set hop costs for KL */
34 int nsets, /* number of sets to partition into */
35 int striping /* partition by striping into pieces? */
36 )
37 {
38 extern int DEBUG_TRACE; /* trace main execution path? */
39 extern int DEBUG_CONNECTED; /* debug flag for connected components */
40 extern int DEBUG_KL; /* debug flag for KL variants */
41 extern int COARSE_NLEVEL_KL; /* how often to invoke KL while uncoarsening */
42 extern int MAPPING_TYPE; /* how to map from eigenvectors to partition */
43 extern int LANCZOS_TYPE; /* which Lanczos variant to use */
44 extern int MAKE_CONNECTED; /* force connectivity in spectral method? */
45 extern double KL_IMBALANCE; /* fractional imbalance allowed in KL */
46 extern int VERTEX_SEPARATOR; /* vertex instead of edge separator? */
47 extern int VERTEX_COVER; /* form/improve vtx separator via matching? */
48 extern int COARSE_BPM; /* apply matching/flow while uncoarsening? */
49 struct connect_data *cdata; /* data for enforcing connectivity */
50 double *yvecs[MAXDIMS + 1]; /* space for pointing to eigenvectors */
51 double evals[MAXDIMS + 1]; /* corresponding eigenvalues */
52 double weights[MAXSETS]; /* vertex weight in each set */
53 double maxdeg; /* maximum weighted degree of a vertex */
54 double total_weight; /* weight of all vertices */
55 double temp_goal[2]; /* goal to simulate bisection while striping */
56 double *fake_goal; /* either goal or temp_goal */
57 int *active; /* keeping track of vtxs in BFS (length nvtxs) */
58 int *bndy_list; /* list of boundary vtxs */
59 int *null_ptr; /* argument to klspiff */
60 int *mark; /* for marking vtxs in BFS (length nvtxs+1) */
61 int vwgt_max; /* largest vertex weight */
62 int max_dev; /* largest allowed deviation from balance */
63 int mediantype; /* how to map from eigenvectors to partition */
64 int solver_flag; /* which Lanczos variant to use */
65 int mkconnected; /* enforce connectivity in spectral method? */
66 int i; /* loop counters */
67 int simple_type; /* which type of simple partitioning to use */
68 maxdeg = 0;
69
70 if (DEBUG_TRACE > 0) {
71 printf("<Entering divide, nvtxs = %d, nedges = %d>\n", nvtxs, nedges);
72 }
73
74 if (nvtxs <= 0) {
75 return;
76 }
77 if (nedges == 0 && global_method != 3) {
78 global_method = 4;
79 local_method = 2;
80 }
81
82 for (i = 0; i < MAXSETS; i++) {
83 weights[i] = 0.0;
84 }
85
86 bndy_list = NULL;
87
88 if (striping) {
89 ndims = 1;
90 mediantype = 4;
91
92 temp_goal[0] = temp_goal[1] = 0;
93 for (i = 0; 2 * i + 1 < nsets; i++) {
94 temp_goal[0] += goal[i];
95 temp_goal[1] += goal[nsets - 1 - i];
96 }
97 i = nsets / 2;
98 if (2 * i != nsets) {
99 temp_goal[0] += .5 * goal[i];
100 temp_goal[1] += .5 * goal[i];
101 }
102 fake_goal = temp_goal;
103 }
104 else {
105 mediantype = MAPPING_TYPE;
106 fake_goal = goal;
107 }
108
109 if (using_vwgts) {
110 vwgt_max = 0;
111 for (i = 1; i <= nvtxs; i++) {
112 if (graph[i]->vwgt > vwgt_max) {
113 vwgt_max = graph[i]->vwgt;
114 }
115 }
116 }
117 else {
118 vwgt_max = 1;
119 }
120
121 /* Perform one of the global partitionings on this sub-graph. */
122 if (global_method == 1) { /* Multilevel method. */
123 mkconnected = MAKE_CONNECTED;
124 solver_flag = LANCZOS_TYPE;
125
126 if (VERTEX_SEPARATOR) {
127 coarsen_klv(graph, nvtxs, nedges, using_vwgts, using_ewgts, term_wgts, igeom, coords,
128 vwgt_max, assignment, goal, architecture, hop_mtx, solver_flag, ndims, nsets,
129 vmax, mediantype, mkconnected, eigtol, COARSE_NLEVEL_KL, 0, &bndy_list, weights,
130 FALSE);
131 if (VERTEX_COVER && !COARSE_BPM) {
132 max_dev = vwgt_max;
133 total_weight = 0;
134 for (i = 0; i < nsets; i++) {
135 total_weight += goal[i];
136 }
137 total_weight *= KL_IMBALANCE / nsets;
138 if (total_weight > max_dev) {
139 max_dev = total_weight;
140 }
141 bpm_improve(graph, assignment, goal, max_dev, &bndy_list, weights, using_vwgts);
142 }
143 }
144 else {
145 coarsen_kl(graph, nvtxs, nedges, using_vwgts, using_ewgts, term_wgts, igeom, coords, vwgt_max,
146 assignment, goal, architecture, hop_mtx, solver_flag, ndims, nsets, vmax,
147 mediantype, mkconnected, eigtol, COARSE_NLEVEL_KL, 0, &bndy_list, weights, FALSE);
148
149 if (VERTEX_COVER) {
150 max_dev = vwgt_max;
151 total_weight = 0;
152 for (i = 0; i < nsets; i++) {
153 total_weight += goal[i];
154 }
155 total_weight *= KL_IMBALANCE / nsets;
156 if (total_weight > max_dev) {
157 max_dev = total_weight;
158 }
159 if (goal[0] - weights[0] <= goal[1] - weights[1]) {
160 i = 0;
161 }
162 else {
163 i = 1;
164 }
165 find_side_bndy(graph, nvtxs, assignment, i, 2, &bndy_list);
166 count_weights(graph, nvtxs, assignment, nsets + 1, weights, using_vwgts);
167 if (DEBUG_KL > 0) {
168 printf("After KL, before bpm_improve\n");
169 countup_vtx_sep(graph, nvtxs, assignment);
170 }
171 bpm_improve(graph, assignment, goal, max_dev, &bndy_list, weights, using_vwgts);
172 }
173 }
174 }
175
176 else if (global_method == 2) { /* Spectral method. */
177 mkconnected = MAKE_CONNECTED;
178 solver_flag = LANCZOS_TYPE;
179
180 active = smalloc(nvtxs * sizeof(int));
181 for (i = 1; i <= ndims; i++) {
182 yvecs[i] = smalloc((nvtxs + 1) * sizeof(double));
183 }
184
185 if (mkconnected) {
186 /* If doing multi-level KL, this happens on coarse graph. */
187 mark = (int *)&(yvecs[1][0]);
188 make_connected(graph, nvtxs, &nedges, mark, active, &cdata, using_ewgts);
189 if (DEBUG_CONNECTED > 0) {
190 printf("Enforcing connectivity\n");
191 print_connected(cdata);
192 }
193 }
194 else if (DEBUG_CONNECTED > 0) {
195 printf("Not enforcing connectivity\n");
196 }
197
198 maxdeg = find_maxdeg(graph, nvtxs, using_ewgts, (float *)NULL);
199 eigensolve(graph, nvtxs, nedges, maxdeg, vwgt_max, vwsqrt, using_vwgts, using_ewgts, term_wgts,
200 igeom, coords, yvecs, evals, architecture, assignment, fake_goal, solver_flag,
201 rqi_flag, vmax, ndims, mediantype, eigtol);
202
203 assign(graph, yvecs, nvtxs, ndims, architecture, nsets, vwsqrt, assignment, active, mediantype,
204 goal, vwgt_max);
205
206 for (i = 1; i <= ndims; i++) {
207 sfree(yvecs[i]);
208 }
209
210 if (mkconnected) {
211 make_unconnected(graph, &nedges, &cdata, using_ewgts);
212 }
213
214 sfree(active);
215 }
216
217 else if (global_method == 3) { /* Inertial method. */
218 inertial(graph, nvtxs, architecture, nsets, igeom, coords, assignment, goal, using_vwgts);
219 }
220
221 else if (global_method == 4) { /* Linear ordering. */
222 simple_type = 3;
223 simple_part(graph, nvtxs, assignment, nsets, simple_type, goal);
224 }
225
226 else if (global_method == 5) { /* Random partitioning. */
227 simple_type = 2;
228 simple_part(graph, nvtxs, assignment, nsets, simple_type, goal);
229 }
230
231 else if (global_method == 6) { /* Scattered partitioning. */
232 simple_type = 1;
233 simple_part(graph, nvtxs, assignment, nsets, simple_type, goal);
234 }
235
236 /* Perform a local refinement, if specified, on the global partitioning. */
237 if (local_method == 1 && global_method != 1) {
238 /* If global_method == 1, already did KL as part of multilevel-KL. */
239 null_ptr = NULL;
240 max_dev = vwgt_max;
241 total_weight = 0;
242 for (i = 0; i < nsets; i++) {
243 total_weight += goal[i];
244 }
245 total_weight *= KL_IMBALANCE / nsets;
246 if (total_weight > max_dev) {
247 max_dev = total_weight;
248 }
249 if (VERTEX_SEPARATOR) {
250 find_bndy(graph, nvtxs, assignment, 2, &bndy_list);
251 count_weights(graph, nvtxs, assignment, nsets + 1, weights, (vwgt_max != 1));
252 klvspiff(graph, nvtxs, assignment, goal, max_dev, &bndy_list, weights);
253 if (VERTEX_COVER) {
254 bpm_improve(graph, assignment, goal, max_dev, &bndy_list, weights, using_vwgts);
255 }
256 }
257 else {
258 if (global_method != 2) {
259 maxdeg = find_maxdeg(graph, nvtxs, using_ewgts, (float *)NULL);
260 }
261 count_weights(graph, nvtxs, assignment, nsets, weights, (vwgt_max != 1));
262 klspiff(graph, nvtxs, assignment, nsets, hop_mtx, goal, term_wgts, max_dev, maxdeg,
263 using_ewgts, &null_ptr, weights);
264 if (VERTEX_COVER) {
265 if (goal[0] - weights[0] <= goal[1] - weights[1]) {
266 i = 0;
267 }
268 else {
269 i = 1;
270 }
271 find_side_bndy(graph, nvtxs, assignment, i, 2, &bndy_list);
272 count_weights(graph, nvtxs, assignment, nsets + 1, weights, (vwgt_max != 1));
273 if (DEBUG_KL > 0) {
274 printf("After KL, before bpm_improve\n");
275 countup_vtx_sep(graph, nvtxs, assignment);
276 }
277 bpm_improve(graph, assignment, goal, max_dev, &bndy_list, weights, using_vwgts);
278 }
279 }
280 }
281 else if (global_method != 1 && VERTEX_COVER) {
282 find_bndy(graph, nvtxs, assignment, 2, &bndy_list);
283 count_weights(graph, nvtxs, assignment, nsets + 1, weights, (vwgt_max != 1));
284 if (DEBUG_KL > 0) {
285 printf("Before bpm_improve\n");
286 countup_vtx_sep(graph, nvtxs, assignment);
287 }
288 max_dev = vwgt_max;
289 total_weight = 0;
290 for (i = 0; i < nsets; i++) {
291 total_weight += goal[i];
292 }
293 total_weight *= KL_IMBALANCE / nsets;
294 if (total_weight > max_dev) {
295 max_dev = total_weight;
296 }
297 bpm_improve(graph, assignment, goal, max_dev, &bndy_list, weights, using_vwgts);
298 }
299
300 if (bndy_list != NULL) {
301 sfree(bndy_list);
302 }
303 }