ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
mlkkm.c
Go to the documentation of this file.
1 /*
2  * Copyright 2005,
3  *
4  * mlkkm.c
5  *
6  * This file contains the top level routines for the multilevel kernel k-means algorithm
7  *
8  *
9  * Started 12/2004
10  * Yuqiang Guan
11  *
12  * $Id: kmetis.c,v 1.1 1998/11/27 17:59:15 karypis Exp $
13  *
14  */
15 
16 #include "metisLib/metis.h"
17 
18 extern int spectral_initialization;
19 
20 /*************************************************************************
21 * This function is the entry point for MLKKM
22 **************************************************************************/
23 void MLKKM_PartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
24  idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *chainlength,
25  int *options, int *edgecut, idxtype *part, int levels)
26 {
27  int i;
28  float *tpwgts;
29 
30  tpwgts = fmalloc(*nparts, "MLKKM: tpwgts");
31  for (i=0; i<*nparts; i++)
32  tpwgts[i] = 1.0/(1.0*(*nparts));
33 
34  MLKKM_WPartGraphKway(nvtxs, xadj, adjncy, vwgt, adjwgt, wgtflag, numflag, nparts, chainlength,
35  tpwgts, options, edgecut, part, levels);
36 
37  free(tpwgts);
38 }
39 
40 
41 /*************************************************************************
42 * This function is the entry point for KWMETIS
43 **************************************************************************/
44 void MLKKM_WPartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt,
45  idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *chainlength,
46  float *tpwgts, int *options, int *edgecut, idxtype *part, int levels)
47 {
48  int i, j;
49  GraphType graph;
50  CtrlType ctrl;
51 
52  if (*numflag == 1)
53  Change2CNumbering(*nvtxs, xadj, adjncy);
54 
55  SetUpGraph(&graph, OP_KMETIS, *nvtxs, 1, xadj, adjncy, vwgt, adjwgt, *wgtflag);
56 
57  if (options[0] == 0) { /* Use the default parameters */
58  ctrl.CType = KMETIS_CTYPE;
59  ctrl.IType = KMETIS_ITYPE;
60  ctrl.RType = KMETIS_RTYPE;
61  ctrl.dbglvl = KMETIS_DBGLVL;
62  //ctrl.cutType = options[10];
63  }
64  else {
65  ctrl.CType = options[OPTION_CTYPE];
66  ctrl.IType = options[OPTION_ITYPE];
67  ctrl.RType = options[OPTION_RTYPE];
68  ctrl.dbglvl = options[OPTION_DBGLVL];
69  //ctrl.cutType = options[10];
70  }
71  ctrl.optype = OP_KMETIS;
72  //ctrl.CoarsenTo = amax((*nvtxs)/(40*log2_metis(*nparts)), 5*(*nparts));
73  ctrl.CoarsenTo = levels;
74  //ctrl.CoarsenTo = amax(40*log2_metis(*nparts), 20*(*nparts));
75  //printf("Coarsen To = %d\n", ctrl.CoarsenTo);
76  ctrl.maxvwgt = floor(1.5*((graph.vwgt ? idxsum(*nvtxs, graph.vwgt) : (*nvtxs))/ctrl.CoarsenTo));
77  ctrl.maxvwgt *= 100;
78  InitRandom(-1);
79 
80  AllocateWorkSpace(&ctrl, &graph, *nparts);
81 
82  IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl));
83  IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr));
84 
85  *edgecut = MLKKMPartitioning(&ctrl, &graph, *nparts, *chainlength, part, tpwgts, 1.03);
86  /*
87  graph.where = idxsmalloc(graph.nvtxs, 0, "Weighted_kernel_k_means: where");
88  graph.where[0]=graph.where[1]=graph.where[2]=graph.where[3]=0;
89  graph.where[4]=graph.where[5]=graph.where[6]=1;
90  Weighted_kernel_k_means(&ctrl, &graph, *nparts, tpwgts, 1.03);
91  */
92 
93  //idxcopy(graph.nvtxs, graph.where, part);
94 
95  IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr));
96  IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl));
97 
98  FreeWorkSpace(&ctrl, &graph);
99 
100  if (*numflag == 1)
101  Change2FNumbering(*nvtxs, xadj, adjncy, part);
102 }
103 
104 /************************************************************************
105 * This function takes a graph and produces a k-way partitioning of it
106 **************************************************************************/
107 /*void spectralInit(GraphType * graph, int nparts, int *numflag, int* options)
108 {
109  int i, j, nvtxs, nedges;
110  idxtype *adjwgt, *adjncy, *xadj, *where;
111  spectral::Driver* d = new spectral::Driver();
112  CtrlType ctrl;
113  double * dense;
114  idxtype *w;
115  float *m_adjwgt;
116 
117  nvtxs = graph->nvtxs;
118  nedges = graph->nedges;
119  xadj = graph->xadj;
120  adjncy = graph->adjncy;
121  adjwgt = graph->adjwgt;
122  where = graph->where;
123 
124  if (*numflag == 1)
125  Change2CNumbering(nvtxs, xadj, adjncy);
126 
127  if (options[0] == 0) { // Use the default parameters
128  ctrl.CType = KMETIS_CTYPE;
129  ctrl.IType = KMETIS_ITYPE;
130  ctrl.RType = KMETIS_RTYPE;
131  ctrl.dbglvl = KMETIS_DBGLVL;
132  }
133  else {
134  ctrl.CType = options[OPTION_CTYPE];
135  ctrl.IType = options[OPTION_ITYPE];
136  ctrl.RType = options[OPTION_RTYPE];
137  ctrl.dbglvl = options[OPTION_DBGLVL];
138  }
139  ctrl.optype = OP_KMETIS;
140  //ctrl.CoarsenTo = amax((nvtxs)/(40*log2_metis(nparts), 20*nparts);
141  ctrl.CoarsenTo = amax((nvtxs)/(40*log2_metis(nparts)), 5*nparts);
142  ctrl.maxvwgt = (int) 1.5*(idxsum(nvtxs, graph->vwgt)/ctrl.CoarsenTo);
143  w = idxsmalloc(nvtxs, 0, "pingpong: weight");
144  Compute_Weights(&ctrl, graph, w);
145  m_adjwgt = fmalloc(nedges, "pingpong: normalized matrix");
146 
147  transform_matrix_half(&ctrl, graph, w, m_adjwgt);
148  dense = new double [nvtxs*nvtxs];
149  sparse2dense(graph, dense, m_adjwgt);
150  //printf("size: %d\n", nvtxs);
151  AllocateWorkSpace(&ctrl, graph, nparts);
152 
153  IFSET(ctrl.dbglvl, DBG_TIME, InitTimers(&ctrl));
154  IFSET(ctrl.dbglvl, DBG_TIME, starttimer(ctrl.TotalTmr));
155 
156  printf("Running spectral over %d nodes and %d clusters....\n", nvtxs, nparts);
157 
158  //d->execute2((int*)graph->xadj, (int*)graph->adjncy,(int*) graph->adjwgt, nvtxs, nedges, nparts);
159  d->execute(dense, nvtxs ,nparts);
160  d->copyClusterID(where, nvtxs);
161 
162  IFSET(ctrl.dbglvl, DBG_TIME, stoptimer(ctrl.TotalTmr));
163  IFSET(ctrl.dbglvl, DBG_TIME, PrintTimers(&ctrl));
164 
165  FreeWorkSpace(&ctrl, graph);
166  //free(dense);
167  free(w);
168  free(m_adjwgt);
169 
170  if (*numflag == 1)
171  Change2FNumbering(nvtxs, xadj, adjncy, where);
172 }
173 */
174 
175 
176 /*************************************************************************
177 * This function takes a graph and produces a k-way partitioning of it
178 **************************************************************************/
179 int MLKKMPartitioning(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, idxtype *part, float *tpwgts, float ubfactor)
180 {
181  int i, j, nvtxs, tvwgt, tpwgts2[2];
182  GraphType *cgraph;
183  int wgtflag=3, numflag=0, options[10], edgecut;
184  float ncut;
185  idxtype *cptr, *cind;
186  int numcomponents;
187  char *mlwkkm_fname = "coarse.graph";
188 
189  cptr = idxmalloc(graph->nvtxs, "MLKKMPartitioning: cptr");
190  cind = idxmalloc(graph->nvtxs, "MLKKMPartitioning: cind");
191  //printf("Computing the number of connected components.\n");
192  /*numcomponents = FindComponents(ctrl, graph, cptr, cind);
193 
194  printf("Number of connected components is %d.\n", numcomponents);
195  */
196  cgraph = Coarsen2Way(ctrl, graph);
197 
198  IFSET(ctrl->dbglvl, DBG_TIME, starttimer(ctrl->InitPartTmr));
199  AllocateKWayPartitionMemory(ctrl, cgraph, nparts);
200 
201  options[0] = 1;
202  options[OPTION_CTYPE] = MATCH_SHEMKWAY;
203  options[OPTION_ITYPE] = IPART_GGPKL;
204  options[OPTION_RTYPE] = RTYPE_FM;
205  options[OPTION_DBGLVL] = 0;
206 
207  if(spectral_initialization == 0)
208  {
209  METIS_WPartGraphRecursive(&cgraph->nvtxs, cgraph->xadj, cgraph->adjncy, cgraph->vwgt,
210  cgraph->adjwgt, &wgtflag, &numflag, &nparts, tpwgts, options,
211  &edgecut, cgraph->where);
212  }
213 // else
214 // spectralInit(cgraph, nparts, &numflag, options);
215 
216  IFSET(ctrl->dbglvl, DBG_TIME, stoptimer(ctrl->InitPartTmr));
217  IFSET(ctrl->dbglvl, DBG_IPART, printf("Initial %d-way partitioning cut: %d\n", nparts, edgecut));
218 
219  IFSET(ctrl->dbglvl, DBG_KWAYPINFO, ComputePartitionInfo(cgraph, nparts, cgraph->where));
220 
221 
222  /* modification begins */
223  /*
224  for (int i=0; i<cgraph->nvtxs; i++)
225  printf("%d ", cgraph->where[i]);
226  printf("*\n");
227  */
228 
229 
230  //WriteCoarsestGraph(cgraph, mlwkkm_fname, &wgtflag);
231  /*
232  if (cutType == NCUT)
233  strcat(mlwkkm_fname, ".iniNC");
234  else
235  strcat(mlwkkm_fname, ".iniRA");
236 
237  ReadCoarsestInit(cgraph, mlwkkm_fname, wgtflag);
238  */
239 
240  /*for random initialization
241 
242  //AllocateKWayPartitionMemory(ctrl, graph, nparts);
243  graph->where = imalloc(graph->nvtxs, "MLKKMPartitioning: where\n");
244  printf("%d \n", graph->nvtxs);
245  RandomInit(graph->nvtxs, nparts, graph->where);
246  pingpong(ctrl, graph, nparts, chain_length, tpwgts, ubfactor);
247 
248  //for random initialization ends here
249  */
250 
251  MLKKMRefine(ctrl, graph, cgraph, nparts, chain_length, tpwgts, ubfactor);
252 
253  /* modification ends */
254 
255  idxcopy(graph->nvtxs, graph->where, part);
256 
257  //ncut = ComputeNCut(graph, part, nparts);
258  //printf(" %d-way Normalized-Cut: %7f\n", nparts, ncut);
259 
260  GKfree((void **) &graph->gdata, (void **) &graph->rdata, LTERM);
261 
262  return graph->mincut;
263 
264 }
265 
#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
#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 starttimer(tmr)
Definition: macros.h:48
void MLKKM_PartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *chainlength, int *options, int *edgecut, idxtype *part, int levels)
Definition: mlkkm.c:23
void MLKKM_WPartGraphKway(int *nvtxs, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *chainlength, float *tpwgts, int *options, int *edgecut, idxtype *part, int levels)
Definition: mlkkm.c:44
int spectral_initialization
Definition: metis.c:2
int MLKKMPartitioning(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, idxtype *part, float *tpwgts, float ubfactor)
Definition: mlkkm.c:179
MiniVec< float, N > floor(const MiniVec< float, N > &a)
Definition: MiniVec.h:75
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
void MLKKMRefine(CtrlType *, GraphType *, GraphType *, int, int, float *, float)
Definition: wkkm.c:1012
#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 idxmalloc
Definition: rename.h:383
#define AllocateWorkSpace
Definition: rename.h:161
#define PrintTimers
Definition: rename.h:374
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