Debian Codegraph @

extern/metis/libmetis/balance.c

raw
1 /*!
2 \file
3 \brief Functions for the edge-based balancing
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: balance.c 10187 2011-06-13 13:46:57Z karypis $ \endverbatim
9 */
10
11 #include "metislib.h"
12
13 /*************************************************************************
14 * This function is the entry poidx_t of the bisection balancing algorithms.
15 **************************************************************************/
16 void Balance2Way(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
17 {
18 if (ComputeLoadImbalanceDiff(graph, 2, ctrl->pijbm, ctrl->ubfactors) <= 0)
19 return;
20
21 if (graph->ncon == 1) {
22 /* return right away if the balance is OK */
23 if (rabs(ntpwgts[0]*graph->tvwgt[0]-graph->pwgts[0]) < 3*graph->tvwgt[0]/graph->nvtxs)
24 return;
25
26 if (graph->nbnd > 0)
27 Bnd2WayBalance(ctrl, graph, ntpwgts);
28 else
29 General2WayBalance(ctrl, graph, ntpwgts);
30 }
31 else {
32 McGeneral2WayBalance(ctrl, graph, ntpwgts);
33 }
34 }
35
36
37 /*************************************************************************
38 * This function balances two partitions by moving boundary nodes
39 * from the domain that is overweight to the one that is underweight.
40 **************************************************************************/
41 void Bnd2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
42 {
43 idx_t i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
44 const idx_t *xadj, *adjncy;
45 idx_t *vwgt, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;
46 idx_t *moved, *perm;
47 rpq_t *queue;
48 idx_t higain, mincut, mindiff;
49 idx_t tpwgts[2];
50
51 WCOREPUSH;
52
53 nvtxs = graph->nvtxs;
54 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
55 vwgt = graph->vwgt;
56 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
57 adjwgt = graph->adjwgt;
58 where = graph->where;
59 id = graph->id;
60 ed = graph->ed;
61 pwgts = graph->pwgts;
62 bndptr = graph->bndptr;
63 bndind = graph->bndind;
64
65 moved = iwspacemalloc(ctrl, nvtxs);
66 perm = iwspacemalloc(ctrl, nvtxs);
67
68 /* Determine from which domain you will be moving data */
69 tpwgts[0] = graph->tvwgt[0]*ntpwgts[0];
70 tpwgts[1] = graph->tvwgt[0] - tpwgts[0];
71 mindiff = iabs(tpwgts[0]-pwgts[0]);
72 from = (pwgts[0] < tpwgts[0] ? 1 : 0);
73 to = (from+1)%2;
74
75 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
76 HIGHS_ORDERING_PRINT("Partitions: [%6"PRIDX" %6"PRIDX"] T[%6"PRIDX" %6"PRIDX"], Nv-Nb[%6"PRIDX" %6"PRIDX"]. ICut: %6"PRIDX" [B]\n",
77 pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd,
78 graph->mincut));
79
80 queue = rpqCreate(nvtxs);
81
82 iset(nvtxs, -1, moved);
83
84
85 /* Insert the boundary nodes of the proper partition whose size is OK in the priority queue */
86 nbnd = graph->nbnd;
87 irandArrayPermute(nbnd, perm, nbnd/5, 1, &ctrl->rng_state);
88 for (ii=0; ii<nbnd; ii++) {
89 i = perm[ii];
90 if (where[bndind[i]] == from && vwgt[bndind[i]] <= mindiff)
91 rpqInsert(queue, bndind[i], ed[bndind[i]]-id[bndind[i]]);
92 }
93
94 mincut = graph->mincut;
95 for (nswaps=0; nswaps<nvtxs; nswaps++) {
96 if ((higain = rpqGetTop(queue)) == -1)
97 break;
98
99 if (pwgts[to]+vwgt[higain] > tpwgts[to])
100 break;
101
102 mincut -= (ed[higain]-id[higain]);
103 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
104
105 where[higain] = to;
106 moved[higain] = nswaps;
107
108 IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO,
109 HIGHS_ORDERING_PRINT("Moved %6"PRIDX" from %"PRIDX". [%3"PRIDX" %3"PRIDX"] %5"PRIDX" [%4"PRIDX" %4"PRIDX"]\n", higain, from, ed[higain]-id[higain], vwgt[higain], mincut, pwgts[0], pwgts[1]));
110
111 /**************************************************************
112 * Update the id[i]/ed[i] values of the affected nodes
113 ***************************************************************/
114 SWAP(id[higain], ed[higain], tmp);
115 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
116 BNDDelete(nbnd, bndind, bndptr, higain);
117
118 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
119 k = adjncy[j];
120 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
121 INC_DEC(id[k], ed[k], kwgt);
122
123 /* Update its boundary information and queue position */
124 if (bndptr[k] != -1) { /* If k was a boundary vertex */
125 if (ed[k] == 0) { /* Not a boundary vertex any more */
126 BNDDelete(nbnd, bndind, bndptr, k);
127 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff) /* Remove it if in the queues */
128 rpqDelete(queue, k);
129 }
130 else { /* If it has not been moved, update its position in the queue */
131 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
132 rpqUpdate(queue, k, ed[k]-id[k]);
133 }
134 }
135 else {
136 if (ed[k] > 0) { /* It will now become a boundary vertex */
137 BNDInsert(nbnd, bndind, bndptr, k);
138 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
139 rpqInsert(queue, k, ed[k]-id[k]);
140 }
141 }
142 }
143 }
144
145 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
146 HIGHS_ORDERING_PRINT("\tMinimum cut: %6"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, pwgts[0], pwgts[1], nbnd));
147
148 graph->mincut = mincut;
149 graph->nbnd = nbnd;
150
151 rpqDestroy(queue);
152
153 WCOREPOP;
154 }
155
156
157 /*************************************************************************
158 * This function balances two partitions by moving the highest gain
159 * (including negative gain) vertices to the other domain.
160 * It is used only when the unbalance is due to non contiguous
161 * subdomains. That is, the are no boundary vertices.
162 * It moves vertices from the domain that is overweight to the one that
163 * is underweight.
164 **************************************************************************/
165 void General2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
166 {
167 idx_t i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
168 const idx_t *xadj, *adjncy;
169 idx_t *vwgt, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;
170 idx_t *moved, *perm;
171 rpq_t *queue;
172 idx_t higain, mincut, mindiff;
173 idx_t tpwgts[2];
174
175 WCOREPUSH;
176
177 nvtxs = graph->nvtxs;
178 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
179 vwgt = graph->vwgt;
180 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
181 adjwgt = graph->adjwgt;
182 where = graph->where;
183 id = graph->id;
184 ed = graph->ed;
185 pwgts = graph->pwgts;
186 bndptr = graph->bndptr;
187 bndind = graph->bndind;
188
189 moved = iwspacemalloc(ctrl, nvtxs);
190 perm = iwspacemalloc(ctrl, nvtxs);
191
192 /* Determine from which domain you will be moving data */
193 tpwgts[0] = graph->tvwgt[0]*ntpwgts[0];
194 tpwgts[1] = graph->tvwgt[0] - tpwgts[0];
195 mindiff = iabs(tpwgts[0]-pwgts[0]);
196 from = (pwgts[0] < tpwgts[0] ? 1 : 0);
197 to = (from+1)%2;
198
199 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
200 HIGHS_ORDERING_PRINT("Partitions: [%6"PRIDX" %6"PRIDX"] T[%6"PRIDX" %6"PRIDX"], Nv-Nb[%6"PRIDX" %6"PRIDX"]. ICut: %6"PRIDX" [B]\n",
201 pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut));
202
203 queue = rpqCreate(nvtxs);
204
205 iset(nvtxs, -1, moved);
206
207
208 /* Insert the nodes of the proper partition whose size is OK in the priority queue */
209 irandArrayPermute(nvtxs, perm, nvtxs/5, 1, &ctrl->rng_state);
210 for (ii=0; ii<nvtxs; ii++) {
211 i = perm[ii];
212 if (where[i] == from && vwgt[i] <= mindiff)
213 rpqInsert(queue, i, ed[i]-id[i]);
214 }
215
216 mincut = graph->mincut;
217 nbnd = graph->nbnd;
218 for (nswaps=0; nswaps<nvtxs; nswaps++) {
219 if ((higain = rpqGetTop(queue)) == -1)
220 break;
221
222 if (pwgts[to]+vwgt[higain] > tpwgts[to])
223 break;
224
225 mincut -= (ed[higain]-id[higain]);
226 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
227
228 where[higain] = to;
229 moved[higain] = nswaps;
230
231 IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO,
232 HIGHS_ORDERING_PRINT("Moved %6"PRIDX" from %"PRIDX". [%3"PRIDX" %3"PRIDX"] %5"PRIDX" [%4"PRIDX" %4"PRIDX"]\n", higain, from, ed[higain]-id[higain], vwgt[higain], mincut, pwgts[0], pwgts[1]));
233
234 /**************************************************************
235 * Update the id[i]/ed[i] values of the affected nodes
236 ***************************************************************/
237 SWAP(id[higain], ed[higain], tmp);
238 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
239 BNDDelete(nbnd, bndind, bndptr, higain);
240 if (ed[higain] > 0 && bndptr[higain] == -1)
241 BNDInsert(nbnd, bndind, bndptr, higain);
242
243 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
244 k = adjncy[j];
245
246 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
247 INC_DEC(id[k], ed[k], kwgt);
248
249 /* Update the queue position */
250 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
251 rpqUpdate(queue, k, ed[k]-id[k]);
252
253 /* Update its boundary information */
254 if (ed[k] == 0 && bndptr[k] != -1)
255 BNDDelete(nbnd, bndind, bndptr, k);
256 else if (ed[k] > 0 && bndptr[k] == -1)
257 BNDInsert(nbnd, bndind, bndptr, k);
258 }
259 }
260
261 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
262 HIGHS_ORDERING_PRINT("\tMinimum cut: %6"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, pwgts[0], pwgts[1], nbnd));
263
264 graph->mincut = mincut;
265 graph->nbnd = nbnd;
266
267 rpqDestroy(queue);
268
269 WCOREPOP;
270 }
271
272
273 /*************************************************************************
274 * This function performs an edge-based FM refinement
275 **************************************************************************/
276 void McGeneral2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
277 {
278 idx_t i, ii, j, k, l, kwgt, nvtxs, ncon, nbnd, nswaps, from, to, pass,
279 me, limit, tmp, cnum;
280 const idx_t *xadj, *adjncy;
281 idx_t *vwgt, *adjwgt, *where, *pwgts, *id, *ed, *bndptr, *bndind;
282 idx_t *moved, *swaps, *perm, *qnum, *qsizes;
283 idx_t higain, mincut, newcut, mincutorder;
284 real_t *invtvwgt, *minbalv, *newbalv, minbal, newbal;
285 rpq_t **queues;
286
287 WCOREPUSH;
288
289 nvtxs = graph->nvtxs;
290 ncon = graph->ncon;
291 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
292 vwgt = graph->vwgt;
293 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
294 adjwgt = graph->adjwgt;
295 invtvwgt = graph->invtvwgt;
296 where = graph->where;
297 id = graph->id;
298 ed = graph->ed;
299 pwgts = graph->pwgts;
300 bndptr = graph->bndptr;
301 bndind = graph->bndind;
302
303 moved = iwspacemalloc(ctrl, nvtxs);
304 swaps = iwspacemalloc(ctrl, nvtxs);
305 perm = iwspacemalloc(ctrl, nvtxs);
306 qnum = iwspacemalloc(ctrl, nvtxs);
307 newbalv = rwspacemalloc(ctrl, ncon);
308 minbalv = rwspacemalloc(ctrl, ncon);
309 qsizes = iwspacemalloc(ctrl, 2*ncon);
310
311 limit = gk_min(gk_max(0.01*nvtxs, 15), 100);
312
313 /* Initialize the queues */
314 queues = (rpq_t **)wspacemalloc(ctrl, 2*ncon*sizeof(rpq_t *));
315 for (i=0; i<2*ncon; i++) {
316 queues[i] = rpqCreate(nvtxs);
317 qsizes[i] = 0;
318 }
319
320 for (i=0; i<nvtxs; i++) {
321 qnum[i] = iargmax_nrm(ncon, vwgt+i*ncon, invtvwgt);
322 qsizes[2*qnum[i]+where[i]]++;
323 }
324
325
326 /* for the empty queues, move into them vertices from other queues */
327 for (from=0; from<2; from++) {
328 for (j=0; j<ncon; j++) {
329 if (qsizes[2*j+from] == 0) {
330 for (i=0; i<nvtxs; i++) {
331 if (where[i] != from)
332 continue;
333
334 k = iargmax2_nrm(ncon, vwgt+i*ncon, invtvwgt);
335 if (k == j &&
336 qsizes[2*qnum[i]+from] > qsizes[2*j+from] &&
337 vwgt[i*ncon+qnum[i]]*invtvwgt[qnum[i]] < 1.3*vwgt[i*ncon+j]*invtvwgt[j]) {
338 qsizes[2*qnum[i]+from]--;
339 qsizes[2*j+from]++;
340 qnum[i] = j;
341 }
342 }
343 }
344 }
345 }
346
347
348 minbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ctrl->ubfactors, minbalv);
349
350 newcut = mincut = graph->mincut;
351 mincutorder = -1;
352
353 if (ctrl->dbglvl&METIS_DBG_REFINE) {
354 HIGHS_ORDERING_PRINT("Parts: [");
355 for (l=0; l<ncon; l++)
356 HIGHS_ORDERING_PRINT("(%6"PRIDX" %6"PRIDX" %.3"PRREAL" %.3"PRREAL") ",
357 pwgts[l], pwgts[ncon+l], ntpwgts[l], ntpwgts[ncon+l]);
358 HIGHS_ORDERING_PRINT("] Nv-Nb[%5"PRIDX", %5"PRIDX"]. ICut: %6"PRIDX", LB: %+.3"PRREAL" [B]\n",
359 graph->nvtxs, graph->nbnd, graph->mincut, minbal);
360 }
361
362 iset(nvtxs, -1, moved);
363
364
365 /* Insert all nodes in the priority queues */
366 nbnd = graph->nbnd;
367 irandArrayPermute(nvtxs, perm, nvtxs/10, 1, &ctrl->rng_state);
368 for (ii=0; ii<nvtxs; ii++) {
369 i = perm[ii];
370 rpqInsert(queues[2*qnum[i]+where[i]], i, ed[i]-id[i]);
371 }
372
373 for (nswaps=0; nswaps<nvtxs; nswaps++) {
374 if (minbal <= 0.0)
375 break;
376
377 SelectQueue(graph, ctrl->pijbm, ctrl->ubfactors, queues, &from, &cnum);
378 to = (from+1)%2;
379
380 if (from == -1 || (higain = rpqGetTop(queues[2*cnum+from])) == -1)
381 break;
382
383 newcut -= (ed[higain]-id[higain]);
384
385 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
386 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
387 newbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ctrl->ubfactors, newbalv);
388
389 if (newbal < minbal || (newbal == minbal &&
390 (newcut < mincut ||
391 (newcut == mincut && BetterBalance2Way(ncon, minbalv, newbalv))))) {
392 mincut = newcut;
393 minbal = newbal;
394 mincutorder = nswaps;
395 rcopy(ncon, newbalv, minbalv);
396 }
397 else if (nswaps-mincutorder > limit) { /* We hit the limit, undo last move */
398 newcut += (ed[higain]-id[higain]);
399 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
400 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
401 break;
402 }
403
404 where[higain] = to;
405 moved[higain] = nswaps;
406 swaps[nswaps] = higain;
407
408 if (ctrl->dbglvl&METIS_DBG_MOVEINFO) {
409 HIGHS_ORDERING_PRINT("Moved %6"PRIDX" from %"PRIDX"(%"PRIDX"). Gain: %5"PRIDX", "
410 "Cut: %5"PRIDX", NPwgts: ", higain, from, cnum, ed[higain]-id[higain], newcut);
411 for (l=0; l<ncon; l++)
412 HIGHS_ORDERING_PRINT("(%6"PRIDX", %6"PRIDX") ", pwgts[l], pwgts[ncon+l]);
413 HIGHS_ORDERING_PRINT(", %+.3"PRREAL" LB: %+.3"PRREAL"\n", minbal, newbal);
414 }
415
416
417 /**************************************************************
418 * Update the id[i]/ed[i] values of the affected nodes
419 ***************************************************************/
420 SWAP(id[higain], ed[higain], tmp);
421 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
422 BNDDelete(nbnd, bndind, bndptr, higain);
423 if (ed[higain] > 0 && bndptr[higain] == -1)
424 BNDInsert(nbnd, bndind, bndptr, higain);
425
426 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
427 k = adjncy[j];
428
429 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
430 INC_DEC(id[k], ed[k], kwgt);
431
432 /* Update the queue position */
433 if (moved[k] == -1)
434 rpqUpdate(queues[2*qnum[k]+where[k]], k, ed[k]-id[k]);
435
436 /* Update its boundary information */
437 if (ed[k] == 0 && bndptr[k] != -1)
438 BNDDelete(nbnd, bndind, bndptr, k);
439 else if (ed[k] > 0 && bndptr[k] == -1)
440 BNDInsert(nbnd, bndind, bndptr, k);
441 }
442 }
443
444
445
446 /****************************************************************
447 * Roll back computations
448 *****************************************************************/
449 for (nswaps--; nswaps>mincutorder; nswaps--) {
450 higain = swaps[nswaps];
451
452 to = where[higain] = (where[higain]+1)%2;
453 SWAP(id[higain], ed[higain], tmp);
454 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
455 BNDDelete(nbnd, bndind, bndptr, higain);
456 else if (ed[higain] > 0 && bndptr[higain] == -1)
457 BNDInsert(nbnd, bndind, bndptr, higain);
458
459 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
460 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+((to+1)%2)*ncon, 1);
461 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
462 k = adjncy[j];
463
464 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
465 INC_DEC(id[k], ed[k], kwgt);
466
467 if (bndptr[k] != -1 && ed[k] == 0)
468 BNDDelete(nbnd, bndind, bndptr, k);
469 if (bndptr[k] == -1 && ed[k] > 0)
470 BNDInsert(nbnd, bndind, bndptr, k);
471 }
472 }
473
474 if (ctrl->dbglvl&METIS_DBG_REFINE) {
475 HIGHS_ORDERING_PRINT("\tMincut: %6"PRIDX" at %5"PRIDX", NBND: %6"PRIDX", NPwgts: [",
476 mincut, mincutorder, nbnd);
477 for (l=0; l<ncon; l++)
478 HIGHS_ORDERING_PRINT("(%6"PRIDX", %6"PRIDX") ", pwgts[l], pwgts[ncon+l]);
479 HIGHS_ORDERING_PRINT("], LB: %.3"PRREAL"\n", ComputeLoadImbalance(graph, 2, ctrl->pijbm));
480 }
481
482 graph->mincut = mincut;
483 graph->nbnd = nbnd;
484
485
486 for (i=0; i<2*ncon; i++)
487 rpqDestroy(queues[i]);
488
489 WCOREPOP;
490 }
491