21 if (graph->
ncon == 1) {
43 idx_t i, ii,
j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
47 idx_t higain, mincut, mindiff;
68 tpwgts[0] = graph->
tvwgt[0]*ntpwgts[0];
69 tpwgts[1] = graph->
tvwgt[0] - tpwgts[0];
70 mindiff =
iabs(tpwgts[0]-pwgts[0]);
71 from = (pwgts[0] < tpwgts[0] ? 1 : 0);
76 pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->
nvtxs, graph->
nbnd,
81 iset(nvtxs, -1, moved);
89 for (ii=0; ii<nbnd; ii++) {
91 ASSERT(ed[bndind[i]] > 0 ||
id[bndind[i]] == 0);
92 ASSERT(bndptr[bndind[i]] != -1);
93 if (where[bndind[i]] == from && vwgt[bndind[i]] <= mindiff)
94 rpqInsert(queue, bndind[i], ed[bndind[i]]-
id[bndind[i]]);
98 for (nswaps=0; nswaps<nvtxs; nswaps++) {
101 ASSERT(bndptr[higain] != -1);
103 if (pwgts[to]+vwgt[higain] > tpwgts[to])
106 mincut -= (ed[higain]-
id[higain]);
107 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
110 moved[higain] = nswaps;
113 printf(
"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]));
118 SWAP(
id[higain], ed[higain], tmp);
119 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
122 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
124 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
128 if (bndptr[k] != -1) {
131 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
135 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
142 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
150 printf(
"\tMinimum cut: %6"PRIDX
", PWGTS: [%6"PRIDX
" %6"PRIDX
"], NBND: %6"PRIDX
"\n", mincut, pwgts[0], pwgts[1], nbnd));
171 idx_t i, ii,
j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;
175 idx_t higain, mincut, mindiff;
180 nvtxs = graph->
nvtxs;
185 where = graph->
where;
188 pwgts = graph->
pwgts;
196 tpwgts[0] = graph->
tvwgt[0]*ntpwgts[0];
197 tpwgts[1] = graph->
tvwgt[0] - tpwgts[0];
198 mindiff =
iabs(tpwgts[0]-pwgts[0]);
199 from = (pwgts[0] < tpwgts[0] ? 1 : 0);
204 pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->
nvtxs, graph->
nbnd, graph->
mincut));
208 iset(nvtxs, -1, moved);
215 for (ii=0; ii<nvtxs; ii++) {
217 if (where[i] == from && vwgt[i] <= mindiff)
223 for (nswaps=0; nswaps<nvtxs; nswaps++) {
227 if (pwgts[to]+vwgt[higain] > tpwgts[to])
230 mincut -= (ed[higain]-
id[higain]);
231 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
234 moved[higain] = nswaps;
237 printf(
"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]));
242 SWAP(
id[higain], ed[higain], tmp);
243 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
245 if (ed[higain] > 0 && bndptr[higain] == -1)
248 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
251 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
255 if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)
259 if (ed[k] == 0 && bndptr[k] != -1)
261 else if (ed[k] > 0 && bndptr[k] == -1)
267 printf(
"\tMinimum cut: %6"PRIDX
", PWGTS: [%6"PRIDX
" %6"PRIDX
"], NBND: %6"PRIDX
"\n", mincut, pwgts[0], pwgts[1], nbnd));
283 idx_t i, ii,
j, k,
l, kwgt, nvtxs,
ncon, nbnd, nswaps, from, to, pass,
284 me, limit, tmp, cnum;
286 idx_t *moved, *swaps, *
perm, *qnum, *qsizes;
287 idx_t higain, mincut, newcut, mincutorder;
288 real_t *invtvwgt, *minbalv, *newbalv, minbal, newbal;
293 nvtxs = graph->
nvtxs;
300 where = graph->
where;
303 pwgts = graph->
pwgts;
318 queues = (rpq_t **)
wspacemalloc(ctrl, 2*ncon*
sizeof(rpq_t *));
319 for (i=0; i<2*
ncon; i++) {
324 for (i=0; i<nvtxs; i++) {
326 qsizes[2*qnum[
i]+where[
i]]++;
331 for (from=0; from<2; from++) {
332 for (j=0; j<
ncon; j++) {
333 if (qsizes[2*j+from] == 0) {
334 for (i=0; i<nvtxs; i++) {
335 if (where[i] != from)
340 qsizes[2*qnum[i]+from] > qsizes[2*j+from] &&
341 vwgt[i*ncon+qnum[i]]*invtvwgt[qnum[i]] < 1.3*vwgt[i*ncon+j]*invtvwgt[j]) {
342 qsizes[2*qnum[
i]+from]--;
355 newcut = mincut = graph->
mincut;
360 for (l=0; l<
ncon; l++)
362 pwgts[l], pwgts[ncon+l], ntpwgts[l], ntpwgts[ncon+l]);
363 printf(
"] Nv-Nb[%5"PRIDX", %5"PRIDX"]. ICut: %6"PRIDX", LB: %+.3"PRREAL
" [B]\n",
367 iset(nvtxs, -1, moved);
375 for (ii=0; ii<nvtxs; ii++) {
377 rpqInsert(queues[2*qnum[i]+where[i]], i, ed[i]-
id[i]);
380 for (nswaps=0; nswaps<nvtxs; nswaps++) {
387 if (from == -1 || (higain =
rpqGetTop(queues[2*cnum+from])) == -1)
390 newcut -= (ed[higain]-
id[higain]);
392 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
393 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
396 if (newbal < minbal || (newbal == minbal &&
401 mincutorder = nswaps;
402 rcopy(ncon, newbalv, minbalv);
404 else if (nswaps-mincutorder > limit) {
405 newcut += (ed[higain]-
id[higain]);
406 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
407 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
412 moved[higain] = nswaps;
413 swaps[nswaps] = higain;
417 "Cut: %5"PRIDX", NPwgts: ", higain, from, cnum, ed[higain]-
id[higain], newcut);
418 for (l=0; l<
ncon; l++)
419 printf(
"(%6"PRIDX
", %6"PRIDX
") ", pwgts[l], pwgts[ncon+l]);
420 printf(
", %+.3"PRREAL" LB: %+.3"PRREAL"\n", minbal, newbal);
427 SWAP(
id[higain], ed[higain], tmp);
428 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
430 if (ed[higain] > 0 && bndptr[higain] == -1)
433 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
436 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
441 rpqUpdate(queues[2*qnum[k]+where[k]], k, ed[k]-
id[k]);
444 if (ed[k] == 0 && bndptr[k] != -1)
446 else if (ed[k] > 0 && bndptr[k] == -1)
456 for (nswaps--; nswaps>mincutorder; nswaps--) {
457 higain = swaps[nswaps];
459 to = where[higain] = (where[higain]+1)%2;
460 SWAP(
id[higain], ed[higain], tmp);
461 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
463 else if (ed[higain] > 0 && bndptr[higain] == -1)
466 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
467 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+((to+1)%2)*ncon, 1);
468 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
471 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
474 if (bndptr[k] != -1 && ed[k] == 0)
476 if (bndptr[k] == -1 && ed[k] > 0)
483 mincut, mincutorder, nbnd);
484 for (l=0; l<
ncon; l++)
485 printf(
"(%6"PRIDX
", %6"PRIDX
") ", pwgts[l], pwgts[ncon+l]);
493 for (i=0; i<2*
ncon; i++)
idx_t idx_t idx_t idx_t * vwgt
#define BNDDelete(nbnd, bndind, bndptr, vtx)
void General2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
#define irandArrayPermute
#define ComputeLoadImbalanceDiff
Q id(Eigen::AngleAxisd(0, Q_z_axis))
void Balance2Way(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
NonlinearFactorGraph graph
#define ComputeLoadImbalance
#define ASSERT(expression)
static const Line3 l(Rot3(), 1, 1)
#define BNDInsert(nbnd, bndind, bndptr, vtx)
#define BetterBalance2Way
idx_t idx_t idx_t idx_t idx_t * perm
idx_t idx_t idx_t idx_t idx_t idx_t idx_t real_t * tpwgts
idx_t idx_t idx_t idx_t idx_t idx_t * adjwgt
#define INC_DEC(a, b, val)
#define ComputeLoadImbalanceDiffVec
void Bnd2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)
idx_t idx_t idx_t idx_t * where
idx_t idx_t idx_t * adjncy
#define IFSET(a, flag, cmd)
void McGeneral2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)