ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
kmetis.c
Go to the documentation of this file.
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 = (int) 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  //pingpong(ctrl, graph, nparts, 0, tpwgts, 1.03);
122 
123  idxcopy(graph->nvtxs, graph->where, part);
124 
125  GKfree((void**) &graph->gdata, (void**) &graph->rdata, LTERM);
126  /*
127  pingpong(ctrl, graph, nparts, 0, tpwgts, ubfactor);
128  ncut = ComputeNCut(graph, part, nparts);
129  printf(" %d-way Normalized-Cut: %7f, Balance: ", nparts, ncut);
130  */
131  return graph->mincut;
132 
133 }
134 
#define OPTION_ITYPE
Definition: defs.h:47
#define KMETIS_DBGLVL
Definition: defs.h:70
#define IPART_GGPKL
Definition: defs.h:128
#define KMETIS_CTYPE
Definition: defs.h:65
#define OPTION_CTYPE
Definition: defs.h:46
#define KMETIS_ITYPE
Definition: defs.h:68
#define MATCH_SHEMKWAY
Definition: defs.h:119
#define DBG_IPART
Definition: defs.h:178
#define KMETIS_RTYPE
Definition: defs.h:69
#define OPTION_RTYPE
Definition: defs.h:48
#define LTERM
Definition: defs.h:23
#define OP_KMETIS
Definition: defs.h:108
#define DBG_KWAYPINFO
Definition: defs.h:180
#define DBG_TIME
Definition: defs.h:174
#define OPTION_DBGLVL
Definition: defs.h:49
#define RTYPE_FM
Definition: defs.h:133
void METIS_PartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *options, int *edgecut, idxtype *part)
Definition: kmetis.c:22
int MlevelKWayPartitioning(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *part, float *tpwgts, float ubfactor)
Definition: kmetis.c:94
void METIS_WPartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, float *tpwgts, int *options, int *edgecut, idxtype *part)
Definition: kmetis.c:43
#define stoptimer(tmr)
Definition: macros.h:49
#define IFSET(a, flag, cmd)
Definition: macros.h:56
#define idxcopy(n, a, b)
Definition: macros.h:39
#define amax(a, b)
Definition: macros.h:24
#define starttimer(tmr)
Definition: macros.h:48
void METIS_WPartGraphRecursive(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, float *tpwgts, int *options, int *edgecut, idxtype *part)
Definition: pmetis.c:45
#define Change2CNumbering
Definition: rename.h:62
#define FreeWorkSpace
Definition: rename.h:162
#define SetUpGraph
Definition: rename.h:72
#define InitTimers
Definition: rename.h:373
#define Change2FNumbering
Definition: rename.h:63
#define GKfree
Definition: rename.h:380
#define Coarsen2Way
Definition: rename.h:34
#define InitRandom
Definition: rename.h:412
#define idxsum
Definition: rename.h:399
#define AllocateKWayPartitionMemory
Definition: rename.h:107
#define fmalloc
Definition: rename.h:384
#define ComputePartitionInfo
Definition: rename.h:356
#define AllocateWorkSpace
Definition: rename.h:161
#define PrintTimers
Definition: rename.h:374
#define RefineKWay
Definition: rename.h:106
int idxtype
Definition: struct.h:17
int dbglvl
Definition: struct.h:224
int CType
Definition: struct.h:225
timer InitPartTmr
Definition: struct.h:238
int optype
Definition: struct.h:230
int IType
Definition: struct.h:226
int maxvwgt
Definition: struct.h:228
int CoarsenTo
Definition: struct.h:223
int RType
Definition: struct.h:227
timer TotalTmr
Definition: struct.h:238
idxtype * adjncy
Definition: struct.h:172
idxtype * where
Definition: struct.h:183
idxtype * rdata
Definition: struct.h:164
int nvtxs
Definition: struct.h:168
idxtype * gdata
Definition: struct.h:164
idxtype * vwgt
Definition: struct.h:170
int mincut
Definition: struct.h:182
idxtype * adjwgt
Definition: struct.h:173
idxtype * xadj
Definition: struct.h:169