31 idx_t i, ii,
j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, limit, tmp;
35 idx_t higain, mincut, mindiff, origdiff, initcut, newcut, mincutorder, avgvwgt;
56 tpwgts[0] = graph->
tvwgt[0]*ntpwgts[0];
57 tpwgts[1] = graph->
tvwgt[0]-tpwgts[0];
60 avgvwgt =
gk_min((pwgts[0]+pwgts[1])/20, 2*(pwgts[0]+pwgts[1])/nvtxs);
68 origdiff =
iabs(tpwgts[0]-pwgts[0]);
69 iset(nvtxs, -1, moved);
70 for (pass=0; pass<niter; pass++) {
75 newcut = mincut = initcut = graph->
mincut;
76 mindiff =
iabs(tpwgts[0]-pwgts[0]);
84 for (ii=0; ii<nbnd; ii++) {
86 ASSERT(ed[bndind[i]] > 0 ||
id[bndind[i]] == 0);
87 ASSERT(bndptr[bndind[i]] != -1);
88 rpqInsert(queues[where[bndind[i]]], bndind[i], ed[bndind[i]]-
id[bndind[i]]);
91 for (nswaps=0; nswaps<nvtxs; nswaps++) {
92 from = (tpwgts[0]-pwgts[0] < tpwgts[1]-pwgts[1] ? 0 : 1);
95 if ((higain =
rpqGetTop(queues[from])) == -1)
97 ASSERT(bndptr[higain] != -1);
99 newcut -= (ed[higain]-
id[higain]);
100 INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);
102 if ((newcut < mincut &&
iabs(tpwgts[0]-pwgts[0]) <= origdiff+avgvwgt) ||
103 (newcut == mincut &&
iabs(tpwgts[0]-pwgts[0]) < mindiff)) {
105 mindiff =
iabs(tpwgts[0]-pwgts[0]);
106 mincutorder = nswaps;
108 else if (nswaps-mincutorder > limit) {
109 newcut += (ed[higain]-
id[higain]);
110 INC_DEC(pwgts[from], pwgts[to], vwgt[higain]);
115 moved[higain] = nswaps;
116 swaps[nswaps] = higain;
119 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], newcut, pwgts[0], pwgts[1]));
124 SWAP(
id[higain], ed[higain], tmp);
125 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
128 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
131 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
135 if (bndptr[k] != -1) {
143 rpqUpdate(queues[where[k]], k, ed[k]-
id[k]);
150 rpqInsert(queues[where[k]], k, ed[k]-
id[k]);
161 for (i=0; i<nswaps; i++)
162 moved[swaps[i]] = -1;
163 for (nswaps--; nswaps>mincutorder; nswaps--) {
164 higain = swaps[nswaps];
166 to = where[higain] = (where[higain]+1)%2;
167 SWAP(
id[higain], ed[higain], tmp);
168 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
170 else if (ed[higain] > 0 && bndptr[higain] == -1)
173 INC_DEC(pwgts[to], pwgts[(to+1)%2], vwgt[higain]);
174 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
177 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
180 if (bndptr[k] != -1 && ed[k] == 0)
182 if (bndptr[k] == -1 && ed[k] > 0)
193 if (mincutorder <= 0 || mincut == initcut)
209 idx_t i, ii,
j, k,
l, kwgt, nvtxs,
ncon, nbnd, nswaps, from, to, pass,
210 me, limit, tmp, cnum;
214 idx_t higain, mincut, initcut, newcut, mincutorder;
215 real_t *invtvwgt, *ubfactors, *minbalv, *newbalv;
216 real_t origbal, minbal, newbal, rgain, ffactor;
221 nvtxs = graph->
nvtxs;
228 where = graph->
where;
231 pwgts = graph->
pwgts;
248 ffactor = .5/
gk_max(20, nvtxs);
251 queues = (rpq_t **)
wspacemalloc(ctrl, 2*ncon*
sizeof(rpq_t *));
252 for (i=0; i<2*
ncon; i++)
254 for (i=0; i<nvtxs; i++)
255 qnum[i] =
iargmax_nrm(ncon, vwgt+i*ncon, invtvwgt);
264 for (i=0; i<
ncon; i++)
265 ubfactors[i] = (ubfactors[i] > 0 ? ctrl->
ubfactors[i]+ubfactors[i] : ctrl->
ubfactors[i]);
271 iset(nvtxs, -1, moved);
272 for (pass=0; pass<niter; pass++) {
273 for (i=0; i<2*
ncon; i++)
277 newcut = mincut = initcut = graph->
mincut;
287 for (ii=0; ii<nbnd; ii++) {
288 i = bndind[perm[ii]];
289 ASSERT(ed[i] > 0 ||
id[i] == 0);
294 rpqInsert(queues[2*qnum[i]+where[i]], i, rgain);
297 for (nswaps=0; nswaps<nvtxs; nswaps++) {
302 if (from == -1 || (higain =
rpqGetTop(queues[2*cnum+from])) == -1)
304 ASSERT(bndptr[higain] != -1);
306 newcut -= (ed[higain]-
id[higain]);
308 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
309 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
312 if ((newcut < mincut && newbal <= ffactor) ||
313 (newcut == mincut && (newbal < minbal ||
317 mincutorder = nswaps;
318 rcopy(ncon, newbalv, minbalv);
320 else if (nswaps-mincutorder > limit) {
321 newcut += (ed[higain]-
id[higain]);
322 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);
323 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
328 moved[higain] = nswaps;
329 swaps[nswaps] = higain;
333 "Cut:%5"PRIDX", NPwgts:", higain, from, cnum, ed[higain]-
id[higain], newcut);
334 for (l=0; l<
ncon; l++)
335 printf(
"(%.3"PRREAL" %.3"PRREAL")", pwgts[l]*invtvwgt[l], pwgts[ncon+l]*invtvwgt[l]);
336 printf(
" %+.3"PRREAL
" LB: %.3"PRREAL
"(%+.3"PRREAL
")\n",
344 SWAP(
id[higain], ed[higain], tmp);
345 if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])
348 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
351 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
355 if (bndptr[k] != -1) {
359 rpqDelete(queues[2*qnum[k]+where[k]], k);
362 if (moved[k] == -1) {
367 rpqUpdate(queues[2*qnum[k]+where[k]], k, rgain);
374 if (moved[k] == -1) {
379 rpqInsert(queues[2*qnum[k]+where[k]], k, rgain);
391 for (i=0; i<nswaps; i++)
392 moved[swaps[i]] = -1;
393 for (nswaps--; nswaps>mincutorder; nswaps--) {
394 higain = swaps[nswaps];
396 to = where[higain] = (where[higain]+1)%2;
397 SWAP(
id[higain], ed[higain], tmp);
398 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
400 else if (ed[higain] > 0 && bndptr[higain] == -1)
403 iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);
404 iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+((to+1)%2)*ncon, 1);
405 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
408 kwgt = (to == where[k] ? adjwgt[
j] : -adjwgt[
j]);
411 if (bndptr[k] != -1 && ed[k] == 0)
413 if (bndptr[k] == -1 && ed[k] > 0)
424 if (mincutorder <= 0 || mincut == initcut)
428 for (i=0; i<2*
ncon; i++)
452 for (max=0.0, part=0; part<2; part++) {
453 for (i=0; i<
ncon; i++) {
454 tmp = graph->
pwgts[part*ncon+
i]*pijbm[part*ncon+
i] - ubfactors[
i];
468 if (
rpqLength(queues[2*(*cnum)+(*from)]) == 0) {
469 for (i=0; i<
ncon; i++) {
470 if (
rpqLength(queues[2*i+(*from)]) > 0) {
471 max = graph->
pwgts[(*from)*ncon+
i]*pijbm[(*from)*ncon+
i] - ubfactors[
i];
477 for (i++; i<
ncon; i++) {
478 tmp = graph->
pwgts[(*from)*ncon+
i]*pijbm[(*from)*ncon+
i] - ubfactors[
i];
479 if (tmp > max &&
rpqLength(queues[2*i+(*from)]) > 0) {
494 for (part=0; part<2; part++) {
495 for (i=0; i<
ncon; i++) {
497 (*from == -1 ||
rpqSeeTopKey(queues[2*i+part]) > max)) {
520 if (mincutorder == -2) {
525 for (i=0; i<graph->
ncon; i++)
529 ntpwgts[i], ntpwgts[graph->
ncon+i]);
530 printf(
"] LB: %.3"PRREAL
"(%+.3"PRREAL
")\n",
536 for (i=0; i<graph->
ncon; i++)
539 printf(
"] LB: %.3"PRREAL
"(%+.3"PRREAL
")\n",
idx_t idx_t idx_t idx_t * vwgt
void SelectQueue(graph_t *graph, real_t *pijbm, real_t *ubfactors, rpq_t **queues, idx_t *from, idx_t *cnum)
idx_t idx_t idx_t idx_t idx_t idx_t idx_t real_t real_t idx_t idx_t idx_t * part
#define BNDDelete(nbnd, bndind, bndptr, vtx)
#define irandArrayPermute
Q id(Eigen::AngleAxisd(0, Q_z_axis))
void FM_Mc2WayCutRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
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
void FM_2WayCutRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
idx_t idx_t idx_t idx_t idx_t idx_t * adjwgt
#define INC_DEC(a, b, val)
#define ComputeLoadImbalanceDiffVec
void FM_2WayRefine(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, idx_t niter)
idx_t idx_t idx_t idx_t * where
idx_t idx_t idx_t * adjncy
void Print2WayRefineStats(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts, real_t deltabal, idx_t mincutorder)
#define IFSET(a, flag, cmd)