PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/external/dmlt/external/gpstuff/SuiteSparse/metis-4.0/Lib/pmetis.c

https://bitbucket.org/palday/fieldtrip
C | 341 lines | 223 code | 69 blank | 49 comment | 37 complexity | f141b16ca8f7a461e96749a4ffc89086 MD5 | raw file
  1. /*
  2. * Copyright 1997, Regents of the University of Minnesota
  3. *
  4. * pmetis.c
  5. *
  6. * This file contains the top level routines for the multilevel recursive
  7. * bisection algorithm PMETIS.
  8. *
  9. * Started 7/24/97
  10. * George
  11. *
  12. * $Id: pmetis.c,v 1.1 1998/11/27 17:59:28 karypis Exp $
  13. *
  14. */
  15. #include <metis.h>
  16. /*************************************************************************
  17. * This function is the entry point for PMETIS
  18. **************************************************************************/
  19. void METIS_PartGraphRecursive(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
  20. idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts,
  21. int *options, int *edgecut, idxtype *part)
  22. {
  23. int i;
  24. float *tpwgts;
  25. tpwgts = fmalloc(*nparts, "KMETIS: tpwgts");
  26. for (i=0; i<*nparts; i++)
  27. tpwgts[i] = 1.0/(1.0*(*nparts));
  28. METIS_WPartGraphRecursive(nvtxs, xadj, adjncy, vwgt, adjwgt, wgtflag, numflag, nparts,
  29. tpwgts, options, edgecut, part);
  30. free(tpwgts);
  31. }
  32. /*************************************************************************
  33. * This function is the entry point for PWMETIS that accepts exact weights
  34. * for the target partitions
  35. **************************************************************************/
  36. void METIS_WPartGraphRecursive(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
  37. idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts,
  38. float *tpwgts, int *options, int *edgecut, idxtype *part)
  39. {
  40. int i, j;
  41. GraphType graph;
  42. CtrlType ctrl;
  43. float *mytpwgts;
  44. if (*numflag == 1)
  45. Change2CNumbering(*nvtxs, xadj, adjncy);
  46. SetUpGraph(&graph, OP_PMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, *wgtflag);
  47. if (options[0] == 0) { /* Use the default parameters */
  48. ctrl.CType = PMETIS_CTYPE;
  49. ctrl.IType = PMETIS_ITYPE;
  50. ctrl.RType = PMETIS_RTYPE;
  51. ctrl.dbglvl = PMETIS_DBGLVL;
  52. }
  53. else {
  54. ctrl.CType = options[OPTION_CTYPE];
  55. ctrl.IType = options[OPTION_ITYPE];
  56. ctrl.RType = options[OPTION_RTYPE];
  57. ctrl.dbglvl = options[OPTION_DBGLVL];
  58. }
  59. ctrl.optype = OP_PMETIS;
  60. ctrl.CoarsenTo = 20;
  61. ctrl.maxvwgt = 1.5*(idxsum(*nvtxs, graph.vwgt)/ctrl.CoarsenTo);
  62. mytpwgts = fmalloc(*nparts, "PWMETIS: mytpwgts");
  63. for (i=0; i<*nparts; i++)
  64. mytpwgts[i] = tpwgts[i];
  65. InitRandom(-1);
  66. AllocateWorkSpace(&ctrl, &graph, *nparts);
  67. IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl));
  68. IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr));
  69. *edgecut = MlevelRecursiveBisection(&ctrl, &graph, *nparts, part, mytpwgts, 1.000, 0);
  70. IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr));
  71. IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl));
  72. FreeWorkSpace(&ctrl, &graph);
  73. free(mytpwgts);
  74. if (*numflag == 1)
  75. Change2FNumbering(*nvtxs, xadj, adjncy, part);
  76. }
  77. /*************************************************************************
  78. * This function takes a graph and produces a bisection of it
  79. **************************************************************************/
  80. int MlevelRecursiveBisection(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *part, float *tpwgts, float ubfactor, int fpart)
  81. {
  82. int i, j, nvtxs, cut, tvwgt, tpwgts2[2];
  83. idxtype *label, *where;
  84. GraphType lgraph, rgraph;
  85. float wsum;
  86. nvtxs = graph->nvtxs;
  87. if (nvtxs == 0) {
  88. printf("\t***Cannot bisect a graph with 0 vertices!\n\t***You are trying to partition a graph into too many parts!\n");
  89. return 0;
  90. }
  91. /* Determine the weights of the partitions */
  92. tvwgt = idxsum(nvtxs, graph->vwgt);
  93. tpwgts2[0] = tvwgt*ssum(nparts/2, tpwgts);
  94. tpwgts2[1] = tvwgt-tpwgts2[0];
  95. MlevelEdgeBisection(ctrl, graph, tpwgts2, ubfactor);
  96. cut = graph->mincut;
  97. /* printf("%5d %5d %5d [%5d %f]\n", tpwgts2[0], tpwgts2[1], cut, tvwgt, ssum(nparts/2, tpwgts));*/
  98. label = graph->label;
  99. where = graph->where;
  100. for (i=0; i<nvtxs; i++)
  101. part[label[i]] = where[i] + fpart;
  102. if (nparts > 2) {
  103. SplitGraphPart(ctrl, graph, &lgraph, &rgraph);
  104. /* printf("%d %d\n", lgraph.nvtxs, rgraph.nvtxs); */
  105. }
  106. /* Free the memory of the top level graph */
  107. GKfree(&graph->gdata, &graph->rdata, &graph->label, LTERM);
  108. /* Scale the fractions in the tpwgts according to the true weight */
  109. wsum = ssum(nparts/2, tpwgts);
  110. sscale(nparts/2, 1.0/wsum, tpwgts);
  111. sscale(nparts-nparts/2, 1.0/(1.0-wsum), tpwgts+nparts/2);
  112. /*
  113. for (i=0; i<nparts; i++)
  114. printf("%5.3f ", tpwgts[i]);
  115. printf("[%5.3f]\n", wsum);
  116. */
  117. /* Do the recursive call */
  118. if (nparts > 3) {
  119. cut += MlevelRecursiveBisection(ctrl, &lgraph, nparts/2, part, tpwgts, ubfactor, fpart);
  120. cut += MlevelRecursiveBisection(ctrl, &rgraph, nparts-nparts/2, part, tpwgts+nparts/2, ubfactor, fpart+nparts/2);
  121. }
  122. else if (nparts == 3) {
  123. cut += MlevelRecursiveBisection(ctrl, &rgraph, nparts-nparts/2, part, tpwgts+nparts/2, ubfactor, fpart+nparts/2);
  124. GKfree(&lgraph.gdata, &lgraph.label, LTERM);
  125. }
  126. return cut;
  127. }
  128. /*************************************************************************
  129. * This function performs multilevel bisection
  130. **************************************************************************/
  131. void MlevelEdgeBisection(CtrlType *ctrl, GraphType *graph, int *tpwgts, float ubfactor)
  132. {
  133. GraphType *cgraph;
  134. cgraph = Coarsen2Way(ctrl, graph);
  135. Init2WayPartition(ctrl, cgraph, tpwgts, ubfactor);
  136. Refine2Way(ctrl, graph, cgraph, tpwgts, ubfactor);
  137. /*
  138. IsConnectedSubdomain(ctrl, graph, 0);
  139. IsConnectedSubdomain(ctrl, graph, 1);
  140. */
  141. }
  142. /*************************************************************************
  143. * This function takes a graph and a bisection and splits it into two graphs.
  144. **************************************************************************/
  145. void SplitGraphPart(CtrlType *ctrl, GraphType *graph, GraphType *lgraph, GraphType *rgraph)
  146. {
  147. int i, j, k, kk, l, istart, iend, mypart, nvtxs, ncon, snvtxs[2], snedges[2], sum;
  148. idxtype *xadj, *vwgt, *adjncy, *adjwgt, *adjwgtsum, *label, *where, *bndptr;
  149. idxtype *sxadj[2], *svwgt[2], *sadjncy[2], *sadjwgt[2], *sadjwgtsum[2], *slabel[2];
  150. idxtype *rename;
  151. idxtype *auxadjncy, *auxadjwgt;
  152. float *nvwgt, *snvwgt[2], *npwgts;
  153. IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->SplitTmr));
  154. nvtxs = graph->nvtxs;
  155. ncon = graph->ncon;
  156. xadj = graph->xadj;
  157. vwgt = graph->vwgt;
  158. nvwgt = graph->nvwgt;
  159. adjncy = graph->adjncy;
  160. adjwgt = graph->adjwgt;
  161. adjwgtsum = graph->adjwgtsum;
  162. label = graph->label;
  163. where = graph->where;
  164. bndptr = graph->bndptr;
  165. npwgts = graph->npwgts;
  166. ASSERT(bndptr != NULL);
  167. rename = idxwspacemalloc(ctrl, nvtxs);
  168. snvtxs[0] = snvtxs[1] = snedges[0] = snedges[1] = 0;
  169. for (i=0; i<nvtxs; i++) {
  170. k = where[i];
  171. rename[i] = snvtxs[k]++;
  172. snedges[k] += xadj[i+1]-xadj[i];
  173. }
  174. SetUpSplitGraph(graph, lgraph, snvtxs[0], snedges[0]);
  175. sxadj[0] = lgraph->xadj;
  176. svwgt[0] = lgraph->vwgt;
  177. snvwgt[0] = lgraph->nvwgt;
  178. sadjwgtsum[0] = lgraph->adjwgtsum;
  179. sadjncy[0] = lgraph->adjncy;
  180. sadjwgt[0] = lgraph->adjwgt;
  181. slabel[0] = lgraph->label;
  182. SetUpSplitGraph(graph, rgraph, snvtxs[1], snedges[1]);
  183. sxadj[1] = rgraph->xadj;
  184. svwgt[1] = rgraph->vwgt;
  185. snvwgt[1] = rgraph->nvwgt;
  186. sadjwgtsum[1] = rgraph->adjwgtsum;
  187. sadjncy[1] = rgraph->adjncy;
  188. sadjwgt[1] = rgraph->adjwgt;
  189. slabel[1] = rgraph->label;
  190. snvtxs[0] = snvtxs[1] = snedges[0] = snedges[1] = 0;
  191. sxadj[0][0] = sxadj[1][0] = 0;
  192. for (i=0; i<nvtxs; i++) {
  193. mypart = where[i];
  194. sum = adjwgtsum[i];
  195. istart = xadj[i];
  196. iend = xadj[i+1];
  197. if (bndptr[i] == -1) { /* This is an interior vertex */
  198. auxadjncy = sadjncy[mypart] + snedges[mypart] - istart;
  199. auxadjwgt = sadjwgt[mypart] + snedges[mypart] - istart;
  200. for(j=istart; j<iend; j++) {
  201. auxadjncy[j] = adjncy[j];
  202. auxadjwgt[j] = adjwgt[j];
  203. }
  204. snedges[mypart] += iend-istart;
  205. }
  206. else {
  207. auxadjncy = sadjncy[mypart];
  208. auxadjwgt = sadjwgt[mypart];
  209. l = snedges[mypart];
  210. for (j=istart; j<iend; j++) {
  211. k = adjncy[j];
  212. if (where[k] == mypart) {
  213. auxadjncy[l] = k;
  214. auxadjwgt[l++] = adjwgt[j];
  215. }
  216. else {
  217. sum -= adjwgt[j];
  218. }
  219. }
  220. snedges[mypart] = l;
  221. }
  222. if (ncon == 1)
  223. svwgt[mypart][snvtxs[mypart]] = vwgt[i];
  224. else {
  225. for (kk=0; kk<ncon; kk++)
  226. snvwgt[mypart][snvtxs[mypart]*ncon+kk] = nvwgt[i*ncon+kk]/npwgts[mypart*ncon+kk];
  227. }
  228. sadjwgtsum[mypart][snvtxs[mypart]] = sum;
  229. slabel[mypart][snvtxs[mypart]] = label[i];
  230. sxadj[mypart][++snvtxs[mypart]] = snedges[mypart];
  231. }
  232. for (mypart=0; mypart<2; mypart++) {
  233. iend = sxadj[mypart][snvtxs[mypart]];
  234. auxadjncy = sadjncy[mypart];
  235. for (i=0; i<iend; i++)
  236. auxadjncy[i] = rename[auxadjncy[i]];
  237. }
  238. lgraph->nedges = snedges[0];
  239. rgraph->nedges = snedges[1];
  240. IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->SplitTmr));
  241. idxwspacefree(ctrl, nvtxs);
  242. }
  243. /*************************************************************************
  244. * Setup the various arrays for the splitted graph
  245. **************************************************************************/
  246. void SetUpSplitGraph(GraphType *graph, GraphType *sgraph, int snvtxs, int snedges)
  247. {
  248. InitGraph(sgraph);
  249. sgraph->nvtxs = snvtxs;
  250. sgraph->nedges = snedges;
  251. sgraph->ncon = graph->ncon;
  252. /* Allocate memory for the splitted graph */
  253. if (graph->ncon == 1) {
  254. sgraph->gdata = idxmalloc(4*snvtxs+1 + 2*snedges, "SetUpSplitGraph: gdata");
  255. sgraph->xadj = sgraph->gdata;
  256. sgraph->vwgt = sgraph->gdata + snvtxs+1;
  257. sgraph->adjwgtsum = sgraph->gdata + 2*snvtxs+1;
  258. sgraph->cmap = sgraph->gdata + 3*snvtxs+1;
  259. sgraph->adjncy = sgraph->gdata + 4*snvtxs+1;
  260. sgraph->adjwgt = sgraph->gdata + 4*snvtxs+1 + snedges;
  261. }
  262. else {
  263. sgraph->gdata = idxmalloc(3*snvtxs+1 + 2*snedges, "SetUpSplitGraph: gdata");
  264. sgraph->xadj = sgraph->gdata;
  265. sgraph->adjwgtsum = sgraph->gdata + snvtxs+1;
  266. sgraph->cmap = sgraph->gdata + 2*snvtxs+1;
  267. sgraph->adjncy = sgraph->gdata + 3*snvtxs+1;
  268. sgraph->adjwgt = sgraph->gdata + 3*snvtxs+1 + snedges;
  269. sgraph->nvwgt = fmalloc(graph->ncon*snvtxs, "SetUpSplitGraph: nvwgt");
  270. }
  271. sgraph->label = idxmalloc(snvtxs, "SetUpSplitGraph: sgraph->label");
  272. }