Debian Codegraph @

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

raw
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
10 #include "params.h" // for MAXSETS
11 #include "smalloc.h" // for sfree
12 #include "structs.h"
13 #include <stdio.h> // for printf, fprintf, NULL, FILE
14 struct bilist;
15
16 /* Think hard about space.
17 Put active list into each routine.
18
19 It may be possible to overlap dvals with active in kl, requiring
20 a total of nvtxs space.
21 */
22 static void free_kl(
23 /* Free everything malloc'd for KL. */
24 struct bilist ****buckets, /* space for bucket sorts */
25 struct bilist **listspace, /* space for all bidirectional elements */
26 int **dvals, /* change in penalty for each possible move */
27 int **tops /* starting dval for each type of move */
28 );
29
30 void klspiff(struct vtx_data **graph, /* list of graph info for each vertex */
31 int nvtxs, /* number of vertices in graph */
32 int *sets, /* local partitioning of vtxs */
33 int nsets, /* number of sets at each level */
34 int (*hops)[MAXSETS], /* hop cost between sets */
35 double *goal, /* desired set sizes */
36 float *term_wgts[], /* weights for terminal propagation */
37 int max_dev, /* largest deviation from balance allowed */
38 double maxdeg, /* largest weighted vertex degree */
39 int using_ewgts, /* are edge weights being used? */
40 int **bndy_list, /* list of vertices on boundary (0 ends) */
41 double *weights /* vertex weights in each set */
42 )
43 {
44 extern FILE *Output_File; /* output file or null */
45 extern double CUT_TO_HOP_COST; /* relative importance of cuts/hops */
46 extern int DEBUG_TRACE; /* debug flag for Kernighan-Lin */
47 extern int DEBUG_KL; /* debug flag for Kernighan-Lin */
48 extern double kl_total_time;
49 extern double kl_init_time;
50 extern double nway_kl_time;
51 struct bilist ****buckets; /* space for bucket sorts */
52 struct bilist **listspace; /* space for all bidirectional elements */
53 float *twptr; /* loops through term_wgts */
54 int **dvals; /* change in penalty for each possible move */
55 int **tops; /* starting dval for each type of move */
56 double time, time1; /* timing variables */
57 float maxterm; /* largest terminal propagation preference */
58 int maxhop; /* maximum hops between sets */
59 int maxdval; /* largest transition cost for a vertex */
60 double cut_cost; /* relative importance of cuts/hops */
61 double hop_cost; /* relative importance of hops/cuts */
62 int error; /* out of space? */
63 int i, j; /* loop counters */
64
65 time = seconds();
66
67 if (DEBUG_TRACE > 0) {
68 printf("<Entering klspiff, nvtxs = %d>\n", nvtxs);
69 }
70
71 /* Find the largest hop value. */
72 maxhop = 0;
73 for (i = 0; i < nsets; i++) {
74 for (j = 0; j < nsets; j++) {
75 if (hops[i][j] > maxhop) {
76 maxhop = hops[i][j];
77 }
78 }
79 }
80
81 maxterm = 0;
82 cut_cost = hop_cost = 1;
83 if (term_wgts[1] != NULL) {
84 for (j = 1; j < nsets; j++) {
85 twptr = term_wgts[j];
86 for (i = nvtxs; i; i--) {
87 ++twptr;
88 if (*twptr > maxterm) {
89 maxterm = *twptr;
90 }
91 else if (-*twptr > maxterm) {
92 maxterm = -*twptr;
93 }
94 }
95 }
96 if (CUT_TO_HOP_COST > 1) {
97 cut_cost = CUT_TO_HOP_COST;
98 }
99 else {
100 hop_cost = 1.0 / CUT_TO_HOP_COST;
101 }
102 }
103
104 maxdval = (2 * maxterm * hop_cost + .5) + (maxdeg * cut_cost + .5) * maxhop;
105
106 /* Allocate a bunch of space for KL. */
107 time1 = seconds();
108 error = kl_init(&buckets, &listspace, &dvals, &tops, nvtxs, nsets, maxdval);
109 kl_init_time += seconds() - time1;
110
111 if (!error) {
112 if (DEBUG_KL > 0) {
113 printf(" Before KL: ");
114 count(graph, nvtxs, sets, nsets, hops, FALSE, using_ewgts);
115 }
116
117 time1 = seconds();
118 error = nway_kl(graph, nvtxs, buckets, listspace, tops, dvals, sets, maxdval, nsets, goal,
119 term_wgts, hops, max_dev, using_ewgts, bndy_list, weights);
120 nway_kl_time += seconds() - time1;
121
122 if (DEBUG_KL > 1) {
123 printf(" After KL:");
124 count(graph, nvtxs, sets, nsets, hops, FALSE, using_ewgts);
125 }
126 }
127
128 if (error) {
129 printf("\nWARNING: No space to perform KL on graph with %d vertices.\n", nvtxs);
130 printf(" NO LOCAL REFINEMENT PERFORMED.\n\n");
131
132 if (Output_File != NULL) {
133 fprintf(Output_File, "\nWARNING: No space to perform KL on graph with %d vertices.\n", nvtxs);
134 fprintf(Output_File, " LOCAL REFINEMENT NOT PERFORMED.\n\n");
135 }
136 }
137
138 free_kl(buckets, listspace, dvals, tops);
139
140 kl_total_time += seconds() - time;
141 }
142
143 static void free_kl(
144 /* Free everything malloc'd for KL. */
145 struct bilist ****buckets, /* space for bucket sorts */
146 struct bilist **listspace, /* space for all bidirectional elements */
147 int **dvals, /* change in penalty for each possible move */
148 int **tops /* starting dval for each type of move */
149 )
150 {
151
152 sfree(dvals);
153 sfree(tops);
154
155 sfree(listspace[0]);
156 sfree(buckets[0][1]);
157 sfree(listspace);
158 sfree(buckets);
159 }