/************************************************************************* * 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 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); }
/************************************************************************* * This function partitions a finite element mesh by partitioning its nodal * graph using KMETIS and then assigning elements in a load balanced fashion. **************************************************************************/ void METIS_PartMeshNodal(int *ne, int *nn, idxtype *elmnts, int *etype, int *numflag, int *nparts, int *edgecut, idxtype *epart, idxtype *npart) { int i, j, k, me; idxtype *xadj, *adjncy, *pwgts; int options[10], pnumflag=0, wgtflag=0; int nnbrs, nbrind[200], nbrwgt[200], maxpwgt; int esize, esizes[] = {-1, 3, 4, 8, 4}; esize = esizes[*etype]; if (*numflag == 1) ChangeMesh2CNumbering((*ne)*esize, elmnts); xadj = idxmalloc(*nn+1, "METIS_MESHPARTNODAL: xadj"); adjncy = idxmalloc(20*(*nn), "METIS_MESHPARTNODAL: adjncy"); METIS_MeshToNodal(ne, nn, elmnts, etype, &pnumflag, xadj, adjncy); adjncy = realloc(adjncy, xadj[*nn]*sizeof(idxtype)); options[0] = 0; METIS_PartGraphKway(nn, xadj, adjncy, NULL, NULL, &wgtflag, &pnumflag, nparts, options, edgecut, npart); /* OK, now compute an element partition based on the nodal partition npart */ idxset(*ne, -1, epart); pwgts = idxsmalloc(*nparts, 0, "METIS_MESHPARTNODAL: pwgts"); for (i=0; i<*ne; i++) { me = npart[elmnts[i*esize]]; for (j=1; j<esize; j++) { if (npart[elmnts[i*esize+j]] != me) break; } if (j == esize) { epart[i] = me; pwgts[me]++; } } maxpwgt = 1.03*(*ne)/(*nparts); for (i=0; i<*ne; i++) { if (epart[i] == -1) { /* Assign the boundary element */ nnbrs = 0; for (j=0; j<esize; j++) { me = npart[elmnts[i*esize+j]]; for (k=0; k<nnbrs; k++) { if (nbrind[k] == me) { nbrwgt[k]++; break; } } if (k == nnbrs) { nbrind[nnbrs] = me; nbrwgt[nnbrs++] = 1; } } /* Try to assign it first to the domain with most things in common */ j = iamax(nnbrs, nbrwgt); if (pwgts[nbrind[j]] < maxpwgt) { epart[i] = nbrind[j]; } else { /* If that fails, assign it to a light domain */ for (j=0; j<nnbrs; j++) { if (pwgts[nbrind[j]] < maxpwgt) { epart[i] = nbrind[j]; break; } } if (j == nnbrs) epart[i] = nbrind[iamax(nnbrs, nbrwgt)]; } pwgts[epart[i]]++; } } if (*numflag == 1) ChangeMesh2FNumbering2((*ne)*esize, elmnts, *ne, *nn, epart, npart); /*GKfree(&xadj, &adjncy, &pwgts, LTERM);*/ GKfree3((void**)&xadj, (void**)&adjncy, (void **)&pwgts); }
/************************************************************************* * This function compresses a graph by merging identical vertices * The compression should lead to at least 10% reduction. **************************************************************************/ void CompressGraph(CtrlType *ctrl, GraphType *graph, int nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *cptr, idxtype *cind) { int i, ii, iii, j, jj, k, l, cnvtxs, cnedges; idxtype *cxadj, *cadjncy, *cvwgt, *mark, *map; KeyValueType *keys; mark = idxsmalloc(nvtxs, -1, "CompressGraph: mark"); map = idxsmalloc(nvtxs, -1, "CompressGraph: map"); keys = (KeyValueType *)GKmalloc(nvtxs*sizeof(KeyValueType), "CompressGraph: keys"); /* Compute a key for each adjacency list */ for (i=0; i<nvtxs; i++) { k = 0; for (j=xadj[i]; j<xadj[i+1]; j++) k += adjncy[j]; keys[i].key = k+i; /* Add the diagonal entry as well */ keys[i].val = i; } ikeysort(nvtxs, keys); l = cptr[0] = 0; for (cnvtxs=i=0; i<nvtxs; i++) { ii = keys[i].val; if (map[ii] == -1) { mark[ii] = i; /* Add the diagonal entry */ for (j=xadj[ii]; j<xadj[ii+1]; j++) mark[adjncy[j]] = i; cind[l++] = ii; map[ii] = cnvtxs; for (j=i+1; j<nvtxs; j++) { iii = keys[j].val; if (keys[i].key != keys[j].key || xadj[ii+1]-xadj[ii] != xadj[iii+1]-xadj[iii]) break; /* Break if keys or degrees are different */ if (map[iii] == -1) { /* Do a comparison if iii has not been mapped */ for (jj=xadj[iii]; jj<xadj[iii+1]; jj++) { if (mark[adjncy[jj]] != i) break; } if (jj == xadj[iii+1]) { /* Identical adjacency structure */ map[iii] = cnvtxs; cind[l++] = iii; } } } cptr[++cnvtxs] = l; } } /* printf("Original: %6d, Compressed: %6d\n", nvtxs, cnvtxs); */ InitGraph(graph); if (cnvtxs >= COMPRESSION_FRACTION*nvtxs) { graph->nvtxs = nvtxs; graph->nedges = xadj[nvtxs]; graph->ncon = 1; graph->xadj = xadj; graph->adjncy = adjncy; graph->gdata = idxmalloc(3*nvtxs+graph->nedges, "CompressGraph: gdata"); graph->vwgt = graph->gdata; graph->adjwgtsum = graph->gdata+nvtxs; graph->cmap = graph->gdata+2*nvtxs; graph->adjwgt = graph->gdata+3*nvtxs; idxset(nvtxs, 1, graph->vwgt); idxset(graph->nedges, 1, graph->adjwgt); for (i=0; i<nvtxs; i++) graph->adjwgtsum[i] = xadj[i+1]-xadj[i]; graph->label = idxmalloc(nvtxs, "CompressGraph: label"); for (i=0; i<nvtxs; i++) graph->label[i] = i; } else { /* Ok, form the compressed graph */ cnedges = 0; for (i=0; i<cnvtxs; i++) { ii = cind[cptr[i]]; cnedges += xadj[ii+1]-xadj[ii]; } /* Allocate memory for the compressed graph*/ graph->gdata = idxmalloc(4*cnvtxs+1 + 2*cnedges, "CompressGraph: gdata"); cxadj = graph->xadj = graph->gdata; cvwgt = graph->vwgt = graph->gdata + cnvtxs+1; graph->adjwgtsum = graph->gdata + 2*cnvtxs+1; graph->cmap = graph->gdata + 3*cnvtxs+1; cadjncy = graph->adjncy = graph->gdata + 4*cnvtxs+1; graph->adjwgt = graph->gdata + 4*cnvtxs+1 + cnedges; /* Now go and compress the graph */ idxset(nvtxs, -1, mark); l = cxadj[0] = 0; for (i=0; i<cnvtxs; i++) { cvwgt[i] = cptr[i+1]-cptr[i]; mark[i] = i; /* Remove any dioganal entries in the compressed graph */ for (j=cptr[i]; j<cptr[i+1]; j++) { ii = cind[j]; for (jj=xadj[ii]; jj<xadj[ii+1]; jj++) { k = map[adjncy[jj]]; if (mark[k] != i) cadjncy[l++] = k; mark[k] = i; } } cxadj[i+1] = l; } graph->nvtxs = cnvtxs; graph->nedges = l; graph->ncon = 1; idxset(graph->nedges, 1, graph->adjwgt); for (i=0; i<cnvtxs; i++) graph->adjwgtsum[i] = cxadj[i+1]-cxadj[i]; graph->label = idxmalloc(cnvtxs, "CompressGraph: label"); for (i=0; i<cnvtxs; i++) graph->label[i] = i; } /*GKfree(&keys, &map, &mark, LTERM);*/ GKfree3((void**)&keys, (void**)&map, (void**)&mark); }