forked from KarypisLab/METIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminconn.c
729 lines (599 loc) · 22.7 KB
/
minconn.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*!
\file
\brief Functions that deal with prunning the number of adjacent subdomains in kmetis
\date Started 7/15/98
\author George
\author Copyright 1997-2009, Regents of the University of Minnesota
\version $Id: minconn.c 17513 2014-08-05 16:20:50Z dominique $
*/
#include "metislib.h"
/*************************************************************************/
/*! This function computes the subdomain graph storing the result in the
pre-allocated worspace arrays */
/*************************************************************************/
void ComputeSubDomainGraph(ctrl_t *ctrl, graph_t *graph)
{
idx_t i, ii, j, pid, other, nparts, nvtxs, nnbrs;
idx_t *xadj, *adjncy, *adjwgt, *where;
idx_t *pptr, *pind;
idx_t nads=0, *vadids, *vadwgts;
WCOREPUSH;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
adjwgt = graph->adjwgt;
where = graph->where;
nparts = ctrl->nparts;
vadids = ctrl->pvec1;
vadwgts = iset(nparts, 0, ctrl->pvec2);
pptr = iwspacemalloc(ctrl, nparts+1);
pind = iwspacemalloc(ctrl, nvtxs);
iarray2csr(nvtxs, nparts, where, pptr, pind);
for (pid=0; pid<nparts; pid++) {
switch (ctrl->objtype) {
case METIS_OBJTYPE_CUT:
{
ckrinfo_t *rinfo;
cnbr_t *nbrs;
rinfo = graph->ckrinfo;
for (nads=0, ii=pptr[pid]; ii<pptr[pid+1]; ii++) {
i = pind[ii];
ASSERT(pid == where[i]);
if (rinfo[i].ed > 0) {
nnbrs = rinfo[i].nnbrs;
nbrs = ctrl->cnbrpool + rinfo[i].inbr;
for (j=0; j<nnbrs; j++) {
other = nbrs[j].pid;
if (vadwgts[other] == 0)
vadids[nads++] = other;
vadwgts[other] += nbrs[j].ed;
}
}
}
}
break;
case METIS_OBJTYPE_VOL:
{
vkrinfo_t *rinfo;
vnbr_t *nbrs;
rinfo = graph->vkrinfo;
for (nads=0, ii=pptr[pid]; ii<pptr[pid+1]; ii++) {
i = pind[ii];
ASSERT(pid == where[i]);
if (rinfo[i].ned > 0) {
nnbrs = rinfo[i].nnbrs;
nbrs = ctrl->vnbrpool + rinfo[i].inbr;
for (j=0; j<nnbrs; j++) {
other = nbrs[j].pid;
if (vadwgts[other] == 0)
vadids[nads++] = other;
vadwgts[other] += nbrs[j].ned;
}
}
}
}
break;
default:
gk_errexit(SIGERR, "Unknown objtype: %d\n", ctrl->objtype);
}
/* See if you have enough memory to store the adjacent info for that subdomain */
if (ctrl->maxnads[pid] < nads) {
ctrl->maxnads[pid] = 2*nads;
ctrl->adids[pid] = irealloc(ctrl->adids[pid], ctrl->maxnads[pid],
"ComputeSubDomainGraph: adids[pid]");
ctrl->adwgts[pid] = irealloc(ctrl->adwgts[pid], ctrl->maxnads[pid],
"ComputeSubDomainGraph: adids[pid]");
}
ctrl->nads[pid] = nads;
for (j=0; j<nads; j++) {
ctrl->adids[pid][j] = vadids[j];
ctrl->adwgts[pid][j] = vadwgts[vadids[j]];
vadwgts[vadids[j]] = 0;
}
}
WCOREPOP;
}
/*************************************************************************/
/*! This function updates the weight of an edge in the subdomain graph by
adding to it the value of ewgt. The update can either increase or
decrease the weight of the subdomain edge based on the value of ewgt.
\param u is the ID of one of the incident subdomains to the edge
\param v is the ID of the other incident subdomains to the edge
\param ewgt is the weight to be added to the subdomain edge
\param nparts is the number of subdomains
\param r_maxndoms is the maximum number of adjacent subdomains and is
updated as necessary. The update is skipped if a NULL value is
supplied.
*/
/*************************************************************************/
void UpdateEdgeSubDomainGraph(ctrl_t *ctrl, idx_t u, idx_t v, idx_t ewgt,
idx_t *r_maxndoms)
{
idx_t i, j, nads;
if (ewgt == 0)
return;
for (i=0; i<2; i++) {
nads = ctrl->nads[u];
/* Find the edge */
for (j=0; j<nads; j++) {
if (ctrl->adids[u][j] == v) {
ctrl->adwgts[u][j] += ewgt;
break;
}
}
if (j == nads) {
/* Deal with the case in which the edge was not found */
ASSERT(ewgt > 0);
if (ctrl->maxnads[u] == nads) {
ctrl->maxnads[u] = 2*(nads+1);
ctrl->adids[u] = irealloc(ctrl->adids[u], ctrl->maxnads[u],
"IncreaseEdgeSubDomainGraph: adids[pid]");
ctrl->adwgts[u] = irealloc(ctrl->adwgts[u], ctrl->maxnads[u],
"IncreaseEdgeSubDomainGraph: adids[pid]");
}
ctrl->adids[u][nads] = v;
ctrl->adwgts[u][nads] = ewgt;
nads++;
if (r_maxndoms != NULL && nads > *r_maxndoms) {
printf("You just increased the maxndoms: %"PRIDX" %"PRIDX"\n",
nads, *r_maxndoms);
*r_maxndoms = nads;
}
}
else {
/* See if the updated edge becomes 0 */
ASSERT(ctrl->adwgts[u][j] >= 0);
if (ctrl->adwgts[u][j] == 0) {
ctrl->adids[u][j] = ctrl->adids[u][nads-1];
ctrl->adwgts[u][j] = ctrl->adwgts[u][nads-1];
nads--;
if (r_maxndoms != NULL && nads+1 == *r_maxndoms)
*r_maxndoms = ctrl->nads[iargmax(ctrl->nparts, ctrl->nads,1)];
}
}
ctrl->nads[u] = nads;
SWAP(u, v, j);
}
}
/*************************************************************************/
/*! This function computes the subdomain graph */
/*************************************************************************/
void EliminateSubDomainEdges(ctrl_t *ctrl, graph_t *graph)
{
idx_t i, ii, j, k, ncon, nparts, scheme, pid_from, pid_to, me, other, nvtxs,
total, max, avg, totalout, nind=0, ncand=0, ncand2, target, target2,
nadd, bestnadd=0;
idx_t min, move, *cpwgt;
idx_t *xadj, *adjncy, *vwgt, *adjwgt, *pwgts, *where, *maxpwgt,
*mypmat, *otherpmat, *kpmat, *ind;
idx_t *nads, **adids, **adwgts;
ikv_t *cand, *cand2;
ipq_t queue;
real_t *tpwgts, badfactor=1.4;
idx_t *pptr, *pind;
idx_t *vmarker=NULL, *pmarker=NULL, *modind=NULL; /* volume specific work arrays */
WCOREPUSH;
nvtxs = graph->nvtxs;
ncon = graph->ncon;
xadj = graph->xadj;
adjncy = graph->adjncy;
vwgt = graph->vwgt;
adjwgt = (ctrl->objtype == METIS_OBJTYPE_VOL ? NULL : graph->adjwgt);
where = graph->where;
pwgts = graph->pwgts; /* We assume that this is properly initialized */
nparts = ctrl->nparts;
tpwgts = ctrl->tpwgts;
cpwgt = iwspacemalloc(ctrl, ncon);
maxpwgt = iwspacemalloc(ctrl, nparts*ncon);
ind = iwspacemalloc(ctrl, nvtxs);
otherpmat = iset(nparts, 0, iwspacemalloc(ctrl, nparts));
cand = ikvwspacemalloc(ctrl, nparts);
cand2 = ikvwspacemalloc(ctrl, nparts);
pptr = iwspacemalloc(ctrl, nparts+1);
pind = iwspacemalloc(ctrl, nvtxs);
iarray2csr(nvtxs, nparts, where, pptr, pind);
if (ctrl->objtype == METIS_OBJTYPE_VOL) {
/* Vol-refinement specific working arrays */
modind = iwspacemalloc(ctrl, nvtxs);
vmarker = iset(nvtxs, 0, iwspacemalloc(ctrl, nvtxs));
pmarker = iset(nparts, -1, iwspacemalloc(ctrl, nparts));
}
/* Compute the pmat matrix and ndoms */
ComputeSubDomainGraph(ctrl, graph);
nads = ctrl->nads;
adids = ctrl->adids;
adwgts = ctrl->adwgts;
mypmat = iset(nparts, 0, ctrl->pvec1);
kpmat = iset(nparts, 0, ctrl->pvec2);
/* Compute the maximum allowed weight for each domain */
for (i=0; i<nparts; i++) {
for (j=0; j<ncon; j++)
maxpwgt[i*ncon+j] =
(ncon == 1 ? 1.25 : 1.025)*tpwgts[i]*graph->tvwgt[j]*ctrl->ubfactors[j];
}
ipqInit(&queue, nparts);
/* Get into the loop eliminating subdomain connections */
while (1) {
total = isum(nparts, nads, 1);
avg = total/nparts;
max = nads[iargmax(nparts, nads,1)];
IFSET(ctrl->dbglvl, METIS_DBG_CONNINFO,
printf("Adjacent Subdomain Stats: Total: %3"PRIDX", "
"Max: %3"PRIDX"[%zu], Avg: %3"PRIDX"\n",
total, max, iargmax(nparts, nads,1), avg));
if (max < badfactor*avg)
break;
/* Add the subdomains that you will try to reduce their connectivity */
ipqReset(&queue);
for (i=0; i<nparts; i++) {
if (nads[i] >= avg + (max-avg)/2)
ipqInsert(&queue, i, nads[i]);
}
move = 0;
while ((me = ipqGetTop(&queue)) != -1) {
totalout = isum(nads[me], adwgts[me], 1);
for (ncand2=0, i=0; i<nads[me]; i++) {
mypmat[adids[me][i]] = adwgts[me][i];
/* keep track of the weakly connected adjacent subdomains */
if (2*nads[me]*adwgts[me][i] < totalout) {
cand2[ncand2].val = adids[me][i];
cand2[ncand2++].key = adwgts[me][i];
}
}
IFSET(ctrl->dbglvl, METIS_DBG_CONNINFO,
printf("Me: %"PRIDX", Degree: %4"PRIDX", TotalOut: %"PRIDX",\n",
me, nads[me], totalout));
/* Sort the connections according to their cut */
ikvsorti(ncand2, cand2);
/* Two schemes are used for eliminating subdomain edges.
The first, tries to eliminate subdomain edges by moving remote groups
of vertices to subdomains that 'me' is already connected to.
The second, tries to eliminate subdomain edges by moving entire sets of
my vertices that connect to the 'other' subdomain to a subdomain that
I'm already connected to.
These two schemes are applied in sequence. */
target = target2 = -1;
for (scheme=0; scheme<2; scheme++) {
for (min=0; min<ncand2; min++) {
other = cand2[min].val;
/* pid_from is the subdomain from where the vertices will be removed.
pid_to is the adjacent subdomain to pid_from that defines the
(me, other) subdomain edge that needs to be removed */
if (scheme == 0) {
pid_from = other;
pid_to = me;
}
else {
pid_from = me;
pid_to = other;
}
/* Go and find the vertices in 'other' that are connected in 'me' */
for (nind=0, ii=pptr[pid_from]; ii<pptr[pid_from+1]; ii++) {
i = pind[ii];
ASSERT(where[i] == pid_from);
for (j=xadj[i]; j<xadj[i+1]; j++) {
if (where[adjncy[j]] == pid_to) {
ind[nind++] = i;
break;
}
}
}
/* Go and construct the otherpmat to see where these nind vertices are
connected to */
iset(ncon, 0, cpwgt);
for (ncand=0, ii=0; ii<nind; ii++) {
i = ind[ii];
iaxpy(ncon, 1, vwgt+i*ncon, 1, cpwgt, 1);
for (j=xadj[i]; j<xadj[i+1]; j++) {
if ((k = where[adjncy[j]]) == pid_from)
continue;
if (otherpmat[k] == 0)
cand[ncand++].val = k;
otherpmat[k] += (adjwgt ? adjwgt[j] : 1);
}
}
for (i=0; i<ncand; i++) {
cand[i].key = otherpmat[cand[i].val];
ASSERT(cand[i].key > 0);
}
ikvsortd(ncand, cand);
IFSET(ctrl->dbglvl, METIS_DBG_CONNINFO,
printf("\tMinOut: %4"PRIDX", to: %3"PRIDX", TtlWgt: %5"PRIDX"[#:%"PRIDX"]\n",
mypmat[other], other, isum(ncon, cpwgt, 1), nind));
/* Go through and select the first domain that is common with 'me', and does
not increase the nads[target] higher than nads[me], subject to the maxpwgt
constraint. Traversal is done from the mostly connected to the least. */
for (i=0; i<ncand; i++) {
k = cand[i].val;
if (mypmat[k] > 0) {
/* Check if balance will go off */
if (!ivecaxpylez(ncon, 1, cpwgt, pwgts+k*ncon, maxpwgt+k*ncon))
continue;
/* get a dense vector out of k's connectivity */
for (j=0; j<nads[k]; j++)
kpmat[adids[k][j]] = adwgts[k][j];
/* Check if the move to domain k will increase the nads of another
subdomain j that the set of vertices being moved are connected
to but domain k is not connected to. */
for (j=0; j<nparts; j++) {
if (otherpmat[j] > 0 && kpmat[j] == 0 && nads[j]+1 >= nads[me])
break;
}
/* There were no bad second level effects. See if you can find a
subdomain to move to. */
if (j == nparts) {
for (nadd=0, j=0; j<nparts; j++) {
if (otherpmat[j] > 0 && kpmat[j] == 0)
nadd++;
}
IFSET(ctrl->dbglvl, METIS_DBG_CONNINFO,
printf("\t\tto=%"PRIDX", nadd=%"PRIDX", %"PRIDX"\n", k, nadd, nads[k]));
if (nads[k]+nadd < nads[me]) {
if (target2 == -1 || nads[target2]+bestnadd > nads[k]+nadd ||
(nads[target2]+bestnadd == nads[k]+nadd && bestnadd > nadd)) {
target2 = k;
bestnadd = nadd;
}
}
if (nadd == 0)
target = k;
}
/* reset kpmat for the next iteration */
for (j=0; j<nads[k]; j++)
kpmat[adids[k][j]] = 0;
}
if (target != -1)
break;
}
/* reset the otherpmat for the next iteration */
for (i=0; i<ncand; i++)
otherpmat[cand[i].val] = 0;
if (target == -1 && target2 != -1)
target = target2;
if (target != -1) {
IFSET(ctrl->dbglvl, METIS_DBG_CONNINFO,
printf("\t\tScheme: %"PRIDX". Moving to %"PRIDX"\n", scheme, target));
move = 1;
break;
}
}
if (target != -1)
break; /* A move was found. No need to try the other scheme */
}
/* reset the mypmat for next iteration */
for (i=0; i<nads[me]; i++)
mypmat[adids[me][i]] = 0;
/* Note that once a target is found the above loops exit right away. So the
following variables are valid */
if (target != -1) {
switch (ctrl->objtype) {
case METIS_OBJTYPE_CUT:
MoveGroupMinConnForCut(ctrl, graph, target, nind, ind);
break;
case METIS_OBJTYPE_VOL:
MoveGroupMinConnForVol(ctrl, graph, target, nind, ind, vmarker,
pmarker, modind);
break;
default:
gk_errexit(SIGERR, "Unknown objtype of %d\n", ctrl->objtype);
}
/* Update the csr representation of the partitioning vector */
iarray2csr(nvtxs, nparts, where, pptr, pind);
}
}
if (move == 0)
break;
}
ipqFree(&queue);
WCOREPOP;
}
/*************************************************************************/
/*! This function moves a collection of vertices and updates their rinfo */
/*************************************************************************/
void MoveGroupMinConnForCut(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t nind,
idx_t *ind)
{
idx_t i, ii, j, jj, k, l, nvtxs, nbnd, from, me;
idx_t *xadj, *adjncy, *adjwgt, *where, *bndptr, *bndind;
ckrinfo_t *myrinfo;
cnbr_t *mynbrs;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
adjwgt = graph->adjwgt;
where = graph->where;
bndptr = graph->bndptr;
bndind = graph->bndind;
nbnd = graph->nbnd;
while (--nind>=0) {
i = ind[nind];
from = where[i];
myrinfo = graph->ckrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = cnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->cnbrpool + myrinfo->inbr;
/* find the location of 'to' in myrinfo or create it if it is not there */
for (k=0; k<myrinfo->nnbrs; k++) {
if (mynbrs[k].pid == to)
break;
}
if (k == myrinfo->nnbrs) {
ASSERT(k < xadj[i+1]-xadj[i]);
mynbrs[k].pid = to;
mynbrs[k].ed = 0;
myrinfo->nnbrs++;
}
/* Update pwgts */
iaxpy(graph->ncon, 1, graph->vwgt+i*graph->ncon, 1, graph->pwgts+to*graph->ncon, 1);
iaxpy(graph->ncon, -1, graph->vwgt+i*graph->ncon, 1, graph->pwgts+from*graph->ncon, 1);
/* Update mincut */
graph->mincut -= mynbrs[k].ed-myrinfo->id;
/* Update subdomain connectivity graph to reflect the move of 'i' */
UpdateEdgeSubDomainGraph(ctrl, from, to, myrinfo->id-mynbrs[k].ed, NULL);
/* Update ID/ED and BND related information for the moved vertex */
UpdateMovedVertexInfoAndBND(i, from, k, to, myrinfo, mynbrs, where, nbnd,
bndptr, bndind, BNDTYPE_REFINE);
/* Update the degrees of adjacent vertices */
for (j=xadj[i]; j<xadj[i+1]; j++) {
ii = adjncy[j];
me = where[ii];
myrinfo = graph->ckrinfo+ii;
UpdateAdjacentVertexInfoAndBND(ctrl, ii, xadj[ii+1]-xadj[ii], me,
from, to, myrinfo, adjwgt[j], nbnd, bndptr, bndind, BNDTYPE_REFINE);
/* Update subdomain graph to reflect the move of 'i' for domains other
than 'from' and 'to' */
if (me != from && me != to) {
UpdateEdgeSubDomainGraph(ctrl, from, me, -adjwgt[j], NULL);
UpdateEdgeSubDomainGraph(ctrl, to, me, adjwgt[j], NULL);
}
}
}
ASSERT(ComputeCut(graph, where) == graph->mincut);
graph->nbnd = nbnd;
}
/*************************************************************************/
/*! This function moves a collection of vertices and updates their rinfo */
/*************************************************************************/
void MoveGroupMinConnForVol(ctrl_t *ctrl, graph_t *graph, idx_t to, idx_t nind,
idx_t *ind, idx_t *vmarker, idx_t *pmarker, idx_t *modind)
{
idx_t i, ii, j, jj, k, l, nvtxs, from, me, other, xgain, ewgt;
idx_t *xadj, *vsize, *adjncy, *where;
vkrinfo_t *myrinfo, *orinfo;
vnbr_t *mynbrs, *onbrs;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
vsize = graph->vsize;
adjncy = graph->adjncy;
where = graph->where;
while (--nind>=0) {
i = ind[nind];
from = where[i];
myrinfo = graph->vkrinfo+i;
if (myrinfo->inbr == -1) {
myrinfo->inbr = vnbrpoolGetNext(ctrl, xadj[i+1]-xadj[i]+1);
myrinfo->nnbrs = 0;
}
mynbrs = ctrl->vnbrpool + myrinfo->inbr;
xgain = (myrinfo->nid == 0 && myrinfo->ned > 0 ? vsize[i] : 0);
//printf("Moving %"PRIDX" from %"PRIDX" to %"PRIDX" [vsize: %"PRIDX"] [xgain: %"PRIDX"]\n",
// i, from, to, vsize[i], xgain);
/* find the location of 'to' in myrinfo or create it if it is not there */
for (k=0; k<myrinfo->nnbrs; k++) {
if (mynbrs[k].pid == to)
break;
}
if (k == myrinfo->nnbrs) {
//printf("Missing neighbor\n");
if (myrinfo->nid > 0)
xgain -= vsize[i];
/* determine the volume gain resulting from that move */
for (j=xadj[i]; j<xadj[i+1]; j++) {
ii = adjncy[j];
other = where[ii];
orinfo = graph->vkrinfo+ii;
onbrs = ctrl->vnbrpool + orinfo->inbr;
ASSERT(other != to)
//printf(" %8d %8d %3d\n", (int)ii, (int)vsize[ii], (int)other);
if (from == other) {
/* Same subdomain vertex: Decrease the gain if 'to' is a new neighbor. */
for (l=0; l<orinfo->nnbrs; l++) {
if (onbrs[l].pid == to)
break;
}
if (l == orinfo->nnbrs)
xgain -= vsize[ii];
}
else {
/* Remote vertex: increase if 'to' is a new subdomain */
for (l=0; l<orinfo->nnbrs; l++) {
if (onbrs[l].pid == to)
break;
}
if (l == orinfo->nnbrs)
xgain -= vsize[ii];
/* Remote vertex: decrease if i is the only connection to 'from' */
for (l=0; l<orinfo->nnbrs; l++) {
if (onbrs[l].pid == from && onbrs[l].ned == 1) {
xgain += vsize[ii];
break;
}
}
}
}
graph->minvol -= xgain;
graph->mincut -= -myrinfo->nid;
ewgt = myrinfo->nid;
}
else {
graph->minvol -= (xgain + mynbrs[k].gv);
graph->mincut -= mynbrs[k].ned-myrinfo->nid;
ewgt = myrinfo->nid-mynbrs[k].ned;
}
/* Update where and pwgts */
where[i] = to;
iaxpy(graph->ncon, 1, graph->vwgt+i*graph->ncon, 1, graph->pwgts+to*graph->ncon, 1);
iaxpy(graph->ncon, -1, graph->vwgt+i*graph->ncon, 1, graph->pwgts+from*graph->ncon, 1);
/* Update subdomain connectivity graph to reflect the move of 'i' */
UpdateEdgeSubDomainGraph(ctrl, from, to, ewgt, NULL);
/* Update the subdomain connectivity of the adjacent vertices */
for (j=xadj[i]; j<xadj[i+1]; j++) {
me = where[adjncy[j]];
if (me != from && me != to) {
UpdateEdgeSubDomainGraph(ctrl, from, me, -1, NULL);
UpdateEdgeSubDomainGraph(ctrl, to, me, 1, NULL);
}
}
/* Update the id/ed/gains/bnd of potentially affected nodes */
KWayVolUpdate(ctrl, graph, i, from, to, NULL, NULL, NULL, NULL,
NULL, BNDTYPE_REFINE, vmarker, pmarker, modind);
/*CheckKWayVolPartitionParams(ctrl, graph);*/
}
ASSERT(ComputeCut(graph, where) == graph->mincut);
ASSERTP(ComputeVolume(graph, where) == graph->minvol,
("%"PRIDX" %"PRIDX"\n", ComputeVolume(graph, where), graph->minvol));
}
/*************************************************************************/
/*! This function computes the subdomain graph. For deubuging purposes. */
/*************************************************************************/
void PrintSubDomainGraph(graph_t *graph, idx_t nparts, idx_t *where)
{
idx_t i, j, k, me, nvtxs, total, max;
idx_t *xadj, *adjncy, *adjwgt, *pmat;
nvtxs = graph->nvtxs;
xadj = graph->xadj;
adjncy = graph->adjncy;
adjwgt = graph->adjwgt;
pmat = ismalloc(nparts*nparts, 0, "ComputeSubDomainGraph: pmat");
for (i=0; i<nvtxs; i++) {
me = where[i];
for (j=xadj[i]; j<xadj[i+1]; j++) {
k = adjncy[j];
if (where[k] != me)
pmat[me*nparts+where[k]] += adjwgt[j];
}
}
/* printf("Subdomain Info\n"); */
total = max = 0;
for (i=0; i<nparts; i++) {
for (k=0, j=0; j<nparts; j++) {
if (pmat[i*nparts+j] > 0)
k++;
}
total += k;
if (k > max)
max = k;
/*
printf("%2"PRIDX" -> %2"PRIDX" ", i, k);
for (j=0; j<nparts; j++) {
if (pmat[i*nparts+j] > 0)
printf("[%2"PRIDX" %4"PRIDX"] ", j, pmat[i*nparts+j]);
}
printf("\n");
*/
}
printf("Total adjacent subdomains: %"PRIDX", Max: %"PRIDX"\n", total, max);
gk_free((void **)&pmat, LTERM);
}