00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <metis.h>
00017
00018
00019
00020
00021 void MocInit2WayPartition(CtrlType *ctrl, GraphType *graph, floattype *tpwgts, floattype ubfactor)
00022 {
00023 int i, dbglvl;
00024
00025 dbglvl = ctrl->dbglvl;
00026 IFSET(ctrl->dbglvl, DBG_REFINE, ctrl->dbglvl -= DBG_REFINE);
00027 IFSET(ctrl->dbglvl, DBG_MOVEINFO, ctrl->dbglvl -= DBG_MOVEINFO);
00028
00029 IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->InitPartTmr));
00030
00031 switch (ctrl->IType) {
00032 case IPART_GGPKL:
00033 if (graph->nedges == 0)
00034 MocRandomBisection(ctrl, graph, tpwgts, ubfactor);
00035 else
00036 MocGrowBisection(ctrl, graph, tpwgts, ubfactor);
00037 break;
00038 case IPART_RANDOM:
00039 MocRandomBisection(ctrl, graph, tpwgts, ubfactor);
00040 break;
00041 default:
00042 errexit("Unknown initial partition type: %d\n", ctrl->IType);
00043 }
00044
00045 IFSET(ctrl->dbglvl, DBG_IPART, printf("Initial Cut: %d\n", graph->mincut));
00046 IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->InitPartTmr));
00047 ctrl->dbglvl = dbglvl;
00048
00049 }
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 void MocGrowBisection(CtrlType *ctrl, GraphType *graph, floattype *tpwgts, floattype ubfactor)
00061 {
00062 int i, j, k, nvtxs, ncon, from, bestcut, mincut, nbfs;
00063 idxtype *bestwhere, *where;
00064
00065 nvtxs = graph->nvtxs;
00066
00067 MocAllocate2WayPartitionMemory(ctrl, graph);
00068 where = graph->where;
00069
00070 bestwhere = idxmalloc(nvtxs, "BisectGraph: bestwhere");
00071 nbfs = 2*(nvtxs <= ctrl->CoarsenTo ? SMALLNIPARTS : LARGENIPARTS);
00072 bestcut = idxsum(graph->nedges, graph->adjwgt);
00073
00074 for (; nbfs>0; nbfs--) {
00075 idxset(nvtxs, 1, where);
00076 where[RandomInRange(nvtxs)] = 0;
00077
00078 MocCompute2WayPartitionParams(ctrl, graph);
00079
00080 MocInit2WayBalance(ctrl, graph, tpwgts);
00081
00082 MocFM_2WayEdgeRefine(ctrl, graph, tpwgts, 4);
00083
00084 MocBalance2Way(ctrl, graph, tpwgts, 1.02);
00085 MocFM_2WayEdgeRefine(ctrl, graph, tpwgts, 4);
00086
00087 if (bestcut > graph->mincut) {
00088 bestcut = graph->mincut;
00089 idxcopy(nvtxs, where, bestwhere);
00090 if (bestcut == 0)
00091 break;
00092 }
00093 }
00094
00095 graph->mincut = bestcut;
00096 idxcopy(nvtxs, bestwhere, where);
00097
00098 GKfree(&bestwhere, LTERM);
00099 }
00100
00101
00102
00103
00104
00105
00106
00107
00108 void MocRandomBisection(CtrlType *ctrl, GraphType *graph, floattype *tpwgts, floattype ubfactor)
00109 {
00110 int i, ii, j, k, nvtxs, ncon, from, bestcut, mincut, nbfs, qnum;
00111 idxtype *bestwhere, *where, *perm;
00112 int counts[MAXNCON];
00113 floattype *nvwgt;
00114
00115 nvtxs = graph->nvtxs;
00116 ncon = graph->ncon;
00117 nvwgt = graph->nvwgt;
00118
00119 MocAllocate2WayPartitionMemory(ctrl, graph);
00120 where = graph->where;
00121
00122 bestwhere = idxmalloc(nvtxs, "BisectGraph: bestwhere");
00123 nbfs = 2*(nvtxs <= ctrl->CoarsenTo ? SMALLNIPARTS : LARGENIPARTS);
00124 bestcut = idxsum(graph->nedges, graph->adjwgt);
00125 perm = idxmalloc(nvtxs, "BisectGraph: perm");
00126
00127 for (; nbfs>0; nbfs--) {
00128 for (i=0; i<ncon; i++)
00129 counts[i] = 0;
00130
00131 RandomPermute(nvtxs, perm, 1);
00132
00133
00134 for (ii=0; ii<nvtxs; ii++) {
00135 i = perm[ii];
00136 qnum = samax(ncon, nvwgt+i*ncon);
00137 where[i] = counts[qnum];
00138 counts[qnum] = (counts[qnum]+1)%2;
00139 }
00140
00141 MocCompute2WayPartitionParams(ctrl, graph);
00142
00143 MocFM_2WayEdgeRefine(ctrl, graph, tpwgts, 6);
00144 MocBalance2Way(ctrl, graph, tpwgts, 1.02);
00145 MocFM_2WayEdgeRefine(ctrl, graph, tpwgts, 6);
00146 MocBalance2Way(ctrl, graph, tpwgts, 1.02);
00147 MocFM_2WayEdgeRefine(ctrl, graph, tpwgts, 6);
00148
00149
00150
00151
00152
00153
00154
00155
00156 if (bestcut > graph->mincut) {
00157 bestcut = graph->mincut;
00158 idxcopy(nvtxs, where, bestwhere);
00159 if (bestcut == 0)
00160 break;
00161 }
00162 }
00163
00164 graph->mincut = bestcut;
00165 idxcopy(nvtxs, bestwhere, where);
00166
00167 GKfree(&bestwhere, &perm, LTERM);
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 void MocInit2WayBalance(CtrlType *ctrl, GraphType *graph, floattype *tpwgts)
00182 {
00183 int i, ii, j, k, l, kwgt, nvtxs, nbnd, ncon, nswaps, from, to, pass, me, cnum, tmp;
00184 idxtype *xadj, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind;
00185 idxtype *perm, *qnum;
00186 floattype *nvwgt, *npwgts;
00187 PQueueType parts[MAXNCON][2];
00188 int higain, oldgain, mincut;
00189
00190 nvtxs = graph->nvtxs;
00191 ncon = graph->ncon;
00192 xadj = graph->xadj;
00193 adjncy = graph->adjncy;
00194 nvwgt = graph->nvwgt;
00195 adjwgt = graph->adjwgt;
00196 where = graph->where;
00197 id = graph->id;
00198 ed = graph->ed;
00199 npwgts = graph->npwgts;
00200 bndptr = graph->bndptr;
00201 bndind = graph->bndind;
00202
00203 perm = idxwspacemalloc(ctrl, nvtxs);
00204 qnum = idxwspacemalloc(ctrl, nvtxs);
00205
00206
00207 from = 1;
00208 to = (from+1)%2;
00209
00210 if (ctrl->dbglvl&DBG_REFINE) {
00211 printf("Parts: [");
00212 for (l=0; l<ncon; l++)
00213 printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
00214 printf("] T[%.3f %.3f], Nv-Nb[%5d, %5d]. ICut: %6d, LB: %.3f [B]\n", tpwgts[0], tpwgts[1],
00215 graph->nvtxs, graph->nbnd, graph->mincut,
00216 Compute2WayHLoadImbalance(ncon, npwgts, tpwgts));
00217 }
00218
00219 for (i=0; i<ncon; i++) {
00220 PQueueInit(ctrl, &parts[i][0], nvtxs, PLUS_GAINSPAN+1);
00221 PQueueInit(ctrl, &parts[i][1], nvtxs, PLUS_GAINSPAN+1);
00222 }
00223
00224 ASSERT(ComputeCut(graph, where) == graph->mincut);
00225 ASSERT(CheckBnd(graph));
00226 ASSERT(CheckGraph(graph));
00227
00228
00229 for (i=0; i<nvtxs; i++)
00230 qnum[i] = samax(ncon, nvwgt+i*ncon);
00231
00232
00233 RandomPermute(nvtxs, perm, 1);
00234 for (ii=0; ii<nvtxs; ii++) {
00235 i = perm[ii];
00236 if (where[i] == from) {
00237 if (ed[i] > 0)
00238 PQueueInsert(&parts[qnum[i]][0], i, ed[i]-id[i]);
00239 else
00240 PQueueInsert(&parts[qnum[i]][1], i, ed[i]-id[i]);
00241 }
00242 }
00243
00244
00245 mincut = graph->mincut;
00246 nbnd = graph->nbnd;
00247 for (nswaps=0; nswaps<nvtxs; nswaps++) {
00248 if (AreAnyVwgtsBelow(ncon, 1.0, npwgts+from*ncon, 0.0, nvwgt, tpwgts[from]))
00249 break;
00250
00251 if ((cnum = SelectQueueOneWay(ncon, npwgts, tpwgts, from, parts)) == -1)
00252 break;
00253
00254 if ((higain = PQueueGetMax(&parts[cnum][0])) == -1)
00255 higain = PQueueGetMax(&parts[cnum][1]);
00256
00257 mincut -= (ed[higain]-id[higain]);
00258 saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1);
00259 saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+from*ncon, 1);
00260
00261 where[higain] = to;
00262
00263 if (ctrl->dbglvl&DBG_MOVEINFO) {
00264 printf("Moved %6d from %d(%d). [%5d] %5d, NPwgts: ", higain, from, cnum, ed[higain]-id[higain], mincut);
00265 for (l=0; l<ncon; l++)
00266 printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
00267 printf(", LB: %.3f\n", Compute2WayHLoadImbalance(ncon, npwgts, tpwgts));
00268 if (ed[higain] == 0 && id[higain] > 0)
00269 printf("\t Pulled from the interior!\n");
00270 }
00271
00272
00273
00274
00275
00276 SWAP(id[higain], ed[higain], tmp);
00277 if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])
00278 BNDDelete(nbnd, bndind, bndptr, higain);
00279 if (ed[higain] > 0 && bndptr[higain] == -1)
00280 BNDInsert(nbnd, bndind, bndptr, higain);
00281
00282 for (j=xadj[higain]; j<xadj[higain+1]; j++) {
00283 k = adjncy[j];
00284 oldgain = ed[k]-id[k];
00285
00286 kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);
00287 INC_DEC(id[k], ed[k], kwgt);
00288
00289
00290 if (where[k] == from) {
00291 if (ed[k] > 0 && bndptr[k] == -1) {
00292 PQueueDelete(&parts[qnum[k]][1], k, oldgain);
00293 PQueueInsert(&parts[qnum[k]][0], k, ed[k]-id[k]);
00294 }
00295 else {
00296 if (bndptr[k] == -1)
00297 printf("What you thought was wrong!\n");
00298 PQueueUpdate(&parts[qnum[k]][0], k, oldgain, ed[k]-id[k]);
00299 }
00300 }
00301
00302
00303 if (ed[k] == 0 && bndptr[k] != -1)
00304 BNDDelete(nbnd, bndind, bndptr, k);
00305 else if (ed[k] > 0 && bndptr[k] == -1)
00306 BNDInsert(nbnd, bndind, bndptr, k);
00307 }
00308
00309 ASSERTP(ComputeCut(graph, where) == mincut, ("%d != %d\n", ComputeCut(graph, where), mincut));
00310
00311 }
00312
00313 if (ctrl->dbglvl&DBG_REFINE) {
00314 printf("\tMincut: %6d, NBND: %6d, NPwgts: ", mincut, nbnd);
00315 for (l=0; l<ncon; l++)
00316 printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]);
00317 printf(", LB: %.3f\n", Compute2WayHLoadImbalance(ncon, npwgts, tpwgts));
00318 }
00319
00320 graph->mincut = mincut;
00321 graph->nbnd = nbnd;
00322
00323 for (i=0; i<ncon; i++) {
00324 PQueueFree(ctrl, &parts[i][0]);
00325 PQueueFree(ctrl, &parts[i][1]);
00326 }
00327
00328 ASSERT(ComputeCut(graph, where) == graph->mincut);
00329 ASSERT(CheckBnd(graph));
00330
00331 idxwspacefree(ctrl, nvtxs);
00332 idxwspacefree(ctrl, nvtxs);
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342 int SelectQueueOneWay(int ncon, floattype *npwgts, floattype *tpwgts, int from, PQueueType queues[MAXNCON][2])
00343 {
00344 int i, cnum=-1;
00345 floattype max=0.0;
00346
00347 for (i=0; i<ncon; i++) {
00348 if (npwgts[from*ncon+i]-tpwgts[from] >= max &&
00349 PQueueGetSize(&queues[i][0]) + PQueueGetSize(&queues[i][1]) > 0) {
00350 max = npwgts[from*ncon+i]-tpwgts[0];
00351 cnum = i;
00352 }
00353 }
00354
00355 return cnum;
00356 }
00357
00358