| 1 |
/* |
| 2 |
* Copyright(C) 1999-2021, 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" |
| 10 |
#include "params.h" |
| 11 |
#include "smalloc.h" |
| 12 |
#include "structs.h" |
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
void balance(struct vtx_data **graph, /* data structure for graph */ |
| 16 |
int nvtxs, /* number of vertices in full graph */ |
| 17 |
int nedges, /* number of edges in graph */ |
| 18 |
int using_vwgts, /* are vertex weights being used? */ |
| 19 |
int using_ewgts, /* are edge weights being used? */ |
| 20 |
double *vwsqrt, /* sqrt of vertex weights (length nvtxs+1) */ |
| 21 |
int igeom, /* geometric dimension for inertial method */ |
| 22 |
float **coords, /* coordinates for inertial method */ |
| 23 |
int *assignment, /* set number of each vtx (length n) */ |
| 24 |
double *goal, /* desired set sizes */ |
| 25 |
int architecture, /* 0=> hypercube, d=> d-dimensional mesh */ |
| 26 |
int ndims_tot, /* number of cuts to make in total */ |
| 27 |
int *mesh_dims, /* shape of mesh */ |
| 28 |
int global_method, /* global partitioning algorithm */ |
| 29 |
int local_method, /* local partitioning algorithm */ |
| 30 |
int rqi_flag, /* should I use multilevel eigensolver? */ |
| 31 |
int vmax, /* if so, how many vertices to coarsen down to? */ |
| 32 |
int ndims, /* number of eigenvectors (2^d sets) */ |
| 33 |
double eigtol, /* tolerance on eigenvectors */ |
| 34 |
int (*hops)[MAXSETS] /* between-set hop cost for KL */ |
| 35 |
) |
| 36 |
{ |
| 37 |
extern int TERM_PROP; /* invoking terminal propagation? */ |
| 38 |
extern int DEBUG_TRACE; /* trace the execution of the code */ |
| 39 |
extern int MATCH_TYPE; /* type of matching to use when coarsening */ |
| 40 |
struct vtx_data **subgraph = NULL; /* data structure for subgraph */ |
| 41 |
struct set_info *set_info = NULL; /* information about each processor subset */ |
| 42 |
struct set_info **set_buckets = NULL; /* buckets for sorting processor sets */ |
| 43 |
struct set_info *set = NULL; /* current processor set information */ |
| 44 |
int hops_special[MAXSETS][MAXSETS]; /* hop mtx for nonstandard cases */ |
| 45 |
float *term_wgts; /* net pull of terminal propagation */ |
| 46 |
float *save_term_wgts; /* saved location of term_wgts */ |
| 47 |
float *all_term_wgts[MAXSETS]; /* net pull on all sets */ |
| 48 |
int *loc2glob; /* mapping from subgraph to graph numbering */ |
| 49 |
int *glob2loc; /* mapping from graph to subgraph numbering */ |
| 50 |
int *setlists; /* space for linked lists of vertices in sets */ |
| 51 |
int *list_ptrs; /* headers of each linked list */ |
| 52 |
int *degree; /* degrees of graph vertices from a subgraph */ |
| 53 |
int subsets[MAXSETS]; /* subsets being created at current step */ |
| 54 |
int setsize[MAXSETS]; /* sizes of sets created by division */ |
| 55 |
double merged_goal[MAXSETS]; /* sizes of sets at this partition level */ |
| 56 |
double sub_vwgt_sum; /* sum of subgraph vertex weights */ |
| 57 |
int cut_dirs[MAXDIMS]; /* directions of processor cuts if mesh */ |
| 58 |
int hops_flag; /* use normal or special hops? */ |
| 59 |
int ndims_real; /* actual dimension of partitioning */ |
| 60 |
int nsets_real; /* actual # subsets to create */ |
| 61 |
int maxsize; /* size of largest subgraph */ |
| 62 |
int nsets_tot; /* total sets to divide subgraph into */ |
| 63 |
int subnvtxs; /* number of vertices in subgraph */ |
| 64 |
int subnedges; /* number of edgess in subgraph */ |
| 65 |
double *subvwsqrt = NULL; /* vwsqrt array for subgraph */ |
| 66 |
int *subassign = NULL; /* set assignments for subgraph */ |
| 67 |
float **subcoords = NULL; /* coordinates for subgraph */ |
| 68 |
int striping; /* partition with parallel cuts? */ |
| 69 |
int pass; /* counts passes through loop */ |
| 70 |
int max_proc_size; /* size of largest processor set */ |
| 71 |
int old_max_proc_size; /* previous size of largest processor set */ |
| 72 |
int new_dir; /* new cut direction? => no terminal prop */ |
| 73 |
int nsets; /* typical number of sets at each cut */ |
| 74 |
int done_dir[3]; /* mesh directions already cut */ |
| 75 |
int i; /* loop counter */ |
| 76 |
|
| 77 |
if (DEBUG_TRACE > 0) { |
| 78 |
printf("<Entering balance>\n"); |
| 79 |
} |
| 80 |
|
| 81 |
if (global_method != 7) { /* Not read from file. */ |
| 82 |
for (i = 1; i <= nvtxs; i++) { |
| 83 |
assignment[i] = 0; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (nvtxs <= 1) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
/* Compute some simple parameters. */ |
| 92 |
save_term_wgts = NULL; |
| 93 |
maxsize = 0; |
| 94 |
nsets = 1 << ndims; |
| 95 |
subsets[2] = 2; /* Needed for vertex separators */ |
| 96 |
|
| 97 |
nsets_tot = 0; |
| 98 |
if (architecture > 0) { |
| 99 |
nsets_tot = mesh_dims[0] * mesh_dims[1] * mesh_dims[2]; |
| 100 |
} |
| 101 |
else if (architecture == 0) { |
| 102 |
nsets_tot = 1 << ndims_tot; |
| 103 |
} |
| 104 |
|
| 105 |
/* Construct data structures for keeping track of processor sets. */ |
| 106 |
set_buckets = smalloc((nsets_tot + 1) * sizeof(struct set_info *)); |
| 107 |
set_info = smalloc(nsets_tot * sizeof(struct set_info)); |
| 108 |
for (i = 0; i < nsets_tot; i++) { |
| 109 |
set_info[i].setnum = i; |
| 110 |
set_info[i].ndims = -1; |
| 111 |
set_info[i].span[0] = -1; |
| 112 |
set_info[i].next = NULL; |
| 113 |
set_buckets[i + 1] = NULL; |
| 114 |
} |
| 115 |
set_buckets[nsets_tot] = &(set_info[0]); |
| 116 |
|
| 117 |
if (architecture > 0) { |
| 118 |
set_info[0].low[0] = set_info[0].low[1] = set_info[0].low[2] = 0; |
| 119 |
set_info[0].span[0] = mesh_dims[0]; |
| 120 |
set_info[0].span[1] = mesh_dims[1]; |
| 121 |
set_info[0].span[2] = mesh_dims[2]; |
| 122 |
} |
| 123 |
else if (architecture == 0) { |
| 124 |
set_info[0].ndims = ndims_tot; |
| 125 |
} |
| 126 |
|
| 127 |
done_dir[0] = done_dir[1] = done_dir[2] = FALSE; |
| 128 |
pass = 0; |
| 129 |
loc2glob = NULL; |
| 130 |
degree = NULL; |
| 131 |
glob2loc = NULL; |
| 132 |
setlists = NULL; |
| 133 |
list_ptrs = NULL; |
| 134 |
|
| 135 |
old_max_proc_size = 2 * nsets_tot; |
| 136 |
max_proc_size = nsets_tot; |
| 137 |
|
| 138 |
while (max_proc_size > 1) { /* Some set still needs to be divided. */ |
| 139 |
|
| 140 |
pass++; |
| 141 |
set = set_buckets[max_proc_size]; |
| 142 |
set_buckets[max_proc_size] = set->next; |
| 143 |
|
| 144 |
/* Divide the processors. */ |
| 145 |
hops_flag = |
| 146 |
divide_procs(architecture, ndims, ndims_tot, set_info, set, subsets, (global_method == 3), |
| 147 |
&ndims_real, &nsets_real, &striping, cut_dirs, mesh_dims, hops_special); |
| 148 |
|
| 149 |
/* If new cut direction, turn off terminal propagation. */ |
| 150 |
new_dir = FALSE; |
| 151 |
if (architecture == 0) { |
| 152 |
if (old_max_proc_size != max_proc_size) { |
| 153 |
new_dir = TRUE; |
| 154 |
} |
| 155 |
} |
| 156 |
else if (architecture > 0) { |
| 157 |
if (!done_dir[cut_dirs[0]]) { |
| 158 |
new_dir = TRUE; |
| 159 |
} |
| 160 |
done_dir[cut_dirs[0]] = TRUE; |
| 161 |
} |
| 162 |
old_max_proc_size = max_proc_size; |
| 163 |
|
| 164 |
/* Now place the new sets into the set_info data structure. */ |
| 165 |
/* This is indexed by number of processors in set. */ |
| 166 |
for (i = 0; i < nsets_real; i++) { |
| 167 |
int j = 0; |
| 168 |
if (architecture > 0) { |
| 169 |
j = set_info[subsets[i]].span[0] * set_info[subsets[i]].span[1] * |
| 170 |
set_info[subsets[i]].span[2]; |
| 171 |
} |
| 172 |
else if (architecture == 0) { |
| 173 |
j = 1 << set_info[subsets[i]].ndims; |
| 174 |
} |
| 175 |
set_info[subsets[i]].next = set_buckets[j]; |
| 176 |
set_buckets[j] = &(set_info[subsets[i]]); |
| 177 |
} |
| 178 |
|
| 179 |
/* Construct desired set sizes for this division step. */ |
| 180 |
if (pass == 1) { /* First partition. */ |
| 181 |
subgraph = graph; |
| 182 |
subnvtxs = nvtxs; |
| 183 |
subnedges = nedges; |
| 184 |
subvwsqrt = vwsqrt; |
| 185 |
subcoords = coords; |
| 186 |
subassign = assignment; |
| 187 |
all_term_wgts[1] = NULL; |
| 188 |
|
| 189 |
if (!using_vwgts) { |
| 190 |
sub_vwgt_sum = subnvtxs; |
| 191 |
} |
| 192 |
else { |
| 193 |
sub_vwgt_sum = 0; |
| 194 |
for (i = 1; i <= subnvtxs; i++) { |
| 195 |
sub_vwgt_sum += subgraph[i]->vwgt; |
| 196 |
} |
| 197 |
} |
| 198 |
merge_goals(goal, merged_goal, set_info, subsets, nsets_real, ndims_tot, architecture, |
| 199 |
mesh_dims, sub_vwgt_sum); |
| 200 |
} |
| 201 |
|
| 202 |
else { /* Not the first cut. */ |
| 203 |
|
| 204 |
/* After first cut, allocate all space we'll need. */ |
| 205 |
if (pass == 2) { |
| 206 |
glob2loc = smalloc((nvtxs + 1) * sizeof(int)); |
| 207 |
loc2glob = smalloc((maxsize + 1) * sizeof(int)); |
| 208 |
if (!using_vwgts) { |
| 209 |
subvwsqrt = NULL; |
| 210 |
} |
| 211 |
else { |
| 212 |
subvwsqrt = smalloc((maxsize + 1) * sizeof(double)); |
| 213 |
} |
| 214 |
|
| 215 |
if (graph != NULL) { |
| 216 |
subgraph = smalloc((maxsize + 1) * sizeof(struct vtx_data *)); |
| 217 |
degree = smalloc((maxsize + 1) * sizeof(int)); |
| 218 |
} |
| 219 |
else { |
| 220 |
subgraph = NULL; |
| 221 |
} |
| 222 |
subassign = smalloc((maxsize + 1) * sizeof(int)); |
| 223 |
if (global_method == 3 || |
| 224 |
(MATCH_TYPE == 5 && (global_method == 1 || (global_method == 2 && rqi_flag)))) { |
| 225 |
subcoords = smalloc(3 * sizeof(float *)); |
| 226 |
subcoords[1] = subcoords[2] = NULL; |
| 227 |
subcoords[0] = smalloc((maxsize + 1) * sizeof(float)); |
| 228 |
if (igeom > 1) { |
| 229 |
subcoords[1] = smalloc((maxsize + 1) * sizeof(float)); |
| 230 |
} |
| 231 |
if (igeom > 2) { |
| 232 |
subcoords[2] = smalloc((maxsize + 1) * sizeof(float)); |
| 233 |
} |
| 234 |
} |
| 235 |
else { |
| 236 |
subcoords = NULL; |
| 237 |
} |
| 238 |
if (TERM_PROP && graph != NULL) { |
| 239 |
term_wgts = smalloc((nsets - 1) * (maxsize + 1) * sizeof(float)); |
| 240 |
save_term_wgts = term_wgts; |
| 241 |
for (i = 1; i < nsets; i++) { |
| 242 |
all_term_wgts[i] = term_wgts; |
| 243 |
term_wgts += maxsize + 1; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* Construct mappings between local and global vertex numbering */ |
| 249 |
subnvtxs = make_maps(setlists, list_ptrs, set->setnum, glob2loc, loc2glob); |
| 250 |
|
| 251 |
if (TERM_PROP && !new_dir && graph != NULL) { |
| 252 |
all_term_wgts[1] = save_term_wgts; |
| 253 |
make_term_props(graph, subnvtxs, loc2glob, assignment, architecture, ndims_tot, ndims_real, |
| 254 |
set_info, set->setnum, nsets_real, nsets_tot, subsets, all_term_wgts, |
| 255 |
using_ewgts); |
| 256 |
} |
| 257 |
else { |
| 258 |
all_term_wgts[1] = NULL; |
| 259 |
} |
| 260 |
|
| 261 |
/* Form the subgraph in our graph format. */ |
| 262 |
if (graph != NULL) { |
| 263 |
make_subgraph(graph, subgraph, subnvtxs, &subnedges, assignment, set->setnum, glob2loc, |
| 264 |
loc2glob, degree, using_ewgts); |
| 265 |
} |
| 266 |
else { |
| 267 |
subnedges = 0; /* Otherwise some output is garbage */ |
| 268 |
} |
| 269 |
|
| 270 |
if (!using_vwgts) { |
| 271 |
sub_vwgt_sum = subnvtxs; |
| 272 |
} |
| 273 |
else { |
| 274 |
sub_vwgt_sum = 0; |
| 275 |
for (i = 1; i <= subnvtxs; i++) { |
| 276 |
sub_vwgt_sum += subgraph[i]->vwgt; |
| 277 |
} |
| 278 |
} |
| 279 |
merge_goals(goal, merged_goal, set_info, subsets, nsets_real, ndims_tot, architecture, |
| 280 |
mesh_dims, sub_vwgt_sum); |
| 281 |
|
| 282 |
/* Condense the relevant vertex weight array. */ |
| 283 |
if (using_vwgts && vwsqrt != NULL) { |
| 284 |
make_subvector(vwsqrt, subvwsqrt, subnvtxs, loc2glob); |
| 285 |
} |
| 286 |
|
| 287 |
if (global_method == 3 || |
| 288 |
(MATCH_TYPE == 5 && (global_method == 1 || (global_method == 2 && rqi_flag)))) { |
| 289 |
make_subgeom(igeom, coords, subcoords, subnvtxs, loc2glob); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (DEBUG_TRACE > 1) { |
| 294 |
printf("About to call divide with nvtxs = %d, nedges = %d, ", subnvtxs, subnedges); |
| 295 |
if (!architecture) { |
| 296 |
printf("ndims = %d\n", set->ndims); |
| 297 |
} |
| 298 |
else if (architecture == 1) { |
| 299 |
printf("mesh = %d\n", set->span[0]); |
| 300 |
} |
| 301 |
else if (architecture == 2) { |
| 302 |
printf("mesh = %dx%d\n", set->span[0], set->span[1]); |
| 303 |
} |
| 304 |
else if (architecture == 3) { |
| 305 |
printf("mesh = %dx%dx%d\n", set->span[0], set->span[1], set->span[2]); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/* Perform a single division step. */ |
| 310 |
chaco_divide(subgraph, subnvtxs, subnedges, using_vwgts, using_ewgts, subvwsqrt, igeom, |
| 311 |
subcoords, subassign, merged_goal, architecture, all_term_wgts, global_method, |
| 312 |
local_method, rqi_flag, vmax, ndims_real, eigtol, |
| 313 |
(hops_flag ? hops_special : hops), nsets_real, striping); |
| 314 |
|
| 315 |
/* Undo the subgraph construction. */ |
| 316 |
if (pass != 1 && graph != NULL) { |
| 317 |
remake_graph(subgraph, subnvtxs, loc2glob, degree, using_ewgts); |
| 318 |
} |
| 319 |
|
| 320 |
/* Prepare for next division */ |
| 321 |
while (max_proc_size > 1 && set_buckets[max_proc_size] == NULL) { |
| 322 |
--max_proc_size; |
| 323 |
} |
| 324 |
|
| 325 |
/* Merge the subgraph partitioning with the graph partitioning. */ |
| 326 |
if (pass == 1) { |
| 327 |
subgraph = NULL; |
| 328 |
subvwsqrt = NULL; |
| 329 |
subcoords = NULL; |
| 330 |
subassign = NULL; |
| 331 |
|
| 332 |
/* Find size of largest subgraph for recursing. */ |
| 333 |
if (max_proc_size > 1) { |
| 334 |
for (i = 0; i < nsets; i++) { |
| 335 |
setsize[i] = 0; |
| 336 |
} |
| 337 |
for (i = 1; i <= nvtxs; i++) { |
| 338 |
++setsize[assignment[i]]; |
| 339 |
} |
| 340 |
maxsize = 0; |
| 341 |
for (i = 0; i < nsets; i++) { |
| 342 |
if (setsize[i] > maxsize) { |
| 343 |
maxsize = setsize[i]; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
for (i = 1; i <= nvtxs; i++) { |
| 349 |
assignment[i] = subsets[assignment[i]]; |
| 350 |
} |
| 351 |
|
| 352 |
/* Construct list of vertices in sets for make_maps. */ |
| 353 |
if (max_proc_size > 1) { |
| 354 |
setlists = smalloc((nvtxs + 1) * sizeof(int)); |
| 355 |
list_ptrs = smalloc(nsets_tot * sizeof(int)); |
| 356 |
|
| 357 |
make_setlists(setlists, list_ptrs, nsets_real, subsets, assignment, loc2glob, nvtxs, TRUE); |
| 358 |
} |
| 359 |
} |
| 360 |
else { |
| 361 |
/* Construct list of vertices in sets for make_maps. */ |
| 362 |
if (max_proc_size > 1) { |
| 363 |
make_setlists(setlists, list_ptrs, nsets_real, subsets, subassign, loc2glob, subnvtxs, |
| 364 |
FALSE); |
| 365 |
} |
| 366 |
|
| 367 |
merge_assignments(assignment, subassign, subsets, subnvtxs, loc2glob); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* Free everything allocated for subgraphs. */ |
| 372 |
sfree(list_ptrs); |
| 373 |
sfree(setlists); |
| 374 |
sfree(save_term_wgts); |
| 375 |
sfree(subassign); |
| 376 |
sfree(loc2glob); |
| 377 |
sfree(glob2loc); |
| 378 |
if (graph != NULL) { |
| 379 |
sfree(degree); |
| 380 |
} |
| 381 |
if (subgraph != NULL) { |
| 382 |
sfree(subgraph); |
| 383 |
} |
| 384 |
if (subvwsqrt != NULL) { |
| 385 |
sfree(subvwsqrt); |
| 386 |
} |
| 387 |
if (subcoords != NULL) { |
| 388 |
if (subcoords[0] != NULL) { |
| 389 |
sfree(subcoords[0]); |
| 390 |
} |
| 391 |
if (subcoords[1] != NULL) { |
| 392 |
sfree(subcoords[1]); |
| 393 |
} |
| 394 |
if (subcoords[2] != NULL) { |
| 395 |
sfree(subcoords[2]); |
| 396 |
} |
| 397 |
sfree(subcoords); |
| 398 |
} |
| 399 |
sfree(set_info); |
| 400 |
sfree(set_buckets); |
| 401 |
} |