00001
00011 #include "metislib.h"
00012
00013
00014
00016
00017 graph_t *SetupGraph(ctrl_t *ctrl, idx_t nvtxs, idx_t ncon, idx_t *xadj,
00018 idx_t *adjncy, idx_t *vwgt, idx_t *vsize, idx_t *adjwgt)
00019 {
00020 idx_t i, j, k, sum;
00021 real_t *nvwgt;
00022 graph_t *graph;
00023
00024
00025 graph = CreateGraph();
00026
00027 graph->nvtxs = nvtxs;
00028 graph->nedges = xadj[nvtxs];
00029 graph->ncon = ncon;
00030
00031 graph->xadj = xadj;
00032 graph->free_xadj = 0;
00033
00034 graph->adjncy = adjncy;
00035 graph->free_adjncy = 0;
00036
00037
00038
00039 if (vwgt) {
00040 graph->vwgt = vwgt;
00041 graph->free_vwgt = 0;
00042 }
00043 else {
00044 vwgt = graph->vwgt = ismalloc(ncon*nvtxs, 1, "SetupGraph: vwgt");
00045 }
00046
00047 graph->tvwgt = imalloc(ncon, "SetupGraph: tvwgts");
00048 graph->invtvwgt = rmalloc(ncon, "SetupGraph: invtvwgts");
00049 for (i=0; i<ncon; i++) {
00050 graph->tvwgt[i] = isum(nvtxs, vwgt+i, ncon);
00051 graph->invtvwgt[i] = 1.0/(graph->tvwgt[i] > 0 ? graph->tvwgt[i] : 1);
00052 }
00053
00054
00055 if (ctrl->objtype == METIS_OBJTYPE_VOL) {
00056
00057 if (vsize) {
00058 graph->vsize = vsize;
00059 graph->free_vsize = 0;
00060 }
00061 else {
00062 vsize = graph->vsize = ismalloc(nvtxs, 1, "SetupGraph: vsize");
00063 }
00064
00065
00066 adjwgt = graph->adjwgt = imalloc(graph->nedges, "SetupGraph: adjwgt");
00067 for (i=0; i<nvtxs; i++) {
00068 for (j=xadj[i]; j<xadj[i+1]; j++)
00069 adjwgt[j] = 1+vsize[i]+vsize[adjncy[j]];
00070 }
00071 }
00072 else {
00073
00074 if (adjwgt) {
00075 graph->adjwgt = adjwgt;
00076 graph->free_adjwgt = 0;
00077 }
00078 else {
00079 adjwgt = graph->adjwgt = ismalloc(graph->nedges, 1, "SetupGraph: adjwgt");
00080 }
00081 }
00082
00083
00084
00085 SetupGraph_tvwgt(graph);
00086
00087 if (ctrl->optype == METIS_OP_PMETIS || ctrl->optype == METIS_OP_OMETIS)
00088 SetupGraph_label(graph);
00089
00090 ASSERT(CheckGraph(graph, ctrl->numflag, 1));
00091
00092 return graph;
00093 }
00094
00095
00096
00098
00099 void SetupGraph_tvwgt(graph_t *graph)
00100 {
00101 idx_t i;
00102
00103 if (graph->tvwgt == NULL)
00104 graph->tvwgt = imalloc(graph->ncon, "SetupGraph_tvwgt: tvwgt");
00105 if (graph->invtvwgt == NULL)
00106 graph->invtvwgt = rmalloc(graph->ncon, "SetupGraph_tvwgt: invtvwgt");
00107
00108 for (i=0; i<graph->ncon; i++) {
00109 graph->tvwgt[i] = isum(graph->nvtxs, graph->vwgt+i, graph->ncon);
00110 graph->invtvwgt[i] = 1.0/(graph->tvwgt[i] > 0 ? graph->tvwgt[i] : 1);
00111 }
00112 }
00113
00114
00115
00117
00118 void SetupGraph_label(graph_t *graph)
00119 {
00120 idx_t i;
00121
00122 if (graph->label == NULL)
00123 graph->label = imalloc(graph->nvtxs, "SetupGraph_label: label");
00124
00125 for (i=0; i<graph->nvtxs; i++)
00126 graph->label[i] = i;
00127 }
00128
00129
00130
00132
00133 graph_t *SetupSplitGraph(graph_t *graph, idx_t snvtxs, idx_t snedges)
00134 {
00135 graph_t *sgraph;
00136
00137 sgraph = CreateGraph();
00138
00139 sgraph->nvtxs = snvtxs;
00140 sgraph->nedges = snedges;
00141 sgraph->ncon = graph->ncon;
00142
00143
00144 sgraph->xadj = imalloc(snvtxs+1, "SetupSplitGraph: xadj");
00145 sgraph->vwgt = imalloc(sgraph->ncon*snvtxs, "SetupSplitGraph: vwgt");
00146 sgraph->adjncy = imalloc(snedges, "SetupSplitGraph: adjncy");
00147 sgraph->adjwgt = imalloc(snedges, "SetupSplitGraph: adjwgt");
00148 sgraph->label = imalloc(snvtxs, "SetupSplitGraph: label");
00149 sgraph->tvwgt = imalloc(sgraph->ncon, "SetupSplitGraph: tvwgt");
00150 sgraph->invtvwgt = rmalloc(sgraph->ncon, "SetupSplitGraph: invtvwgt");
00151
00152 if (graph->vsize)
00153 sgraph->vsize = imalloc(snvtxs, "SetupSplitGraph: vsize");
00154
00155 return sgraph;
00156 }
00157
00158
00159
00161
00162 graph_t *CreateGraph(void)
00163 {
00164 graph_t *graph;
00165
00166 graph = (graph_t *)gk_malloc(sizeof(graph_t), "CreateGraph: graph");
00167
00168 InitGraph(graph);
00169
00170 return graph;
00171 }
00172
00173
00174
00176
00177 void InitGraph(graph_t *graph)
00178 {
00179 memset((void *)graph, 0, sizeof(graph_t));
00180
00181
00182 graph->nvtxs = -1;
00183 graph->nedges = -1;
00184 graph->ncon = -1;
00185 graph->mincut = -1;
00186 graph->minvol = -1;
00187 graph->nbnd = -1;
00188
00189
00190 graph->xadj = NULL;
00191 graph->vwgt = NULL;
00192 graph->vsize = NULL;
00193 graph->adjncy = NULL;
00194 graph->adjwgt = NULL;
00195 graph->label = NULL;
00196 graph->cmap = NULL;
00197 graph->tvwgt = NULL;
00198 graph->invtvwgt = NULL;
00199
00200
00201 graph->free_xadj = 1;
00202 graph->free_vwgt = 1;
00203 graph->free_vsize = 1;
00204 graph->free_adjncy = 1;
00205 graph->free_adjwgt = 1;
00206
00207
00208
00209 graph->where = NULL;
00210 graph->pwgts = NULL;
00211 graph->id = NULL;
00212 graph->ed = NULL;
00213 graph->bndptr = NULL;
00214 graph->bndind = NULL;
00215 graph->nrinfo = NULL;
00216 graph->ckrinfo = NULL;
00217 graph->vkrinfo = NULL;
00218
00219
00220 graph->coarser = NULL;
00221 graph->finer = NULL;
00222 }
00223
00224
00225
00227
00228 void FreeRData(graph_t *graph)
00229 {
00230
00231
00232
00233 if ((void *)graph->ckrinfo == (void *)graph->vkrinfo)
00234 graph->ckrinfo = NULL;
00235
00236
00237
00238 gk_free((void **)&graph->where, &graph->pwgts, &graph->id, &graph->ed,
00239 &graph->bndptr, &graph->bndind, &graph->nrinfo, &graph->ckrinfo,
00240 &graph->vkrinfo, LTERM);
00241 }
00242
00243
00244
00246
00247 void FreeGraph(graph_t **r_graph)
00248 {
00249 graph_t *graph;
00250
00251 graph = *r_graph;
00252
00253
00254 if (graph->free_xadj)
00255 gk_free((void **)&graph->xadj, LTERM);
00256 if (graph->free_vwgt)
00257 gk_free((void **)&graph->vwgt, LTERM);
00258 if (graph->free_vsize)
00259 gk_free((void **)&graph->vsize, LTERM);
00260 if (graph->free_adjncy)
00261 gk_free((void **)&graph->adjncy, LTERM);
00262 if (graph->free_adjwgt)
00263 gk_free((void **)&graph->adjwgt, LTERM);
00264
00265
00266 FreeRData(graph);
00267
00268 gk_free((void **)&graph->tvwgt, &graph->invtvwgt, &graph->label,
00269 &graph->cmap, &graph, LTERM);
00270
00271 *r_graph = NULL;
00272 }
00273
00274