Debian Codegraph @

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

raw
1 /*
2 * Copyright(C) 1999-2020, 2022, 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
10 #include "params.h" // for MAXSETS
11 #include "smalloc.h" // for sfree, smalloc_ret
12 #include "structs.h"
13 #include <math.h> // for sqrt
14 #include <stdio.h> // for NULL, printf, fprintf, etc
15
16 extern void reflect_input(int nvtxs, /* number of vertices in graph */
17 int nedges, /* number of edges in graph */
18 int igeom, /* geometric dimension for inertial method */
19 char *graphname, /* name of graph input file */
20 char *geomname, /* name of geometry input file */
21 char *inassignname, /* name of assignment input file */
22 char *outassignname, /* name of assignment output file */
23 char *outfilename, /* name of information output file */
24 int architecture, /* 0=> hypercube, d=> d-dimensional mesh */
25 int ndims_tot, /* total number of cuts to make */
26 int mesh_dims[3], /* size of mesh */
27 int global_method, /* global partitioning algorithm */
28 int local_method, /* local partitioning algorithm */
29 int rqi_flag, /* use RQI/Symmlq eigensolver? */
30 int vmax, /* smallest acceptable coarsened nvtxs */
31 int ndims, /* partitioning level */
32 double eigtol, /* tolerance on eigenvectors */
33 long seed, /* random number seed */
34 FILE *outfile); /* file to write output to */
35
36 double *SQRTS; /* precomputed square roots for efficiency */
37
38 double DOUBLE_EPSILON; /* machine precision */
39 double DOUBLE_MAX; /* largest double precision value */
40
41 char *Graph_File_Name = NULL; /* Input graph file name */
42 char *Geometry_File_Name = NULL; /* Input coordinate file name */
43 char *Assign_In_File_Name = NULL; /* Input assignment file name */
44
45 FILE *Output_File = NULL; /* Pointer to output file or NULL */
46
47 int EXPERT = 0; /* user mode */
48
49 int submain(struct vtx_data **graph, /* data structure for graph */
50 int nvtxs, /* number of vertices in full graph */
51 int nedges, /* number of edges in graph */
52 int using_vwgts, /* are vertex weights being used? */
53 int using_ewgts, /* are edge weights being used? */
54 int igeom, /* geometry dimension if using inertial method */
55 float **coords, /* coordinates of vertices if used */
56 char *outassignname, /* name of assignment output file */
57 char *outfilename, /* in which to print output metrics */
58 int *assignment, /* set number of each vtx (length n) */
59 double *goal, /* desired sizes for each set */
60 int architecture, /* 0=> hypercube, d=> d-dimensional mesh */
61 int ndims_tot, /* total number hypercube dimensions */
62 int mesh_dims[3], /* extent of mesh in 3 directions */
63 int global_method, /* global partitioning algorithm */
64 int local_method, /* local partitioning algorithm */
65 int rqi_flag, /* use RQI/Symmlq eigensolver? */
66 int vmax, /* if so, how many vtxs to coarsen down to */
67 int ndims, /* number of eigenvectors (2^d sets) */
68 double eigtol, /* tolerance on eigenvectors */
69 long seed /* for random graph mutations */
70 )
71 {
72 extern int ECHO; /* controls output to file or screen */
73 extern int CHECK_INPUT; /* should I check input for correctness? */
74 extern int SEQUENCE; /* just generate spectal ordering? */
75 extern int OUTPUT_ASSIGN; /* print assignment to a file? */
76 extern int OUTPUT_METRICS; /* controls formatting of output */
77 extern int PERTURB; /* perturb matrix if quad/octasection? */
78 extern int NSQRTS; /* number of square roots to precompute */
79 extern int KL_METRIC; /* KL interset cost: 1=>cuts, 2=>hops */
80 extern int LANCZOS_TYPE; /* type of Lanczos to use */
81 extern int REFINE_MAP; /* use greedy strategy to improve mapping? */
82 extern int REFINE_PARTITION; /* number of calls to pairwise_refine to make */
83 extern int VERTEX_COVER; /* use matching to reduce vertex separator? */
84 extern int CONNECTED_DOMAINS; /* force subdomain connectivity at end? */
85 extern int INTERNAL_VERTICES; /* greedily increase internal vtxs? */
86 extern int DEBUG_INTERNAL; /* debug code about force_internal? */
87 extern int DEBUG_REFINE_PART; /* debug code about refine_part? */
88 extern int DEBUG_REFINE_MAP; /* debug code about refine_map? */
89 extern int DEBUG_MACH_PARAMS; /* print out computed machine params? */
90 extern int DEBUG_TRACE; /* trace main execution path */
91 extern int PRINT_HEADERS; /* print section headings for output? */
92 extern int TIME_KERNELS; /* benchmark some numerical kernels? */
93 extern double start_time; /* time code was entered */
94 extern double total_time; /* (almost) total time spent in code */
95 extern double check_input_time; /* time spent checking input */
96 extern double partition_time; /* time spent partitioning graph */
97 extern double kernel_time; /* time spent benchmarking kernels */
98 extern double count_time; /* time spent evaluating the answer */
99 extern double print_assign_time; /* time spent writing output file */
100 FILE *outfile; /* output file */
101 struct vtx_data **graph2; /* data structure for graph */
102 int hop_mtx[MAXSETS][MAXSETS]; /* between-set hop cost for KL */
103 double *vwsqrt; /* sqrt of vertex weights (length nvtxs+1) */
104 double time, time1; /* timing variables */
105 char *graphname, *geomname; /* names of input files */
106 char *inassignname; /* name of assignment input file */
107 int old_nsqrts; /* old value of NSQRTS */
108 int append; /* append output to existing file? */
109 int nsets; /* number of sets created by each divide */
110 int nsets_tot; /* total number of sets */
111 int bits; /* used in computing hops */
112 int flag; /* return code from check_input */
113 int old_perturb = 0; /* saves original perturbation flag */
114 int i, j, k; /* loop counters */
115
116 if (DEBUG_TRACE > 0) {
117 printf("<Entering submain>\n");
118 }
119
120 /* First check all the input for consistency. */
121
122 if (architecture == 1) {
123 mesh_dims[1] = mesh_dims[2] = 1;
124 }
125 else if (architecture == 2) {
126 mesh_dims[2] = 1;
127 }
128
129 /* Check for simple special case of 1 processor. */
130 k = 0;
131 if (architecture == 0) {
132 k = 1 << ndims_tot;
133 }
134 else if (architecture > 0) {
135 k = mesh_dims[0] * mesh_dims[1] * mesh_dims[2];
136 }
137
138 if (k == 1) {
139 for (i = 1; i <= nvtxs; i++) {
140 assignment[i] = 0;
141 }
142
143 if (OUTPUT_ASSIGN > 0 && outassignname != NULL) {
144 time1 = seconds();
145 assign_out(nvtxs, assignment, k, outassignname);
146 print_assign_time += seconds() - time1;
147 }
148 return (0);
149 }
150
151 graphname = Graph_File_Name;
152 geomname = Geometry_File_Name;
153 inassignname = Assign_In_File_Name;
154
155 /* Turn of perturbation if using bisection */
156 if (ndims == 1) {
157 old_perturb = PERTURB;
158 PERTURB = FALSE;
159 }
160
161 if (ECHO < 0 && outfilename != NULL) { /* Open output file */
162 outfile = fopen(outfilename, "r");
163 if (outfile != NULL) {
164 append = TRUE;
165 fclose(outfile);
166 }
167 else {
168 append = FALSE;
169 }
170 outfile = fopen(outfilename, "a");
171 if (append) {
172 fprintf(outfile, "\n------------------------------------------------\n\n");
173 }
174 }
175 else {
176 outfile = NULL;
177 }
178
179 Output_File = outfile;
180
181 if (outfile != NULL && PRINT_HEADERS) {
182 fprintf(outfile, "\n Chaco 2.0\n");
183 fprintf(outfile, " Sandia National Laboratories\n\n");
184 }
185
186 if (CHECK_INPUT) { /* Check the input for inconsistencies. */
187 time1 = seconds();
188
189 flag = check_input(graph, nvtxs, nedges, igeom, coords, graphname, assignment, goal,
190 architecture, ndims_tot, mesh_dims, global_method, local_method, rqi_flag,
191 &vmax, ndims, eigtol);
192
193 check_input_time += seconds() - time1;
194
195 if (flag) {
196 strout("ERROR IN INPUT.\n");
197 return (1);
198 }
199 }
200
201 if (ECHO != 0) {
202 reflect_input(nvtxs, nedges, igeom, graphname, geomname, inassignname, outassignname,
203 outfilename, architecture, ndims_tot, mesh_dims, global_method, local_method,
204 rqi_flag, vmax, ndims, eigtol, seed, outfile);
205 }
206
207 if (PRINT_HEADERS) {
208 printf("\n\nStarting to partition ...\n\n");
209 if (Output_File != NULL) {
210 fprintf(Output_File,
211 "\n\nStarting to partition ... (residual, warning and error messages only)\n\n");
212 }
213 }
214
215 time = seconds();
216
217 /* Perform some one-time initializations. */
218 setrandom(seed);
219 machine_params(&DOUBLE_EPSILON, &DOUBLE_MAX);
220
221 if (DEBUG_MACH_PARAMS > 0) {
222 printf("Machine parameters:\n");
223 printf(" DOUBLE_EPSILON = %e\n", DOUBLE_EPSILON);
224 printf(" DOUBLE_MAX = %e\n", DOUBLE_MAX);
225 }
226
227 nsets = (1 << ndims);
228
229 old_nsqrts = NSQRTS;
230 if (nvtxs < NSQRTS && !using_vwgts) {
231 NSQRTS = nvtxs;
232 }
233 SQRTS = smalloc_ret((NSQRTS + 1) * sizeof(double));
234 if (SQRTS == NULL) {
235 strout("ERROR: No space to allocate sqrts\n");
236 return (1);
237 }
238 for (i = 1; i <= NSQRTS; i++) {
239 SQRTS[i] = sqrt((double)i);
240 }
241
242 if (using_vwgts && (global_method == 1 || global_method == 2)) {
243 vwsqrt = smalloc_ret((nvtxs + 1) * sizeof(double));
244 if (vwsqrt == NULL) {
245 strout("ERROR: No space to allocate vwsqrt\n");
246 sfree(SQRTS);
247 NSQRTS = old_nsqrts;
248 return (1);
249 }
250 makevwsqrt(vwsqrt, graph, nvtxs);
251 }
252 else {
253 vwsqrt = NULL;
254 }
255
256 if (TIME_KERNELS) {
257 time1 = seconds();
258 time_kernels(graph, nvtxs, vwsqrt);
259 kernel_time += seconds() - time1;
260 }
261
262 if (SEQUENCE) {
263 sequence(graph, nvtxs, nedges, using_ewgts, vwsqrt, LANCZOS_TYPE, rqi_flag, vmax, eigtol);
264 goto End_Label;
265 }
266
267 /* Initialize cost function for KL-spiff */
268 if (global_method == 1 || local_method == 1) {
269 for (i = 0; i < nsets; i++) {
270 hop_mtx[i][i] = 0;
271 for (j = 0; j < i; j++) {
272 hop_mtx[i][j] = 0;
273 if (KL_METRIC == 2) { /* Count hypercube hops */
274 bits = i ^ j;
275 while (bits) {
276 if (bits & 1) {
277 ++hop_mtx[i][j];
278 }
279 bits >>= 1;
280 }
281 }
282 else if (KL_METRIC == 1) { /* Count cut edges */
283 hop_mtx[i][j] = 1;
284 }
285 hop_mtx[j][i] = hop_mtx[i][j];
286 }
287 }
288 }
289
290 graph2 = graph;
291 if (global_method == 3 && local_method != 1 && !VERTEX_COVER && !using_vwgts) {
292 graph2 = NULL;
293 }
294
295 if (!(global_method == 7 && local_method == 2)) {
296 balance(graph2, nvtxs, nedges, using_vwgts, using_ewgts, vwsqrt, igeom, coords, assignment,
297 goal, architecture, ndims_tot, mesh_dims, global_method, local_method, rqi_flag, vmax,
298 ndims, eigtol, hop_mtx);
299 }
300
301 partition_time += seconds() - time - kernel_time;
302
303 nsets_tot = 0;
304 if (architecture == 0) {
305 nsets_tot = 1 << ndims_tot;
306 }
307 else if (architecture > 0) {
308 nsets_tot = mesh_dims[0] * mesh_dims[1] * mesh_dims[2];
309 }
310
311 if (graph != NULL) {
312 j = TRUE;
313 for (i = 1; i <= REFINE_PARTITION && j; i++) { /* Reduce cuts w/ KL? */
314 if (DEBUG_REFINE_PART > 0) {
315 printf("\n\nBefore pass %d to refine partition:\n", i);
316 if (outfile != NULL) {
317 fprintf(outfile, "\n\nBefore pass %d to refine partition:\n", i);
318 }
319 countup(graph, nvtxs, assignment, ndims, architecture, ndims_tot, mesh_dims, OUTPUT_METRICS,
320 outfile, using_ewgts);
321 }
322 j = refine_part(graph, nvtxs, using_ewgts, assignment, architecture, ndims_tot, mesh_dims,
323 goal);
324 }
325 }
326
327 if (graph != NULL) {
328 if (INTERNAL_VERTICES) {
329 if (DEBUG_INTERNAL > 0) {
330 printf("\n\nBefore increasing internal vertices:\n");
331 if (outfile != NULL) {
332 fprintf(outfile, "\n\nBefore increasing internal vertices:\n");
333 }
334 countup(graph, nvtxs, assignment, ndims, architecture, ndims_tot, mesh_dims, OUTPUT_METRICS,
335 outfile, using_ewgts);
336 }
337 force_internal(graph, nvtxs, using_ewgts, assignment, goal, nsets_tot, INTERNAL_VERTICES);
338 }
339 }
340
341 if (graph != NULL) {
342 if (CONNECTED_DOMAINS > 0) { /* Force subdomains to be connected */
343 connect_enforce(graph, nvtxs, using_ewgts, assignment, goal, nsets_tot, &i, &j);
344 printf("\nConnectivity enforcement moved %d vertices total\n", i);
345 printf(" largest moved subset = %d\n\n", j);
346 }
347 }
348
349 if (graph != NULL) {
350 if (REFINE_MAP) { /* Improve the mapping to processors? */
351 if (DEBUG_REFINE_MAP > 0) {
352 printf("\n\nBefore refining mapping to processors:\n");
353 if (outfile != NULL) {
354 fprintf(outfile, "\n\nBefore refining mapping to processors:\n");
355 }
356 countup(graph, nvtxs, assignment, ndims, architecture, ndims_tot, mesh_dims, OUTPUT_METRICS,
357 outfile, using_ewgts);
358 }
359 refine_map(graph, nvtxs, using_ewgts, assignment, architecture, ndims_tot, mesh_dims);
360 }
361 }
362
363 if (OUTPUT_ASSIGN > 0 && outassignname != NULL) {
364 time1 = seconds();
365 if (architecture == 0) {
366 k = 1 << ndims_tot;
367 }
368 else if (architecture > 0) {
369 k = mesh_dims[0] * mesh_dims[1] * mesh_dims[2];
370 }
371 assign_out(nvtxs, assignment, k, outassignname);
372 print_assign_time += seconds() - time1;
373 }
374
375 if (OUTPUT_METRICS != 0) { /* Compute graph metrics of partition. */
376 time1 = seconds();
377 if (graph != NULL) {
378 if (PRINT_HEADERS) {
379 printf("\n\n Partitioning Results\n");
380 if (outfile != NULL) {
381 fprintf(outfile, "\n\n Partitioning Results\n");
382 }
383 }
384
385 countup(graph, nvtxs, assignment, ndims, architecture, ndims_tot, mesh_dims, OUTPUT_METRICS,
386 outfile, using_ewgts);
387 }
388 count_time += seconds() - time1;
389 }
390
391 /* Invoke communication simulator? */
392 /*
393 if (graph != NULL) {
394 if (SIMULATOR > 0) {
395 simulate(graph, nvtxs, ndims, architecture, ndims_tot,
396 mesh_dims, assignment, using_ewgts, outfile);
397 }
398 }
399 */
400
401 End_Label:
402
403 if (vwsqrt != NULL) {
404 sfree(vwsqrt);
405 }
406 if (SQRTS != NULL) {
407 sfree(SQRTS);
408 }
409
410 /* Turn perturbation back on for next invocation. */
411 if (ndims == 1) {
412 PERTURB = old_perturb;
413 }
414 NSQRTS = old_nsqrts;
415
416 total_time += seconds() - start_time;
417 if (outfile != NULL) {
418 time_out(outfile);
419 fclose(outfile);
420 }
421 Output_File = NULL;
422
423 return (0);
424 }