Debian Codegraph @

extern/metis/libmetis/fm.c

raw
1 /*!
2 \file
3 \brief Functions for the edge-based FM refinement
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: fm.c 10187 2011-06-13 13:46:57Z karypis $ \endverbatim
9 */
10
11 #include "metislib.h"
12
13
14 /*************************************************************************
15 * This function performs an edge-based FM refinement
16 **************************************************************************/
17 void FM_2WayRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
18 {
19 if (graph->ncon == 1)
20 FM_2WayCutRefine(ctrl, graph, ntpwgts, niter);
21 else
22 FM_Mc2WayCutRefine(ctrl, graph, ntpwgts, niter);
23 }
24
25
26 /*************************************************************************/
27 /*! This function performs a cut-focused FM refinement */
28 /*************************************************************************/
29 void FM_2WayCutRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
30 {
31 idx_t i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, limit, tmp;
32 const idx_t *xadj, *adjncy;
33 idx_t *vwgt, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;
34 idx_t *moved, *swaps, *perm;
35 rpq_t *queues[2];
36 idx_t higain, mincut, mindiff, origdiff, initcut, newcut, mincutorder, avgvwgt;
37 idx_t tpwgts[2];
38
39 WCOREPUSH;
40
41 nvtxs = graph->nvtxs;
42 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
43 vwgt = graph->vwgt;
44 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
45 adjwgt = graph->adjwgt;
46 where = graph->where;
47 id = graph->id;
48 ed = graph->ed;
49 pwgts = graph->pwgts;
50 bndptr = graph->bndptr;
51 bndind = graph->bndind;
52
53 moved = iwspacemalloc(ctrl, nvtxs);
54 swaps = iwspacemalloc(ctrl, nvtxs);
55 perm = iwspacemalloc(ctrl, nvtxs);
56
57 tpwgts[0] = graph->tvwgt[0]*ntpwgts[0];
58 tpwgts[1] = graph->tvwgt[0]-tpwgts[0];
59
60 limit = gk_min(gk_max(0.01*nvtxs, 15), 100);
61 avgvwgt = gk_min((pwgts[0]+pwgts[1])/20, 2*(pwgts[0]+pwgts[1])/nvtxs);
62
63 queues[0] = rpqCreate(nvtxs);
64 queues[1] = rpqCreate(nvtxs);
65
66 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
67 Print2WayRefineStats(ctrl, graph, ntpwgts, 0, -2));
68
69 origdiff = iabs(tpwgts[0]-pwgts[0]);
70 iset(nvtxs, -1, moved);
71 for (pass=0; pass<niter; pass++) { /* Do a number of passes */
72 rpqReset(queues[0]);
73 rpqReset(queues[1]);
74
75 mincutorder = -1;
76 newcut = mincut = initcut = graph->mincut;
77 mindiff = iabs(tpwgts[0]-pwgts[0]);
78
79
80 /* Insert boundary nodes in the priority queues */
81 nbnd = graph->nbnd;
82 irandArrayPermute(nbnd, perm, nbnd, 1, &ctrl->rng_state);
83 for (ii=0; ii<nbnd; ii++) {
84 i = perm[ii];
85 rpqInsert(queues[where[bndind[i]]], bndind[i], ed[bndind[i]]-id[bndind[i]]);
86 }
87
88 for (nswaps=0; nswaps<nvtxs; nswaps++) {
89 from = (tpwgts[0]-pwgts[0] < tpwgts[1]-pwgts[1] ? 0 : 1);
90 to = (from+1)%2;
91
92 if ((higain = rpqGetTop(queues[from])) == -1)
93 break;
94
95 newcut -= (ed[higain]-id[higain]);
96 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
97
98 if ((newcut < mincut && iabs(tpwgts[0]-pwgts[0]) <= origdiff+avgvwgt) ||
99 (newcut == mincut && iabs(tpwgts[0]-pwgts[0]) < mindiff)) {
100 mincut = newcut;
101 mindiff = iabs(tpwgts[0]-pwgts[0]);
102 mincutorder = nswaps;
103 }
104 else if (nswaps-mincutorder > limit) { /* We hit the limit, undo last move */
105 newcut += (ed[higain]-id[higain]);
106 INC_DEC(pwgts[from], pwgts[to], vwgt[higain]);
107 break;
108 }
109
110 where[higain] = to;
111 moved[higain] = nswaps;
112 swaps[nswaps] = higain;
113
114 IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO,
115 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], newcut, pwgts[0], pwgts[1]));
116
117 /**************************************************************
118 * Update the id[i]/ed[i] values of the affected nodes
119 ***************************************************************/
120 SWAP(id[higain], ed[higain], tmp);
121 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
122 BNDDelete(nbnd, bndind, bndptr, higain);
123
124 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
125 k = adjncy[j];
126
127 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
128 INC_DEC(id[k], ed[k], kwgt);
129
130 /* Update its boundary information and queue position */
131 if (bndptr[k] != -1) { /* If k was a boundary vertex */
132 if (ed[k] == 0) { /* Not a boundary vertex any more */
133 BNDDelete(nbnd, bndind, bndptr, k);
134 if (moved[k] == -1) /* Remove it if in the queues */
135 rpqDelete(queues[where[k]], k);
136 }
137 else { /* If it has not been moved, update its position in the queue */
138 if (moved[k] == -1)
139 rpqUpdate(queues[where[k]], k, ed[k]-id[k]);
140 }
141 }
142 else {
143 if (ed[k] > 0) { /* It will now become a boundary vertex */
144 BNDInsert(nbnd, bndind, bndptr, k);
145 if (moved[k] == -1)
146 rpqInsert(queues[where[k]], k, ed[k]-id[k]);
147 }
148 }
149 }
150
151 }
152
153
154 /****************************************************************
155 * Roll back computations
156 *****************************************************************/
157 for (i=0; i<nswaps; i++)
158 moved[swaps[i]] = -1; /* reset moved array */
159 for (nswaps--; nswaps>mincutorder; nswaps--) {
160 higain = swaps[nswaps];
161
162 to = where[higain] = (where[higain]+1)%2;
163 SWAP(id[higain], ed[higain], tmp);
164 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
165 BNDDelete(nbnd, bndind, bndptr, higain);
166 else if (ed[higain] > 0 && bndptr[higain] == -1)
167 BNDInsert(nbnd, bndind, bndptr, higain);
168
169 INC_DEC(pwgts[to], pwgts[(to+1)%2], vwgt[higain]);
170 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
171 k = adjncy[j];
172
173 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
174 INC_DEC(id[k], ed[k], kwgt);
175
176 if (bndptr[k] != -1 && ed[k] == 0)
177 BNDDelete(nbnd, bndind, bndptr, k);
178 if (bndptr[k] == -1 && ed[k] > 0)
179 BNDInsert(nbnd, bndind, bndptr, k);
180 }
181 }
182
183 graph->mincut = mincut;
184 graph->nbnd = nbnd;
185
186 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
187 Print2WayRefineStats(ctrl, graph, ntpwgts, 0, mincutorder));
188
189 if (mincutorder <= 0 || mincut == initcut)
190 break;
191 }
192
193 rpqDestroy(queues[0]);
194 rpqDestroy(queues[1]);
195
196 WCOREPOP;
197 }
198
199
200 /*************************************************************************/
201 /*! This function performs a cut-focused multi-constraint FM refinement */
202 /*************************************************************************/
203 void FM_Mc2WayCutRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
204 {
205 idx_t i, ii, j, k, l, kwgt, nvtxs, ncon, nbnd, nswaps, from, to, pass,
206 me, limit, tmp, cnum;
207 const idx_t *xadj, *adjncy;
208 idx_t *vwgt, *adjwgt, *pwgts, *where, *id, *ed,
209 *bndptr, *bndind;
210 idx_t *moved, *swaps, *perm, *qnum;
211 idx_t higain, mincut, initcut, newcut, mincutorder;
212 real_t *invtvwgt, *ubfactors, *minbalv, *newbalv;
213 real_t origbal, minbal, newbal, rgain, ffactor;
214 rpq_t **queues;
215
216 WCOREPUSH;
217
218 nvtxs = graph->nvtxs;
219 ncon = graph->ncon;
220 xadj = graph->xadj_orig ? graph->xadj_orig : graph->xadj;
221 vwgt = graph->vwgt;
222 adjncy = graph->adjncy_orig ? graph->adjncy_orig : graph->adjncy;
223 adjwgt = graph->adjwgt;
224 invtvwgt = graph->invtvwgt;
225 where = graph->where;
226 id = graph->id;
227 ed = graph->ed;
228 pwgts = graph->pwgts;
229 bndptr = graph->bndptr;
230 bndind = graph->bndind;
231
232 moved = iwspacemalloc(ctrl, nvtxs);
233 swaps = iwspacemalloc(ctrl, nvtxs);
234 perm = iwspacemalloc(ctrl, nvtxs);
235 qnum = iwspacemalloc(ctrl, nvtxs);
236 ubfactors = rwspacemalloc(ctrl, ncon);
237 newbalv = rwspacemalloc(ctrl, ncon);
238 minbalv = rwspacemalloc(ctrl, ncon);
239
240 limit = gk_min(gk_max(0.01*nvtxs, 25), 150);
241
242
243 /* Determine a fudge factor to allow the refinement routines to get out
244 of tight balancing constraints. */
245 ffactor = .5/gk_max(20, nvtxs);
246
247 /* Initialize the queues */
248 queues = (rpq_t **)wspacemalloc(ctrl, 2*ncon*sizeof(rpq_t *));
249 for (i=0; i<2*ncon; i++)
250 queues[i] = rpqCreate(nvtxs);
251 for (i=0; i<nvtxs; i++)
252 qnum[i] = iargmax_nrm(ncon, vwgt+i*ncon, invtvwgt);
253
254 /* Determine the unbalance tolerance for each constraint. The tolerance is
255 equal to the maximum of the original load imbalance and the user-supplied
256 allowed tolerance. The rationale behind this approach is to allow the
257 refinement routine to improve the cut, without having to worry about fixing
258 load imbalance problems. The load imbalance is addressed by the balancing
259 routines. */
260 origbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ctrl->ubfactors, ubfactors);
261 for (i=0; i<ncon; i++)
262 ubfactors[i] = (ubfactors[i] > 0 ? ctrl->ubfactors[i]+ubfactors[i] : ctrl->ubfactors[i]);
263
264
265 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
266 Print2WayRefineStats(ctrl, graph, ntpwgts, origbal, -2));
267
268 iset(nvtxs, -1, moved);
269 for (pass=0; pass<niter; pass++) { /* Do a number of passes */
270 for (i=0; i<2*ncon; i++)
271 rpqReset(queues[i]);
272
273 mincutorder = -1;
274 newcut = mincut = initcut = graph->mincut;
275
276 minbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ubfactors, minbalv);
277
278
279 /* Insert boundary nodes in the priority queues */
280 nbnd = graph->nbnd;
281 irandArrayPermute(nbnd, perm, nbnd/5, 1, &ctrl->rng_state);
282 for (ii=0; ii<nbnd; ii++) {
283 i = bndind[perm[ii]];
284 //rgain = 1.0*(ed[i]-id[i])/sqrt(vwgt[i*ncon+qnum[i]]+1);
285 //rgain = (ed[i]-id[i] > 0 ? 1.0*(ed[i]-id[i])/sqrt(vwgt[i*ncon+qnum[i]]+1) : ed[i]-id[i]);
286 rgain = ed[i]-id[i];
287 rpqInsert(queues[2*qnum[i]+where[i]], i, rgain);
288 }
289
290 for (nswaps=0; nswaps<nvtxs; nswaps++) {
291 SelectQueue(graph, ctrl->pijbm, ubfactors, queues, &from, &cnum);
292
293 to = (from+1)%2;
294
295 if (from == -1 || (higain = rpqGetTop(queues[2*cnum+from])) == -1)
296 break;
297
298 newcut -= (ed[higain]-id[higain]);
299
300 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
301 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
302 newbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ubfactors, newbalv);
303
304 if ((newcut < mincut && newbal <= ffactor) ||
305 (newcut == mincut && (newbal < minbal ||
306 (newbal == minbal && BetterBalance2Way(ncon, minbalv, newbalv))))) {
307 mincut = newcut;
308 minbal = newbal;
309 mincutorder = nswaps;
310 rcopy(ncon, newbalv, minbalv);
311 }
312 else if (nswaps-mincutorder > limit) { /* We hit the limit, undo last move */
313 newcut += (ed[higain]-id[higain]);
314 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
315 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
316 break;
317 }
318
319 where[higain] = to;
320 moved[higain] = nswaps;
321 swaps[nswaps] = higain;
322
323 if (ctrl->dbglvl&METIS_DBG_MOVEINFO) {
324 HIGHS_ORDERING_PRINT("Moved%6"PRIDX" from %"PRIDX"(%"PRIDX") Gain:%5"PRIDX", "
325 "Cut:%5"PRIDX", NPwgts:", higain, from, cnum, ed[higain]-id[higain], newcut);
326 for (l=0; l<ncon; l++)
327 HIGHS_ORDERING_PRINT("(%.3"PRREAL" %.3"PRREAL")", pwgts[l]*invtvwgt[l], pwgts[ncon+l]*invtvwgt[l]);
328 HIGHS_ORDERING_PRINT(" %+.3"PRREAL" LB: %.3"PRREAL"(%+.3"PRREAL")\n",
329 minbal, ComputeLoadImbalance(graph, 2, ctrl->pijbm), newbal);
330 }
331
332
333 /**************************************************************
334 * Update the id[i]/ed[i] values of the affected nodes
335 ***************************************************************/
336 SWAP(id[higain], ed[higain], tmp);
337 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
338 BNDDelete(nbnd, bndind, bndptr, higain);
339
340 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
341 k = adjncy[j];
342
343 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
344 INC_DEC(id[k], ed[k], kwgt);
345
346 /* Update its boundary information and queue position */
347 if (bndptr[k] != -1) { /* If k was a boundary vertex */
348 if (ed[k] == 0) { /* Not a boundary vertex any more */
349 BNDDelete(nbnd, bndind, bndptr, k);
350 if (moved[k] == -1) /* Remove it if in the queues */
351 rpqDelete(queues[2*qnum[k]+where[k]], k);
352 }
353 else { /* If it has not been moved, update its position in the queue */
354 if (moved[k] == -1) {
355 //rgain = 1.0*(ed[k]-id[k])/sqrt(vwgt[k*ncon+qnum[k]]+1);
356 //rgain = (ed[k]-id[k] > 0 ?
357 // 1.0*(ed[k]-id[k])/sqrt(vwgt[k*ncon+qnum[k]]+1) : ed[k]-id[k]);
358 rgain = ed[k]-id[k];
359 rpqUpdate(queues[2*qnum[k]+where[k]], k, rgain);
360 }
361 }
362 }
363 else {
364 if (ed[k] > 0) { /* It will now become a boundary vertex */
365 BNDInsert(nbnd, bndind, bndptr, k);
366 if (moved[k] == -1) {
367 //rgain = 1.0*(ed[k]-id[k])/sqrt(vwgt[k*ncon+qnum[k]]+1);
368 //rgain = (ed[k]-id[k] > 0 ?
369 // 1.0*(ed[k]-id[k])/sqrt(vwgt[k*ncon+qnum[k]]+1) : ed[k]-id[k]);
370 rgain = ed[k]-id[k];
371 rpqInsert(queues[2*qnum[k]+where[k]], k, rgain);
372 }
373 }
374 }
375 }
376
377 }
378
379
380 /****************************************************************
381 * Roll back computations
382 *****************************************************************/
383 for (i=0; i<nswaps; i++)
384 moved[swaps[i]] = -1; /* reset moved array */
385 for (nswaps--; nswaps>mincutorder; nswaps--) {
386 higain = swaps[nswaps];
387
388 to = where[higain] = (where[higain]+1)%2;
389 SWAP(id[higain], ed[higain], tmp);
390 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
391 BNDDelete(nbnd, bndind, bndptr, higain);
392 else if (ed[higain] > 0 && bndptr[higain] == -1)
393 BNDInsert(nbnd, bndind, bndptr, higain);
394
395 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
396 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+((to+1)%2)*ncon, 1);
397 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
398 k = adjncy[j];
399
400 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
401 INC_DEC(id[k], ed[k], kwgt);
402
403 if (bndptr[k] != -1 && ed[k] == 0)
404 BNDDelete(nbnd, bndind, bndptr, k);
405 if (bndptr[k] == -1 && ed[k] > 0)
406 BNDInsert(nbnd, bndind, bndptr, k);
407 }
408 }
409
410 graph->mincut = mincut;
411 graph->nbnd = nbnd;
412
413 IFSET(ctrl->dbglvl, METIS_DBG_REFINE,
414 Print2WayRefineStats(ctrl, graph, ntpwgts, minbal, mincutorder));
415
416 if (mincutorder <= 0 || mincut == initcut)
417 break;
418 }
419
420 for (i=0; i<2*ncon; i++)
421 rpqDestroy(queues[i]);
422
423 WCOREPOP;
424 }
425
426
427 /*************************************************************************/
428 /*! This function selects the partition number and the queue from which
429 we will move vertices out. */
430 /*************************************************************************/
431 void SelectQueue(graph_t *graph, real_t *pijbm, real_t *ubfactors,
432 rpq_t **queues, idx_t *from, idx_t *cnum)
433 {
434 idx_t ncon, i, part;
435 real_t max, tmp;
436
437 ncon = graph->ncon;
438
439 *from = -1;
440 *cnum = -1;
441
442 /* First determine the side and the queue, irrespective of the presence of nodes.
443 The side & queue is determined based on the most violated balancing constraint. */
444 for (max=0.0, part=0; part<2; part++) {
445 for (i=0; i<ncon; i++) {
446 tmp = graph->pwgts[part*ncon+i]*pijbm[part*ncon+i] - ubfactors[i];
447 /* the '=' in the test below is to ensure that under tight constraints
448 the partition that is at the max is selected */
449 if (tmp >= max) {
450 max = tmp;
451 *from = part;
452 *cnum = i;
453 }
454 }
455 }
456
457
458 if (*from != -1) {
459 /* in case the desired queue is empty, select a queue from the same side */
460 if (rpqLength(queues[2*(*cnum)+(*from)]) == 0) {
461 for (i=0; i<ncon; i++) {
462 if (rpqLength(queues[2*i+(*from)]) > 0) {
463 max = graph->pwgts[(*from)*ncon+i]*pijbm[(*from)*ncon+i] - ubfactors[i];
464 *cnum = i;
465 break;
466 }
467 }
468
469 for (i++; i<ncon; i++) {
470 tmp = graph->pwgts[(*from)*ncon+i]*pijbm[(*from)*ncon+i] - ubfactors[i];
471 if (tmp > max && rpqLength(queues[2*i+(*from)]) > 0) {
472 max = tmp;
473 *cnum = i;
474 }
475 }
476 }
477
478 }
479 else {
480 /* the partitioning does not violate balancing constraints, in which case select
481 a queue based on cut criteria */
482 for (part=0; part<2; part++) {
483 for (i=0; i<ncon; i++) {
484 if (rpqLength(queues[2*i+part]) > 0 &&
485 (*from == -1 || rpqSeeTopKey(queues[2*i+part]) > max)) {
486 max = rpqSeeTopKey(queues[2*i+part]);
487 *from = part;
488 *cnum = i;
489 }
490 }
491 }
492 }
493 }
494
495
496 /*************************************************************************/
497 /*! Prints statistics about the refinement */
498 /*************************************************************************/
499 void Print2WayRefineStats(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts,
500 real_t deltabal, idx_t mincutorder)
501 {
502 int i;
503
504 if (mincutorder == -2) {
505 HIGHS_ORDERING_PRINT("Parts: ");
506 HIGHS_ORDERING_PRINT("Nv-Nb[%5"PRIDX" %5"PRIDX"] ICut: %6"PRIDX,
507 graph->nvtxs, graph->nbnd, graph->mincut);
508 HIGHS_ORDERING_PRINT(" [");
509 for (i=0; i<graph->ncon; i++)
510 HIGHS_ORDERING_PRINT("(%.3"PRREAL" %.3"PRREAL" T:%.3"PRREAL" %.3"PRREAL")",
511 graph->pwgts[i]*graph->invtvwgt[i],
512 graph->pwgts[graph->ncon+i]*graph->invtvwgt[i],
513 ntpwgts[i], ntpwgts[graph->ncon+i]);
514 HIGHS_ORDERING_PRINT("] LB: %.3"PRREAL"(%+.3"PRREAL")\n",
515 ComputeLoadImbalance(graph, 2, ctrl->pijbm), deltabal);
516 }
517 else {
518 HIGHS_ORDERING_PRINT("\tMincut: %6"PRIDX" at %5"PRIDX" NBND %6"PRIDX" NPwgts: [",
519 graph->mincut, mincutorder, graph->nbnd);
520 for (i=0; i<graph->ncon; i++)
521 HIGHS_ORDERING_PRINT("(%.3"PRREAL" %.3"PRREAL")",
522 graph->pwgts[i]*graph->invtvwgt[i], graph->pwgts[graph->ncon+i]*graph->invtvwgt[i]);
523 HIGHS_ORDERING_PRINT("] LB: %.3"PRREAL"(%+.3"PRREAL")\n",
524 ComputeLoadImbalance(graph, 2, ctrl->pijbm), deltabal);
525 }
526 }
527