24 idx_t i,
j, k,
l, fmt,
ncon, nfields, readew, readvw, readvs, edge, ewgt;
26 char *line=
NULL, fmtstr[256], *curstr, *newstr;
42 }
while (line[0] ==
'%');
50 errexit(
"The input file does not specify the number of vertices and edges.\n");
57 errexit(
"Cannot read this type of file format [fmt=%"PRIDX
"]!\n", fmt);
59 sprintf(fmtstr,
"%03"PRIDX, fmt%1000);
60 readvs = (fmtstr[0] ==
'1');
61 readvw = (fmtstr[1] ==
'1');
62 readew = (fmtstr[2] ==
'1');
67 if (ncon > 0 && !readvw)
69 "------------------------------------------------------------------------------\n" 70 "*** I detected an error in your input file ***\n\n" 71 "You specified ncon=%"PRIDX
", but the fmt parameter does not specify vertex weights\n" 72 "Make sure that the fmt parameter is set to either 10 or 11.\n" 73 "------------------------------------------------------------------------------\n", ncon);
76 ncon = graph->
ncon = (ncon == 0 ? 1 :
ncon);
88 for (xadj[0]=0, k=0, i=0; i<graph->
nvtxs; i++) {
91 errexit(
"Premature end of input file while reading vertex %"PRIDX
".\n", i+1);
92 }
while (line[0] ==
'%');
100 if (newstr == curstr)
101 errexit(
"The line for vertex %"PRIDX
" does not have vsize information\n", i+1);
103 errexit(
"The size for vertex %"PRIDX
" must be >= 0\n", i+1);
110 for (l=0; l<
ncon; l++) {
111 vwgt[i*ncon+
l] =
strtoidx(curstr, &newstr, 10);
112 if (newstr == curstr)
113 errexit(
"The line for vertex %"PRIDX
" does not have enough weights " 114 "for the %"PRIDX
" constraints.\n", i+1, ncon);
115 if (vwgt[i*ncon+l] < 0)
116 errexit(
"The weight vertex %"PRIDX
" and constraint %"PRIDX
" must be >= 0\n", i+1, l);
122 edge =
strtoidx(curstr, &newstr, 10);
123 if (newstr == curstr)
127 if (edge < 1 || edge > graph->
nvtxs)
128 errexit(
"Edge %"PRIDX
" for vertex %"PRIDX
" is out of bounds\n", edge, i+1);
132 ewgt =
strtoidx(curstr, &newstr, 10);
133 if (newstr == curstr)
134 errexit(
"Premature end of line for vertex %"PRIDX
"\n", i+1);
136 errexit(
"The weight (%"PRIDX
") for edge (%"PRIDX
", %"PRIDX
") must be positive.\n",
142 errexit(
"There are more edges in the file than the %"PRIDX
" specified.\n",
154 printf(
"------------------------------------------------------------------------------\n");
155 printf(
"*** I detected an error in your input file ***\n\n");
156 printf(
"In the first line of the file, you specified that the graph contained\n" 157 "%"PRIDX
" edges. However, I only found %"PRIDX
" edges in the file.\n",
159 if (2*k == graph->
nedges) {
160 printf(
"\n *> I detected that you specified twice the number of edges that you have in\n");
161 printf(
" the file. Remember that the number of edges specified in the first line\n");
162 printf(
" counts each edge between vertices v and u only once.\n\n");
164 printf(
"Please specify the correct number of edges in the first line of the file.\n");
165 printf(
"------------------------------------------------------------------------------\n");
182 size_t nlines, ntokens;
183 char *line=
NULL, *curstr, *newstr;
202 }
while (line[0] ==
'%');
209 errexit(
"The input file does not specify the number of elements.\n");
212 errexit(
"The supplied number of elements:%"PRIDX" must be positive.\n", mesh->
ne);
214 if (mesh->
ne > nlines)
215 errexit(
"The file has %zu lines which smaller than the number of " 216 "elements of %"PRIDX" specified in the header line.\n", nlines, mesh->
ne);
220 eind = mesh->
eind =
imalloc(ntokens,
"ReadMesh: eind");
221 ewgt = mesh->
ewgt =
ismalloc((ncon == 0 ? 1 : ncon)*mesh->
ne, 1,
"ReadMesh: ewgt");
227 for (eptr[0]=0, k=0, i=0; i<mesh->
ne; i++) {
230 errexit(
"Premature end of input file while reading element %"PRIDX".\n", i+1);
231 }
while (line[0] ==
'%');
237 for (l=0; l<
ncon; l++) {
238 ewgt[i*ncon+
l] =
strtoidx(curstr, &newstr, 10);
239 if (newstr == curstr)
240 errexit(
"The line for vertex %"PRIDX" does not have enough weights " 241 "for the %"PRIDX" constraints.\n", i+1, ncon);
242 if (ewgt[i*ncon+l] < 0)
243 errexit(
"The weight for element %"PRIDX
" and constraint %"PRIDX
" must be >= 0\n", i+1, l);
248 node =
strtoidx(curstr, &newstr, 10);
249 if (newstr == curstr)
262 mesh->
ncon = (ncon == 0 ? 1 :
ncon);
263 mesh->
nn =
imax(eptr[mesh->
ne], eind)+1;
277 idx_t i,
j, from, to, fromcnum, tocnum, nleft;
279 char *line=
NULL, *curstr, *newstr;
286 for (i=0; i<params->
nparts; i++) {
287 for (j=0; j<
ncon; j++)
298 while (
gk_getline(&line, &lnlen, fpin) != -1) {
305 from =
strtoidx(curstr, &newstr, 10);
306 if (newstr == curstr)
307 errexit(
"The 'from' component of line <%s> in the tpwgts file is incorrect.\n", line);
310 if (curstr[0] ==
'-') {
311 to =
strtoidx(curstr+1, &newstr, 10);
312 if (newstr == curstr)
313 errexit(
"The 'to' component of line <%s> in the tpwgts file is incorrect.\n", line);
320 if (curstr[0] ==
':') {
321 fromcnum =
strtoidx(curstr+1, &newstr, 10);
322 if (newstr == curstr)
323 errexit(
"The 'fromcnum' component of line <%s> in the tpwgts file is incorrect.\n", line);
326 if (curstr[0] ==
'-') {
327 tocnum =
strtoidx(curstr+1, &newstr, 10);
328 if (newstr == curstr)
329 errexit(
"The 'tocnum' component of line <%s> in the tpwgts file is incorrect.\n", line);
341 if (curstr[0] ==
'=') {
343 if (newstr == curstr)
344 errexit(
"The 'wgt' component of line <%s> in the tpwgts file is incorrect.\n", line);
348 errexit(
"The 'wgt' component of line <%s> in the tpwgts file is missing.\n", line);
354 if (from < 0 || to < 0 || from >= params->
nparts || to >= params->
nparts)
356 if (fromcnum < 0 || tocnum < 0 || fromcnum >= ncon || tocnum >= ncon)
357 errexit(
"Invalid constraint number range for %"PRIDX
":%"PRIDX
"\n",
359 if (awgt <= 0.0 || awgt >= 1.0)
361 for (i=from; i<=to; i++) {
362 for (j=fromcnum; j<=tocnum; j++)
363 params->
tpwgts[i*ncon+j] = awgt;
370 for (j=0; j<
ncon; j++) {
372 for (twgt=0.0, nleft=params->
nparts, i=0; i<params->
nparts; i++) {
373 if (params->
tpwgts[i*ncon+j] > 0) {
374 twgt += params->
tpwgts[i*ncon+
j];
386 errexit(
"The total specified target partition weights for constraint #%"PRIDX 387 " of %"PRREAL" exceeds 1.0.\n", j, twgt);
389 awgt = (1.0 - twgt)/nleft;
390 for (i=0; i<params->
nparts; i++)
391 params->
tpwgts[i*ncon+j] =
392 (params->
tpwgts[i*ncon+j] < 0 ? awgt : params->
tpwgts[i*ncon+j]);
412 fpin =
gk_fopen(filename,
"r", __func__);
413 for (i=0; i<graph->
nvtxs; i++) {
414 if (fscanf(fpin,
"%"SCIDX"\n", vector+i) != 1)
415 errexit(
"[%s] Premature end of file %s at line %d [nvtxs: %d]\n",
416 __func__, filename, i, graph->
nvtxs);
431 sprintf(filename,
"%s.part.%"PRIDX, fname, nparts);
433 fpout =
gk_fopen(filename,
"w", __func__);
436 fprintf(fpout,
"%" PRIDX "\n", part[i]);
452 sprintf(filename,
"%s.epart.%"PRIDX,fname, nparts);
454 fpout =
gk_fopen(filename,
"w", __func__);
457 fprintf(fpout,
"%" PRIDX "\n", epart[i]);
462 sprintf(filename,
"%s.npart.%"PRIDX,fname, nparts);
464 fpout =
gk_fopen(filename,
"w", __func__);
467 fprintf(fpout,
"%" PRIDX "\n", npart[i]);
483 sprintf(filename,
"%s.iperm", fname);
485 fpout =
gk_fopen(filename,
"w", __func__);
488 fprintf(fpout,
"%" PRIDX "\n", iperm[i]);
501 int hasvwgt=0, hasewgt=0, hasvsize=0;
504 nvtxs = graph->
nvtxs;
509 vsize = graph->
vsize;
514 for (i=0; i<nvtxs*
ncon; i++) {
522 for (i=0; i<nvtxs; i++) {
530 for (i=0; i<xadj[nvtxs]; i++) {
531 if (adjwgt[i] != 1) {
538 fpout =
gk_fopen(filename,
"w", __func__);
541 fprintf(fpout,
"%"PRIDX" %"PRIDX, nvtxs, xadj[nvtxs]/2);
542 if (hasvwgt || hasvsize || hasewgt) {
543 fprintf(fpout,
" %d%d%d", hasvsize, hasvwgt, hasewgt);
545 fprintf(fpout,
" %d", (
int)graph->
ncon);
550 for (i=0; i<nvtxs; i++) {
551 fprintf(fpout,
"\n");
553 fprintf(fpout,
" %"PRIDX, vsize[i]);
556 for (j=0; j<
ncon; j++)
557 fprintf(fpout,
" %"PRIDX, vwgt[i*ncon+j]);
560 for (j=xadj[i]; j<xadj[i+1]; j++) {
561 fprintf(fpout,
" %"PRIDX, adjncy[j]+1);
563 fprintf(fpout,
" %"PRIDX, adjwgt[j]);
void gk_getfilestats(char *fname, size_t *r_nlines, size_t *r_ntokens, size_t *r_max_nlntokens, size_t *r_nbytes)
idx_t idx_t idx_t idx_t * vwgt
FILE * gk_fopen(char *, char *, const char *)
void errexit(char *f_str,...)
idx_t idx_t idx_t idx_t idx_t idx_t idx_t real_t real_t idx_t idx_t idx_t * part
mesh_t * ReadMesh(params_t *params)
char * gk_strchr_replace(char *str, char *fromlist, char *tolist)
Replaces certain characters in a string.
int gk_fexists(char *fname)
idx_t idx_t idx_t idx_t idx_t idx_t real_t idx_t idx_t idx_t * epart
idx_t idx_t idx_t idx_t idx_t idx_t idx_t * nparts
idx_t idx_t idx_t idx_t idx_t idx_t * iperm
NonlinearFactorGraph graph
void WritePartition(char *fname, idx_t *part, idx_t n, idx_t nparts)
static const SmartProjectionParams params
idx_t idx_t idx_t idx_t idx_t * vsize
void ReadTPwgts(params_t *params, idx_t ncon)
static const Line3 l(Rot3(), 1, 1)
gk_idx_t gk_getline(char **lineptr, size_t *n, FILE *stream)
void WriteMeshPartition(char *fname, idx_t nparts, idx_t ne, idx_t *epart, idx_t nn, idx_t *npart)
idx_t idx_t idx_t idx_t idx_t idx_t real_t idx_t idx_t idx_t idx_t * npart
graph_t * ReadGraph(params_t *params)
idx_t idx_t idx_t idx_t idx_t idx_t * adjwgt
void gk_free(void **ptr1,...)
idx_t idx_t idx_t * adjncy
void ReadPOVector(graph_t *graph, char *filename, idx_t *vector)
void WriteGraph(graph_t *graph, char *filename)
void WritePermutation(char *fname, idx_t *iperm, idx_t n)