1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
|
/*
* Copyright 1997, Regents of the University of Minnesota
*
* io.c
*
* This file contains routines related to I/O
*
* Started 8/28/94
* George
*
* $Id: io.c 11932 2012-05-10 18:18:23Z dominique $
*
*/
#include "metisbin.h"
/*************************************************************************/
/*! This function reads in a sparse graph */
/*************************************************************************/
graph_t *ReadGraph(params_t *params)
{
idx_t i, j, k, l, fmt, ncon, nfields, readew, readvw, readvs, edge, ewgt;
idx_t *xadj, *adjncy, *vwgt, *adjwgt, *vsize;
char *line=NULL, fmtstr[256], *curstr, *newstr;
size_t lnlen=0;
FILE *fpin;
graph_t *graph;
if (!gk_fexists(params->filename))
errexit("File %s does not exist!\n", params->filename);
graph = CreateGraph();
fpin = gk_fopen(params->filename, "r", "ReadGRaph: Graph");
/* Skip comment lines until you get to the first valid line */
do {
if (gk_getline(&line, &lnlen, fpin) == -1)
errexit("Premature end of input file: file: %s\n", params->filename);
} while (line[0] == '%');
fmt = ncon = 0;
nfields = sscanf(line, "%"SCIDX" %"SCIDX" %"SCIDX" %"SCIDX,
&(graph->nvtxs), &(graph->nedges), &fmt, &ncon);
if (nfields < 2)
errexit("The input file does not specify the number of vertices and edges.\n");
if (graph->nvtxs <= 0 || graph->nedges <= 0)
errexit("The supplied nvtxs:%"PRIDX" and nedges:%"PRIDX" must be positive.\n",
graph->nvtxs, graph->nedges);
if (fmt > 111)
errexit("Cannot read this type of file format [fmt=%"PRIDX"]!\n", fmt);
sprintf(fmtstr, "%03"PRIDX, fmt%1000);
readvs = (fmtstr[0] == '1');
readvw = (fmtstr[1] == '1');
readew = (fmtstr[2] == '1');
/*printf("%s %"PRIDX" %"PRIDX" %"PRIDX"\n", fmtstr, readvs, readvw, readew); */
if (ncon > 0 && !readvw)
errexit(
"------------------------------------------------------------------------------\n"
"*** I detected an error in your input file ***\n\n"
"You specified ncon=%"PRIDX", but the fmt parameter does not specify vertex weights\n"
"Make sure that the fmt parameter is set to either 10 or 11.\n"
"------------------------------------------------------------------------------\n", ncon);
graph->nedges *=2;
ncon = graph->ncon = (ncon == 0 ? 1 : ncon);
xadj = graph->xadj = ismalloc(graph->nvtxs+1, 0, "ReadGraph: xadj");
adjncy = graph->adjncy = imalloc(graph->nedges, "ReadGraph: adjncy");
vwgt = graph->vwgt = ismalloc(ncon*graph->nvtxs, 1, "ReadGraph: vwgt");
adjwgt = graph->adjwgt = ismalloc(graph->nedges, 1, "ReadGraph: adjwgt");
vsize = graph->vsize = ismalloc(graph->nvtxs, 1, "ReadGraph: vsize");
/*----------------------------------------------------------------------
* Read the sparse graph file
*---------------------------------------------------------------------*/
for (xadj[0]=0, k=0, i=0; i<graph->nvtxs; i++) {
do {
if (gk_getline(&line, &lnlen, fpin) == -1)
errexit("Premature end of input file while reading vertex %"PRIDX".\n", i+1);
} while (line[0] == '%');
curstr = line;
newstr = NULL;
/* Read vertex sizes */
if (readvs) {
vsize[i] = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
errexit("The line for vertex %"PRIDX" does not have vsize information\n", i+1);
if (vsize[i] < 0)
errexit("The size for vertex %"PRIDX" must be >= 0\n", i+1);
curstr = newstr;
}
/* Read vertex weights */
if (readvw) {
for (l=0; l<ncon; l++) {
vwgt[i*ncon+l] = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
errexit("The line for vertex %"PRIDX" does not have enough weights "
"for the %"PRIDX" constraints.\n", i+1, ncon);
if (vwgt[i*ncon+l] < 0)
errexit("The weight vertex %"PRIDX" and constraint %"PRIDX" must be >= 0\n", i+1, l);
curstr = newstr;
}
}
while (1) {
edge = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
break; /* End of line */
curstr = newstr;
if (edge < 1 || edge > graph->nvtxs)
errexit("Edge %"PRIDX" for vertex %"PRIDX" is out of bounds\n", edge, i+1);
ewgt = 1;
if (readew) {
ewgt = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
errexit("Premature end of line for vertex %"PRIDX"\n", i+1);
if (ewgt <= 0)
errexit("The weight (%"PRIDX") for edge (%"PRIDX", %"PRIDX") must be positive.\n",
ewgt, i+1, edge);
curstr = newstr;
}
if (k == graph->nedges)
errexit("There are more edges in the file than the %"PRIDX" specified.\n",
graph->nedges/2);
adjncy[k] = edge-1;
adjwgt[k] = ewgt;
k++;
}
xadj[i+1] = k;
}
gk_fclose(fpin);
if (k != graph->nedges) {
printf("------------------------------------------------------------------------------\n");
printf("*** I detected an error in your input file ***\n\n");
printf("In the first line of the file, you specified that the graph contained\n"
"%"PRIDX" edges. However, I only found %"PRIDX" edges in the file.\n",
graph->nedges/2, k/2);
if (2*k == graph->nedges) {
printf("\n *> I detected that you specified twice the number of edges that you have in\n");
printf(" the file. Remember that the number of edges specified in the first line\n");
printf(" counts each edge between vertices v and u only once.\n\n");
}
printf("Please specify the correct number of edges in the first line of the file.\n");
printf("------------------------------------------------------------------------------\n");
exit(0);
}
gk_free((void *)&line, LTERM);
return graph;
}
/*************************************************************************/
/*! This function reads in a mesh */
/*************************************************************************/
mesh_t *ReadMesh(params_t *params)
{
idx_t i, j, k, l, nfields, ncon, node;
idx_t *eptr, *eind, *ewgt;
size_t nlines, ntokens;
char *line=NULL, *curstr, *newstr;
size_t lnlen=0;
FILE *fpin;
mesh_t *mesh;
if (!gk_fexists(params->filename))
errexit("File %s does not exist!\n", params->filename);
mesh = CreateMesh();
/* get some file stats */
gk_getfilestats(params->filename, &nlines, &ntokens, NULL, NULL);
fpin = gk_fopen(params->filename, "r", __func__);
/* Skip comment lines until you get to the first valid line */
do {
if (gk_getline(&line, &lnlen, fpin) == -1)
errexit("Premature end of input file: file: %s\n", params->filename);
} while (line[0] == '%');
mesh->ncon = 0;
nfields = sscanf(line, "%"SCIDX" %"SCIDX, &(mesh->ne), &(mesh->ncon));
if (nfields < 1)
errexit("The input file does not specify the number of elements.\n");
if (mesh->ne <= 0)
errexit("The supplied number of elements:%"PRIDX" must be positive.\n", mesh->ne);
if (mesh->ne > nlines)
errexit("The file has %zu lines which smaller than the number of "
"elements of %"PRIDX" specified in the header line.\n", nlines, mesh->ne);
ncon = mesh->ncon;
eptr = mesh->eptr = ismalloc(mesh->ne+1, 0, "ReadMesh: eptr");
eind = mesh->eind = imalloc(ntokens, "ReadMesh: eind");
ewgt = mesh->ewgt = ismalloc((ncon == 0 ? 1 : ncon)*mesh->ne, 1, "ReadMesh: ewgt");
/*----------------------------------------------------------------------
* Read the mesh file
*---------------------------------------------------------------------*/
for (eptr[0]=0, k=0, i=0; i<mesh->ne; i++) {
do {
if (gk_getline(&line, &lnlen, fpin) == -1)
errexit("Premature end of input file while reading element %"PRIDX".\n", i+1);
} while (line[0] == '%');
curstr = line;
newstr = NULL;
/* Read element weights */
for (l=0; l<ncon; l++) {
ewgt[i*ncon+l] = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
errexit("The line for vertex %"PRIDX" does not have enough weights "
"for the %"PRIDX" constraints.\n", i+1, ncon);
if (ewgt[i*ncon+l] < 0)
errexit("The weight for element %"PRIDX" and constraint %"PRIDX" must be >= 0\n", i+1, l);
curstr = newstr;
}
while (1) {
node = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
break; /* End of line */
curstr = newstr;
if (node < 1)
errexit("Node %"PRIDX" for element %"PRIDX" is out of bounds\n", node, i+1);
eind[k++] = node-1;
}
eptr[i+1] = k;
}
gk_fclose(fpin);
mesh->ncon = (ncon == 0 ? 1 : ncon);
mesh->nn = imax(eptr[mesh->ne], eind)+1;
gk_free((void *)&line, LTERM);
return mesh;
}
/*************************************************************************/
/*! This function reads in the target partition weights. If no file is
specified the weights are set to 1/nparts */
/*************************************************************************/
void ReadTPwgts(params_t *params, idx_t ncon)
{
idx_t i, j, from, to, fromcnum, tocnum, nleft;
real_t awgt=0.0, twgt;
char *line=NULL, *curstr, *newstr;
size_t lnlen=0;
FILE *fpin;
params->tpwgts = rsmalloc(params->nparts*ncon, -1.0, "ReadTPwgts: tpwgts");
if (params->tpwgtsfile == NULL) {
for (i=0; i<params->nparts; i++) {
for (j=0; j<ncon; j++)
params->tpwgts[i*ncon+j] = 1.0/params->nparts;
}
return;
}
if (!gk_fexists(params->tpwgtsfile))
errexit("Graph file %s does not exist!\n", params->tpwgtsfile);
fpin = gk_fopen(params->tpwgtsfile, "r", "ReadTPwgts: tpwgtsfile");
while (gk_getline(&line, &lnlen, fpin) != -1) {
gk_strchr_replace(line, " ", "");
/* start extracting the fields */
curstr = line;
newstr = NULL;
from = strtoidx(curstr, &newstr, 10);
if (newstr == curstr)
errexit("The 'from' component of line <%s> in the tpwgts file is incorrect.\n", line);
curstr = newstr;
if (curstr[0] == '-') {
to = strtoidx(curstr+1, &newstr, 10);
if (newstr == curstr)
errexit("The 'to' component of line <%s> in the tpwgts file is incorrect.\n", line);
curstr = newstr;
}
else {
to = from;
}
if (curstr[0] == ':') {
fromcnum = strtoidx(curstr+1, &newstr, 10);
if (newstr == curstr)
errexit("The 'fromcnum' component of line <%s> in the tpwgts file is incorrect.\n", line);
curstr = newstr;
if (curstr[0] == '-') {
tocnum = strtoidx(curstr+1, &newstr, 10);
if (newstr == curstr)
errexit("The 'tocnum' component of line <%s> in the tpwgts file is incorrect.\n", line);
curstr = newstr;
}
else {
tocnum = fromcnum;
}
}
else {
fromcnum = 0;
tocnum = ncon-1;
}
if (curstr[0] == '=') {
awgt = strtoreal(curstr+1, &newstr);
if (newstr == curstr)
errexit("The 'wgt' component of line <%s> in the tpwgts file is incorrect.\n", line);
curstr = newstr;
}
else {
errexit("The 'wgt' component of line <%s> in the tpwgts file is missing.\n", line);
}
/*printf("Read: %"PRIDX"-%"PRIDX":%"PRIDX"-%"PRIDX"=%"PRREAL"\n",
from, to, fromcnum, tocnum, awgt);*/
if (from < 0 || to < 0 || from >= params->nparts || to >= params->nparts)
errexit("Invalid partition range for %"PRIDX":%"PRIDX"\n", from, to);
if (fromcnum < 0 || tocnum < 0 || fromcnum >= ncon || tocnum >= ncon)
errexit("Invalid constraint number range for %"PRIDX":%"PRIDX"\n",
fromcnum, tocnum);
if (awgt <= 0.0 || awgt >= 1.0)
errexit("Invalid partition weight of %"PRREAL"\n", awgt);
for (i=from; i<=to; i++) {
for (j=fromcnum; j<=tocnum; j++)
params->tpwgts[i*ncon+j] = awgt;
}
}
gk_fclose(fpin);
/* Assign weight to the unspecified constraints x partitions */
for (j=0; j<ncon; j++) {
/* Sum up the specified weights for the jth constraint */
for (twgt=0.0, nleft=params->nparts, i=0; i<params->nparts; i++) {
if (params->tpwgts[i*ncon+j] > 0) {
twgt += params->tpwgts[i*ncon+j];
nleft--;
}
}
/* Rescale the weights to be on the safe side */
if (nleft == 0)
rscale(params->nparts, 1.0/twgt, params->tpwgts+j, ncon);
/* Assign the left-over weight to the remaining partitions */
if (nleft > 0) {
if (twgt > 1)
errexit("The total specified target partition weights for constraint #%"PRIDX
" of %"PRREAL" exceeds 1.0.\n", j, twgt);
awgt = (1.0 - twgt)/nleft;
for (i=0; i<params->nparts; i++)
params->tpwgts[i*ncon+j] =
(params->tpwgts[i*ncon+j] < 0 ? awgt : params->tpwgts[i*ncon+j]);
}
}
#ifdef HAVE_GETLINE
free(line);
line = NULL; /* set to null to match behavior of gk_free() */
#else
gk_free((void *)&line, LTERM);
#endif
}
/*************************************************************************/
/*! This function reads in a partition/ordering vector */
/**************************************************************************/
void ReadPOVector(graph_t *graph, char *filename, idx_t *vector)
{
idx_t i;
FILE *fpin;
fpin = gk_fopen(filename, "r", __func__);
for (i=0; i<graph->nvtxs; i++) {
if (fscanf(fpin, "%"SCIDX"\n", vector+i) != 1)
errexit("[%s] Premature end of file %s at line %d [nvtxs: %d]\n",
__func__, filename, i, graph->nvtxs);
}
gk_fclose(fpin);
}
/*************************************************************************/
/*! This function writes out the partition vector */
/*************************************************************************/
void WritePartition(char *fname, idx_t *part, idx_t n, idx_t nparts)
{
FILE *fpout;
idx_t i;
char filename[MAXLINE];
sprintf(filename, "%s.part.%"PRIDX, fname, nparts);
fpout = gk_fopen(filename, "w", __func__);
for (i=0; i<n; i++)
fprintf(fpout,"%" PRIDX "\n", part[i]);
gk_fclose(fpout);
}
/*************************************************************************/
/*! This function writes out the partition vectors for a mesh */
/*************************************************************************/
void WriteMeshPartition(char *fname, idx_t nparts, idx_t ne, idx_t *epart,
idx_t nn, idx_t *npart)
{
FILE *fpout;
idx_t i;
char filename[256];
sprintf(filename,"%s.epart.%"PRIDX,fname, nparts);
fpout = gk_fopen(filename, "w", __func__);
for (i=0; i<ne; i++)
fprintf(fpout,"%" PRIDX "\n", epart[i]);
gk_fclose(fpout);
sprintf(filename,"%s.npart.%"PRIDX,fname, nparts);
fpout = gk_fopen(filename, "w", __func__);
for (i=0; i<nn; i++)
fprintf(fpout, "%" PRIDX "\n", npart[i]);
gk_fclose(fpout);
}
/*************************************************************************/
/*! This function writes out the permutation vector */
/*************************************************************************/
void WritePermutation(char *fname, idx_t *iperm, idx_t n)
{
FILE *fpout;
idx_t i;
char filename[MAXLINE];
sprintf(filename, "%s.iperm", fname);
fpout = gk_fopen(filename, "w", __func__);
for (i=0; i<n; i++)
fprintf(fpout, "%" PRIDX "\n", iperm[i]);
gk_fclose(fpout);
}
/*************************************************************************/
/*! This function writes a graph into a file */
/*************************************************************************/
void WriteGraph(graph_t *graph, char *filename)
{
idx_t i, j, nvtxs, ncon;
idx_t *xadj, *adjncy, *adjwgt, *vwgt, *vsize;
int hasvwgt=0, hasewgt=0, hasvsize=0;
FILE *fpout;
nvtxs = graph->nvtxs;
ncon = graph->ncon;
xadj = graph->xadj;
adjncy = graph->adjncy;
vwgt = graph->vwgt;
vsize = graph->vsize;
adjwgt = graph->adjwgt;
/* determine if the graph has non-unity vwgt, vsize, or adjwgt */
if (vwgt) {
for (i=0; i<nvtxs*ncon; i++) {
if (vwgt[i] != 1) {
hasvwgt = 1;
break;
}
}
}
if (vsize) {
for (i=0; i<nvtxs; i++) {
if (vsize[i] != 1) {
hasvsize = 1;
break;
}
}
}
if (adjwgt) {
for (i=0; i<xadj[nvtxs]; i++) {
if (adjwgt[i] != 1) {
hasewgt = 1;
break;
}
}
}
fpout = gk_fopen(filename, "w", __func__);
/* write the header line */
fprintf(fpout, "%"PRIDX" %"PRIDX, nvtxs, xadj[nvtxs]/2);
if (hasvwgt || hasvsize || hasewgt) {
fprintf(fpout, " %d%d%d", hasvsize, hasvwgt, hasewgt);
if (hasvwgt)
fprintf(fpout, " %d", (int)graph->ncon);
}
/* write the rest of the graph */
for (i=0; i<nvtxs; i++) {
fprintf(fpout, "\n");
if (hasvsize)
fprintf(fpout, " %"PRIDX, vsize[i]);
if (hasvwgt) {
for (j=0; j<ncon; j++)
fprintf(fpout, " %"PRIDX, vwgt[i*ncon+j]);
}
for (j=xadj[i]; j<xadj[i+1]; j++) {
fprintf(fpout, " %"PRIDX, adjncy[j]+1);
if (hasewgt)
fprintf(fpout, " %"PRIDX, adjwgt[j]);
}
}
gk_fclose(fpout);
}
|