| 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" |
| 10 |
#include "params.h" |
| 11 |
#include "smalloc.h" |
| 12 |
#include "structs.h" |
| 13 |
#include <stdio.h> |
| 14 |
|
| 15 |
/* Recursively apply median to a SINGLE vector of values */ |
| 16 |
|
| 17 |
void rec_median_1(struct vtx_data **graph, /* data structure with vertex weights */ |
| 18 |
double *vals, /* values of which to find median */ |
| 19 |
int nvtxs, /* number of values I own */ |
| 20 |
int *active, /* space for list of nmyvals ints */ |
| 21 |
int cube_or_mesh, /* 0=> hypercube, other=> mesh */ |
| 22 |
int nsets, /* number of sets to divide into */ |
| 23 |
double *goal, /* desired sizes for sets */ |
| 24 |
int using_vwgts, /* are vertex weights being used? */ |
| 25 |
int *assign, /* set each vertex gets assigned to */ |
| 26 |
int top /* is this the top call in the recursion? */ |
| 27 |
) |
| 28 |
{ |
| 29 |
struct vtx_data **sub_graph; /* subgraph data structure with vertex weights */ |
| 30 |
double *sub_vals; /* subgraph entries in vals vector */ |
| 31 |
double merged_goal[MAXSETS / 2]; /* combined goal values */ |
| 32 |
double sub_vwgt_sum; /* sum of vertex weights in subgraph */ |
| 33 |
int *loc2glob; /* mapping from subgraph to graph numbering */ |
| 34 |
int mapping[MAXSETS]; /* appropriate set number */ |
| 35 |
int *sub_assign; /* assignment returned from subgraph */ |
| 36 |
int sub_nvtxs; /* number of vertices in subgraph */ |
| 37 |
int sub_nsets; /* number of sets in side of first partition */ |
| 38 |
int setsize[2]; /* number of vertices in two subsets */ |
| 39 |
int maxsize; /* number of vertices in larger subset */ |
| 40 |
int ndims; /* number of bits defining set */ |
| 41 |
int mesh_dims[3]; /* shape of fictitious mesh */ |
| 42 |
int i, j; /* loop counters */ |
| 43 |
|
| 44 |
cube_or_mesh = (cube_or_mesh != 0); |
| 45 |
mesh_dims[1] = mesh_dims[2] = 1; |
| 46 |
|
| 47 |
if (!cube_or_mesh) { |
| 48 |
for (i = 0; i < 2; i++) { |
| 49 |
merged_goal[i] = 0; |
| 50 |
for (j = i; j < nsets; j += 2) { |
| 51 |
merged_goal[i] += goal[j]; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
else { |
| 56 |
merged_goal[0] = merged_goal[1] = 0; |
| 57 |
for (i = 0; i < (nsets + 1) / 2; i++) { |
| 58 |
merged_goal[0] += goal[i]; |
| 59 |
} |
| 60 |
for (i = (nsets + 1) / 2; i < nsets; i++) { |
| 61 |
merged_goal[1] += goal[i]; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
median(graph, vals, nvtxs, active, merged_goal, using_vwgts, assign); |
| 66 |
|
| 67 |
if (nsets > 2) { |
| 68 |
/* Find size of largest subproblem. */ |
| 69 |
setsize[0] = setsize[1] = 0; |
| 70 |
for (i = 1; i <= nvtxs; i++) { |
| 71 |
++setsize[assign[i]]; |
| 72 |
} |
| 73 |
maxsize = max(setsize[0], setsize[1]); |
| 74 |
|
| 75 |
sub_assign = smalloc((maxsize + 1) * sizeof(int)); |
| 76 |
sub_vals = smalloc((maxsize + 1) * sizeof(double)); |
| 77 |
loc2glob = smalloc((maxsize + 1) * sizeof(int)); |
| 78 |
if (!using_vwgts) { |
| 79 |
sub_graph = NULL; |
| 80 |
} |
| 81 |
else { |
| 82 |
sub_graph = (struct vtx_data **)smalloc((maxsize + 1) * sizeof(struct vtx_data *)); |
| 83 |
} |
| 84 |
|
| 85 |
for (i = 0; i < 2; i++) { |
| 86 |
/* Construct subproblem. */ |
| 87 |
if (i == 0) { |
| 88 |
sub_nsets = (nsets + 1) / 2; |
| 89 |
} |
| 90 |
else { |
| 91 |
sub_nsets = nsets / 2; |
| 92 |
} |
| 93 |
sub_nvtxs = setsize[i]; |
| 94 |
|
| 95 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 96 |
sub_assign[j] = 0; |
| 97 |
} |
| 98 |
make_maps2(assign, nvtxs, i, (int *)NULL, loc2glob); |
| 99 |
|
| 100 |
if (sub_nsets > 1) { |
| 101 |
|
| 102 |
if (!using_vwgts) { |
| 103 |
sub_vwgt_sum = sub_nvtxs; |
| 104 |
} |
| 105 |
else { |
| 106 |
sub_vwgt_sum = 0; |
| 107 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 108 |
sub_graph[j] = graph[loc2glob[j]]; |
| 109 |
sub_vwgt_sum += sub_graph[j]->vwgt; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
make_subvector(vals, sub_vals, sub_nvtxs, loc2glob); |
| 114 |
|
| 115 |
mesh_dims[0] = nsets; |
| 116 |
make_subgoal(goal, merged_goal, 2, cube_or_mesh, nsets, mesh_dims, i, sub_vwgt_sum); |
| 117 |
|
| 118 |
rec_median_1(sub_graph, sub_vals, sub_nvtxs, active, cube_or_mesh, sub_nsets, merged_goal, |
| 119 |
using_vwgts, sub_assign, FALSE); |
| 120 |
} |
| 121 |
/* Merge subassignment with current assignment. */ |
| 122 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 123 |
assign[loc2glob[j]] |= (sub_assign[j] << 1); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/* Now I have the set striped in bit reversed order. */ |
| 128 |
if (top) { |
| 129 |
ndims = 0; |
| 130 |
for (i = 1; i < nsets; i <<= 1) { |
| 131 |
ndims++; |
| 132 |
} |
| 133 |
|
| 134 |
for (i = 0; i < nsets; i++) { |
| 135 |
mapping[i] = bit_reverse(i, ndims); |
| 136 |
} |
| 137 |
|
| 138 |
for (i = 1; i <= nvtxs; i++) { |
| 139 |
assign[i] = mapping[assign[i]]; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (sub_graph != NULL) { |
| 144 |
sfree(sub_graph); |
| 145 |
} |
| 146 |
sfree(loc2glob); |
| 147 |
sfree(sub_vals); |
| 148 |
sfree(sub_assign); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* Recursively apply median to a SEVERAL vectors of values */ |
| 153 |
/* Divide with first, and use result to divide with second, etc. */ |
| 154 |
/* Note: currently only works for power-of-two number of processors. */ |
| 155 |
|
| 156 |
void rec_median_k(struct vtx_data **graph, /* data structure with vertex weights */ |
| 157 |
double **vals, /* values of which to find median */ |
| 158 |
int nvtxs, /* number of values I own */ |
| 159 |
int *active, /* space for list of nmyvals ints */ |
| 160 |
int ndims, /* number of dimensions to divide */ |
| 161 |
int cube_or_mesh, /* 0 => hypercube, d => d-dimensional mesh */ |
| 162 |
double *goal, /* desired sizes for sets */ |
| 163 |
int using_vwgts, /* are vertex weights being used? */ |
| 164 |
int *assign /* set each vertex gets assigned to */ |
| 165 |
) |
| 166 |
{ |
| 167 |
struct vtx_data **sub_graph; /* subgraph data structure with vertex weights */ |
| 168 |
double *sub_vals[MAXDIMS]; /* subgraph entries in vals vectors */ |
| 169 |
double merged_goal[MAXSETS / 2]; /* combined goal values */ |
| 170 |
double sub_vwgt_sum; /* sum of vertex weights in subgraph */ |
| 171 |
int *loc2glob; /* mapping from subgraph to graph numbering */ |
| 172 |
int *sub_assign; /* assignment returned from subgraph */ |
| 173 |
int sub_nvtxs; /* number of vertices in subgraph */ |
| 174 |
int setsize[2]; /* number of vertices in two subsets */ |
| 175 |
int maxsize; /* number of vertices in larger subset */ |
| 176 |
int nsets; /* number of sets we are dividing into */ |
| 177 |
int mesh_dims[3]; /* shape of fictitious mesh */ |
| 178 |
int i, j; /* loop counters */ |
| 179 |
|
| 180 |
mesh_dims[1] = mesh_dims[2] = 1; |
| 181 |
|
| 182 |
/* Note: This is HYPERCUBE/MESH dependent. We'll want to combine the */ |
| 183 |
/* sizes of different sets on the different architectures. */ |
| 184 |
nsets = 1 << ndims; |
| 185 |
for (i = 0; i < 2; i++) { |
| 186 |
merged_goal[i] = 0; |
| 187 |
for (j = i; j < nsets; j += 2) { |
| 188 |
merged_goal[i] += goal[j]; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
median(graph, vals[1], nvtxs, active, merged_goal, using_vwgts, assign); |
| 193 |
|
| 194 |
if (ndims > 1) { |
| 195 |
/* Find size of largest subproblem. */ |
| 196 |
setsize[0] = setsize[1] = 0; |
| 197 |
for (i = 1; i <= nvtxs; i++) { |
| 198 |
++setsize[assign[i]]; |
| 199 |
} |
| 200 |
maxsize = max(setsize[0], setsize[1]); |
| 201 |
|
| 202 |
sub_assign = smalloc((maxsize + 1) * sizeof(int)); |
| 203 |
for (i = 1; i < ndims; i++) { |
| 204 |
sub_vals[i] = smalloc((maxsize + 1) * sizeof(double)); |
| 205 |
} |
| 206 |
loc2glob = smalloc((maxsize + 1) * sizeof(int)); |
| 207 |
if (!using_vwgts) { |
| 208 |
sub_graph = NULL; |
| 209 |
} |
| 210 |
else { |
| 211 |
sub_graph = (struct vtx_data **)smalloc((maxsize + 1) * sizeof(struct vtx_data *)); |
| 212 |
} |
| 213 |
|
| 214 |
for (i = 0; i < 2; i++) { |
| 215 |
/* Construct subproblem. */ |
| 216 |
|
| 217 |
sub_nvtxs = setsize[i]; |
| 218 |
|
| 219 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 220 |
sub_assign[j] = 0; |
| 221 |
} |
| 222 |
|
| 223 |
make_maps2(assign, nvtxs, i, (int *)NULL, loc2glob); |
| 224 |
|
| 225 |
if (!using_vwgts) { |
| 226 |
sub_vwgt_sum = sub_nvtxs; |
| 227 |
} |
| 228 |
else { |
| 229 |
sub_vwgt_sum = 0; |
| 230 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 231 |
sub_graph[j] = graph[loc2glob[j]]; |
| 232 |
sub_vwgt_sum += sub_graph[j]->vwgt; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
for (j = 2; j <= ndims; j++) { |
| 237 |
make_subvector(vals[j], sub_vals[j - 1], sub_nvtxs, loc2glob); |
| 238 |
} |
| 239 |
|
| 240 |
mesh_dims[0] = nsets; |
| 241 |
make_subgoal(goal, merged_goal, 2, cube_or_mesh, nsets, mesh_dims, i, sub_vwgt_sum); |
| 242 |
|
| 243 |
rec_median_k(sub_graph, sub_vals, sub_nvtxs, active, ndims - 1, cube_or_mesh, merged_goal, |
| 244 |
using_vwgts, sub_assign); |
| 245 |
|
| 246 |
/* Merge subassignment with current assignment. */ |
| 247 |
for (j = 1; j <= sub_nvtxs; j++) { |
| 248 |
assign[loc2glob[j]] |= (sub_assign[j] << 1); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
sfree(sub_graph); |
| 253 |
sfree(loc2glob); |
| 254 |
for (i = 1; i < ndims; i++) { |
| 255 |
sfree(sub_vals[i]); |
| 256 |
} |
| 257 |
sfree(sub_assign); |
| 258 |
} |
| 259 |
} |