Debian Codegraph @

packages/seacas/libraries/chaco/klspiff/buckets_bi.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 /* Routine slightly streamlined for case with only two sets. */
22
23 void bucketsorts_bi(struct vtx_data **graph, /* graph data structure */
24 int nvtxs, /* number of vertices */
25 struct bilist ****buckets, /* array of lists for bucket sort */
26 struct bilist **listspace, /* list data structure for each vertex */
27 int **dvals, /* d-values for each vertex for removing */
28 int *sets, /* processor each vertex is assigned to */
29 float *term_wgts[], /* weights for terminal propagation */
30 int maxdval, /* maximum possible dvalue for a vertex */
31 int nsets, /* number of sets being divided into */
32 int parity, /* work in forward or backward direction? */
33 int (*hops)[MAXSETS], /* hop cost between sets */
34 int *bspace, /* indices for randomly ordering vtxs */
35 int list_length, /* number of values in bspace to work with */
36 int npass, /* which pass through KL is this? */
37 int using_ewgts /* are edge weights being used? */
38 )
39 {
40 extern int KL_RANDOM; /* use randomness in KL? */
41 extern int KL_UNDO_LIST; /* only sort vertices who have moved. */
42 extern double CUT_TO_HOP_COST; /* ..if so, relative cut/hop importance */
43 struct bilist **bptr = NULL; /* loops through set of buckets */
44 struct bilist *lptr = NULL; /* pointer to an element in listspace */
45 float *ewptr = NULL; /* loops through edge weights */
46 float *twptr = NULL; /* weights for terminal propagation */
47 int *bsptr = NULL; /* loops through bspace */
48 int *edges = NULL; /* edge list for a vertex */
49 int myset; /* set current vertex belongs to */
50 int other_set; /* set current vertex doesn't belong to */
51 int set; /* set that neighboring vertex belongs to */
52 int weight; /* edge weight for a particular edge */
53 int vtx; /* vertex in graph */
54 int val; /* terminal propagation rounded value */
55 double cut_cost; /* relative cut/hop importance */
56 double hop_cost; /* relative hop/cut importance */
57 int myhop; /* hops associated with current vertex */
58 int i, j; /* loop counters */
59
60 /* For each vertex, compute d-values for each possible transition. */
61 /* Then store them in each appropriate bucket. */
62
63 if (npass == 1 || !KL_UNDO_LIST || list_length == nvtxs) {
64 /* Empty all the buckets. */
65 /* Last clause catches case where lists weren't undone. */
66 bptr = buckets[0][1];
67 for (i = nsets * (nsets - 1) * (2 * maxdval + 1); i; i--) {
68 *bptr++ = NULL;
69 }
70 }
71
72 /* Randomize the order of the vertices */
73
74 if ((KL_UNDO_LIST && list_length == nvtxs) || !KL_UNDO_LIST) {
75 list_length = nvtxs;
76 bsptr = bspace;
77 if (parity) {
78 for (i = 1; i <= nvtxs; i++) {
79 *bsptr++ = i;
80 }
81 }
82 else {
83 for (i = nvtxs; i; i--) {
84 *bsptr++ = i;
85 }
86 }
87 }
88 if (KL_RANDOM) {
89 randomize(bspace - 1, list_length);
90 }
91
92 /* Now compute d-vals by seeing which sets neighbors belong to. */
93 cut_cost = hop_cost = 1;
94 if (term_wgts[1] != NULL) {
95 if (CUT_TO_HOP_COST > 1) {
96 cut_cost = CUT_TO_HOP_COST;
97 }
98 else {
99 hop_cost = 1.0 / CUT_TO_HOP_COST;
100 }
101 }
102
103 weight = cut_cost + .5;
104
105 bsptr = bspace;
106 twptr = term_wgts[1];
107 for (i = 0; i < list_length; i++) { /* Loop through vertices. */
108 vtx = *bsptr++;
109 myset = sets[vtx];
110 other_set = !myset;
111
112 /* Initialize all the preference values. */
113 if (twptr != NULL) {
114 /* Using terminal propagation. Round to integer value. */
115 if (twptr[vtx] < 0) {
116 val = -twptr[vtx] * hop_cost + .5;
117 val = -val;
118 }
119 else {
120 val = twptr[vtx] * hop_cost + .5;
121 }
122 if (myset == 0) {
123 dvals[vtx][0] = val;
124 }
125 else {
126 dvals[vtx][0] = -val;
127 }
128 }
129 else {
130 dvals[vtx][0] = 0;
131 }
132
133 /* First count the neighbors in each set. */
134 edges = graph[vtx]->edges;
135 if (using_ewgts) {
136 ewptr = graph[vtx]->ewgts;
137 }
138 for (j = graph[vtx]->nedges - 1; j; j--) {
139 set = sets[*(++edges)];
140 if (set < 0) {
141 set = -set - 1;
142 }
143 if (using_ewgts) {
144 weight = *(++ewptr) * cut_cost + .5;
145 }
146 myhop = hops[myset][set];
147
148 dvals[vtx][0] += weight * (myhop - hops[other_set][set]);
149 }
150
151 /* Now add to appropriate buckets. */
152 lptr = listspace[0];
153 add2bilist(&lptr[vtx], &buckets[myset][other_set][dvals[vtx][0] + maxdval]);
154 }
155 }