/* * Copyright 1997, Regents of the University of Minnesota * * mfm.c * * This file contains code that implements the edge-based FM refinement * * Started 7/23/97 * George * * $Id: mfm.c,v 1.3 1998/11/30 14:50:44 karypis Exp $ */ #include /************************************************************************* * This function performs an edge-based FM refinement **************************************************************************/ void MocFM_2WayEdgeRefine(CtrlType *ctrl, GraphType *graph, float *tpwgts, int npasses) { int i, ii, j, k, l, kwgt, nvtxs, ncon, nbnd, nswaps, from, to, pass, me, limit, tmp, cnum; idxtype *xadj, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind; idxtype *moved, *swaps, *perm, *qnum; float *nvwgt, *npwgts, mindiff[MAXNCON], origbal, minbal, newbal; PQueueType parts[MAXNCON][2]; int higain, oldgain, mincut, initcut, newcut, mincutorder; float rtpwgts[2]; nvtxs = graph->nvtxs; ncon = graph->ncon; xadj = graph->xadj; nvwgt = graph->nvwgt; adjncy = graph->adjncy; adjwgt = graph->adjwgt; where = graph->where; id = graph->id; ed = graph->ed; npwgts = graph->npwgts; bndptr = graph->bndptr; bndind = graph->bndind; moved = idxwspacemalloc(ctrl, nvtxs); swaps = idxwspacemalloc(ctrl, nvtxs); perm = idxwspacemalloc(ctrl, nvtxs); qnum = idxwspacemalloc(ctrl, nvtxs); limit = amin(amax(0.01*nvtxs, 25), 150); /* Initialize the queues */ for (i=0; idbglvl&DBG_REFINE) { printf("Parts: ["); for (l=0; lnvtxs, graph->nbnd, graph->mincut, origbal); } idxset(nvtxs, -1, moved); for (pass=0; passmincut; for (i=0; imincut); ASSERT(CheckBnd(graph)); /* Insert boundary nodes in the priority queues */ nbnd = graph->nbnd; RandomPermute(nbnd, perm, 1); for (ii=0; ii 0 || id[i] == 0); ASSERT(bndptr[i] != -1); PQueueInsert(&parts[qnum[i]][where[i]], i, ed[i]-id[i]); } for (nswaps=0; nswaps limit) { /* We hit the limit, undo last move */ newcut += (ed[higain]-id[higain]); saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+from*ncon, 1); saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1); break; } where[higain] = to; moved[higain] = nswaps; swaps[nswaps] = higain; if (ctrl->dbglvl&DBG_MOVEINFO) { printf("Moved %6d from %d(%d). Gain: %5d, Cut: %5d, NPwgts: ", higain, from, cnum, ed[higain]-id[higain], newcut); for (l=0; l 0) { /* It will now become a boundary vertex */ BNDInsert(nbnd, bndind, bndptr, k); if (moved[k] == -1) PQueueInsert(&parts[qnum[k]][where[k]], k, ed[k]-id[k]); } } } } /**************************************************************** * Roll back computations *****************************************************************/ for (i=0; imincutorder; nswaps--) { higain = swaps[nswaps]; to = where[higain] = (where[higain]+1)%2; SWAP(id[higain], ed[higain], tmp); if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1]) BNDDelete(nbnd, bndind, bndptr, higain); else if (ed[higain] > 0 && bndptr[higain] == -1) BNDInsert(nbnd, bndind, bndptr, higain); saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1); saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+((to+1)%2)*ncon, 1); for (j=xadj[higain]; j 0) BNDInsert(nbnd, bndind, bndptr, k); } } if (ctrl->dbglvl&DBG_REFINE) { printf("\tMincut: %6d at %5d, NBND: %6d, NPwgts: [", mincut, mincutorder, nbnd); for (l=0; lmincut = mincut; graph->nbnd = nbnd; if (mincutorder == -1 || mincut == initcut) break; } for (i=0; i= maxdiff) { maxdiff = npwgts[part*ncon+i]-tpwgts[part]; *from = part; *cnum = i; } } } /* printf("Selected1 %d(%d) -> %d [%5f]\n", *from, *cnum, PQueueGetSize(&queues[*cnum][*from]), maxdiff); */ if (*from != -1 && PQueueGetSize(&queues[*cnum][*from]) == 0) { /* The desired queue is empty, select a node from that side anyway */ for (i=0; i 0) { max = npwgts[(*from)*ncon + i]; *cnum = i; break; } } for (i++; i max && PQueueGetSize(&queues[i][*from]) > 0) { max = npwgts[(*from)*ncon + i]; *cnum = i; } } } /* Check to see if you can focus on the cut */ if (maxdiff <= 0.0 || *from == -1) { maxgain = -100000; for (part=0; part<2; part++) { for (i=0; i 0 && PQueueGetKey(&queues[i][part]) > maxgain) { maxgain = PQueueGetKey(&queues[i][part]); *from = part; *cnum = i; } } } } /* printf("Selected2 %d(%d) -> %d\n", *from, *cnum, PQueueGetSize(&queues[*cnum][*from])); */ } /************************************************************************* * This function checks if the balance achieved is better than the diff * For now, it uses a 2-norm measure **************************************************************************/ int BetterBalance(int ncon, float *npwgts, float *tpwgts, float *diff) { int i; float ndiff[MAXNCON]; for (i=0; i