| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief Functions for computing matchings during graph coarsening |
| 4 |
|
| 5 |
\date Started 7/23/97 |
| 6 |
\author George |
| 7 |
\author Copyright 1997-2011, Regents of the University of Minnesota |
| 8 |
\version\verbatim $Id: coarsen.c 20398 2016-11-22 17:17:12Z karypis $ \endverbatim |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
#include "metislib.h" |
| 13 |
|
| 14 |
#define UNMATCHEDFOR2HOP 0.10 /* The fraction of unmatched vertices that triggers 2-hop */ |
| 15 |
|
| 16 |
|
| 17 |
/*************************************************************************/ |
| 18 |
/*! This function takes a graph and creates a sequence of coarser graphs. |
| 19 |
It implements the coarsening phase of the multilevel paradigm. |
| 20 |
*/ |
| 21 |
/*************************************************************************/ |
| 22 |
graph_t *CoarsenGraph(ctrl_t *ctrl, graph_t *graph) |
| 23 |
{ |
| 24 |
idx_t i, eqewgts, level=0; |
| 25 |
|
| 26 |
|
| 27 |
/* determine if the weights on the edges are all the same */ |
| 28 |
for (eqewgts=1, i=1; i<graph->nedges; i++) { |
| 29 |
if (graph->adjwgt[0] != graph->adjwgt[i]) { |
| 30 |
eqewgts = 0; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/* set the maximum allowed coarsest vertex weight */ |
| 36 |
for (i=0; i<graph->ncon; i++) |
| 37 |
ctrl->maxvwgt[i] = 1.5*graph->tvwgt[i]/ctrl->CoarsenTo; |
| 38 |
|
| 39 |
do { |
| 40 |
IFSET(ctrl->dbglvl, METIS_DBG_COARSEN, PrintCGraphStats(ctrl, graph)); |
| 41 |
|
| 42 |
/* allocate memory for cmap, if it has not already been done due to |
| 43 |
multiple cuts */ |
| 44 |
if (graph->cmap == NULL) |
| 45 |
graph->cmap = imalloc(graph->nvtxs); |
| 46 |
|
| 47 |
/* determine which matching scheme you will use */ |
| 48 |
switch (ctrl->ctype) { |
| 49 |
case METIS_CTYPE_RM: |
| 50 |
Match_RM(ctrl, graph); |
| 51 |
break; |
| 52 |
case METIS_CTYPE_SHEM: |
| 53 |
if (eqewgts || graph->nedges == 0) |
| 54 |
Match_RM(ctrl, graph); |
| 55 |
else |
| 56 |
Match_SHEM(ctrl, graph); |
| 57 |
break; |
| 58 |
default: |
| 59 |
GK_ERREXIT("Unknown ctype: %d\n", ctrl->ctype); |
| 60 |
} |
| 61 |
|
| 62 |
graph = graph->coarser; |
| 63 |
eqewgts = 0; |
| 64 |
level++; |
| 65 |
|
| 66 |
|
| 67 |
} while (graph->nvtxs > ctrl->CoarsenTo && |
| 68 |
graph->nvtxs < COARSEN_FRACTION*graph->finer->nvtxs && |
| 69 |
graph->nedges > graph->nvtxs/2); |
| 70 |
|
| 71 |
IFSET(ctrl->dbglvl, METIS_DBG_COARSEN, PrintCGraphStats(ctrl, graph)); |
| 72 |
|
| 73 |
return graph; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/*************************************************************************/ |
| 78 |
/*! This function takes a graph and creates a sequence of nlevels coarser |
| 79 |
graphs, where nlevels is an input parameter. |
| 80 |
*/ |
| 81 |
/*************************************************************************/ |
| 82 |
graph_t *CoarsenGraphNlevels(ctrl_t *ctrl, graph_t *graph, idx_t nlevels) |
| 83 |
{ |
| 84 |
idx_t i, eqewgts, level; |
| 85 |
|
| 86 |
|
| 87 |
/* determine if the weights on the edges are all the same */ |
| 88 |
for (eqewgts=1, i=1; i<graph->nedges; i++) { |
| 89 |
if (graph->adjwgt[0] != graph->adjwgt[i]) { |
| 90 |
eqewgts = 0; |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/* set the maximum allowed coarsest vertex weight */ |
| 96 |
for (i=0; i<graph->ncon; i++) |
| 97 |
ctrl->maxvwgt[i] = 1.5*graph->tvwgt[i]/ctrl->CoarsenTo; |
| 98 |
|
| 99 |
for (level=0; level<nlevels; level++) { |
| 100 |
IFSET(ctrl->dbglvl, METIS_DBG_COARSEN, PrintCGraphStats(ctrl, graph)); |
| 101 |
|
| 102 |
/* allocate memory for cmap, if it has not already been done due to |
| 103 |
multiple cuts */ |
| 104 |
if (graph->cmap == NULL) |
| 105 |
graph->cmap = imalloc(graph->nvtxs); |
| 106 |
|
| 107 |
/* determine which matching scheme you will use */ |
| 108 |
switch (ctrl->ctype) { |
| 109 |
case METIS_CTYPE_RM: |
| 110 |
Match_RM(ctrl, graph); |
| 111 |
break; |
| 112 |
case METIS_CTYPE_SHEM: |
| 113 |
if (eqewgts || graph->nedges == 0) |
| 114 |
Match_RM(ctrl, graph); |
| 115 |
else |
| 116 |
Match_SHEM(ctrl, graph); |
| 117 |
break; |
| 118 |
default: |
| 119 |
GK_ERREXIT("Unknown ctype: %d\n", ctrl->ctype); |
| 120 |
} |
| 121 |
|
| 122 |
graph = graph->coarser; |
| 123 |
eqewgts = 0; |
| 124 |
|
| 125 |
|
| 126 |
if (graph->nvtxs < ctrl->CoarsenTo || |
| 127 |
graph->nvtxs > COARSEN_FRACTION*graph->finer->nvtxs || |
| 128 |
graph->nedges < graph->nvtxs/2) |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
IFSET(ctrl->dbglvl, METIS_DBG_COARSEN, PrintCGraphStats(ctrl, graph)); |
| 133 |
|
| 134 |
return graph; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/*************************************************************************/ |
| 139 |
/*! This function finds a matching by randomly selecting one of the |
| 140 |
unmatched adjacent vertices. |
| 141 |
*/ |
| 142 |
/**************************************************************************/ |
| 143 |
idx_t Match_RM(ctrl_t *ctrl, graph_t *graph) |
| 144 |
{ |
| 145 |
idx_t i, pi, ii, j, jj, jjinc, k, nvtxs, ncon, cnvtxs, maxidx, |
| 146 |
last_unmatched, avgdegree, bnum; |
| 147 |
const idx_t *xadj, *adjncy; |
| 148 |
idx_t *vwgt, *adjwgt, *maxvwgt; |
| 149 |
idx_t *match, *cmap, *degrees, *perm, *tperm; |
| 150 |
size_t nunmatched=0; |
| 151 |
|
| 152 |
WCOREPUSH; |
| 153 |
|
| 154 |
|
| 155 |
nvtxs = graph->nvtxs; |
| 156 |
ncon = graph->ncon; |
| 157 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 158 |
vwgt = graph->vwgt; |
| 159 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 160 |
adjwgt = graph->adjwgt; |
| 161 |
cmap = graph->cmap; |
| 162 |
|
| 163 |
maxvwgt = ctrl->maxvwgt; |
| 164 |
|
| 165 |
match = iset(nvtxs, UNMATCHED, iwspacemalloc(ctrl, nvtxs)); |
| 166 |
perm = iwspacemalloc(ctrl, nvtxs); |
| 167 |
tperm = iwspacemalloc(ctrl, nvtxs); |
| 168 |
degrees = iwspacemalloc(ctrl, nvtxs); |
| 169 |
|
| 170 |
/* Determine a "random" traversal order that is biased towards |
| 171 |
low-degree vertices */ |
| 172 |
irandArrayPermute(nvtxs, tperm, nvtxs/8, 1, &ctrl->rng_state); |
| 173 |
|
| 174 |
avgdegree = 4.0*(xadj[nvtxs]/nvtxs); |
| 175 |
for (i=0; i<nvtxs; i++) { |
| 176 |
bnum = sqrt(1+xadj[i+1]-xadj[i]); |
| 177 |
degrees[i] = (bnum > avgdegree ? avgdegree : bnum); |
| 178 |
} |
| 179 |
BucketSortKeysInc(ctrl, nvtxs, avgdegree, degrees, tperm, perm); |
| 180 |
|
| 181 |
|
| 182 |
/* Traverse the vertices and compute the matching */ |
| 183 |
for (cnvtxs=0, last_unmatched=0, pi=0; pi<nvtxs; pi++) { |
| 184 |
i = perm[pi]; |
| 185 |
|
| 186 |
if (match[i] == UNMATCHED) { /* Unmatched */ |
| 187 |
maxidx = i; |
| 188 |
|
| 189 |
if ((ncon == 1 ? vwgt[i] < maxvwgt[0] : ivecle(ncon, vwgt+i*ncon, maxvwgt))) { |
| 190 |
/* Deal with island vertices. Find a non-island and match it with. |
| 191 |
The matching ignores ctrl->maxvwgt requirements */ |
| 192 |
if (xadj[i] == xadj[i+1]) { |
| 193 |
last_unmatched = gk_max(pi, last_unmatched)+1; |
| 194 |
for (; last_unmatched<nvtxs; last_unmatched++) { |
| 195 |
j = perm[last_unmatched]; |
| 196 |
if (match[j] == UNMATCHED) { |
| 197 |
maxidx = j; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
else { |
| 203 |
/* Find a random matching, subject to maxvwgt constraints */ |
| 204 |
if (ncon == 1) { |
| 205 |
/* single constraint version */ |
| 206 |
for (j=xadj[i]; j<xadj[i+1]; j++) { |
| 207 |
k = adjncy[j]; |
| 208 |
if (match[k] == UNMATCHED && vwgt[i]+vwgt[k] <= maxvwgt[0]) { |
| 209 |
maxidx = k; |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
/* If it did not match, record for a 2-hop matching. */ |
| 215 |
if (maxidx == i && 2*vwgt[i] < maxvwgt[0]) { |
| 216 |
nunmatched++; |
| 217 |
maxidx = UNMATCHED; |
| 218 |
} |
| 219 |
} |
| 220 |
else { |
| 221 |
/* multi-constraint version */ |
| 222 |
for (j=xadj[i]; j<xadj[i+1]; j++) { |
| 223 |
k = adjncy[j]; |
| 224 |
if (match[k] == UNMATCHED && |
| 225 |
ivecaxpylez(ncon, 1, vwgt+i*ncon, vwgt+k*ncon, maxvwgt)) { |
| 226 |
maxidx = k; |
| 227 |
break; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/* If it did not match, record for a 2-hop matching. */ |
| 232 |
if (maxidx == i && ivecaxpylez(ncon, 2, vwgt+i*ncon, vwgt+i*ncon, maxvwgt)) { |
| 233 |
nunmatched++; |
| 234 |
maxidx = UNMATCHED; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
if (maxidx != UNMATCHED) { |
| 241 |
cmap[i] = cmap[maxidx] = cnvtxs++; |
| 242 |
match[i] = maxidx; |
| 243 |
match[maxidx] = i; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
/* see if a 2-hop matching is required/allowed */ |
| 249 |
if (!ctrl->no2hop && nunmatched > UNMATCHEDFOR2HOP*nvtxs) |
| 250 |
cnvtxs = Match_2Hop(ctrl, graph, perm, match, cnvtxs, nunmatched); |
| 251 |
|
| 252 |
|
| 253 |
/* match the final unmatched vertices with themselves and reorder the vertices |
| 254 |
of the coarse graph for memory-friendly contraction */ |
| 255 |
for (cnvtxs=0, i=0; i<nvtxs; i++) { |
| 256 |
if (match[i] == UNMATCHED) { |
| 257 |
match[i] = i; |
| 258 |
cmap[i] = cnvtxs++; |
| 259 |
} |
| 260 |
else { |
| 261 |
if (i <= match[i]) |
| 262 |
cmap[i] = cmap[match[i]] = cnvtxs++; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
CreateCoarseGraph(ctrl, graph, cnvtxs, match); |
| 268 |
|
| 269 |
WCOREPOP; |
| 270 |
|
| 271 |
return cnvtxs; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
/**************************************************************************/ |
| 276 |
/*! This function finds a matching using the HEM heuristic. The vertices |
| 277 |
are visited based on increasing degree to ensure that all vertices are |
| 278 |
given a chance to match with something. |
| 279 |
*/ |
| 280 |
/**************************************************************************/ |
| 281 |
idx_t Match_SHEM(ctrl_t *ctrl, graph_t *graph) |
| 282 |
{ |
| 283 |
idx_t i, pi, ii, j, jj, jjinc, k, nvtxs, ncon, cnvtxs, maxidx, maxwgt, |
| 284 |
last_unmatched, avgdegree, bnum; |
| 285 |
const idx_t *xadj, *adjncy; |
| 286 |
idx_t *vwgt, *adjwgt, *maxvwgt; |
| 287 |
idx_t *match, *cmap, *degrees, *perm, *tperm; |
| 288 |
size_t nunmatched=0; |
| 289 |
|
| 290 |
WCOREPUSH; |
| 291 |
|
| 292 |
|
| 293 |
nvtxs = graph->nvtxs; |
| 294 |
ncon = graph->ncon; |
| 295 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 296 |
vwgt = graph->vwgt; |
| 297 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 298 |
adjwgt = graph->adjwgt; |
| 299 |
cmap = graph->cmap; |
| 300 |
|
| 301 |
maxvwgt = ctrl->maxvwgt; |
| 302 |
|
| 303 |
match = iset(nvtxs, UNMATCHED, iwspacemalloc(ctrl, nvtxs)); |
| 304 |
perm = iwspacemalloc(ctrl, nvtxs); |
| 305 |
tperm = iwspacemalloc(ctrl, nvtxs); |
| 306 |
degrees = iwspacemalloc(ctrl, nvtxs); |
| 307 |
|
| 308 |
/* Determine a "random" traversal order that is biased towards low-degree vertices */ |
| 309 |
irandArrayPermute(nvtxs, tperm, nvtxs/8, 1, &ctrl->rng_state); |
| 310 |
|
| 311 |
avgdegree = 4.0*(xadj[nvtxs]/nvtxs); |
| 312 |
for (i=0; i<nvtxs; i++) { |
| 313 |
bnum = sqrt(1+xadj[i+1]-xadj[i]); |
| 314 |
degrees[i] = (bnum > avgdegree ? avgdegree : bnum); |
| 315 |
} |
| 316 |
BucketSortKeysInc(ctrl, nvtxs, avgdegree, degrees, tperm, perm); |
| 317 |
|
| 318 |
|
| 319 |
/* Traverse the vertices and compute the matching */ |
| 320 |
for (cnvtxs=0, last_unmatched=0, pi=0; pi<nvtxs; pi++) { |
| 321 |
i = perm[pi]; |
| 322 |
|
| 323 |
if (match[i] == UNMATCHED) { /* Unmatched */ |
| 324 |
maxidx = i; |
| 325 |
maxwgt = -1; |
| 326 |
|
| 327 |
if ((ncon == 1 ? vwgt[i] < maxvwgt[0] : ivecle(ncon, vwgt+i*ncon, maxvwgt))) { |
| 328 |
/* Deal with island vertices. Find a non-island and match it with. |
| 329 |
The matching ignores ctrl->maxvwgt requirements */ |
| 330 |
if (xadj[i] == xadj[i+1]) { |
| 331 |
last_unmatched = gk_max(pi, last_unmatched)+1; |
| 332 |
for (; last_unmatched<nvtxs; last_unmatched++) { |
| 333 |
j = perm[last_unmatched]; |
| 334 |
if (match[j] == UNMATCHED) { |
| 335 |
maxidx = j; |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
else { |
| 341 |
/* Find a heavy-edge matching, subject to maxvwgt constraints */ |
| 342 |
if (ncon == 1) { |
| 343 |
/* single constraint version */ |
| 344 |
for (j=xadj[i]; j<xadj[i+1]; j++) { |
| 345 |
k = adjncy[j]; |
| 346 |
if (match[k] == UNMATCHED && |
| 347 |
maxwgt < adjwgt[j] && vwgt[i]+vwgt[k] <= maxvwgt[0]) { |
| 348 |
maxidx = k; |
| 349 |
maxwgt = adjwgt[j]; |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* If it did not match, record for a 2-hop matching. */ |
| 354 |
if (maxidx == i && 2*vwgt[i] < maxvwgt[0]) { |
| 355 |
nunmatched++; |
| 356 |
maxidx = UNMATCHED; |
| 357 |
} |
| 358 |
} |
| 359 |
else { |
| 360 |
/* multi-constraint version */ |
| 361 |
for (j=xadj[i]; j<xadj[i+1]; j++) { |
| 362 |
k = adjncy[j]; |
| 363 |
if (match[k] == UNMATCHED && |
| 364 |
ivecaxpylez(ncon, 1, vwgt+i*ncon, vwgt+k*ncon, maxvwgt) && |
| 365 |
(maxwgt < adjwgt[j] || |
| 366 |
(maxwgt == adjwgt[j] && |
| 367 |
BetterVBalance(ncon, graph->invtvwgt, vwgt+i*ncon, |
| 368 |
vwgt+maxidx*ncon, vwgt+k*ncon)))) { |
| 369 |
maxidx = k; |
| 370 |
maxwgt = adjwgt[j]; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
/* If it did not match, record for a 2-hop matching. */ |
| 375 |
if (maxidx == i && ivecaxpylez(ncon, 2, vwgt+i*ncon, vwgt+i*ncon, maxvwgt)) { |
| 376 |
nunmatched++; |
| 377 |
maxidx = UNMATCHED; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if (maxidx != UNMATCHED) { |
| 384 |
cmap[i] = cmap[maxidx] = cnvtxs++; |
| 385 |
match[i] = maxidx; |
| 386 |
match[maxidx] = i; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
/* see if a 2-hop matching is required/allowed */ |
| 392 |
if (!ctrl->no2hop && nunmatched > UNMATCHEDFOR2HOP*nvtxs) |
| 393 |
cnvtxs = Match_2Hop(ctrl, graph, perm, match, cnvtxs, nunmatched); |
| 394 |
|
| 395 |
|
| 396 |
/* match the final unmatched vertices with themselves and reorder the vertices |
| 397 |
of the coarse graph for memory-friendly contraction */ |
| 398 |
for (cnvtxs=0, i=0; i<nvtxs; i++) { |
| 399 |
if (match[i] == UNMATCHED) { |
| 400 |
match[i] = i; |
| 401 |
cmap[i] = cnvtxs++; |
| 402 |
} |
| 403 |
else { |
| 404 |
if (i <= match[i]) |
| 405 |
cmap[i] = cmap[match[i]] = cnvtxs++; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
CreateCoarseGraph(ctrl, graph, cnvtxs, match); |
| 411 |
|
| 412 |
WCOREPOP; |
| 413 |
|
| 414 |
return cnvtxs; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/*************************************************************************/ |
| 419 |
/*! This function matches the unmatched vertices using a 2-hop matching |
| 420 |
that involves vertices that are two hops away from each other. */ |
| 421 |
/**************************************************************************/ |
| 422 |
idx_t Match_2Hop(ctrl_t *ctrl, graph_t *graph, idx_t *perm, idx_t *match, |
| 423 |
idx_t cnvtxs, size_t nunmatched) |
| 424 |
{ |
| 425 |
|
| 426 |
cnvtxs = Match_2HopAny(ctrl, graph, perm, match, cnvtxs, &nunmatched, 2); |
| 427 |
cnvtxs = Match_2HopAll(ctrl, graph, perm, match, cnvtxs, &nunmatched, 64); |
| 428 |
if (nunmatched > 1.5*UNMATCHEDFOR2HOP*graph->nvtxs) |
| 429 |
cnvtxs = Match_2HopAny(ctrl, graph, perm, match, cnvtxs, &nunmatched, 3); |
| 430 |
if (nunmatched > 2.0*UNMATCHEDFOR2HOP*graph->nvtxs) |
| 431 |
cnvtxs = Match_2HopAny(ctrl, graph, perm, match, cnvtxs, &nunmatched, graph->nvtxs); |
| 432 |
|
| 433 |
return cnvtxs; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
/*************************************************************************/ |
| 438 |
/*! This function matches the unmatched vertices whose degree is less than |
| 439 |
maxdegree using a 2-hop matching that involves vertices that are two |
| 440 |
hops away from each other. |
| 441 |
The requirement of the 2-hop matching is a simple non-empty overlap |
| 442 |
between the adjacency lists of the vertices. */ |
| 443 |
/**************************************************************************/ |
| 444 |
idx_t Match_2HopAny(ctrl_t *ctrl, graph_t *graph, idx_t *perm, idx_t *match, |
| 445 |
idx_t cnvtxs, size_t *r_nunmatched, size_t maxdegree) |
| 446 |
{ |
| 447 |
idx_t i, pi, ii, j, jj, k, nvtxs; |
| 448 |
const idx_t *xadj, *adjncy; |
| 449 |
idx_t *colptr, *rowind; |
| 450 |
idx_t *cmap; |
| 451 |
size_t nunmatched; |
| 452 |
|
| 453 |
|
| 454 |
nvtxs = graph->nvtxs; |
| 455 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 456 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 457 |
cmap = graph->cmap; |
| 458 |
|
| 459 |
nunmatched = *r_nunmatched; |
| 460 |
|
| 461 |
/* create the inverted index */ |
| 462 |
WCOREPUSH; |
| 463 |
colptr = iset(nvtxs, 0, iwspacemalloc(ctrl, nvtxs+1)); |
| 464 |
for (i=0; i<nvtxs; i++) { |
| 465 |
if (match[i] == UNMATCHED && xadj[i+1]-xadj[i] < maxdegree) { |
| 466 |
for (j=xadj[i]; j<xadj[i+1]; j++) |
| 467 |
colptr[adjncy[j]]++; |
| 468 |
} |
| 469 |
} |
| 470 |
MAKECSR(i, nvtxs, colptr); |
| 471 |
|
| 472 |
rowind = iwspacemalloc(ctrl, colptr[nvtxs]); |
| 473 |
for (pi=0; pi<nvtxs; pi++) { |
| 474 |
i = perm[pi]; |
| 475 |
if (match[i] == UNMATCHED && xadj[i+1]-xadj[i] < maxdegree) { |
| 476 |
for (j=xadj[i]; j<xadj[i+1]; j++) |
| 477 |
rowind[colptr[adjncy[j]]++] = i; |
| 478 |
} |
| 479 |
} |
| 480 |
SHIFTCSR(i, nvtxs, colptr); |
| 481 |
|
| 482 |
/* compute matchings by going down the inverted index */ |
| 483 |
for (pi=0; pi<nvtxs; pi++) { |
| 484 |
i = perm[pi]; |
| 485 |
if (colptr[i+1]-colptr[i] < 2) |
| 486 |
continue; |
| 487 |
|
| 488 |
for (jj=colptr[i+1], j=colptr[i]; j<jj; j++) { |
| 489 |
if (match[rowind[j]] == UNMATCHED) { |
| 490 |
for (jj--; jj>j; jj--) { |
| 491 |
if (match[rowind[jj]] == UNMATCHED) { |
| 492 |
cmap[rowind[j]] = cmap[rowind[jj]] = cnvtxs++; |
| 493 |
match[rowind[j]] = rowind[jj]; |
| 494 |
match[rowind[jj]] = rowind[j]; |
| 495 |
nunmatched -= 2; |
| 496 |
break; |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
WCOREPOP; |
| 503 |
|
| 504 |
*r_nunmatched = nunmatched; |
| 505 |
return cnvtxs; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
/*************************************************************************/ |
| 510 |
/*! This function matches the unmatched vertices whose degree is less than |
| 511 |
maxdegree using a 2-hop matching that involves vertices that are two |
| 512 |
hops away from each other. |
| 513 |
The requirement of the 2-hop matching is that of identical adjacency |
| 514 |
lists. |
| 515 |
*/ |
| 516 |
/**************************************************************************/ |
| 517 |
idx_t Match_2HopAll(ctrl_t *ctrl, graph_t *graph, idx_t *perm, idx_t *match, |
| 518 |
idx_t cnvtxs, size_t *r_nunmatched, size_t maxdegree) |
| 519 |
{ |
| 520 |
idx_t i, pi, pk, ii, j, jj, k, nvtxs, mask, idegree; |
| 521 |
const idx_t *xadj, *adjncy; |
| 522 |
idx_t *cmap, *mark; |
| 523 |
ikv_t *keys; |
| 524 |
size_t nunmatched, ncand; |
| 525 |
|
| 526 |
nvtxs = graph->nvtxs; |
| 527 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 528 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 529 |
cmap = graph->cmap; |
| 530 |
|
| 531 |
nunmatched = *r_nunmatched; |
| 532 |
mask = IDX_MAX/maxdegree; |
| 533 |
|
| 534 |
WCOREPUSH; |
| 535 |
|
| 536 |
/* collapse vertices with identical adjacency lists */ |
| 537 |
keys = ikvwspacemalloc(ctrl, nunmatched); |
| 538 |
for (ncand=0, pi=0; pi<nvtxs; pi++) { |
| 539 |
i = perm[pi]; |
| 540 |
idegree = xadj[i+1]-xadj[i]; |
| 541 |
if (match[i] == UNMATCHED && idegree > 1 && idegree < maxdegree) { |
| 542 |
for (k=0, j=xadj[i]; j<xadj[i+1]; j++) |
| 543 |
k += adjncy[j]%mask; |
| 544 |
keys[ncand].val = i; |
| 545 |
keys[ncand].key = (k%mask)*maxdegree + idegree; |
| 546 |
ncand++; |
| 547 |
} |
| 548 |
} |
| 549 |
ikvsorti(ncand, keys); |
| 550 |
|
| 551 |
mark = iset(nvtxs, 0, iwspacemalloc(ctrl, nvtxs)); |
| 552 |
for (pi=0; pi<ncand; pi++) { |
| 553 |
i = keys[pi].val; |
| 554 |
if (match[i] != UNMATCHED) |
| 555 |
continue; |
| 556 |
|
| 557 |
for (j=xadj[i]; j<xadj[i+1]; j++) |
| 558 |
mark[adjncy[j]] = i; |
| 559 |
|
| 560 |
for (pk=pi+1; pk<ncand; pk++) { |
| 561 |
k = keys[pk].val; |
| 562 |
if (match[k] != UNMATCHED) |
| 563 |
continue; |
| 564 |
|
| 565 |
if (keys[pi].key != keys[pk].key) |
| 566 |
break; |
| 567 |
if (xadj[i+1]-xadj[i] != xadj[k+1]-xadj[k]) |
| 568 |
break; |
| 569 |
|
| 570 |
for (jj=xadj[k]; jj<xadj[k+1]; jj++) { |
| 571 |
if (mark[adjncy[jj]] != i) |
| 572 |
break; |
| 573 |
} |
| 574 |
if (jj == xadj[k+1]) { |
| 575 |
cmap[i] = cmap[k] = cnvtxs++; |
| 576 |
match[i] = k; |
| 577 |
match[k] = i; |
| 578 |
nunmatched -= 2; |
| 579 |
break; |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
WCOREPOP; |
| 584 |
|
| 585 |
*r_nunmatched = nunmatched; |
| 586 |
return cnvtxs; |
| 587 |
} |
| 588 |
|
| 589 |
/*************************************************************************/ |
| 590 |
/*! This function prints various stats for each graph during coarsening |
| 591 |
*/ |
| 592 |
/*************************************************************************/ |
| 593 |
void PrintCGraphStats(ctrl_t *ctrl, graph_t *graph) |
| 594 |
{ |
| 595 |
idx_t i; |
| 596 |
|
| 597 |
HIGHS_ORDERING_PRINT("%10"PRIDX" %10"PRIDX" %10"PRIDX" [%"PRIDX"] [", |
| 598 |
graph->nvtxs, graph->nedges, isum(graph->nedges, graph->adjwgt, 1), ctrl->CoarsenTo); |
| 599 |
|
| 600 |
for (i=0; i<graph->ncon; i++) |
| 601 |
HIGHS_ORDERING_PRINT(" %8"PRIDX":%8"PRIDX, ctrl->maxvwgt[i], graph->tvwgt[i]); |
| 602 |
HIGHS_ORDERING_PRINT(" ]\n"); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
/*************************************************************************/ |
| 607 |
/*! This function creates the coarser graph. Depending on the size of the |
| 608 |
candidate adjacency lists it either uses a hash table or an array |
| 609 |
to do duplicate detection. |
| 610 |
*/ |
| 611 |
/*************************************************************************/ |
| 612 |
void CreateCoarseGraph(ctrl_t *ctrl, graph_t *graph, idx_t cnvtxs, |
| 613 |
idx_t *match) |
| 614 |
{ |
| 615 |
idx_t j, jj, k, kk, l, m, istart, iend, nvtxs, nedges, ncon, |
| 616 |
cnedges, v, u, mask; |
| 617 |
const idx_t *xadj, *adjncy; |
| 618 |
idx_t *vwgt, *vsize, *adjwgt; |
| 619 |
idx_t *cmap, *htable, *dtable; |
| 620 |
idx_t *cxadj, *cvwgt, *cvsize, *cadjncy, *cadjwgt; |
| 621 |
graph_t *cgraph; |
| 622 |
int dovsize, dropedges; |
| 623 |
idx_t cv, nkeys, droppedewgt; |
| 624 |
idx_t *keys=NULL, *medianewgts=NULL, *noise=NULL; |
| 625 |
|
| 626 |
WCOREPUSH; |
| 627 |
|
| 628 |
dovsize = 0; |
| 629 |
dropedges = ctrl->dropedges; |
| 630 |
|
| 631 |
mask = HTLENGTH; |
| 632 |
|
| 633 |
nvtxs = graph->nvtxs; |
| 634 |
ncon = graph->ncon; |
| 635 |
xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj; |
| 636 |
vwgt = graph->vwgt; |
| 637 |
vsize = graph->vsize; |
| 638 |
adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy; |
| 639 |
adjwgt = graph->adjwgt; |
| 640 |
cmap = graph->cmap; |
| 641 |
|
| 642 |
/* Setup structures for dropedges */ |
| 643 |
if (dropedges) { |
| 644 |
for (nkeys=0, v=0; v<nvtxs; v++) |
| 645 |
nkeys = gk_max(nkeys, xadj[v+1]-xadj[v]); |
| 646 |
nkeys = 2*nkeys+1; |
| 647 |
|
| 648 |
keys = iwspacemalloc(ctrl, nkeys); |
| 649 |
noise = iwspacemalloc(ctrl, cnvtxs); |
| 650 |
medianewgts = iset(cnvtxs, -1, iwspacemalloc(ctrl, cnvtxs)); |
| 651 |
|
| 652 |
for (v=0; v<cnvtxs; v++) |
| 653 |
noise[v] = irandInRange(128, &ctrl->rng_state); |
| 654 |
} |
| 655 |
|
| 656 |
/* Initialize the coarser graph */ |
| 657 |
cgraph = SetupCoarseGraph(graph, cnvtxs, dovsize); |
| 658 |
cxadj = cgraph->xadj; |
| 659 |
cvwgt = cgraph->vwgt; |
| 660 |
cvsize = cgraph->vsize; |
| 661 |
cadjncy = cgraph->adjncy; |
| 662 |
cadjwgt = cgraph->adjwgt; |
| 663 |
|
| 664 |
htable = iset(mask+1, -1, iwspacemalloc(ctrl, mask+1)); /* hash table */ |
| 665 |
dtable = iset(cnvtxs, -1, iwspacemalloc(ctrl, cnvtxs)); /* direct table */ |
| 666 |
|
| 667 |
cxadj[0] = cnvtxs = cnedges = 0; |
| 668 |
for (v=0; v<nvtxs; v++) { |
| 669 |
if ((u = match[v]) < v) |
| 670 |
continue; |
| 671 |
|
| 672 |
|
| 673 |
/* take care of the vertices */ |
| 674 |
if (ncon == 1) |
| 675 |
cvwgt[cnvtxs] = vwgt[v]; |
| 676 |
else |
| 677 |
icopy(ncon, vwgt+v*ncon, cvwgt+cnvtxs*ncon); |
| 678 |
|
| 679 |
if (dovsize) |
| 680 |
cvsize[cnvtxs] = vsize[v]; |
| 681 |
|
| 682 |
if (v != u) { |
| 683 |
if (ncon == 1) |
| 684 |
cvwgt[cnvtxs] += vwgt[u]; |
| 685 |
else |
| 686 |
iaxpy(ncon, 1, vwgt+u*ncon, 1, cvwgt+cnvtxs*ncon, 1); |
| 687 |
|
| 688 |
if (dovsize) |
| 689 |
cvsize[cnvtxs] += vsize[u]; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
/* take care of the edges */ |
| 694 |
if ((xadj[v+1]-xadj[v] + xadj[u+1]-xadj[u]) < (mask>>2)) { /* use mask */ |
| 695 |
/* put the ID of the contracted node itself at the start, so that it can be |
| 696 |
* removed easily */ |
| 697 |
htable[cnvtxs&mask] = 0; |
| 698 |
cadjncy[0] = cnvtxs; |
| 699 |
nedges = 1; |
| 700 |
|
| 701 |
istart = xadj[v]; |
| 702 |
iend = xadj[v+1]; |
| 703 |
for (j=istart; j<iend; j++) { |
| 704 |
k = cmap[adjncy[j]]; |
| 705 |
for (kk=k&mask; htable[kk]!=-1 && cadjncy[htable[kk]]!=k; kk=((kk+1)&mask)); |
| 706 |
if ((m = htable[kk]) == -1) { |
| 707 |
cadjncy[nedges] = k; |
| 708 |
cadjwgt[nedges] = adjwgt[j]; |
| 709 |
htable[kk] = nedges++; |
| 710 |
} |
| 711 |
else { |
| 712 |
cadjwgt[m] += adjwgt[j]; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
if (v != u) { |
| 717 |
istart = xadj[u]; |
| 718 |
iend = xadj[u+1]; |
| 719 |
for (j=istart; j<iend; j++) { |
| 720 |
k = cmap[adjncy[j]]; |
| 721 |
for (kk=k&mask; htable[kk]!=-1 && cadjncy[htable[kk]]!=k; kk=((kk+1)&mask)); |
| 722 |
if ((m = htable[kk]) == -1) { |
| 723 |
cadjncy[nedges] = k; |
| 724 |
cadjwgt[nedges] = adjwgt[j]; |
| 725 |
htable[kk] = nedges++; |
| 726 |
} |
| 727 |
else { |
| 728 |
cadjwgt[m] += adjwgt[j]; |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
/* reset the htable -- reverse order (LIFO) is critical to prevent cadjncy[-1] |
| 734 |
* indexing due to a remove of an earlier entry */ |
| 735 |
for (j=nedges-1; j>=0; j--) { |
| 736 |
k = cadjncy[j]; |
| 737 |
for (kk=k&mask; cadjncy[htable[kk]]!=k; kk=((kk+1)&mask)); |
| 738 |
htable[kk] = -1; |
| 739 |
} |
| 740 |
|
| 741 |
/* remove the contracted vertex from the list */ |
| 742 |
cadjncy[0] = cadjncy[--nedges]; |
| 743 |
cadjwgt[0] = cadjwgt[nedges]; |
| 744 |
} |
| 745 |
else { |
| 746 |
nedges = 0; |
| 747 |
istart = xadj[v]; |
| 748 |
iend = xadj[v+1]; |
| 749 |
for (j=istart; j<iend; j++) { |
| 750 |
k = cmap[adjncy[j]]; |
| 751 |
if ((m = dtable[k]) == -1) { |
| 752 |
cadjncy[nedges] = k; |
| 753 |
cadjwgt[nedges] = adjwgt[j]; |
| 754 |
dtable[k] = nedges++; |
| 755 |
} |
| 756 |
else { |
| 757 |
cadjwgt[m] += adjwgt[j]; |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
if (v != u) { |
| 762 |
istart = xadj[u]; |
| 763 |
iend = xadj[u+1]; |
| 764 |
for (j=istart; j<iend; j++) { |
| 765 |
k = cmap[adjncy[j]]; |
| 766 |
if ((m = dtable[k]) == -1) { |
| 767 |
cadjncy[nedges] = k; |
| 768 |
cadjwgt[nedges] = adjwgt[j]; |
| 769 |
dtable[k] = nedges++; |
| 770 |
} |
| 771 |
else { |
| 772 |
cadjwgt[m] += adjwgt[j]; |
| 773 |
} |
| 774 |
} |
| 775 |
|
| 776 |
/* Remove the contracted self-loop, when present */ |
| 777 |
if ((j = dtable[cnvtxs]) != -1) { |
| 778 |
cadjncy[j] = cadjncy[--nedges]; |
| 779 |
cadjwgt[j] = cadjwgt[nedges]; |
| 780 |
dtable[cnvtxs] = -1; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
/* Zero out the dtable */ |
| 785 |
for (j=0; j<nedges; j++) |
| 786 |
dtable[cadjncy[j]] = -1; |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
/* Determine the median weight of the incident edges, which will be used |
| 791 |
to keep an edge (u, v) iff wgt(u, v) >= min(medianewgts[u], medianewgts[v]) */ |
| 792 |
if (dropedges) { |
| 793 |
medianewgts[cnvtxs] = 8; /* default for island nodes */ |
| 794 |
if (nedges > 0) { |
| 795 |
for (j=0; j<nedges; j++) |
| 796 |
keys[j] = (cadjwgt[j]<<8) + noise[cnvtxs] + noise[cadjncy[j]]; |
| 797 |
isortd(nedges, keys); |
| 798 |
medianewgts[cnvtxs] = keys[gk_min(nedges-1, ((xadj[v+1]-xadj[v] + xadj[u+1]-xadj[u])>>1))]; |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
cadjncy += nedges; |
| 803 |
cadjwgt += nedges; |
| 804 |
cnedges += nedges; |
| 805 |
cxadj[++cnvtxs] = cnedges; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
/* compact the adjacency structure of the coarser graph to keep only +ve edges */ |
| 810 |
if (dropedges) { |
| 811 |
droppedewgt = 0; |
| 812 |
|
| 813 |
cadjncy = cgraph->adjncy; |
| 814 |
cadjwgt = cgraph->adjwgt; |
| 815 |
|
| 816 |
cnedges = 0; |
| 817 |
for (u=0; u<cnvtxs; u++) { |
| 818 |
istart = cxadj[u]; |
| 819 |
iend = cxadj[u+1]; |
| 820 |
for (j=istart; j<iend; j++) { |
| 821 |
v = cadjncy[j]; |
| 822 |
if ((cadjwgt[j]<<8) + noise[u] + noise[v] >= gk_min(medianewgts[u], medianewgts[v])) { |
| 823 |
cadjncy[cnedges] = cadjncy[j]; |
| 824 |
cadjwgt[cnedges++] = cadjwgt[j]; |
| 825 |
} |
| 826 |
else |
| 827 |
droppedewgt += cadjwgt[j]; |
| 828 |
} |
| 829 |
cxadj[u] = cnedges; |
| 830 |
} |
| 831 |
SHIFTCSR(j, cnvtxs, cxadj); |
| 832 |
|
| 833 |
cgraph->droppedewgt = droppedewgt; |
| 834 |
} |
| 835 |
|
| 836 |
cgraph->nedges = cnedges; |
| 837 |
|
| 838 |
for (j=0; j<ncon; j++) { |
| 839 |
cgraph->tvwgt[j] = isum(cgraph->nvtxs, cgraph->vwgt+j, ncon); |
| 840 |
cgraph->invtvwgt[j] = 1.0/(cgraph->tvwgt[j] > 0 ? cgraph->tvwgt[j] : 1); |
| 841 |
} |
| 842 |
|
| 843 |
ReAdjustMemory(ctrl, graph, cgraph); |
| 844 |
|
| 845 |
WCOREPOP; |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
/*************************************************************************/ |
| 850 |
/*! Setup the various arrays for the coarse graph |
| 851 |
*/ |
| 852 |
/*************************************************************************/ |
| 853 |
graph_t *SetupCoarseGraph(graph_t *graph, idx_t cnvtxs, int dovsize) |
| 854 |
{ |
| 855 |
graph_t *cgraph; |
| 856 |
|
| 857 |
cgraph = CreateGraph(); |
| 858 |
|
| 859 |
cgraph->nvtxs = cnvtxs; |
| 860 |
cgraph->ncon = graph->ncon; |
| 861 |
|
| 862 |
cgraph->finer = graph; |
| 863 |
graph->coarser = cgraph; |
| 864 |
|
| 865 |
/* Allocate memory for the coarser graph. |
| 866 |
NOTE: The +1 in the adjwgt/adjncy is to allow the optimization of self-loop |
| 867 |
detection by adding ahead of time the self-loop. That optimization |
| 868 |
requires a +1 adjncy/adjwgt array for the limit case where the |
| 869 |
coarser graph is of the same size of the previous graph. */ |
| 870 |
cgraph->xadj = imalloc(cnvtxs+1); |
| 871 |
cgraph->adjncy = imalloc(graph->nedges+1); |
| 872 |
cgraph->adjwgt = imalloc(graph->nedges+1); |
| 873 |
cgraph->vwgt = imalloc(cgraph->ncon*cnvtxs); |
| 874 |
cgraph->tvwgt = imalloc(cgraph->ncon); |
| 875 |
cgraph->invtvwgt = rmalloc(cgraph->ncon); |
| 876 |
|
| 877 |
if (dovsize) |
| 878 |
cgraph->vsize = imalloc(cnvtxs); |
| 879 |
|
| 880 |
return cgraph; |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
/*************************************************************************/ |
| 885 |
/*! This function re-adjusts the amount of memory that was allocated if |
| 886 |
it will lead to significant savings |
| 887 |
*/ |
| 888 |
/*************************************************************************/ |
| 889 |
void ReAdjustMemory(ctrl_t *ctrl, graph_t *graph, graph_t *cgraph) |
| 890 |
{ |
| 891 |
if (cgraph->nedges > 10000 && cgraph->nedges < 0.9*graph->nedges) { |
| 892 |
cgraph->adjncy = irealloc(cgraph->adjncy, cgraph->nedges); |
| 893 |
cgraph->adjwgt = irealloc(cgraph->adjwgt, cgraph->nedges); |
| 894 |
} |
| 895 |
} |