1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*
* Copyright 1997, Regents of the University of Minnesota
*
* mfm.c
*
* This file contains code that implements the edge-based FM refinement
*
* Started 7/23/97
* George
*
* $Id: mfm.c,v 1.1 2003/07/24 18:39:09 karypis Exp $
*/
#include <metis.h>
/*************************************************************************
* This function performs an edge-based FM refinement
**************************************************************************/
void MocFM_2WayEdgeRefine(CtrlType *ctrl, GraphType *graph, float *tpwgts, int npasses)
{
int i, ii, j, k, l, kwgt, nvtxs, ncon, nbnd, nswaps, from, to, pass, me, limit, tmp, cnum;
idxtype *xadj, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind;
idxtype *moved, *swaps, *perm, *qnum;
float *nvwgt, *npwgts, mindiff[MAXNCON], origbal, minbal, newbal;
PQueueType parts[MAXNCON][2];
int higain, oldgain, mincut, initcut, newcut, mincutorder;
float rtpwgts[2];
nvtxs = graph->nvtxs;
ncon = graph->ncon;
xadj = graph->xadj;
nvwgt = graph->nvwgt;
adjncy = graph->adjncy;
adjwgt = graph->adjwgt;
where = graph->where;
id = graph->id;
ed = graph->ed;
npwgts = graph->npwgts;
bndptr = graph->bndptr;
bndind = graph->bndind;
moved = idxwspacemalloc(ctrl, nvtxs);
swaps = idxwspacemalloc(ctrl, nvtxs);
perm = idxwspacemalloc(ctrl, nvtxs);
qnum = idxwspacemalloc(ctrl, nvtxs);
limit = amin(amax(0.01*nvtxs, 25), 150);
/* Initialize the queues */
for (i=0; i<ncon; i++) {
PQueueInit(ctrl, &parts[i][0], nvtxs, PLUS_GAINSPAN+1);
PQueueInit(ctrl, &parts[i][1], nvtxs, PLUS_GAINSPAN+1);
}
for (i=0; i<nvtxs; i++)
qnum[i] = samax(ncon, nvwgt+i*ncon);
origbal = Compute2WayHLoadImbalance(ncon, npwgts, tpwgts);
rtpwgts[0] = origbal*tpwgts[0];
rtpwgts[1] = origbal*tpwgts[1];
if (ctrl->dbglvl&DBG_REFINE) {
printf("Parts: [");
for (l=0; l<ncon; l++)
printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
printf("] T[%.3f %.3f], Nv-Nb[%5d, %5d]. ICut: %6d, LB: %.3f\n", tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut, origbal);
}
idxset(nvtxs, -1, moved);
for (pass=0; pass<npasses; pass++) { /* Do a number of passes */
for (i=0; i<ncon; i++) {
PQueueReset(&parts[i][0]);
PQueueReset(&parts[i][1]);
}
mincutorder = -1;
newcut = mincut = initcut = graph->mincut;
for (i=0; i<ncon; i++)
mindiff[i] = fabs(tpwgts[0]-npwgts[i]);
minbal = Compute2WayHLoadImbalance(ncon, npwgts, tpwgts);
ASSERT(ComputeCut(graph, where) == graph->mincut);
ASSERT(CheckBnd(graph));
/* Insert boundary nodes in the priority queues */
nbnd = graph->nbnd;
RandomPermute(nbnd, perm, 1);
for (ii=0; ii<nbnd; ii++) {
i = bndind[perm[ii]];
ASSERT(ed[i] > 0 || id[i] == 0);
ASSERT(bndptr[i] != -1);
PQueueInsert(&parts[qnum[i]][where[i]], i, ed[i]-id[i]);
}
for (nswaps=0; nswaps<nvtxs; nswaps++) {
SelectQueue(ncon, npwgts, rtpwgts, &from, &cnum, parts);
to = (from+1)%2;
if (from == -1 || (higain = PQueueGetMax(&parts[cnum][from])) == -1)
break;
ASSERT(bndptr[higain] != -1);
saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1);
saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+from*ncon, 1);
newcut -= (ed[higain]-id[higain]);
newbal = Compute2WayHLoadImbalance(ncon, npwgts, tpwgts);
if ((newcut < mincut && newbal-origbal <= .00001) ||
(newcut == mincut && (newbal < minbal ||
(newbal == minbal && BetterBalance(ncon, npwgts, tpwgts, mindiff))))) {
mincut = newcut;
minbal = newbal;
mincutorder = nswaps;
for (i=0; i<ncon; i++)
mindiff[i] = fabs(tpwgts[0]-npwgts[i]);
}
else if (nswaps-mincutorder > limit) { /* We hit the limit, undo last move */
newcut += (ed[higain]-id[higain]);
saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+from*ncon, 1);
saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1);
break;
}
where[higain] = to;
moved[higain] = nswaps;
swaps[nswaps] = higain;
if (ctrl->dbglvl&DBG_MOVEINFO) {
printf("Moved %6d from %d(%d). Gain: %5d, Cut: %5d, NPwgts: ", higain, from, cnum, ed[higain]-id[higain], newcut);
for (l=0; l<ncon; l++)
printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
printf(", %.3f LB: %.3f\n", minbal, newbal);
}
/**************************************************************
* Update the id[i]/ed[i] values of the affected nodes
***************************************************************/
SWAP(id[higain], ed[higain], tmp);
if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
BNDDelete(nbnd, bndind, bndptr, higain);
for (j=xadj[higain]; j<xadj[higain+1]; j++) {
k = adjncy[j];
oldgain = ed[k]-id[k];
kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
INC_DEC(id[k], ed[k], kwgt);
/* Update its boundary information and queue position */
if (bndptr[k] != -1) { /* If k was a boundary vertex */
if (ed[k] == 0) { /* Not a boundary vertex any more */
BNDDelete(nbnd, bndind, bndptr, k);
if (moved[k] == -1) /* Remove it if in the queues */
PQueueDelete(&parts[qnum[k]][where[k]], k, oldgain);
}
else { /* If it has not been moved, update its position in the queue */
if (moved[k] == -1)
PQueueUpdate(&parts[qnum[k]][where[k]], k, oldgain, ed[k]-id[k]);
}
}
else {
if (ed[k] > 0) { /* It will now become a boundary vertex */
BNDInsert(nbnd, bndind, bndptr, k);
if (moved[k] == -1)
PQueueInsert(&parts[qnum[k]][where[k]], k, ed[k]-id[k]);
}
}
}
}
/****************************************************************
* Roll back computations
*****************************************************************/
for (i=0; i<nswaps; i++)
moved[swaps[i]] = -1; /* reset moved array */
for (nswaps--; nswaps>mincutorder; nswaps--) {
higain = swaps[nswaps];
to = where[higain] = (where[higain]+1)%2;
SWAP(id[higain], ed[higain], tmp);
if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
BNDDelete(nbnd, bndind, bndptr, higain);
else if (ed[higain] > 0 && bndptr[higain] == -1)
BNDInsert(nbnd, bndind, bndptr, higain);
saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1);
saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+((to+1)%2)*ncon, 1);
for (j=xadj[higain]; j<xadj[higain+1]; j++) {
k = adjncy[j];
kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
INC_DEC(id[k], ed[k], kwgt);
if (bndptr[k] != -1 && ed[k] == 0)
BNDDelete(nbnd, bndind, bndptr, k);
if (bndptr[k] == -1 && ed[k] > 0)
BNDInsert(nbnd, bndind, bndptr, k);
}
}
if (ctrl->dbglvl&DBG_REFINE) {
printf("\tMincut: %6d at %5d, NBND: %6d, NPwgts: [", mincut, mincutorder, nbnd);
for (l=0; l<ncon; l++)
printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
printf("], LB: %.3f\n", Compute2WayHLoadImbalance(ncon, npwgts, tpwgts));
}
graph->mincut = mincut;
graph->nbnd = nbnd;
if (mincutorder == -1 || mincut == initcut)
break;
}
for (i=0; i<ncon; i++) {
PQueueFree(ctrl, &parts[i][0]);
PQueueFree(ctrl, &parts[i][1]);
}
idxwspacefree(ctrl, nvtxs);
idxwspacefree(ctrl, nvtxs);
idxwspacefree(ctrl, nvtxs);
idxwspacefree(ctrl, nvtxs);
}
/*************************************************************************
* This function selects the partition number and the queue from which
* we will move vertices out
**************************************************************************/
void SelectQueue(int ncon, float *npwgts, float *tpwgts, int *from, int *cnum, PQueueType queues[MAXNCON][2])
{
int i, part, maxgain=0;
float max, maxdiff=0.0;
*from = -1;
*cnum = -1;
/* First determine the side and the queue, irrespective of the presence of nodes */
for (part=0; part<2; part++) {
for (i=0; i<ncon; i++) {
if (npwgts[part*ncon+i]-tpwgts[part] >= maxdiff) {
maxdiff = npwgts[part*ncon+i]-tpwgts[part];
*from = part;
*cnum = i;
}
}
}
/* printf("Selected %d(%d) -> %d\n", *from, *cnum, PQueueGetSize(&queues[*cnum][*from])); */
if (*from != -1 && PQueueGetSize(&queues[*cnum][*from]) == 0) {
/* The desired queue is empty, select a node from that side anyway */
for (i=0; i<ncon; i++) {
if (PQueueGetSize(&queues[i][*from]) > 0) {
max = npwgts[(*from)*ncon + i];
*cnum = i;
break;
}
}
for (i++; i<ncon; i++) {
if (npwgts[(*from)*ncon + i] > max && PQueueGetSize(&queues[i][*from]) > 0) {
max = npwgts[(*from)*ncon + i];
*cnum = i;
}
}
}
/* Check to see if you can focus on the cut */
if (maxdiff <= 0.0 || *from == -1) {
maxgain = -100000;
for (part=0; part<2; part++) {
for (i=0; i<ncon; i++) {
if (PQueueGetSize(&queues[i][part]) > 0 && PQueueGetKey(&queues[i][part]) > maxgain) {
maxgain = PQueueGetKey(&queues[i][part]);
*from = part;
*cnum = i;
}
}
}
}
}
/*************************************************************************
* This function checks if the balance achieved is better than the diff
* For now, it uses a 2-norm measure
**************************************************************************/
int BetterBalance(int ncon, float *npwgts, float *tpwgts, float *diff)
{
int i;
float ndiff[MAXNCON];
for (i=0; i<ncon; i++)
ndiff[i] = fabs(tpwgts[0]-npwgts[i]);
return snorm2(ncon, ndiff) < snorm2(ncon, diff);
}
/*************************************************************************
* This function computes the load imbalance over all the constrains
**************************************************************************/
float Compute2WayHLoadImbalance(int ncon, float *npwgts, float *tpwgts)
{
int i;
float max=0.0, temp;
for (i=0; i<ncon; i++) {
/* temp = amax(npwgts[i]/tpwgts[0], npwgts[ncon+i]/tpwgts[1]); */
temp = fabs(tpwgts[0]-npwgts[i])/tpwgts[0];
max = (max < temp ? temp : max);
}
return 1.0+max;
}
/*************************************************************************
* This function computes the load imbalance over all the constrains
* For now assume that we just want balanced partitionings
**************************************************************************/
void Compute2WayHLoadImbalanceVec(int ncon, float *npwgts, float *tpwgts, float *lbvec)
{
int i;
for (i=0; i<ncon; i++)
lbvec[i] = 1.0 + fabs(tpwgts[0]-npwgts[i])/tpwgts[0];
}
|