1 | /* |
---|
2 | * Copyright 1997, Regents of the University of Minnesota |
---|
3 | * |
---|
4 | * kmetis.c |
---|
5 | * |
---|
6 | * This file contains the top level routines for the multilevel k-way partitioning |
---|
7 | * algorithm KMETIS. |
---|
8 | * |
---|
9 | * Started 7/28/97 |
---|
10 | * George |
---|
11 | * |
---|
12 | * $Id: kmetis.c,v 1.1 1998/11/27 17:59:15 karypis Exp $ |
---|
13 | * |
---|
14 | */ |
---|
15 | |
---|
16 | #include <metis.h> |
---|
17 | |
---|
18 | |
---|
19 | /************************************************************************* |
---|
20 | * This function is the entry point for KMETIS |
---|
21 | **************************************************************************/ |
---|
22 | void METIS_PartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, |
---|
23 | idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, |
---|
24 | int *options, int *edgecut, idxtype *part) |
---|
25 | { |
---|
26 | int i; |
---|
27 | float *tpwgts; |
---|
28 | |
---|
29 | tpwgts = fmalloc(*nparts, "KMETIS: tpwgts"); |
---|
30 | for (i=0; i<*nparts; i++) |
---|
31 | tpwgts[i] = 1.0/(1.0*(*nparts)); |
---|
32 | |
---|
33 | METIS_WPartGraphKway(nvtxs, xadj, adjncy, vwgt, adjwgt, wgtflag, numflag, nparts, |
---|
34 | tpwgts, options, edgecut, part); |
---|
35 | |
---|
36 | free(tpwgts); |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | /************************************************************************* |
---|
41 | * This function is the entry point for KWMETIS |
---|
42 | **************************************************************************/ |
---|
43 | void METIS_WPartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, |
---|
44 | idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, |
---|
45 | float *tpwgts, int *options, int *edgecut, idxtype *part) |
---|
46 | { |
---|
47 | int i, j; |
---|
48 | GraphType graph; |
---|
49 | CtrlType ctrl; |
---|
50 | |
---|
51 | if (*numflag == 1) |
---|
52 | Change2CNumbering(*nvtxs, xadj, adjncy); |
---|
53 | |
---|
54 | SetUpGraph(&graph, OP_KMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, *wgtflag); |
---|
55 | |
---|
56 | if (options[0] == 0) { /* Use the default parameters */ |
---|
57 | ctrl.CType = KMETIS_CTYPE; |
---|
58 | ctrl.IType = KMETIS_ITYPE; |
---|
59 | ctrl.RType = KMETIS_RTYPE; |
---|
60 | ctrl.dbglvl = KMETIS_DBGLVL; |
---|
61 | } |
---|
62 | else { |
---|
63 | ctrl.CType = options[OPTION_CTYPE]; |
---|
64 | ctrl.IType = options[OPTION_ITYPE]; |
---|
65 | ctrl.RType = options[OPTION_RTYPE]; |
---|
66 | ctrl.dbglvl = options[OPTION_DBGLVL]; |
---|
67 | } |
---|
68 | ctrl.optype = OP_KMETIS; |
---|
69 | ctrl.CoarsenTo = amax((*nvtxs)/(40*log2(*nparts)), 20*(*nparts)); |
---|
70 | ctrl.maxvwgt = 1.5*((graph.vwgt ? idxsum(*nvtxs, graph.vwgt) : (*nvtxs))/ctrl.CoarsenTo); |
---|
71 | |
---|
72 | InitRandom(-1); |
---|
73 | |
---|
74 | AllocateWorkSpace(&ctrl, &graph, *nparts); |
---|
75 | |
---|
76 | IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl)); |
---|
77 | IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr)); |
---|
78 | |
---|
79 | *edgecut = MlevelKWayPartitioning(&ctrl, &graph, *nparts, part, tpwgts, 1.03); |
---|
80 | |
---|
81 | IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr)); |
---|
82 | IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl)); |
---|
83 | |
---|
84 | FreeWorkSpace(&ctrl, &graph); |
---|
85 | |
---|
86 | if (*numflag == 1) |
---|
87 | Change2FNumbering(*nvtxs, xadj, adjncy, part); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | /************************************************************************* |
---|
92 | * This function takes a graph and produces a bisection of it |
---|
93 | **************************************************************************/ |
---|
94 | int MlevelKWayPartitioning(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *part, float *tpwgts, float ubfactor) |
---|
95 | { |
---|
96 | int i, j, nvtxs, tvwgt, tpwgts2[2]; |
---|
97 | GraphType *cgraph; |
---|
98 | int wgtflag=3, numflag=0, options[10], edgecut; |
---|
99 | |
---|
100 | cgraph = Coarsen2Way(ctrl, graph); |
---|
101 | |
---|
102 | IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->InitPartTmr)); |
---|
103 | AllocateKWayPartitionMemory(ctrl, cgraph, nparts); |
---|
104 | |
---|
105 | options[0] = 1; |
---|
106 | options[OPTION_CTYPE] = MATCH_SHEMKWAY; |
---|
107 | options[OPTION_ITYPE] = IPART_GGPKL; |
---|
108 | options[OPTION_RTYPE] = RTYPE_FM; |
---|
109 | options[OPTION_DBGLVL] = 0; |
---|
110 | |
---|
111 | METIS_WPartGraphRecursive(&cgraph->nvtxs, cgraph->xadj, cgraph->adjncy, cgraph->vwgt, |
---|
112 | cgraph->adjwgt, &wgtflag, &numflag, &nparts, tpwgts, options, |
---|
113 | &edgecut, cgraph->where); |
---|
114 | |
---|
115 | IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->InitPartTmr)); |
---|
116 | IFSET(ctrl->dbglvl, DBG_IPART, printf("Initial %d-way partitioning cut: %d\n", nparts, edgecut)); |
---|
117 | |
---|
118 | IFSET(ctrl->dbglvl, DBG_KWAYPINFO, ComputePartitionInfo(cgraph, nparts, cgraph->where)); |
---|
119 | |
---|
120 | RefineKWay(ctrl, graph, cgraph, nparts, tpwgts, ubfactor); |
---|
121 | |
---|
122 | idxcopy(graph->nvtxs, graph->where, part); |
---|
123 | |
---|
124 | GKfree(&graph->gdata, &graph->rdata, LTERM); |
---|
125 | |
---|
126 | return graph->mincut; |
---|
127 | |
---|
128 | } |
---|
129 | |
---|