00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <parmetislib.h>
00015 #define PE 0
00016
00017
00018
00019
00020 void RedoMyLink(CtrlType *ctrl, GraphType *graph, idxtype *home, int me,
00021 int you, floattype *flows, floattype *sr_cost, floattype *sr_lbavg)
00022 {
00023 int h, i, r;
00024 int nvtxs, nedges, ncon;
00025 int pass, lastseed, totalv;
00026 idxtype *xadj, *adjncy, *adjwgt, *where, *vsize;
00027 idxtype *costwhere, *lbwhere, *selectwhere;
00028 idxtype *rdata, *ed, *id, *bndptr, *bndind, *perm;
00029 floattype *nvwgt, mycost;
00030 floattype lbavg, lbvec[MAXNCON];
00031 floattype best_lbavg, other_lbavg = -1.0, bestcost, othercost = -1.0;
00032 floattype npwgts[2*MAXNCON], pwgts[MAXNCON*2], tpwgts[MAXNCON*2];
00033 floattype ipc_factor, redist_factor, ftmp;
00034 int mype;
00035 MPI_Comm_rank(MPI_COMM_WORLD, &mype);
00036
00037 nvtxs = graph->nvtxs;
00038 nedges = graph->nedges;
00039 ncon = graph->ncon;
00040 xadj = graph->xadj;
00041 nvwgt = graph->nvwgt;
00042 vsize = graph->vsize;
00043 adjncy = graph->adjncy;
00044 adjwgt = graph->adjwgt;
00045 where = graph->where;
00046 ipc_factor = ctrl->ipc_factor;
00047 redist_factor = ctrl->redist_factor;
00048
00049
00050
00051
00052 rdata = idxmalloc(7*nvtxs, "rdata");
00053 id = graph->sendind = rdata;
00054 ed = graph->recvind = rdata + nvtxs;
00055 bndptr = graph->sendptr = rdata + 2*nvtxs;
00056 bndind = graph->recvptr = rdata + 3*nvtxs;
00057 costwhere = rdata + 4*nvtxs;
00058 lbwhere = rdata + 5*nvtxs;
00059 perm = rdata + 6*nvtxs;
00060 graph->gnpwgts = npwgts;
00061
00062 RandomPermute(nvtxs, perm, 1);
00063 idxcopy(nvtxs, where, costwhere);
00064 idxcopy(nvtxs, where, lbwhere);
00065
00066
00067
00068
00069 sset(ncon*2, 0.0, pwgts);
00070 for (h=0; h<ncon; h++) {
00071 tpwgts[h] = -1.0 * flows[h];
00072 tpwgts[ncon+h] = flows[h];
00073 }
00074
00075 for (i=0; i<nvtxs; i++) {
00076 if (where[i] == me) {
00077 for (h=0; h<ncon; h++) {
00078 tpwgts[h] += nvwgt[i*ncon+h];
00079 pwgts[h] += nvwgt[i*ncon+h];
00080 }
00081 }
00082 else {
00083 ASSERTS(where[i] == you);
00084 for (h=0; h<ncon; h++) {
00085 tpwgts[ncon+h] += nvwgt[i*ncon+h];
00086 pwgts[ncon+h] += nvwgt[i*ncon+h];
00087 }
00088 }
00089 }
00090
00091
00092 for (h=0; h<ncon; h++) {
00093 if (tpwgts[h] < 0.0) {
00094 tpwgts[ncon+h] += tpwgts[h];
00095 tpwgts[h] = 0.0;
00096 }
00097
00098 if (tpwgts[ncon+h] < 0.0) {
00099 tpwgts[h] += tpwgts[ncon+h];
00100 tpwgts[ncon+h] = 0.0;
00101 }
00102 }
00103
00104
00105
00106
00107 bestcost = (floattype)idxsum(nedges, adjwgt)*ipc_factor + (floattype)idxsum(nvtxs, vsize)*redist_factor;
00108 best_lbavg = 10.0;
00109
00110 lastseed = 0;
00111 for (pass = N_MOC_REDO_PASSES; pass>0; pass--) {
00112 idxset(nvtxs, 1, where);
00113
00114
00115
00116
00117 r = perm[lastseed] % nvtxs;
00118 lastseed = (lastseed+1) % nvtxs;
00119 where[r] = 0;
00120
00121 Moc_Serial_Compute2WayPartitionParams(graph);
00122 Moc_Serial_Init2WayBalance(graph, tpwgts);
00123 Moc_Serial_FM_2WayRefine(graph, tpwgts, 4);
00124 Moc_Serial_Balance2Way(graph, tpwgts, 1.02);
00125 Moc_Serial_FM_2WayRefine(graph, tpwgts, 4);
00126
00127 for (i=0; i<nvtxs; i++)
00128 where[i] = (where[i] == 0) ? me : you;
00129
00130 for (i=0; i<ncon; i++) {
00131 ftmp = (pwgts[i]+pwgts[ncon+i])/2.0;
00132 if (ftmp != 0.0)
00133 lbvec[i] = fabs(npwgts[i]-tpwgts[i])/ftmp;
00134 else
00135 lbvec[i] = 0.0;
00136 }
00137 lbavg = savg(ncon, lbvec);
00138
00139 totalv = 0;
00140 for (i=0; i<nvtxs; i++)
00141 if (where[i] != home[i])
00142 totalv += vsize[i];
00143
00144 mycost = (floattype)(graph->mincut)*ipc_factor + (floattype)totalv*redist_factor;
00145
00146 if (bestcost >= mycost) {
00147 bestcost = mycost;
00148 other_lbavg = lbavg;
00149 idxcopy(nvtxs, where, costwhere);
00150 }
00151
00152 if (best_lbavg >= lbavg) {
00153 best_lbavg = lbavg;
00154 othercost = mycost;
00155 idxcopy(nvtxs, where, lbwhere);
00156 }
00157 }
00158
00159 if (other_lbavg <= .05) {
00160 selectwhere = costwhere;
00161 *sr_cost = bestcost;
00162 *sr_lbavg = other_lbavg;
00163 }
00164 else {
00165 selectwhere = lbwhere;
00166 *sr_cost = othercost;
00167 *sr_lbavg = best_lbavg;
00168 }
00169
00170 idxcopy(nvtxs, selectwhere, where);
00171
00172 GKfree((void **)&rdata, LTERM);
00173 return;
00174 }
00175