/************************************************************************* * Let the game begin **************************************************************************/ int main(int argc, char *argv[]) { GraphType graph; char filename[256]; idxtype wgtflag; if (argc != 2) { mprintf("Usage: %s <GraphFile>\n", argv[0]); exit(0); } strcpy(filename, argv[1]); ReadGraph(&graph, filename, &wgtflag); if (graph.nvtxs == 0) { mprintf("Empty graph!\n"); exit(0); } mprintf("**********************************************************************\n"); mprintf("%s", METISTITLE); mprintf("Graph Information ---------------------------------------------------\n"); mprintf(" Name: %s, #Vertices: %D, #Edges: %D\n\n", filename, graph.nvtxs, graph.nedges/2); mprintf("Checking Graph... ---------------------------------------------------\n"); if (CheckGraph(&graph)) mprintf(" The format of the graph is correct!\n"); else mprintf(" The format of the graph is incorrect!\n"); mprintf("\n**********************************************************************\n"); gk_free((void **)&graph.xadj, &graph.adjncy, &graph.vwgt, &graph.adjwgt, LTERM); }
/* evison */ int CheckBiGraph(bigraph_t *bigraph, int numflag, int verbose){ idx_t err = 0; if (!CheckGraph(bigraph->super, numflag, verbose)) return 0; if (bigraph->nrows + bigraph->ncols != bigraph->super->nvtxs) err++; if (bigraph->area != bigraph->nrows * bigraph->ncols) err++; if (bigraph->nz * 2 != bigraph->super->nedges) err++; return (err == 0 ? 1 : 0); }
/************************************************************************* * This function is the entry point for KWMETIS **************************************************************************/ void METIS_mCPartGraphKway(int *nvtxs, int *ncon, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, floattype *rubvec, int *options, int *edgecut, idxtype *part) { int i, j; GraphType graph; CtrlType ctrl; if (*numflag == 1) Change2CNumbering(*nvtxs, xadj, adjncy); SetUpGraph(&graph, OP_KMETIS, *nvtxs, *ncon, xadj, adjncy, vwgt, adjwgt, *wgtflag); if (options[0] == 0) { /* Use the default parameters */ ctrl.CType = McKMETIS_CTYPE; ctrl.IType = McKMETIS_ITYPE; ctrl.RType = McKMETIS_RTYPE; ctrl.dbglvl = McKMETIS_DBGLVL; } else { ctrl.CType = options[OPTION_CTYPE]; ctrl.IType = options[OPTION_ITYPE]; ctrl.RType = options[OPTION_RTYPE]; ctrl.dbglvl = options[OPTION_DBGLVL]; } ctrl.optype = OP_KMETIS; ctrl.CoarsenTo = amax((*nvtxs)/(20*log2Int(*nparts)), 30*(*nparts)); ctrl.nmaxvwgt = 1.5/(1.0*ctrl.CoarsenTo); InitRandom(-1); AllocateWorkSpace(&ctrl, &graph, *nparts); IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl)); IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr)); ASSERT(CheckGraph(&graph)); *edgecut = MCMlevelKWayPartitioning(&ctrl, &graph, *nparts, part, rubvec); IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr)); IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl)); FreeWorkSpace(&ctrl, &graph); if (*numflag == 1) Change2FNumbering(*nvtxs, xadj, adjncy, part); }
int main(int argc, char *argv[]) { graph_t *graph, *fgraph; char filename[256]; idx_t wgtflag; params_t params; if (argc != 2 && argc != 3) { printf("Usage: %s <GraphFile> [FixedGraphFile (for storing the fixed graph)]\n", argv[0]); exit(0); } memset((void *) ¶ms, 0, sizeof(params_t)); params.filename = gk_strdup(argv[1]); graph = ReadGraph(¶ms); if (graph->nvtxs == 0) { printf("Empty graph!\n"); exit(0); } printf("**********************************************************************\n"); printf("%s", METISTITLE); printf(" (HEAD: %s, Built on: %s, %s)\n", SVNINFO, __DATE__, __TIME__); printf(" size of idx_t: %zubits, real_t: %zubits, idx_t *: %zubits\n", 8 * sizeof(idx_t), 8 * sizeof(real_t), 8 * sizeof(idx_t * )); printf("\n"); printf("Graph Information ---------------------------------------------------\n"); printf(" Name: %s, #Vertices: %"PRIDX", #Edges: %"PRIDX"\n\n", params.filename, graph->nvtxs, graph->nedges / 2); printf("Checking Graph... ---------------------------------------------------\n"); if (CheckGraph(graph, 1, 1)) { printf(" The format of the graph is correct!\n"); } else { printf(" The format of the graph is incorrect!\n"); if (argc == 3) { fgraph = FixGraph(graph); WriteGraph(fgraph, argv[2]); FreeGraph(&fgraph); printf(" A corrected version was stored at %s\n", argv[2]); } } printf("\n**********************************************************************\n"); FreeGraph(&graph); gk_free((void **) ¶ms.filename, ¶ms.tpwgtsfile, ¶ms.tpwgts, LTERM); }
int METIS_NodeND(idx_t *nvtxs, idx_t *xadj, idx_t *adjncy, idx_t *vwgt, idx_t *options, idx_t *perm, idx_t *iperm) { int sigrval=0, renumber=0; idx_t i, ii, j, l, nnvtxs=0; graph_t *graph=NULL; ctrl_t *ctrl; idx_t *cptr, *cind, *piperm; int numflag = 0; /* set up malloc cleaning code and signal catchers */ if (!gk_malloc_init()) return METIS_ERROR_MEMORY; gk_sigtrap(); if ((sigrval = gk_sigcatch()) != 0) goto SIGTHROW; /* set up the run time parameters */ ctrl = SetupCtrl(METIS_OP_OMETIS, options, 1, 3, NULL, NULL); if (!ctrl) { gk_siguntrap(); return METIS_ERROR_INPUT; } /* if required, change the numbering to 0 */ if (ctrl->numflag == 1) { Change2CNumbering(*nvtxs, xadj, adjncy); renumber = 1; } IFSET(ctrl->dbglvl, METIS_DBG_TIME, InitTimers(ctrl)); IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_startcputimer(ctrl->TotalTmr)); /* prune the dense columns */ if (ctrl->pfactor > 0.0) { piperm = imalloc(*nvtxs, "OMETIS: piperm"); graph = PruneGraph(ctrl, *nvtxs, xadj, adjncy, vwgt, piperm, ctrl->pfactor); if (graph == NULL) { /* if there was no prunning, cleanup the pfactor */ gk_free((void **)&piperm, LTERM); ctrl->pfactor = 0.0; } else { nnvtxs = graph->nvtxs; ctrl->compress = 0; /* disable compression if prunning took place */ } } /* compress the graph; note that compression only happens if not prunning has taken place. */ if (ctrl->compress) { cptr = imalloc(*nvtxs+1, "OMETIS: cptr"); cind = imalloc(*nvtxs, "OMETIS: cind"); graph = CompressGraph(ctrl, *nvtxs, xadj, adjncy, vwgt, cptr, cind); if (graph == NULL) { /* if there was no compression, cleanup the compress flag */ gk_free((void **)&cptr, &cind, LTERM); ctrl->compress = 0; } else { nnvtxs = graph->nvtxs; ctrl->cfactor = 1.0*(*nvtxs)/nnvtxs; if (ctrl->cfactor > 1.5 && ctrl->nseps == 1) ctrl->nseps = 2; //ctrl->nseps = (idx_t)(ctrl->cfactor*ctrl->nseps); } } /* if no prunning and no compression, setup the graph in the normal way. */ if (ctrl->pfactor == 0.0 && ctrl->compress == 0) graph = SetupGraph(ctrl, *nvtxs, 1, xadj, adjncy, vwgt, NULL, NULL); ASSERT(CheckGraph(graph, ctrl->numflag, 1)); /* allocate workspace memory */ AllocateWorkSpace(ctrl, graph); /* do the nested dissection ordering */ if (ctrl->ccorder) MlevelNestedDissectionCC(ctrl, graph, iperm, graph->nvtxs); else MlevelNestedDissection(ctrl, graph, iperm, graph->nvtxs); if (ctrl->pfactor > 0.0) { /* Order any prunned vertices */ icopy(nnvtxs, iperm, perm); /* Use perm as an auxiliary array */ for (i=0; i<nnvtxs; i++) iperm[piperm[i]] = perm[i]; for (i=nnvtxs; i<*nvtxs; i++) iperm[piperm[i]] = i; gk_free((void **)&piperm, LTERM); } else if (ctrl->compress) { /* Uncompress the ordering */ /* construct perm from iperm */ for (i=0; i<nnvtxs; i++) perm[iperm[i]] = i; for (l=ii=0; ii<nnvtxs; ii++) { i = perm[ii]; for (j=cptr[i]; j<cptr[i+1]; j++) iperm[cind[j]] = l++; } gk_free((void **)&cptr, &cind, LTERM); } for (i=0; i<*nvtxs; i++) perm[iperm[i]] = i; IFSET(ctrl->dbglvl, METIS_DBG_TIME, gk_stopcputimer(ctrl->TotalTmr)); IFSET(ctrl->dbglvl, METIS_DBG_TIME, PrintTimers(ctrl)); /* clean up */ FreeCtrl(&ctrl); SIGTHROW: /* if required, change the numbering back to 1 */ if (renumber) Change2FNumberingOrder(*nvtxs, xadj, adjncy, perm, iperm); gk_siguntrap(); gk_malloc_cleanup(0); return metis_rcode(sigrval); }
/************************************************************************* * This function balances two partitions by moving the highest gain * (including negative gain) vertices to the other domain. * It is used only when tha unbalance is due to non contigous * subdomains. That is, the are no boundary vertices. * It moves vertices from the domain that is overweight to the one that * is underweight. **************************************************************************/ void MocInit2WayBalance(CtrlType *ctrl, GraphType *graph, float *tpwgts) { int i, ii, j, k, l, kwgt, nvtxs, nbnd, ncon, nswaps, from, to, pass, me, cnum, tmp; idxtype *xadj, *adjncy, *adjwgt, *where, *id, *ed, *bndptr, *bndind; idxtype *perm, *qnum; float *nvwgt, *npwgts; PQueueType parts[MAXNCON][2]; int higain, oldgain, mincut; nvtxs = graph->nvtxs; ncon = graph->ncon; xadj = graph->xadj; adjncy = graph->adjncy; nvwgt = graph->nvwgt; adjwgt = graph->adjwgt; where = graph->where; id = graph->id; ed = graph->ed; npwgts = graph->npwgts; bndptr = graph->bndptr; bndind = graph->bndind; perm = idxwspacemalloc(ctrl, nvtxs); qnum = idxwspacemalloc(ctrl, nvtxs); /* This is called for initial partitioning so we know from where to pick nodes */ from = 1; to = (from+1)%2; if (ctrl->dbglvl&DBG_REFINE) { printf("Parts: ["); for (l=0; l<ncon; l++) printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]); printf("] T[%.3f %.3f], Nv-Nb[%5d, %5d]. ICut: %6d, LB: %.3f [B]\n", tpwgts[0], tpwgts[1], graph->nvtxs, graph->nbnd, graph->mincut, Compute2WayHLoadImbalance(ncon, npwgts, tpwgts)); } for (i=0; i<ncon; i++) { PQueueInit(ctrl, &parts[i][0], nvtxs, PLUS_GAINSPAN+1); PQueueInit(ctrl, &parts[i][1], nvtxs, PLUS_GAINSPAN+1); } ASSERT(ComputeCut(graph, where) == graph->mincut); ASSERT(CheckBnd(graph)); ASSERT(CheckGraph(graph)); /* Compute the queues in which each vertex will be assigned to */ for (i=0; i<nvtxs; i++) qnum[i] = samax(ncon, nvwgt+i*ncon); /* Insert the nodes of the proper partition in the appropriate priority queue */ RandomPermute(nvtxs, perm, 1); for (ii=0; ii<nvtxs; ii++) { i = perm[ii]; if (where[i] == from) { if (ed[i] > 0) PQueueInsert(&parts[qnum[i]][0], i, ed[i]-id[i]); else PQueueInsert(&parts[qnum[i]][1], i, ed[i]-id[i]); } } mincut = graph->mincut; nbnd = graph->nbnd; for (nswaps=0; nswaps<nvtxs; nswaps++) { if (AreAnyVwgtsBelow(ncon, 1.0, npwgts+from*ncon, 0.0, nvwgt, tpwgts[from])) break; if ((cnum = SelectQueueOneWay(ncon, npwgts, tpwgts, from, parts)) == -1) break; if ((higain = PQueueGetMax(&parts[cnum][0])) == -1) higain = PQueueGetMax(&parts[cnum][1]); mincut -= (ed[higain]-id[higain]); saxpy(ncon, 1.0, nvwgt+higain*ncon, 1, npwgts+to*ncon, 1); saxpy(ncon, -1.0, nvwgt+higain*ncon, 1, npwgts+from*ncon, 1); where[higain] = to; if (ctrl->dbglvl&DBG_MOVEINFO) { printf("Moved %6d from %d(%d). [%5d] %5d, NPwgts: ", higain, from, cnum, ed[higain]-id[higain], mincut); for (l=0; l<ncon; l++) printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]); printf(", LB: %.3f\n", Compute2WayHLoadImbalance(ncon, npwgts, tpwgts)); if (ed[higain] == 0 && id[higain] > 0) printf("\t Pulled from the interior!\n"); } /************************************************************** * Update the id[i]/ed[i] values of the affected nodes ***************************************************************/ SWAP(id[higain], ed[higain], tmp); if (ed[higain] == 0 && bndptr[higain] != -1 && xadj[higain] < xadj[higain+1]) BNDDelete(nbnd, bndind, bndptr, higain); if (ed[higain] > 0 && bndptr[higain] == -1) BNDInsert(nbnd, bndind, bndptr, higain); for (j=xadj[higain]; j<xadj[higain+1]; j++) { k = adjncy[j]; oldgain = ed[k]-id[k]; kwgt = (to == where[k] ? adjwgt[j] : -adjwgt[j]); INC_DEC(id[k], ed[k], kwgt); /* Update the queue position */ if (where[k] == from) { if (ed[k] > 0 && bndptr[k] == -1) { /* It moves in boundary */ PQueueDelete(&parts[qnum[k]][1], k, oldgain); PQueueInsert(&parts[qnum[k]][0], k, ed[k]-id[k]); } else { /* It must be in the boundary already */ if (bndptr[k] == -1) printf("What you thought was wrong!\n"); PQueueUpdate(&parts[qnum[k]][0], k, oldgain, ed[k]-id[k]); } } /* Update its boundary information */ if (ed[k] == 0 && bndptr[k] != -1) BNDDelete(nbnd, bndind, bndptr, k); else if (ed[k] > 0 && bndptr[k] == -1) BNDInsert(nbnd, bndind, bndptr, k); } ASSERTP(ComputeCut(graph, where) == mincut, ("%d != %d\n", ComputeCut(graph, where), mincut)); } if (ctrl->dbglvl&DBG_REFINE) { printf("\tMincut: %6d, NBND: %6d, NPwgts: ", mincut, nbnd); for (l=0; l<ncon; l++) printf("(%.3f, %.3f) ", npwgts[l], npwgts[ncon+l]); printf(", LB: %.3f\n", Compute2WayHLoadImbalance(ncon, npwgts, tpwgts)); } graph->mincut = mincut; graph->nbnd = nbnd; for (i=0; i<ncon; i++) { PQueueFree(ctrl, &parts[i][0]); PQueueFree(ctrl, &parts[i][1]); } ASSERT(ComputeCut(graph, where) == graph->mincut); ASSERT(CheckBnd(graph)); idxwspacefree(ctrl, nvtxs); idxwspacefree(ctrl, nvtxs); }
/************************************************************************* * This function is the entry point for PWMETIS that accepts exact weights * for the target partitions **************************************************************************/ void METIS_mCPartGraphRecursive2(int *nvtxs, int *ncon, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, float *tpwgts, int *options, int *edgecut, idxtype *part) { int i, j; GraphType graph; CtrlType ctrl; float *mytpwgts; float avgwgt; if (*numflag == 1) Change2CNumbering(*nvtxs, xadj, adjncy); SetUpGraph(&graph, OP_PMETIS, *nvtxs, *ncon, xadj, adjncy, vwgt, adjwgt, *wgtflag); graph.npwgts = NULL; mytpwgts = fmalloc(*nparts, "mytpwgts"); scopy(*nparts, tpwgts, mytpwgts); if (options[0] == 0) { /* Use the default parameters */ ctrl.CType = McPMETIS_CTYPE; ctrl.IType = McPMETIS_ITYPE; ctrl.RType = McPMETIS_RTYPE; ctrl.dbglvl = McPMETIS_DBGLVL; } else { ctrl.CType = options[OPTION_CTYPE]; ctrl.IType = options[OPTION_ITYPE]; ctrl.RType = options[OPTION_RTYPE]; ctrl.dbglvl = options[OPTION_DBGLVL]; } ctrl.optype = OP_PMETIS; ctrl.CoarsenTo = 100; ctrl.nmaxvwgt = 1.5/(1.0*ctrl.CoarsenTo); InitRandom(options[7]); AllocateWorkSpace(&ctrl, &graph, *nparts); IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl)); IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr)); ASSERT(CheckGraph(&graph)); *edgecut = MCMlevelRecursiveBisection2(&ctrl, &graph, *nparts, mytpwgts, part, 1.000, 0); /* { idxtype wgt[2048], minwgt, maxwgt, sumwgt; printf("nvtxs: %d, nparts: %d, ncon: %d\n", graph.nvtxs, *nparts, *ncon); for (i=0; i<(*nparts)*(*ncon); i++) wgt[i] = 0; for (i=0; i<graph.nvtxs; i++) for (j=0; j<*ncon; j++) wgt[part[i]*(*ncon)+j] += vwgt[i*(*ncon)+j]; for (j=0; j<*ncon; j++) { minwgt = maxwgt = sumwgt = 0; for (i=0; i<(*nparts); i++) { minwgt = (wgt[i*(*ncon)+j] < wgt[minwgt*(*ncon)+j]) ? i : minwgt; maxwgt = (wgt[i*(*ncon)+j] > wgt[maxwgt*(*ncon)+j]) ? i : maxwgt; sumwgt += wgt[i*(*ncon)+j]; } avgwgt = (float)sumwgt / (float)*nparts; printf("min: %5d, max: %5d, avg: %5.2f, balance: %6.3f\n", wgt[minwgt*(*ncon)+j], wgt[maxwgt*(*ncon)+j], avgwgt, (float)wgt[maxwgt*(*ncon)+j] / avgwgt); } printf("\n"); } */ IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr)); IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl)); FreeWorkSpace(&ctrl, &graph); GKfree((void**)(void *)&mytpwgts, LTERM); if (*numflag == 1) Change2FNumbering(*nvtxs, xadj, adjncy, part); }
/************************************************************************** * mexFunction: gateway routine for MATLAB interface. ***************************************************************************/ void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { // Argument checking if (nrhs != 5) mexErrMsgIdAndTxt(FUNC_NAME, "Wrong input."); if (nlhs != 3) mexErrMsgIdAndTxt(FUNC_NAME, "Wrong output."); // Input and output variables idx_t nvtxs = (idx_t) mxGetScalar(nvtxs_in); idx_t *xadj; GetIdxArray(xadj_in,&xadj); idx_t *adjncy; GetIdxArray(adjncy_in,&adjncy); idx_t *vwgt; GetIdxArray(vwgt_in,&vwgt); idx_t options[METIS_NOPTIONS]; GetOptions(options_in, options); idx_t *sepidx; idx_t *lgraphidx; idx_t *rgraphidx; // Metis main function idx_t i, nnvtxs=0; idx_t ptlgraph, ptrgraph, ptsep; graph_t *graph=NULL; ctrl_t *ctrl; idx_t *piperm; idx_t snvtxs[3]; idx_t *where; /* set up malloc cleaning code and signal catchers */ if (!gk_malloc_init()) CheckReturn( METIS_ERROR_MEMORY, FUNC_NAME ); // set up the run time parameters ctrl = SetupCtrl(METIS_OP_OMETIS, options, 1, 3, NULL, NULL); // prune the dense columns if (ctrl->pfactor > 0.0) { piperm = imalloc(nvtxs, "OMETIS: piperm"); graph = PruneGraph(ctrl, nvtxs, xadj, adjncy, vwgt, piperm, ctrl->pfactor); if (graph == NULL) { // if there was no prunning, cleanup the pfactor gk_free((void **)&piperm, LTERM); ctrl->pfactor = 0.0; } else { nnvtxs = graph->nvtxs; // disable compression if prunning took place ctrl->compress = 0; } } // compress the graph if (ctrl->compress) ctrl->compress = 0; // if no prunning and no compression, setup the graph in the normal way. if (ctrl->pfactor == 0.0 && ctrl->compress == 0) graph = SetupGraph(ctrl, nvtxs, 1, xadj, adjncy, vwgt, NULL, NULL); ASSERT(CheckGraph(graph, ctrl->numflag, 1)); /* allocate workspace memory */ AllocateWorkSpace(ctrl, graph); MlevelNodeBisectionMultiple(ctrl, graph); snvtxs[0] = 0; snvtxs[1] = 0; snvtxs[2] = 0; if (ctrl->pfactor > 0.0) snvtxs[2] += nvtxs-nnvtxs; where = graph->where; for (i=0; i<graph->nvtxs; i++) snvtxs[where[i]]++; lgraphidx = (idx_t*) mxCalloc (snvtxs[0], sizeof(idx_t)); rgraphidx = (idx_t*) mxCalloc (snvtxs[1], sizeof(idx_t)); sepidx = (idx_t*) mxCalloc (snvtxs[2], sizeof(idx_t)); ptlgraph = 0; ptrgraph = 0; ptsep = 0; if (ctrl->pfactor > 0.0) { for (i=0; i<graph->nvtxs; i++) if (where[i] == 0) lgraphidx[ptlgraph++] = piperm[i]; else if (where[i] == 1) rgraphidx[ptrgraph++] = piperm[i]; else sepidx[ptsep++] = piperm[i]; for (i=nnvtxs; i<nvtxs; i++) sepidx[ptsep++] = piperm[i]; gk_free((void **)&piperm, LTERM); } else { for (i=0; i<graph->nvtxs; i++) if (where[i] == 0) lgraphidx[ptlgraph++] = i; else if (where[i] == 1) rgraphidx[ptrgraph++] = i; else sepidx[ptsep++] = i; } /* clean up */ FreeCtrl(&ctrl); // Output lgraphidx_out = mxCreateDoubleMatrix(1,ptlgraph,mxREAL); mxSetData(lgraphidx_out,mxMalloc(sizeof(double)*ptlgraph)); double *lgraphidx_out_pr = mxGetPr(lgraphidx_out); for(idx_t i=0; i<ptlgraph; i++) lgraphidx_out_pr[i] = (double) lgraphidx[i]; rgraphidx_out = mxCreateDoubleMatrix(1,ptrgraph,mxREAL); mxSetData(rgraphidx_out,mxMalloc(sizeof(double)*ptrgraph)); double *rgraphidx_out_pr = mxGetPr(rgraphidx_out); for(idx_t i=0; i<ptrgraph; i++) rgraphidx_out_pr[i] = (double) rgraphidx[i]; sepidx_out = mxCreateDoubleMatrix(1,ptsep,mxREAL); mxSetData(sepidx_out,mxMalloc(sizeof(double)*ptsep)); double *sepidx_out_pr = mxGetPr(sepidx_out); for(idx_t i=0; i<ptsep; i++) sepidx_out_pr[i] = (double) sepidx[i]; }