/************************************************************************* * This function takes a graph and produces a bisection of it **************************************************************************/ void MlevelNestedDissectionCC(CtrlType *ctrl, GraphType *graph, idxtype *order, float ubfactor, int lastvtx) { int i, j, nvtxs, nbnd, tvwgt, tpwgts2[2], nsgraphs, ncmps, rnvtxs; idxtype *label, *bndind; idxtype *cptr, *cind; GraphType *sgraphs; nvtxs = graph->nvtxs; /* Determine the weights of the partitions */ tvwgt = idxsum(nvtxs, graph->vwgt); tpwgts2[0] = tvwgt/2; tpwgts2[1] = tvwgt-tpwgts2[0]; MlevelNodeBisectionMultiple(ctrl, graph, tpwgts2, ubfactor); IFSET(ctrl->dbglvl, DBG_SEPINFO, printf("Nvtxs: %6d, [%6d %6d %6d]\n", graph->nvtxs, graph->pwgts[0], graph->pwgts[1], graph->pwgts[2])); /* Order the nodes in the separator */ nbnd = graph->nbnd; bndind = graph->bndind; label = graph->label; for (i=0; i<nbnd; i++) order[label[bndind[i]]] = --lastvtx; cptr = idxmalloc(nvtxs, "MlevelNestedDissectionCC: cptr"); cind = idxmalloc(nvtxs, "MlevelNestedDissectionCC: cind"); ncmps = FindComponents(ctrl, graph, cptr, cind); /* if (ncmps > 2) printf("[%5d] has %3d components\n", nvtxs, ncmps); */ sgraphs = (GraphType *)GKmalloc(ncmps*sizeof(GraphType), "MlevelNestedDissectionCC: sgraphs"); nsgraphs = SplitGraphOrderCC(ctrl, graph, sgraphs, ncmps, cptr, cind); /*GKfree(&cptr, &cind, LTERM);*/ GKfree2((void **)&cptr, (void **)&cind); /* Free the memory of the top level graph */ /*GKfree(&graph->gdata, &graph->rdata, &graph->label, LTERM);*/ GKfree3((void**)&graph->gdata, (void**)&graph->rdata, (void**)&graph->label); /* Go and process the subgraphs */ for (rnvtxs=i=0; i<nsgraphs; i++) { if (sgraphs[i].adjwgt == NULL) { MMDOrder(ctrl, sgraphs+i, order, lastvtx-rnvtxs); /*GKfree(&sgraphs[i].gdata, &sgraphs[i].label, LTERM);*/ GKfree2((void**)&sgraphs[i].gdata, (void**)&sgraphs[i].label); } else { MlevelNestedDissectionCC(ctrl, sgraphs+i, order, ubfactor, lastvtx-rnvtxs); } rnvtxs += sgraphs[i].nvtxs; } free(sgraphs); }
void MlevelNestedDissectionCC(ctrl_t *ctrl, graph_t *graph, idx_t *order, idx_t lastvtx) { idx_t i, j, nvtxs, nbnd, ncmps, rnvtxs, snvtxs; idx_t *label, *bndind; idx_t *cptr, *cind; graph_t **sgraphs; nvtxs = graph->nvtxs; MlevelNodeBisectionMultiple(ctrl, graph); IFSET(ctrl->dbglvl, METIS_DBG_SEPINFO, printf("Nvtxs: %6"PRIDX", [%6"PRIDX" %6"PRIDX" %6"PRIDX"]\n", graph->nvtxs, graph->pwgts[0], graph->pwgts[1], graph->pwgts[2])); /* Order the nodes in the separator */ nbnd = graph->nbnd; bndind = graph->bndind; label = graph->label; for (i=0; i<nbnd; i++) order[label[bndind[i]]] = --lastvtx; WCOREPUSH; cptr = iwspacemalloc(ctrl, nvtxs+1); cind = iwspacemalloc(ctrl, nvtxs); ncmps = FindSepInducedComponents(ctrl, graph, cptr, cind); if (ctrl->dbglvl&METIS_DBG_INFO) { if (ncmps > 2) printf(" Bisection resulted in %"PRIDX" connected components\n", ncmps); } sgraphs = SplitGraphOrderCC(ctrl, graph, ncmps, cptr, cind); WCOREPOP; /* Free the memory of the top level graph */ FreeGraph(&graph); /* Go and process the subgraphs */ for (rnvtxs=i=0; i<ncmps; i++) { /* Save the number of vertices in sgraphs[i] because sgraphs[i] is freed inside MlevelNestedDissectionCC, and as such it will be undefined. */ snvtxs = sgraphs[i]->nvtxs; if (sgraphs[i]->nvtxs > MMDSWITCH && sgraphs[i]->nedges > 0) { MlevelNestedDissectionCC(ctrl, sgraphs[i], order, lastvtx-rnvtxs); } else { MMDOrder(ctrl, sgraphs[i], order, lastvtx-rnvtxs); FreeGraph(&sgraphs[i]); } rnvtxs += snvtxs; } gk_free((void **)&sgraphs, LTERM); }
/************************************************************************* * This function takes a graph and produces a bisection of it **************************************************************************/ void MlevelNestedDissection(CtrlType *ctrl, GraphType *graph, idxtype *order, float ubfactor, int lastvtx) { int i, j, nvtxs, nbnd, tvwgt, tpwgts2[2]; idxtype *label, *bndind; GraphType lgraph, rgraph; nvtxs = graph->nvtxs; /* Determine the weights of the partitions */ tvwgt = idxsum(nvtxs, graph->vwgt); tpwgts2[0] = tvwgt/2; tpwgts2[1] = tvwgt-tpwgts2[0]; switch (ctrl->optype) { case OP_OEMETIS: MlevelEdgeBisection(ctrl, graph, tpwgts2, ubfactor); IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->SepTmr)); ConstructMinCoverSeparator(ctrl, graph, ubfactor); IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->SepTmr)); break; case OP_ONMETIS: MlevelNodeBisectionMultiple(ctrl, graph, tpwgts2, ubfactor); IFSET(ctrl->dbglvl, DBG_SEPINFO, printf("Nvtxs: %6d, [%6d %6d %6d]\n", graph->nvtxs, graph->pwgts[0], graph->pwgts[1], graph->pwgts[2])); break; } /* Order the nodes in the separator */ nbnd = graph->nbnd; bndind = graph->bndind; label = graph->label; for (i=0; i<nbnd; i++) order[label[bndind[i]]] = --lastvtx; SplitGraphOrder(ctrl, graph, &lgraph, &rgraph); /* Free the memory of the top level graph */ /*GKfree(&graph->gdata, &graph->rdata, &graph->label, LTERM);*/ GKfree3((void **)&graph->gdata, (void**)&graph->rdata, (void **)&graph->label); if (rgraph.nvtxs > MMDSWITCH) MlevelNestedDissection(ctrl, &rgraph, order, ubfactor, lastvtx); else { MMDOrder(ctrl, &rgraph, order, lastvtx); /*GKfree(&rgraph.gdata, &rgraph.rdata, &rgraph.label, LTERM);*/ GKfree3((void**)&rgraph.gdata, (void**)&rgraph.rdata, (void**)&rgraph.label); } if (lgraph.nvtxs > MMDSWITCH) MlevelNestedDissection(ctrl, &lgraph, order, ubfactor, lastvtx-rgraph.nvtxs); else { MMDOrder(ctrl, &lgraph, order, lastvtx-rgraph.nvtxs); /*GKfree(&lgraph.gdata, &lgraph.rdata, &lgraph.label, LTERM);*/ GKfree3((void**)&lgraph.gdata, (void**)&lgraph.rdata, (void**)&lgraph.label); } }
/************************************************************************* * This function is the entry point for ONWMETIS. It requires weights on the * vertices. It is for the case that the matrix has been pre-compressed. **************************************************************************/ void METIS_NodeComputeSeparator(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *options, int *sepsize, idxtype *part) { int i, j, tvwgt, tpwgts[2]; GraphType graph; CtrlType ctrl; SetUpGraph(&graph, OP_ONMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, 3); tvwgt = idxsum(*nvtxs, graph.vwgt); if (options[0] == 0) { /* Use the default parameters */ ctrl.CType = ONMETIS_CTYPE; ctrl.IType = ONMETIS_ITYPE; ctrl.RType = ONMETIS_RTYPE; ctrl.dbglvl = ONMETIS_DBGLVL; } else { ctrl.CType = options[OPTION_CTYPE]; ctrl.IType = options[OPTION_ITYPE]; ctrl.RType = options[OPTION_RTYPE]; ctrl.dbglvl = options[OPTION_DBGLVL]; } ctrl.oflags = 0; ctrl.pfactor = 0; ctrl.nseps = 5; ctrl.optype = OP_ONMETIS; ctrl.CoarsenTo = amin(100, *nvtxs-1); ctrl.maxvwgt = 1.5*tvwgt/ctrl.CoarsenTo; InitRandom(options[7]); AllocateWorkSpace(&ctrl, &graph, 2); /*============================================================ * Perform the bisection *============================================================*/ tpwgts[0] = tvwgt/2; tpwgts[1] = tvwgt-tpwgts[0]; MlevelNodeBisectionMultiple(&ctrl, &graph, tpwgts, 1.05); *sepsize = graph.pwgts[2]; idxcopy(*nvtxs, graph.where, part); GKfree((void**)&graph.gdata, &graph.rdata, &graph.label, LTERM); FreeWorkSpace(&ctrl, &graph); }
/************************************************************************* * This function is the entry point for ONWMETIS. It requires weights on the * vertices. It is for the case that the matrix has been pre-compressed. **************************************************************************/ void METIS_NodeComputeSeparator(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, float *ubfactor, int *options, int *sepsize, idxtype *part) { int i, j, tvwgt, tpwgts[2]; GraphType graph; CtrlType ctrl; SetUpGraph(&graph, OP_ONMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, 3); tvwgt = idxsum(*nvtxs, graph.vwgt); if (options[0] == 0) { /* Use the default parameters */ ctrl.CType = ONMETIS_CTYPE; ctrl.IType = ONMETIS_ITYPE; ctrl.RType = ONMETIS_RTYPE; ctrl.dbglvl = ONMETIS_DBGLVL; } else { ctrl.CType = options[OPTION_CTYPE]; ctrl.IType = options[OPTION_ITYPE]; ctrl.RType = options[OPTION_RTYPE]; ctrl.dbglvl = options[OPTION_DBGLVL]; } ctrl.oflags = OFLAG_COMPRESS; /* For by-passing the pre-coarsening for multiple runs */ ctrl.RType = 2; /* Standard 1-sided node refinement code */ ctrl.pfactor = 0; ctrl.nseps = 5; /* This should match NUM_INIT_MSECTIONS in ParMETISLib/defs.h */ ctrl.optype = OP_ONMETIS; InitRandom(options[7]); AllocateWorkSpace(&ctrl, &graph, 2); /*============================================================ * Perform the bisection *============================================================*/ tpwgts[0] = tvwgt/2; tpwgts[1] = tvwgt-tpwgts[0]; MlevelNodeBisectionMultiple(&ctrl, &graph, tpwgts, *ubfactor*.95); *sepsize = graph.pwgts[2]; idxcopy(*nvtxs, graph.where, part); GKfree((void **)&graph.gdata, &graph.rdata, &graph.label, LTERM); FreeWorkSpace(&ctrl, &graph); }
void MlevelNestedDissection(ctrl_t *ctrl, graph_t *graph, idx_t *order, idx_t lastvtx) { idx_t i, j, nvtxs, nbnd; idx_t *label, *bndind; graph_t *lgraph, *rgraph; nvtxs = graph->nvtxs; MlevelNodeBisectionMultiple(ctrl, graph); IFSET(ctrl->dbglvl, METIS_DBG_SEPINFO, printf("Nvtxs: %6"PRIDX", [%6"PRIDX" %6"PRIDX" %6"PRIDX"]\n", graph->nvtxs, graph->pwgts[0], graph->pwgts[1], graph->pwgts[2])); /* Order the nodes in the separator */ nbnd = graph->nbnd; bndind = graph->bndind; label = graph->label; for (i=0; i<nbnd; i++) order[label[bndind[i]]] = --lastvtx; SplitGraphOrder(ctrl, graph, &lgraph, &rgraph); /* Free the memory of the top level graph */ FreeGraph(&graph); /* Recurse on lgraph first, as its lastvtx depends on rgraph->nvtxs, which will not be defined upon return from MlevelNestedDissection. */ if (lgraph->nvtxs > MMDSWITCH && lgraph->nedges > 0) MlevelNestedDissection(ctrl, lgraph, order, lastvtx-rgraph->nvtxs); else { MMDOrder(ctrl, lgraph, order, lastvtx-rgraph->nvtxs); FreeGraph(&lgraph); } if (rgraph->nvtxs > MMDSWITCH && rgraph->nedges > 0) MlevelNestedDissection(ctrl, rgraph, order, lastvtx); else { MMDOrder(ctrl, rgraph, order, lastvtx); FreeGraph(&rgraph); } }
/************************************************************************* * This function takes a graph and produces a bisection of it **************************************************************************/ void MlevelNestedDissectionP(CtrlType *ctrl, GraphType *graph, idxtype *order, int lastvtx, int npes, int cpos, idxtype *sizes) { int i, j, nvtxs, nbnd, tvwgt, tpwgts2[2]; idxtype *label, *bndind; GraphType lgraph, rgraph; float ubfactor; nvtxs = graph->nvtxs; if (nvtxs == 0) { GKfree((void**)&graph->gdata, &graph->rdata, &graph->label, LTERM); return; } /* Determine the weights of the partitions */ tvwgt = idxsum(nvtxs, graph->vwgt); tpwgts2[0] = tvwgt/2; tpwgts2[1] = tvwgt-tpwgts2[0]; if (cpos >= npes-1) ubfactor = ORDER_UNBALANCE_FRACTION; else ubfactor = 1.05; MlevelNodeBisectionMultiple(ctrl, graph, tpwgts2, ubfactor); IFSET(ctrl->dbglvl, DBG_SEPINFO, printf("Nvtxs: %6d, [%6d %6d %6d]\n", graph->nvtxs, graph->pwgts[0], graph->pwgts[1], graph->pwgts[2])); if (cpos < npes-1) { sizes[2*npes-2-cpos] = graph->pwgts[2]; sizes[2*npes-2-(2*cpos+1)] = graph->pwgts[1]; sizes[2*npes-2-(2*cpos+2)] = graph->pwgts[0]; } /* Order the nodes in the separator */ nbnd = graph->nbnd; bndind = graph->bndind; label = graph->label; for (i=0; i<nbnd; i++) order[label[bndind[i]]] = --lastvtx; SplitGraphOrder(ctrl, graph, &lgraph, &rgraph); /* Free the memory of the top level graph */ GKfree((void**)&graph->gdata, &graph->rdata, &graph->label, LTERM); if (rgraph.nvtxs > MMDSWITCH || 2*cpos+1 < npes-1) MlevelNestedDissectionP(ctrl, &rgraph, order, lastvtx, npes, 2*cpos+1, sizes); else { MMDOrder(ctrl, &rgraph, order, lastvtx); GKfree((void**)&rgraph.gdata, &rgraph.rdata, &rgraph.label, LTERM); } if (lgraph.nvtxs > MMDSWITCH || 2*cpos+2 < npes-1) MlevelNestedDissectionP(ctrl, &lgraph, order, lastvtx-rgraph.nvtxs, npes, 2*cpos+2, sizes); else { MMDOrder(ctrl, &lgraph, order, lastvtx-rgraph.nvtxs); GKfree((void**)&lgraph.gdata, &lgraph.rdata, &lgraph.label, LTERM); } }
/************************************************************************** * 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]; }