/* * Copyright 1997, Regents of the University of Minnesota * * mfm2.c * * This file contains code that implements the edge-based FM refinement * * Started 7/23/97 * George * * $Id: mfm2.c,v 1.2 1998/11/30 14:50:44 karypis Exp $ */ #include /************************************************************************* * This function performs an edge-based FM refinement **************************************************************************/ void MocFM_2WayEdgeRefine2(CtrlType *ctrl, GraphType *graph, float *tpwgts, float *orgubvec, 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, origdiff[MAXNCON], origbal[MAXNCON], minbal[MAXNCON]; PQueueType parts[MAXNCON][2]; int higain, oldgain, mincut, initcut, newcut, mincutorder; float *maxwgt, *minwgt, ubvec[MAXNCON], tvec[MAXNCON]; 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, 15), 100); Compute2WayHLoadImbalanceVec(ncon, npwgts, tpwgts, origbal); for (i=0; idbglvl&DBG_REFINE) { printf("Parts: ["); for (l=0; lnvtxs, graph->nbnd, graph->mincut); for (i=0; imincut; Compute2WayHLoadImbalanceVec(ncon, npwgts, tpwgts, minbal); ASSERT(ComputeCut(graph, where) == graph->mincut); 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 = diff; *from = j; *cnum = i; } } } 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] - maxwgt[(*from)*ncon+i]); *cnum = i; break; } } for (i++; i max && PQueueGetSize(&queues[i][*from]) > 0) { max = diff; *cnum = i; } } } /* Check to see if you can focus on the cut */ if (maxdiff <= 0.0 || *from == -1) { maxgain = -100000; for (j=0; j<2; j++) { for (i=0; i 0 && PQueueGetKey(&queues[i][j]) > maxgain) { maxgain = PQueueGetKey(&queues[i][j]); *from = j; *cnum = i; } } } /* printf("(%2d %2d) %3d\n", *from, *cnum, maxgain); */ } } /************************************************************************* * This function checks if the newbal is better than oldbal given the * ubvector ubvec **************************************************************************/ int IsBetter2wayBalance(int ncon, float *newbal, float *oldbal, float *ubvec) { int i, j; float max1=0.0, max2=0.0, sum1=0.0, sum2=0.0, tmp; for (i=0; i max2) return 0; else return sum1 <= sum2; }