ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
balance.c
Go to the documentation of this file.
1 /*
2  * Copyright 1997, Regents of the University of Minnesota
3  *
4  * balance.c
5  *
6  * This file contains code that is used to forcefully balance either
7  * bisections or k-sections
8  *
9  * Started 7/29/97
10  * George
11  *
12  * $Id: balance.c,v 1.1 1998/11/27 17:59:10 karypis Exp $
13  *
14  */
15 
16 #include "metis.h"
17 
18 void Bnd2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts);
19 void General2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts);
20 
21 /*************************************************************************
22 * This function is the entry point of the bisection balancing algorithms.
23 **************************************************************************/
24 void Balance2Way(CtrlType *ctrl, GraphType *graph, int *tpwgts, float ubfactor)
25 {
26  int i, j, nvtxs, from, imax, gain, mindiff;
27  idxtype *id, *ed;
28 
29  /* Return right away if the balance is OK */
30  mindiff = abs(tpwgts[0]-graph->pwgts[0]);
31  if (mindiff < 3*(graph->pwgts[0]+graph->pwgts[1])/graph->nvtxs)
32  return;
33  if (graph->pwgts[0] > tpwgts[0] && graph->pwgts[0] < (int)(ubfactor*tpwgts[0]))
34  return;
35  if (graph->pwgts[1] > tpwgts[1] && graph->pwgts[1] < (int)(ubfactor*tpwgts[1]))
36  return;
37 
38  if (graph->nbnd > 0)
39  Bnd2WayBalance(ctrl, graph, tpwgts);
40  else
41  General2WayBalance(ctrl, graph, tpwgts);
42 
43 }
44 
45 
46 
47 /*************************************************************************
48 * This function balances two partitions by moving boundary nodes
49 * from the domain that is overweight to the one that is underweight.
50 **************************************************************************/
51 void Bnd2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts)
52 {
53  int i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
54  idxtype *xadj, *vwgt, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;
55  idxtype *moved, *perm;
56  PQueueType parts;
57  int higain, oldgain, mincut, mindiff;
58 
59  nvtxs = graph->nvtxs;
60  xadj = graph->xadj;
61  vwgt = graph->vwgt;
62  adjncy = graph->adjncy;
63  adjwgt = graph->adjwgt;
64  where = graph->where;
65  id = graph->id;
66  ed = graph->ed;
67  pwgts = graph->pwgts;
68  bndptr = graph->bndptr;
69  bndind = graph->bndind;
70 
71  moved = idxwspacemalloc(ctrl, nvtxs);
72  perm = idxwspacemalloc(ctrl, nvtxs);
73 
74  /* Determine from which domain you will be moving data */
75  mindiff = abs(tpwgts[0]-pwgts[0]);
76  from = (pwgts[0] < tpwgts[0] ? 1 : 0);
77  to = (from+1)%2;
78 
79  IFSET(ctrl->dbglvl, DBG_REFINE,
80  printf("Partitions: [%6d %6d] T[%6d %6d], Nv-Nb[%6d %6d]. ICut: %6d [B]\n",
81  pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut));
82 
83  tmp = graph->adjwgtsum[idxamax(nvtxs, graph->adjwgtsum)];
84  PQueueInit(ctrl, &parts, nvtxs, tmp);
85 
86  idxset(nvtxs, -1, moved);
87 
88  ASSERT(ComputeCut(graph, where) == graph->mincut);
89  ASSERT(CheckBnd(graph));
90 
91  /* Insert the boundary nodes of the proper partition whose size is OK in the priority queue */
92  nbnd = graph->nbnd;
93  RandomPermute(nbnd, perm, 1);
94  for (ii=0; ii<nbnd; ii++) {
95  i = perm[ii];
96  ASSERT(ed[bndind[i]] > 0 || id[bndind[i]] == 0);
97  ASSERT(bndptr[bndind[i]] != -1);
98  if (where[bndind[i]] == from && vwgt[bndind[i]] <= mindiff)
99  PQueueInsert(&parts, bndind[i], ed[bndind[i]]-id[bndind[i]]);
100  }
101 
102  mincut = graph->mincut;
103  for (nswaps=0; nswaps<nvtxs; nswaps++) {
104  if ((higain = PQueueGetMax(&parts)) == -1)
105  break;
106  ASSERT(bndptr[higain] != -1);
107 
108  if (pwgts[to]+vwgt[higain] > tpwgts[to])
109  break;
110 
111  mincut -= (ed[higain]-id[higain]);
112  INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
113 
114  where[higain] = to;
115  moved[higain] = nswaps;
116 
117  IFSET(ctrl->dbglvl, DBG_MOVEINFO,
118  printf("Moved %6d from %d. [%3d %3d] %5d [%4d %4d]\n", higain, from, ed[higain]-id[higain], vwgt[higain], mincut, pwgts[0], pwgts[1]));
119 
120  /**************************************************************
121  * Update the id[i]/ed[i] values of the affected nodes
122  ***************************************************************/
123  SWAP(id[higain], ed[higain], tmp);
124  if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
125  BNDDelete(nbnd, bndind, bndptr, higain);
126 
127  for (j=xadj[higain]; j<xadj[higain+1]; j++) {
128  k = adjncy[j];
129  oldgain = ed[k]-id[k];
130 
131  kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
132  INC_DEC(id[k], ed[k], kwgt);
133 
134  /* Update its boundary information and queue position */
135  if (bndptr[k] != -1) { /* If k was a boundary vertex */
136  if (ed[k] == 0) { /* Not a boundary vertex any more */
137  BNDDelete(nbnd, bndind, bndptr, k);
138  if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff) /* Remove it if in the queues */
139  PQueueDelete(&parts, k, oldgain);
140  }
141  else { /* If it has not been moved, update its position in the queue */
142  if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
143  PQueueUpdate(&parts, k, oldgain, ed[k]-id[k]);
144  }
145  }
146  else {
147  if (ed[k] > 0) { /* It will now become a boundary vertex */
148  BNDInsert(nbnd, bndind, bndptr, k);
149  if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
150  PQueueInsert(&parts, k, ed[k]-id[k]);
151  }
152  }
153  }
154  }
155 
156  IFSET(ctrl->dbglvl, DBG_REFINE,
157  printf("\tMinimum cut: %6d, PWGTS: [%6d %6d], NBND: %6d\n", mincut, pwgts[0], pwgts[1], nbnd));
158 
159  graph->mincut = mincut;
160  graph->nbnd = nbnd;
161 
162  PQueueFree(ctrl, &parts);
163 
164  idxwspacefree(ctrl, nvtxs);
165  idxwspacefree(ctrl, nvtxs);
166 }
167 
168 
169 /*************************************************************************
170 * This function balances two partitions by moving the highest gain
171 * (including negative gain) vertices to the other domain.
172 * It is used only when tha unbalance is due to non contigous
173 * subdomains. That is, the are no boundary vertices.
174 * It moves vertices from the domain that is overweight to the one that
175 * is underweight.
176 **************************************************************************/
177 void General2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts)
178 {
179  int i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
180  idxtype *xadj, *vwgt, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;
181  idxtype *moved, *perm;
182  PQueueType parts;
183  int higain, oldgain, mincut, mindiff;
184 
185  nvtxs = graph->nvtxs;
186  xadj = graph->xadj;
187  vwgt = graph->vwgt;
188  adjncy = graph->adjncy;
189  adjwgt = graph->adjwgt;
190  where = graph->where;
191  id = graph->id;
192  ed = graph->ed;
193  pwgts = graph->pwgts;
194  bndptr = graph->bndptr;
195  bndind = graph->bndind;
196 
197  moved = idxwspacemalloc(ctrl, nvtxs);
198  perm = idxwspacemalloc(ctrl, nvtxs);
199 
200  /* Determine from which domain you will be moving data */
201  mindiff = abs(tpwgts[0]-pwgts[0]);
202  from = (pwgts[0] < tpwgts[0] ? 1 : 0);
203  to = (from+1)%2;
204 
205  IFSET(ctrl->dbglvl, DBG_REFINE,
206  printf("Partitions: [%6d %6d] T[%6d %6d], Nv-Nb[%6d %6d]. ICut: %6d [B]\n",
207  pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut));
208 
209  tmp = graph->adjwgtsum[idxamax(nvtxs, graph->adjwgtsum)];
210  PQueueInit(ctrl, &parts, nvtxs, tmp);
211 
212  idxset(nvtxs, -1, moved);
213 
214  ASSERT(ComputeCut(graph, where) == graph->mincut);
215  ASSERT(CheckBnd(graph));
216 
217  /* Insert the nodes of the proper partition whose size is OK in the priority queue */
218  RandomPermute(nvtxs, perm, 1);
219  for (ii=0; ii<nvtxs; ii++) {
220  i = perm[ii];
221  if (where[i] == from && vwgt[i] <= mindiff)
222  PQueueInsert(&parts, i, ed[i]-id[i]);
223  }
224 
225  mincut = graph->mincut;
226  nbnd = graph->nbnd;
227  for (nswaps=0; nswaps<nvtxs; nswaps++) {
228  if ((higain = PQueueGetMax(&parts)) == -1)
229  break;
230 
231  if (pwgts[to]+vwgt[higain] > tpwgts[to])
232  break;
233 
234  mincut -= (ed[higain]-id[higain]);
235  INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
236 
237  where[higain] = to;
238  moved[higain] = nswaps;
239 
240  IFSET(ctrl->dbglvl, DBG_MOVEINFO,
241  printf("Moved %6d from %d. [%3d %3d] %5d [%4d %4d]\n", higain, from, ed[higain]-id[higain], vwgt[higain], mincut, pwgts[0], pwgts[1]));
242 
243  /**************************************************************
244  * Update the id[i]/ed[i] values of the affected nodes
245  ***************************************************************/
246  SWAP(id[higain], ed[higain], tmp);
247  if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
248  BNDDelete(nbnd, bndind, bndptr, higain);
249  if (ed[higain] > 0 && bndptr[higain] == -1)
250  BNDInsert(nbnd, bndind, bndptr, higain);
251 
252  for (j=xadj[higain]; j<xadj[higain+1]; j++) {
253  k = adjncy[j];
254  oldgain = ed[k]-id[k];
255 
256  kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
257  INC_DEC(id[k], ed[k], kwgt);
258 
259  /* Update the queue position */
260  if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
261  PQueueUpdate(&parts, k, oldgain, ed[k]-id[k]);
262 
263  /* Update its boundary information */
264  if (ed[k] == 0 && bndptr[k] != -1)
265  BNDDelete(nbnd, bndind, bndptr, k);
266  else if (ed[k] > 0 && bndptr[k] == -1)
267  BNDInsert(nbnd, bndind, bndptr, k);
268  }
269  }
270 
271  IFSET(ctrl->dbglvl, DBG_REFINE,
272  printf("\tMinimum cut: %6d, PWGTS: [%6d %6d], NBND: %6d\n", mincut, pwgts[0], pwgts[1], nbnd));
273 
274  graph->mincut = mincut;
275  graph->nbnd = nbnd;
276 
277  PQueueFree(ctrl, &parts);
278 
279  idxwspacefree(ctrl, nvtxs);
280  idxwspacefree(ctrl, nvtxs);
281 }
void Balance2Way(CtrlType *ctrl, GraphType *graph, int *tpwgts, float ubfactor)
Definition: balance.c:24
void General2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts)
Definition: balance.c:177
void Bnd2WayBalance(CtrlType *ctrl, GraphType *graph, int *tpwgts)
Definition: balance.c:51
__host__ __device__ int2 abs(int2 v)
Definition: cutil_math.h:1267
#define DBG_MOVEINFO
Definition: defs.h:179
#define DBG_REFINE
Definition: defs.h:177
#define INC_DEC(a, b, val)
Definition: macros.h:34
#define ASSERT(expr)
Definition: macros.h:125
#define SWAP(a, b, tmp)
Definition: macros.h:31
#define BNDInsert(nbnd, bndind, bndptr, vtx)
Definition: macros.h:97
#define BNDDelete(nbnd, bndind, bndptr, vtx)
Definition: macros.h:104
#define IFSET(a, flag, cmd)
Definition: macros.h:56
#define PQueueInit
Definition: rename.h:315
#define PQueueUpdate
Definition: rename.h:320
#define ComputeCut
Definition: rename.h:43
#define PQueueGetMax
Definition: rename.h:322
#define PQueueInsert
Definition: rename.h:318
#define RandomPermute
Definition: rename.h:410
#define CheckBnd
Definition: rename.h:44
#define idxset
Definition: rename.h:390
#define idxamax
Definition: rename.h:393
#define PQueueDelete
Definition: rename.h:319
#define idxwspacemalloc
Definition: rename.h:164
#define idxwspacefree
Definition: rename.h:165
#define PQueueFree
Definition: rename.h:317
int idxtype
Definition: struct.h:17
int dbglvl
Definition: struct.h:224
int nbnd
Definition: struct.h:184
idxtype * adjncy
Definition: struct.h:172
idxtype * bndind
Definition: struct.h:185
idxtype * where
Definition: struct.h:183
int nvtxs
Definition: struct.h:168
idxtype * ed
Definition: struct.h:188
idxtype * vwgt
Definition: struct.h:170
idxtype * adjwgtsum
Definition: struct.h:175
idxtype * pwgts
Definition: struct.h:183
idxtype * id
Definition: struct.h:188
int mincut
Definition: struct.h:182
idxtype * adjwgt
Definition: struct.h:173
idxtype * bndptr
Definition: struct.h:185
idxtype * xadj
Definition: struct.h:169