Debian Codegraph @

extern/metis/libmetis/graph.c

raw
1 /**
2 \file
3 \brief Functions that deal with setting up the graphs for METIS.
4
5 \date Started 7/25/1997
6 \author George
7 \author Copyright 1997-2009, Regents of the University of Minnesota
8 \version\verbatim $Id: graph.c 15817 2013-11-25 14:58:41Z karypis $ \endverbatim
9 */
10
11 #include "metislib.h"
12
13
14 /*************************************************************************/
15 /*! This function sets up the graph from the user input */
16 /*************************************************************************/
17 graph_t *SetupGraph(ctrl_t *ctrl, idx_t nvtxs, idx_t ncon, const idx_t *xadj,
18 const idx_t *adjncy, idx_t *vwgt, idx_t *vsize, idx_t *adjwgt)
19 {
20 idx_t i, j, k, sum;
21 real_t *nvwgt;
22 graph_t *graph;
23
24 /* allocate the graph and fill in the fields */
25 graph = CreateGraph();
26
27 graph->nvtxs = nvtxs;
28 graph->nedges = xadj[nvtxs];
29 graph->ncon = ncon;
30
31 graph->xadj_orig = xadj;
32 graph->xadj = NULL;
33 graph->free_xadj = 0;
34
35 graph->adjncy_orig = adjncy;
36 graph->adjncy = NULL;
37 graph->free_adjncy = 0;
38
39 graph->droppedewgt = 0;
40
41 /* setup the vertex weights */
42 if (vwgt) {
43 graph->vwgt = vwgt;
44 graph->free_vwgt = 0;
45 }
46 else {
47 vwgt = graph->vwgt = ismalloc(ncon*nvtxs, 1);
48 }
49
50 graph->tvwgt = imalloc(ncon);
51 graph->invtvwgt = rmalloc(ncon);
52 for (i=0; i<ncon; i++) {
53 graph->tvwgt[i] = isum(nvtxs, vwgt+i, ncon);
54 graph->invtvwgt[i] = 1.0/(graph->tvwgt[i] > 0 ? graph->tvwgt[i] : 1);
55 }
56
57
58 /* setup the edge weights */
59 if (adjwgt) {
60 graph->adjwgt = adjwgt;
61 graph->free_adjwgt = 0;
62 }
63 else {
64 adjwgt = graph->adjwgt = ismalloc(graph->nedges, 1);
65 }
66
67
68
69 /* setup various derived info */
70 SetupGraph_tvwgt(graph);
71
72 if (ctrl->optype == METIS_OP_OMETIS)
73 SetupGraph_label(graph);
74
75
76 return graph;
77 }
78
79
80 /*************************************************************************/
81 /*! Set's up the tvwgt/invtvwgt info */
82 /*************************************************************************/
83 void SetupGraph_tvwgt(graph_t *graph)
84 {
85 idx_t i;
86
87 if (graph->tvwgt == NULL)
88 graph->tvwgt = imalloc(graph->ncon);
89 if (graph->invtvwgt == NULL)
90 graph->invtvwgt = rmalloc(graph->ncon);
91
92 for (i=0; i<graph->ncon; i++) {
93 graph->tvwgt[i] = isum(graph->nvtxs, graph->vwgt+i, graph->ncon);
94 graph->invtvwgt[i] = 1.0/(graph->tvwgt[i] > 0 ? graph->tvwgt[i] : 1);
95 }
96 }
97
98
99 /*************************************************************************/
100 /*! Set's up the label info */
101 /*************************************************************************/
102 void SetupGraph_label(graph_t *graph)
103 {
104 idx_t i;
105
106 if (graph->label == NULL)
107 graph->label = imalloc(graph->nvtxs);
108
109 for (i=0; i<graph->nvtxs; i++)
110 graph->label[i] = i;
111 }
112
113
114 /*************************************************************************/
115 /*! Setup the various arrays for the split graph */
116 /*************************************************************************/
117 graph_t *SetupSplitGraph(graph_t *graph, idx_t snvtxs, idx_t snedges)
118 {
119 graph_t *sgraph;
120
121 sgraph = CreateGraph();
122
123 sgraph->nvtxs = snvtxs;
124 sgraph->nedges = snedges;
125 sgraph->ncon = graph->ncon;
126
127 /* Allocate memory for the split graph */
128 sgraph->xadj = imalloc(snvtxs+1);
129 sgraph->vwgt = imalloc(sgraph->ncon*snvtxs);
130 sgraph->adjncy = imalloc(snedges);
131 sgraph->adjwgt = imalloc(snedges);
132 sgraph->label = imalloc(snvtxs);
133 sgraph->tvwgt = imalloc(sgraph->ncon);
134 sgraph->invtvwgt = rmalloc(sgraph->ncon);
135
136 if (graph->vsize)
137 sgraph->vsize = imalloc(snvtxs);
138
139 return sgraph;
140 }
141
142
143 /*************************************************************************/
144 /*! This function creates and initializes a graph_t data structure */
145 /*************************************************************************/
146 graph_t *CreateGraph(void)
147 {
148 graph_t *graph;
149
150 graph = (graph_t *)malloc(sizeof(graph_t));
151
152 InitGraph(graph);
153
154 return graph;
155 }
156
157
158 /*************************************************************************/
159 /*! This function initializes a graph_t data structure */
160 /*************************************************************************/
161 void InitGraph(graph_t *graph)
162 {
163 memset((void *)graph, 0, sizeof(graph_t));
164
165 /* graph size constants */
166 graph->nvtxs = -1;
167 graph->nedges = -1;
168 graph->ncon = -1;
169 graph->mincut = -1;
170 graph->minvol = -1;
171 graph->nbnd = -1;
172
173 /* memory for the graph structure */
174 graph->xadj = NULL;
175 graph->vwgt = NULL;
176 graph->vsize = NULL;
177 graph->adjncy = NULL;
178 graph->adjwgt = NULL;
179 graph->label = NULL;
180 graph->cmap = NULL;
181 graph->tvwgt = NULL;
182 graph->invtvwgt = NULL;
183
184 graph->xadj_orig = NULL;
185 graph->adjncy_orig = NULL;
186
187 /* by default these are set to true, but the can be explicitly changed afterwards */
188 graph->free_xadj = 1;
189 graph->free_vwgt = 1;
190 graph->free_vsize = 1;
191 graph->free_adjncy = 1;
192 graph->free_adjwgt = 1;
193
194
195 /* memory for the partition/refinement structure */
196 graph->where = NULL;
197 graph->pwgts = NULL;
198 graph->id = NULL;
199 graph->ed = NULL;
200 graph->bndptr = NULL;
201 graph->bndind = NULL;
202 graph->nrinfo = NULL;
203
204 /* linked-list structure */
205 graph->coarser = NULL;
206 graph->finer = NULL;
207
208 }
209
210
211 /*************************************************************************/
212 /*! This function frees the memory storing the structure of the graph */
213 /*************************************************************************/
214 void FreeSData(graph_t *graph)
215 {
216 /* free graph structure */
217 if (graph->free_xadj)
218 gk_free((void **)&graph->xadj);
219 if (graph->free_vwgt)
220 gk_free((void **)&graph->vwgt);
221 if (graph->free_vsize)
222 gk_free((void **)&graph->vsize);
223 if (graph->free_adjncy)
224 gk_free((void **)&graph->adjncy);
225 if (graph->free_adjwgt)
226 gk_free((void **)&graph->adjwgt);
227 }
228
229
230 /*************************************************************************/
231 /*! This function frees the refinement/partition memory stored in a graph */
232 /*************************************************************************/
233 void FreeRData(graph_t *graph)
234 {
235 /* free partition/refinement structure */
236 gk_free((void **)&graph->where);
237 gk_free((void**)&graph->pwgts);
238 gk_free((void**)&graph->id);
239 gk_free((void**)&graph->ed);
240 gk_free((void**)&graph->bndptr);
241 gk_free((void**)&graph->bndind);
242 gk_free((void**)&graph->nrinfo);
243 }
244
245
246 /*************************************************************************/
247 /*! This function deallocates any memory stored in a graph */
248 /*************************************************************************/
249 void FreeGraph(graph_t **r_graph)
250 {
251 graph_t *graph;
252
253 graph = *r_graph;
254
255 /* free the graph structure's fields */
256 FreeSData(graph);
257
258 /* free the partition/refinement fields */
259 FreeRData(graph);
260
261 gk_free((void **)&graph->tvwgt);
262 gk_free((void**)&graph->invtvwgt);
263 gk_free((void**)&graph->label);
264 gk_free((void**)&graph->cmap);
265 gk_free((void**)&graph);
266
267 *r_graph = NULL;
268 }