Debian Codegraph @

packages/seacas/libraries/chaco/klspiff/buckets.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 "params.h" // for MAXSETS
10 #include "structs.h" // for vtx_data, bilist
11 #include <stdio.h> // for NULL
12
13 /* Idea:
14 'buckets[i][j]' is a set of buckets to sort moves from i to j.
15 listspace[i] is space for lists in buckets[i][j].
16 Loop through all nonequal pairs [i][j], taking the first element
17 in each list. Compare them all to find the largest allowed move.
18 Make that move, and save it in movelist.
19 */
20
21 void bucketsorts(struct vtx_data **graph, /* graph data structure */
22 int nvtxs, /* number of vertices */
23 struct bilist ****buckets, /* array of lists for bucket sort */
24 struct bilist **listspace, /* list data structure for each vertex */
25 int **dvals, /* d-values for each vertex for removing */
26 int *sets, /* processor each vertex is assigned to */
27 float *term_wgts[], /* weights for terminal prapogation */
28 int maxdval, /* maximum possible dvalue for a vertex */
29 int nsets, /* number of sets being divided into */
30 int parity, /* work in forward or backward direction? */
31 int (*hops)[MAXSETS], /* hop cost between sets */
32 int *bspace, /* indices for randomly ordering vtxs */
33 int list_length, /* number of values in bspace to work with */
34 int npass, /* which pass through KL is this? */
35 int using_ewgts /* are edge weights being used? */
36 )
37 {
38 extern int KL_RANDOM; /* use randomness in KL? */
39 extern int KL_UNDO_LIST; /* only sort vertices who have moved. */
40 extern double CUT_TO_HOP_COST; /* if term_prop, cut/hop importance */
41 struct bilist **bptr = NULL; /* loops through set of buckets */
42 struct bilist *lptr = NULL; /* pointer to an element in listspace */
43 float *ewptr = NULL; /* loops through edge weights */
44 int *bsptr = NULL; /* loops through bspace */
45 int *edges = NULL; /* edge list for a vertex */
46 int myset; /* set that current vertex belongs to */
47 int newset; /* set current vertex could move to */
48 int set; /* set that neighboring vertex belongs to */
49 int weight; /* edge weight for a particular edge */
50 int vtx; /* vertex in graph */
51 float tval; /* terminal propagation value */
52 int val; /* terminal propagation rounded value */
53 double cut_cost; /* relative cut/hop importance */
54 double hop_cost; /* relative hop/cut importance */
55 int myhop; /* hops associated with current vertex */
56 int i, j, l; /* loop counters */
57
58 /* For each vertex, compute d-values for each possible transition. */
59 /* Then store them in each appropriate bucket. */
60
61 if (npass == 1 || !KL_UNDO_LIST || list_length == nvtxs) {
62 /* Empty all the buckets. */
63 /* Last clause catches case where lists weren't undone. */
64 bptr = buckets[0][1];
65 for (i = nsets * (nsets - 1) * (2 * maxdval + 1); i; i--) {
66 *bptr++ = NULL;
67 }
68 }
69
70 /* Randomize the order of the vertices */
71
72 if (list_length == nvtxs || !KL_UNDO_LIST) {
73 bsptr = bspace;
74 list_length = nvtxs;
75 if (parity) {
76 for (i = 1; i <= nvtxs; i++) {
77 *bsptr++ = i;
78 }
79 }
80 else {
81 for (i = nvtxs; i; i--) {
82 *bsptr++ = i;
83 }
84 }
85 }
86 if (KL_RANDOM) {
87 randomize(bspace - 1, list_length);
88 }
89
90 /* Now compute d-vals by seeing which sets neighbors belong to. */
91 cut_cost = hop_cost = 1;
92 if (term_wgts[1] != NULL) {
93 if (CUT_TO_HOP_COST > 1) {
94 cut_cost = CUT_TO_HOP_COST;
95 }
96 else {
97 hop_cost = 1.0 / CUT_TO_HOP_COST;
98 }
99 }
100 weight = cut_cost + .5;
101 bsptr = bspace;
102 for (i = 0; i < list_length; i++) { /* Loop through vertices. */
103 vtx = *bsptr++;
104 myset = sets[vtx];
105
106 /* Initialize all the preference values. */
107 if (term_wgts[1] != NULL) {
108 /* Using terminal propagation. */
109 if (myset == 0) { /* No terminal value. */
110 for (newset = 1; newset < nsets; newset++) {
111 tval = (term_wgts[newset])[vtx];
112 if (tval < 0) {
113 val = -tval * hop_cost + .5;
114 val = -val;
115 }
116 else {
117 val = tval * hop_cost + .5;
118 }
119 dvals[vtx][newset - 1] = val;
120 }
121 }
122 else {
123 tval = -(term_wgts[myset])[vtx];
124 if (tval < 0) {
125 val = -tval * hop_cost + .5;
126 val = -val;
127 }
128 else {
129 val = tval * hop_cost + .5;
130 }
131 dvals[vtx][0] = val;
132 l = 1;
133 for (newset = 1; newset < nsets; newset++) {
134 if (newset != myset) {
135 tval = (term_wgts[newset])[vtx] - (term_wgts[myset])[vtx];
136 if (tval < 0) {
137 val = -tval * hop_cost + .5;
138 val = -val;
139 }
140 else {
141 val = tval * hop_cost + .5;
142 }
143 dvals[vtx][l] = val;
144 l++;
145 }
146 }
147 }
148 }
149 else {
150 for (j = 0; j < nsets - 1; j++) {
151 dvals[vtx][j] = 0;
152 }
153 }
154
155 /* First count the neighbors in each set. */
156 edges = graph[vtx]->edges;
157 if (using_ewgts) {
158 ewptr = graph[vtx]->ewgts;
159 }
160 for (j = graph[vtx]->nedges - 1; j; j--) {
161 set = sets[*(++edges)];
162 if (set < 0) {
163 set = -set - 1;
164 }
165 if (using_ewgts) {
166 weight = *(++ewptr) * cut_cost + .5;
167 }
168 myhop = hops[myset][set];
169
170 l = 0;
171 for (newset = 0; newset < nsets; newset++) {
172 if (newset != myset) {
173 dvals[vtx][l] += weight * (myhop - hops[newset][set]);
174 l++;
175 }
176 }
177 }
178
179 /* Now add to appropriate buckets. */
180 l = 0;
181 for (newset = 0; newset < nsets; newset++) {
182 if (newset != myset) {
183 lptr = listspace[l];
184 add2bilist(&lptr[vtx], &buckets[myset][newset][dvals[vtx][l] + maxdval]);
185 ++l;
186 }
187 }
188 }
189 }