| 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 "defs.h" // for TRUE |
| 10 |
#include "smalloc.h" // for sfree, smalloc |
| 11 |
#include "structs.h" // for vtx_data, heap |
| 12 |
#include <stdio.h> // for NULL, printf |
| 13 |
|
| 14 |
/* Move vertices between domains to ensure each domain is connected. */ |
| 15 |
/* Note: This will likely result in load imbalance. */ |
| 16 |
|
| 17 |
void connect_enforce(struct vtx_data **graph, /* data structure for graph */ |
| 18 |
int nvtxs, /* number of vertices in full graph */ |
| 19 |
int using_ewgts, /* are edge weights being used? */ |
| 20 |
int *assignment, /* set number of each vtx (length n) */ |
| 21 |
double *goal, /* desired sizes for each set */ |
| 22 |
int nsets_tot, /* total number sets created */ |
| 23 |
int *total_move, /* total number of vertices moved */ |
| 24 |
int *max_move /* largest connected component moved */ |
| 25 |
) |
| 26 |
{ |
| 27 |
struct vtx_data **subgraph; /* data structure for domain graph */ |
| 28 |
int subnvtxs; /* number of vertices in a domain */ |
| 29 |
int subnedges; /* number of edges in a domain */ |
| 30 |
struct heap *heap; /* data structure for sorting set sizes */ |
| 31 |
int *heap_map; /* pointers from sets to heap locations */ |
| 32 |
int *list_ptrs; /* header of vtx list for each domain */ |
| 33 |
int *setlists; /* linked list of vtxs for each domain */ |
| 34 |
int *vtxlist; /* space for breadth first search list */ |
| 35 |
int *comp_lists; /* list of vtxs in each connected comp */ |
| 36 |
int *clist_ptrs; /* pointers to heads of comp_lists */ |
| 37 |
int *subsets; /* list of active domains (all of them) */ |
| 38 |
int *subsets2; /* list of active domains (all of them) */ |
| 39 |
double *set_size; /* weighted sizes of different partitions */ |
| 40 |
double size; /* size of subset being moved to new domain */ |
| 41 |
int *bndy_list; /* list of domains adjacent to component */ |
| 42 |
double *bndy_size; /* size of these boundaries */ |
| 43 |
double *comp_size; /* sizes of different connected components */ |
| 44 |
double comp_size_max; /* size of largest connected component */ |
| 45 |
int comp_max_index = 0; /* which component is largest? */ |
| 46 |
int *glob2loc; /* global to domain renumbering */ |
| 47 |
int *loc2glob; /* domain to global renumbering */ |
| 48 |
int *degree; /* number of neighbors of a vertex */ |
| 49 |
int *comp_flag; /* component number for each vtx */ |
| 50 |
double ewgt; /* edge weight */ |
| 51 |
int nbndy; /* number of sets adjacent to component */ |
| 52 |
int domain; /* which subdomain I'm working on */ |
| 53 |
int new_domain; /* subdomain to move some vertices to */ |
| 54 |
double max_bndy; /* max connectivity to other domain */ |
| 55 |
int ncomps; /* number of connected components */ |
| 56 |
int change; /* how many vertices change set? */ |
| 57 |
int max_change; /* largest subset moved together */ |
| 58 |
int vtx; /* vertex in a connected component */ |
| 59 |
int set; /* set a neighboring vertex is in */ |
| 60 |
int i, j, k, l; /* loop counters */ |
| 61 |
|
| 62 |
change = 0; |
| 63 |
max_change = 0; |
| 64 |
|
| 65 |
/* Allocate space & initialize some values. */ |
| 66 |
|
| 67 |
set_size = smalloc(nsets_tot * sizeof(double)); |
| 68 |
bndy_size = smalloc(nsets_tot * sizeof(double)); |
| 69 |
bndy_list = smalloc(nsets_tot * sizeof(int)); |
| 70 |
|
| 71 |
setlists = smalloc((nvtxs + 1) * sizeof(int)); |
| 72 |
list_ptrs = smalloc(nsets_tot * sizeof(int)); |
| 73 |
|
| 74 |
glob2loc = smalloc((nvtxs + 1) * sizeof(int)); |
| 75 |
loc2glob = smalloc((nvtxs + 1) * sizeof(int)); |
| 76 |
subsets = smalloc(nsets_tot * sizeof(int)); |
| 77 |
heap = (struct heap *)smalloc((nsets_tot + 1) * sizeof(struct heap)); |
| 78 |
heap_map = smalloc(nsets_tot * sizeof(int)); |
| 79 |
|
| 80 |
for (i = 0; i < nsets_tot; i++) { |
| 81 |
set_size[i] = 0; |
| 82 |
bndy_list[i] = 0; |
| 83 |
bndy_size[i] = 0; |
| 84 |
subsets[i] = i; |
| 85 |
} |
| 86 |
|
| 87 |
for (i = 1; i <= nvtxs; i++) { |
| 88 |
set_size[assignment[i]] += graph[i]->vwgt; |
| 89 |
} |
| 90 |
|
| 91 |
for (i = 0; i < nsets_tot; i++) { |
| 92 |
heap[i + 1].tag = i; |
| 93 |
heap[i + 1].val = set_size[i] - goal[i]; |
| 94 |
} |
| 95 |
|
| 96 |
make_setlists(setlists, list_ptrs, nsets_tot, subsets, assignment, NULL, nvtxs, TRUE); |
| 97 |
|
| 98 |
heap_build(heap, nsets_tot, heap_map); |
| 99 |
|
| 100 |
for (i = 0; i < nsets_tot; i++) { |
| 101 |
/* Find largest remaining set to work on next */ |
| 102 |
size = heap_extract_max(heap, nsets_tot - i, &domain, heap_map); |
| 103 |
|
| 104 |
/* Construct subdomain graph. */ |
| 105 |
subnvtxs = make_maps(setlists, list_ptrs, domain, glob2loc, loc2glob); |
| 106 |
if (subnvtxs > 1) { |
| 107 |
|
| 108 |
subgraph = (struct vtx_data **)smalloc((subnvtxs + 1) * sizeof(struct vtx_data *)); |
| 109 |
degree = smalloc((subnvtxs + 1) * sizeof(int)); |
| 110 |
|
| 111 |
make_subgraph(graph, subgraph, subnvtxs, &subnedges, assignment, domain, glob2loc, loc2glob, |
| 112 |
degree, using_ewgts); |
| 113 |
|
| 114 |
/* Find connected components. */ |
| 115 |
comp_flag = smalloc((subnvtxs + 1) * sizeof(int)); |
| 116 |
vtxlist = smalloc(subnvtxs * sizeof(int)); |
| 117 |
ncomps = find_comps(subgraph, subnvtxs, comp_flag, vtxlist); |
| 118 |
sfree(vtxlist); |
| 119 |
|
| 120 |
/* Restore original graph */ |
| 121 |
remake_graph(subgraph, subnvtxs, loc2glob, degree, using_ewgts); |
| 122 |
sfree(degree); |
| 123 |
sfree(subgraph); |
| 124 |
|
| 125 |
if (ncomps > 1) { |
| 126 |
|
| 127 |
/* Figure out sizes of components */ |
| 128 |
comp_size = smalloc(ncomps * sizeof(double)); |
| 129 |
for (j = 0; j < ncomps; j++) { |
| 130 |
comp_size[j] = 0; |
| 131 |
} |
| 132 |
for (j = 1; j <= subnvtxs; j++) { |
| 133 |
comp_size[comp_flag[j]] += graph[loc2glob[j]]->vwgt; |
| 134 |
} |
| 135 |
comp_size_max = 0; |
| 136 |
for (j = 0; j < ncomps; j++) { |
| 137 |
if (comp_size[j] > comp_size_max) { |
| 138 |
comp_size_max = comp_size[j]; |
| 139 |
comp_max_index = j; |
| 140 |
} |
| 141 |
} |
| 142 |
for (j = 0; j < ncomps; j++) { |
| 143 |
if (j != comp_max_index) { |
| 144 |
change += comp_size[j]; |
| 145 |
if (comp_size[j] > max_change) { |
| 146 |
max_change = comp_size[j]; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
sfree(comp_size); |
| 151 |
|
| 152 |
/* Make data structures for traversing components */ |
| 153 |
comp_lists = smalloc((subnvtxs + 1) * sizeof(int)); |
| 154 |
clist_ptrs = smalloc(ncomps * sizeof(int)); |
| 155 |
if (ncomps > nsets_tot) { |
| 156 |
subsets2 = smalloc(ncomps * sizeof(int)); |
| 157 |
for (j = 0; j < ncomps; j++) { |
| 158 |
subsets2[j] = j; |
| 159 |
} |
| 160 |
} |
| 161 |
else { |
| 162 |
subsets2 = subsets; |
| 163 |
} |
| 164 |
make_setlists(comp_lists, clist_ptrs, ncomps, subsets2, comp_flag, NULL, subnvtxs, TRUE); |
| 165 |
if (ncomps > nsets_tot) { |
| 166 |
sfree(subsets2); |
| 167 |
} |
| 168 |
|
| 169 |
/* Move all but the largest component. */ |
| 170 |
ewgt = 1; |
| 171 |
for (j = 0; j < ncomps; j++) { |
| 172 |
if (j != comp_max_index) { |
| 173 |
|
| 174 |
/* Figure out to which other domain it is most connected. */ |
| 175 |
nbndy = 0; |
| 176 |
k = clist_ptrs[j]; |
| 177 |
while (k != 0) { |
| 178 |
vtx = loc2glob[k]; |
| 179 |
for (l = 1; l <= graph[vtx]->nedges; l++) { |
| 180 |
set = assignment[graph[vtx]->edges[l]]; |
| 181 |
if (set != domain) { |
| 182 |
if (bndy_size[set] == 0) { |
| 183 |
bndy_list[nbndy++] = set; |
| 184 |
} |
| 185 |
if (using_ewgts) { |
| 186 |
ewgt = graph[vtx]->ewgts[l]; |
| 187 |
} |
| 188 |
bndy_size[set] += ewgt; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
k = comp_lists[k]; |
| 193 |
} |
| 194 |
|
| 195 |
/* Select a new domain. */ |
| 196 |
/* Instead of just big boundary, penalize too-large sets. */ |
| 197 |
/* Could be more aggressive to improve balance. */ |
| 198 |
max_bndy = 0; |
| 199 |
new_domain = -1; |
| 200 |
for (k = 0; k < nbndy; k++) { |
| 201 |
l = bndy_list[k]; |
| 202 |
if (bndy_size[l] * goal[l] / (set_size[l] + 1) > max_bndy) { |
| 203 |
new_domain = bndy_list[k]; |
| 204 |
max_bndy = bndy_size[l] * goal[l] / (set_size[l] + 1); |
| 205 |
} |
| 206 |
} |
| 207 |
if (new_domain == -1) { |
| 208 |
printf("Error in connect_enforce: new_domain = -1. Disconnected graph?\n"); |
| 209 |
new_domain = domain; |
| 210 |
} |
| 211 |
|
| 212 |
/* Clear bndy_size array */ |
| 213 |
for (k = 0; k < nbndy; k++) { |
| 214 |
bndy_size[bndy_list[k]] = 0; |
| 215 |
} |
| 216 |
|
| 217 |
k = clist_ptrs[j]; |
| 218 |
|
| 219 |
size = 0; |
| 220 |
|
| 221 |
while (k != 0) { |
| 222 |
vtx = loc2glob[k]; |
| 223 |
assignment[vtx] = new_domain; |
| 224 |
size += graph[vtx]->vwgt; |
| 225 |
|
| 226 |
/* Finally, update setlists and list_ptrs */ |
| 227 |
/* Note: current domain setlist now bad, but not used |
| 228 |
again */ |
| 229 |
setlists[vtx] = list_ptrs[new_domain]; |
| 230 |
list_ptrs[new_domain] = vtx; |
| 231 |
|
| 232 |
k = comp_lists[k]; |
| 233 |
} |
| 234 |
/* |
| 235 |
printf("Updating set %d (from %d) to size %g\n", new_domain, domain, |
| 236 |
set_size[new_domain] + size - goal[new_domain]); |
| 237 |
*/ |
| 238 |
if (heap_map[new_domain] > 0) { |
| 239 |
set_size[new_domain] += size; |
| 240 |
heap_update_val(heap, heap_map[new_domain], set_size[new_domain] - goal[new_domain], |
| 241 |
heap_map); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
sfree(clist_ptrs); |
| 247 |
sfree(comp_lists); |
| 248 |
} |
| 249 |
sfree(comp_flag); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
sfree(heap_map); |
| 254 |
sfree(heap); |
| 255 |
sfree(subsets); |
| 256 |
sfree(loc2glob); |
| 257 |
sfree(glob2loc); |
| 258 |
sfree(list_ptrs); |
| 259 |
sfree(setlists); |
| 260 |
sfree(bndy_list); |
| 261 |
sfree(bndy_size); |
| 262 |
sfree(set_size); |
| 263 |
|
| 264 |
*total_move = change; |
| 265 |
*max_move = max_change; |
| 266 |
} |