Path: blob/devel/elmergrid/src/metis-5.1.0/libmetis/balance.c
3206 views
/*!1\file2\brief Functions for the edge-based balancing34\date Started 7/23/975\author George6\author Copyright 1997-2011, Regents of the University of Minnesota7\version\verbatim $Id: balance.c 10187 2011-06-13 13:46:57Z karypis $ \endverbatim8*/910#include "metislib.h"1112/*************************************************************************13* This function is the entry poidx_t of the bisection balancing algorithms.14**************************************************************************/15void Balance2Way(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)16{17if (ComputeLoadImbalanceDiff(graph, 2, ctrl->pijbm, ctrl->ubfactors) <= 0)18return;1920if (graph->ncon == 1) {21/* return right away if the balance is OK */22if (iabs(ntpwgts[0]*graph->tvwgt[0]-graph->pwgts[0]) < 3*graph->tvwgt[0]/graph->nvtxs)23return;2425if (graph->nbnd > 0)26Bnd2WayBalance(ctrl, graph, ntpwgts);27else28General2WayBalance(ctrl, graph, ntpwgts);29}30else {31McGeneral2WayBalance(ctrl, graph, ntpwgts);32}33}343536/*************************************************************************37* This function balances two partitions by moving boundary nodes38* from the domain that is overweight to the one that is underweight.39**************************************************************************/40void Bnd2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)41{42idx_t i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;43idx_t *xadj, *vwgt, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;44idx_t *moved, *perm;45rpq_t *queue;46idx_t higain, mincut, mindiff;47idx_t tpwgts[2];4849WCOREPUSH;5051nvtxs = graph->nvtxs;52xadj = graph->xadj;53vwgt = graph->vwgt;54adjncy = graph->adjncy;55adjwgt = graph->adjwgt;56where = graph->where;57id = graph->id;58ed = graph->ed;59pwgts = graph->pwgts;60bndptr = graph->bndptr;61bndind = graph->bndind;6263moved = iwspacemalloc(ctrl, nvtxs);64perm = iwspacemalloc(ctrl, nvtxs);6566/* Determine from which domain you will be moving data */67tpwgts[0] = graph->tvwgt[0]*ntpwgts[0];68tpwgts[1] = graph->tvwgt[0] - tpwgts[0];69mindiff = iabs(tpwgts[0]-pwgts[0]);70from = (pwgts[0] < tpwgts[0] ? 1 : 0);71to = (from+1)%2;7273IFSET(ctrl->dbglvl, METIS_DBG_REFINE,74printf("Partitions: [%6"PRIDX" %6"PRIDX"] T[%6"PRIDX" %6"PRIDX"], Nv-Nb[%6"PRIDX" %6"PRIDX"]. ICut: %6"PRIDX" [B]\n",75pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd,76graph->mincut));7778queue = rpqCreate(nvtxs);7980iset(nvtxs, -1, moved);8182ASSERT(ComputeCut(graph, where) == graph->mincut);83ASSERT(CheckBnd(graph));8485/* Insert the boundary nodes of the proper partition whose size is OK in the priority queue */86nbnd = graph->nbnd;87irandArrayPermute(nbnd, perm, nbnd/5, 1);88for (ii=0; ii<nbnd; ii++) {89i = perm[ii];90ASSERT(ed[bndind[i]] > 0 || id[bndind[i]] == 0);91ASSERT(bndptr[bndind[i]] != -1);92if (where[bndind[i]] == from && vwgt[bndind[i]] <= mindiff)93rpqInsert(queue, bndind[i], ed[bndind[i]]-id[bndind[i]]);94}9596mincut = graph->mincut;97for (nswaps=0; nswaps<nvtxs; nswaps++) {98if ((higain = rpqGetTop(queue)) == -1)99break;100ASSERT(bndptr[higain] != -1);101102if (pwgts[to]+vwgt[higain] > tpwgts[to])103break;104105mincut -= (ed[higain]-id[higain]);106INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);107108where[higain] = to;109moved[higain] = nswaps;110111IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO,112printf("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]));113114/**************************************************************115* Update the id[i]/ed[i] values of the affected nodes116***************************************************************/117SWAP(id[higain], ed[higain], tmp);118if (ed[higain] == 0 && xadj[higain] < xadj[higain+1])119BNDDelete(nbnd, bndind, bndptr, higain);120121for (j=xadj[higain]; j<xadj[higain+1]; j++) {122k = adjncy[j];123kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);124INC_DEC(id[k], ed[k], kwgt);125126/* Update its boundary information and queue position */127if (bndptr[k] != -1) { /* If k was a boundary vertex */128if (ed[k] == 0) { /* Not a boundary vertex any more */129BNDDelete(nbnd, bndind, bndptr, k);130if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff) /* Remove it if in the queues */131rpqDelete(queue, k);132}133else { /* If it has not been moved, update its position in the queue */134if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)135rpqUpdate(queue, k, ed[k]-id[k]);136}137}138else {139if (ed[k] > 0) { /* It will now become a boundary vertex */140BNDInsert(nbnd, bndind, bndptr, k);141if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)142rpqInsert(queue, k, ed[k]-id[k]);143}144}145}146}147148IFSET(ctrl->dbglvl, METIS_DBG_REFINE,149printf("\tMinimum cut: %6"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, pwgts[0], pwgts[1], nbnd));150151graph->mincut = mincut;152graph->nbnd = nbnd;153154rpqDestroy(queue);155156WCOREPOP;157}158159160/*************************************************************************161* This function balances two partitions by moving the highest gain162* (including negative gain) vertices to the other domain.163* It is used only when tha unbalance is due to non contigous164* subdomains. That is, the are no boundary vertices.165* It moves vertices from the domain that is overweight to the one that166* is underweight.167**************************************************************************/168void General2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)169{170idx_t i, ii, j, k, kwgt, nvtxs, nbnd, nswaps, from, to, pass, me, tmp;171idx_t *xadj, *vwgt, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind, *pwgts;172idx_t *moved, *perm;173rpq_t *queue;174idx_t higain, mincut, mindiff;175idx_t tpwgts[2];176177WCOREPUSH;178179nvtxs = graph->nvtxs;180xadj = graph->xadj;181vwgt = graph->vwgt;182adjncy = graph->adjncy;183adjwgt = graph->adjwgt;184where = graph->where;185id = graph->id;186ed = graph->ed;187pwgts = graph->pwgts;188bndptr = graph->bndptr;189bndind = graph->bndind;190191moved = iwspacemalloc(ctrl, nvtxs);192perm = iwspacemalloc(ctrl, nvtxs);193194/* Determine from which domain you will be moving data */195tpwgts[0] = graph->tvwgt[0]*ntpwgts[0];196tpwgts[1] = graph->tvwgt[0] - tpwgts[0];197mindiff = iabs(tpwgts[0]-pwgts[0]);198from = (pwgts[0] < tpwgts[0] ? 1 : 0);199to = (from+1)%2;200201IFSET(ctrl->dbglvl, METIS_DBG_REFINE,202printf("Partitions: [%6"PRIDX" %6"PRIDX"] T[%6"PRIDX" %6"PRIDX"], Nv-Nb[%6"PRIDX" %6"PRIDX"]. ICut: %6"PRIDX" [B]\n",203pwgts[0], pwgts[1], tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut));204205queue = rpqCreate(nvtxs);206207iset(nvtxs, -1, moved);208209ASSERT(ComputeCut(graph, where) == graph->mincut);210ASSERT(CheckBnd(graph));211212/* Insert the nodes of the proper partition whose size is OK in the priority queue */213irandArrayPermute(nvtxs, perm, nvtxs/5, 1);214for (ii=0; ii<nvtxs; ii++) {215i = perm[ii];216if (where[i] == from && vwgt[i] <= mindiff)217rpqInsert(queue, i, ed[i]-id[i]);218}219220mincut = graph->mincut;221nbnd = graph->nbnd;222for (nswaps=0; nswaps<nvtxs; nswaps++) {223if ((higain = rpqGetTop(queue)) == -1)224break;225226if (pwgts[to]+vwgt[higain] > tpwgts[to])227break;228229mincut -= (ed[higain]-id[higain]);230INC_DEC(pwgts[to], pwgts[from], vwgt[higain]);231232where[higain] = to;233moved[higain] = nswaps;234235IFSET(ctrl->dbglvl, METIS_DBG_MOVEINFO,236printf("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]));237238/**************************************************************239* Update the id[i]/ed[i] values of the affected nodes240***************************************************************/241SWAP(id[higain], ed[higain], tmp);242if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])243BNDDelete(nbnd, bndind, bndptr, higain);244if (ed[higain] > 0 && bndptr[higain] == -1)245BNDInsert(nbnd, bndind, bndptr, higain);246247for (j=xadj[higain]; j<xadj[higain+1]; j++) {248k = adjncy[j];249250kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);251INC_DEC(id[k], ed[k], kwgt);252253/* Update the queue position */254if (moved[k] == -1 && where[k] == from && vwgt[k] <= mindiff)255rpqUpdate(queue, k, ed[k]-id[k]);256257/* Update its boundary information */258if (ed[k] == 0 && bndptr[k] != -1)259BNDDelete(nbnd, bndind, bndptr, k);260else if (ed[k] > 0 && bndptr[k] == -1)261BNDInsert(nbnd, bndind, bndptr, k);262}263}264265IFSET(ctrl->dbglvl, METIS_DBG_REFINE,266printf("\tMinimum cut: %6"PRIDX", PWGTS: [%6"PRIDX" %6"PRIDX"], NBND: %6"PRIDX"\n", mincut, pwgts[0], pwgts[1], nbnd));267268graph->mincut = mincut;269graph->nbnd = nbnd;270271rpqDestroy(queue);272273WCOREPOP;274}275276277/*************************************************************************278* This function performs an edge-based FM refinement279**************************************************************************/280void McGeneral2WayBalance(ctrl_t *ctrl, graph_t *graph, real_t *ntpwgts)281{282idx_t i, ii, j, k, l, kwgt, nvtxs, ncon, nbnd, nswaps, from, to, pass,283me, limit, tmp, cnum;284idx_t *xadj, *adjncy, *vwgt, *adjwgt, *where, *pwgts, *id, *ed, *bndptr, *bndind;285idx_t *moved, *swaps, *perm, *qnum, *qsizes;286idx_t higain, mincut, newcut, mincutorder;287real_t *invtvwgt, *minbalv, *newbalv, minbal, newbal;288rpq_t **queues;289290WCOREPUSH;291292nvtxs = graph->nvtxs;293ncon = graph->ncon;294xadj = graph->xadj;295vwgt = graph->vwgt;296adjncy = graph->adjncy;297adjwgt = graph->adjwgt;298invtvwgt = graph->invtvwgt;299where = graph->where;300id = graph->id;301ed = graph->ed;302pwgts = graph->pwgts;303bndptr = graph->bndptr;304bndind = graph->bndind;305306moved = iwspacemalloc(ctrl, nvtxs);307swaps = iwspacemalloc(ctrl, nvtxs);308perm = iwspacemalloc(ctrl, nvtxs);309qnum = iwspacemalloc(ctrl, nvtxs);310newbalv = rwspacemalloc(ctrl, ncon);311minbalv = rwspacemalloc(ctrl, ncon);312qsizes = iwspacemalloc(ctrl, 2*ncon);313314limit = gk_min(gk_max(0.01*nvtxs, 15), 100);315316/* Initialize the queues */317queues = (rpq_t **)wspacemalloc(ctrl, 2*ncon*sizeof(rpq_t *));318for (i=0; i<2*ncon; i++) {319queues[i] = rpqCreate(nvtxs);320qsizes[i] = 0;321}322323for (i=0; i<nvtxs; i++) {324qnum[i] = iargmax_nrm(ncon, vwgt+i*ncon, invtvwgt);325qsizes[2*qnum[i]+where[i]]++;326}327328329/* for the empty queues, move into them vertices from other queues */330for (from=0; from<2; from++) {331for (j=0; j<ncon; j++) {332if (qsizes[2*j+from] == 0) {333for (i=0; i<nvtxs; i++) {334if (where[i] != from)335continue;336337k = iargmax2_nrm(ncon, vwgt+i*ncon, invtvwgt);338if (k == j &&339qsizes[2*qnum[i]+from] > qsizes[2*j+from] &&340vwgt[i*ncon+qnum[i]]*invtvwgt[qnum[i]] < 1.3*vwgt[i*ncon+j]*invtvwgt[j]) {341qsizes[2*qnum[i]+from]--;342qsizes[2*j+from]++;343qnum[i] = j;344}345}346}347}348}349350351minbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ctrl->ubfactors, minbalv);352ASSERT(minbal > 0.0);353354newcut = mincut = graph->mincut;355mincutorder = -1;356357if (ctrl->dbglvl&METIS_DBG_REFINE) {358printf("Parts: [");359for (l=0; l<ncon; l++)360printf("(%6"PRIDX" %6"PRIDX" %.3"PRREAL" %.3"PRREAL") ",361pwgts[l], pwgts[ncon+l], ntpwgts[l], ntpwgts[ncon+l]);362printf("] Nv-Nb[%5"PRIDX", %5"PRIDX"]. ICut: %6"PRIDX", LB: %+.3"PRREAL" [B]\n",363graph->nvtxs, graph->nbnd, graph->mincut, minbal);364}365366iset(nvtxs, -1, moved);367368ASSERT(ComputeCut(graph, where) == graph->mincut);369ASSERT(CheckBnd(graph));370371/* Insert all nodes in the priority queues */372nbnd = graph->nbnd;373irandArrayPermute(nvtxs, perm, nvtxs/10, 1);374for (ii=0; ii<nvtxs; ii++) {375i = perm[ii];376rpqInsert(queues[2*qnum[i]+where[i]], i, ed[i]-id[i]);377}378379for (nswaps=0; nswaps<nvtxs; nswaps++) {380if (minbal <= 0.0)381break;382383SelectQueue(graph, ctrl->pijbm, ctrl->ubfactors, queues, &from, &cnum);384to = (from+1)%2;385386if (from == -1 || (higain = rpqGetTop(queues[2*cnum+from])) == -1)387break;388389newcut -= (ed[higain]-id[higain]);390391iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);392iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);393newbal = ComputeLoadImbalanceDiffVec(graph, 2, ctrl->pijbm, ctrl->ubfactors, newbalv);394395if (newbal < minbal || (newbal == minbal &&396(newcut < mincut ||397(newcut == mincut && BetterBalance2Way(ncon, minbalv, newbalv))))) {398mincut = newcut;399minbal = newbal;400mincutorder = nswaps;401rcopy(ncon, newbalv, minbalv);402}403else if (nswaps-mincutorder > limit) { /* We hit the limit, undo last move */404newcut += (ed[higain]-id[higain]);405iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+from*ncon, 1);406iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);407break;408}409410where[higain] = to;411moved[higain] = nswaps;412swaps[nswaps] = higain;413414if (ctrl->dbglvl&METIS_DBG_MOVEINFO) {415printf("Moved %6"PRIDX" from %"PRIDX"(%"PRIDX"). Gain: %5"PRIDX", "416"Cut: %5"PRIDX", NPwgts: ", higain, from, cnum, ed[higain]-id[higain], newcut);417for (l=0; l<ncon; l++)418printf("(%6"PRIDX", %6"PRIDX") ", pwgts[l], pwgts[ncon+l]);419printf(", %+.3"PRREAL" LB: %+.3"PRREAL"\n", minbal, newbal);420}421422423/**************************************************************424* Update the id[i]/ed[i] values of the affected nodes425***************************************************************/426SWAP(id[higain], ed[higain], tmp);427if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])428BNDDelete(nbnd, bndind, bndptr, higain);429if (ed[higain] > 0 && bndptr[higain] == -1)430BNDInsert(nbnd, bndind, bndptr, higain);431432for (j=xadj[higain]; j<xadj[higain+1]; j++) {433k = adjncy[j];434435kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);436INC_DEC(id[k], ed[k], kwgt);437438/* Update the queue position */439if (moved[k] == -1)440rpqUpdate(queues[2*qnum[k]+where[k]], k, ed[k]-id[k]);441442/* Update its boundary information */443if (ed[k] == 0 && bndptr[k] != -1)444BNDDelete(nbnd, bndind, bndptr, k);445else if (ed[k] > 0 && bndptr[k] == -1)446BNDInsert(nbnd, bndind, bndptr, k);447}448}449450451452/****************************************************************453* Roll back computations454*****************************************************************/455for (nswaps--; nswaps>mincutorder; nswaps--) {456higain = swaps[nswaps];457458to = where[higain] = (where[higain]+1)%2;459SWAP(id[higain], ed[higain], tmp);460if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1])461BNDDelete(nbnd, bndind, bndptr, higain);462else if (ed[higain] > 0 && bndptr[higain] == -1)463BNDInsert(nbnd, bndind, bndptr, higain);464465iaxpy(ncon, 1, vwgt+higain*ncon, 1, pwgts+to*ncon, 1);466iaxpy(ncon, -1, vwgt+higain*ncon, 1, pwgts+((to+1)%2)*ncon, 1);467for (j=xadj[higain]; j<xadj[higain+1]; j++) {468k = adjncy[j];469470kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]);471INC_DEC(id[k], ed[k], kwgt);472473if (bndptr[k] != -1 && ed[k] == 0)474BNDDelete(nbnd, bndind, bndptr, k);475if (bndptr[k] == -1 && ed[k] > 0)476BNDInsert(nbnd, bndind, bndptr, k);477}478}479480if (ctrl->dbglvl&METIS_DBG_REFINE) {481printf("\tMincut: %6"PRIDX" at %5"PRIDX", NBND: %6"PRIDX", NPwgts: [",482mincut, mincutorder, nbnd);483for (l=0; l<ncon; l++)484printf("(%6"PRIDX", %6"PRIDX") ", pwgts[l], pwgts[ncon+l]);485printf("], LB: %.3"PRREAL"\n", ComputeLoadImbalance(graph, 2, ctrl->pijbm));486}487488graph->mincut = mincut;489graph->nbnd = nbnd;490491492for (i=0; i<2*ncon; i++)493rpqDestroy(queues[i]);494495WCOREPOP;496}497498499500