Zoltan2  Version of the Day
Zoltan2_ChacoReader.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
50 #include <stdio.h>
51 #include <ctype.h>
52 #include <string.h>
53 #include <stdlib.h>
54 
55 namespace Zoltan2 {
56 
57 #define LINE_LENGTH 200
58 static char chaco_line[LINE_LENGTH]; /* space to hold values */
59 static int chaco_offset = 0; /* offset into line for next data */
60 static int chaco_break_pnt = LINE_LENGTH; /* place in sequence to pause */
61 static int chaco_save_pnt; /* place in sequence to save */
62 static void chaco_flush_line(FILE*);
63 
69  FILE* infile, /* file to read value from */
70  int *end_flag /* 0 => OK, 1 => EOL, -1 => EOF */
71 )
72 {
73  double val; /* return value */
74  char *ptr; /* ptr to next string to read */
75  char *ptr2; /* ptr to next string to read */
76  int length; /* length of line to read */
77  int length_left; /* length of line still around */
78  int white_seen; /* have I detected white space yet? */
79  int done; /* checking for end of scan */
80  int i; /* loop counter */
81 
82  *end_flag = 0;
83 
84  if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
85  if (chaco_offset >= chaco_break_pnt) { /* Copy rest of line back to beginning. */
86  length_left = LINE_LENGTH - chaco_save_pnt - 1;
87  ptr2 = chaco_line;
88  ptr = &chaco_line[chaco_save_pnt];
89  for (i=length_left; i; i--) *ptr2++ = *ptr++;
90  length = chaco_save_pnt + 1;
91  }
92  else {
93  length = LINE_LENGTH;
94  length_left = 0;
95  }
96 
97  /* Now read next line, or next segment of current one. */
98  ptr2 = fgets(&chaco_line[length_left], length, infile);
99 
100  if (ptr2 == (char *) NULL) { /* We've hit end of file. */
101  *end_flag = -1;
102  return((double) 0.0);
103  }
104 
105  if ((chaco_line[LINE_LENGTH - 2] != '\n') && (chaco_line[LINE_LENGTH - 2] != '\f')
106  && (strlen(chaco_line) == LINE_LENGTH - 1)){
107  /* Line too long. Find last safe place in chaco_line. */
108  chaco_break_pnt = LINE_LENGTH - 1;
109  chaco_save_pnt = chaco_break_pnt;
110  white_seen = 0;
111  done = 0;
112  while (!done) {
113  --chaco_break_pnt;
114  if (chaco_line[chaco_break_pnt] != '\0') {
115  if (isspace((int)(chaco_line[chaco_break_pnt]))) {
116  if (!white_seen) {
117  chaco_save_pnt = chaco_break_pnt + 1;
118  white_seen = 1;
119  }
120  }
121  else if (white_seen) {
122  done= 1;
123  }
124  }
125  }
126  }
127  else {
128  chaco_break_pnt = LINE_LENGTH;
129  }
130 
131  chaco_offset = 0;
132  }
133 
134  while (isspace((int)(chaco_line[chaco_offset])) && chaco_offset < LINE_LENGTH) chaco_offset++;
135  if (chaco_line[chaco_offset] == '%' || chaco_line[chaco_offset] == '#') {
136  *end_flag = 1;
137  if (chaco_break_pnt < LINE_LENGTH) {
138  chaco_flush_line(infile);
139  }
140  return((double) 0.0);
141  }
142 
143  ptr = &(chaco_line[chaco_offset]);
144  val = strtod(ptr, &ptr2);
145 
146  if (ptr2 == ptr) { /* End of input line. */
147  chaco_offset = 0;
148  *end_flag = 1;
149  return((double) 0.0);
150  }
151  else {
152  chaco_offset = (int) (ptr2 - chaco_line) / sizeof(char);
153  }
154 
155  return(val);
156 }
157 
158 
164 FILE *infile, /* file to read value from */
165 int *end_flag /* 0 => OK, 1 => EOL, -1 => EOF */
166 )
167 {
168  int val; /* return value */
169  char *ptr; /* ptr to next string to read */
170  char *ptr2; /* ptr to next string to read */
171  int length; /* length of line to read */
172  int length_left; /* length of line still around */
173  int white_seen; /* have I detected white space yet? */
174  int done; /* checking for end of scan */
175  int i; /* loop counter */
176 
177  *end_flag = 0;
178 
179  if (chaco_offset == 0 || chaco_offset >= chaco_break_pnt) {
180  if (chaco_offset >= chaco_break_pnt) { /* Copy rest of line back to beginning. */
181  length_left = LINE_LENGTH - chaco_save_pnt - 1;
182  ptr2 = chaco_line;
183  ptr = &chaco_line[chaco_save_pnt];
184  for (i=length_left; i; i--) *ptr2++ = *ptr++;
185  length = chaco_save_pnt + 1;
186  }
187  else {
188  length = LINE_LENGTH;
189  length_left = 0;
190  }
191 
192  /* Now read next line, or next segment of current one. */
193  ptr2 = fgets(&chaco_line[length_left], length, infile);
194 
195  if (ptr2 == (char *) NULL) { /* We've hit end of file. */
196  *end_flag = -1;
197  return(0);
198  }
199 
200  if ((chaco_line[LINE_LENGTH - 2] != '\n') && (chaco_line[LINE_LENGTH - 2] != '\f')
201  && (strlen(chaco_line) == LINE_LENGTH - 1)){
202  /* Line too long. Find last safe place in line. */
203  chaco_break_pnt = LINE_LENGTH - 1;
204  chaco_save_pnt = chaco_break_pnt;
205  white_seen = 0;
206  done = 0;
207  while (!done) {
208  --chaco_break_pnt;
209  if (chaco_line[chaco_break_pnt] != '\0') {
210  if (isspace((int)(chaco_line[chaco_break_pnt]))) {
211  if (!white_seen) {
212  chaco_save_pnt = chaco_break_pnt + 1;
213  white_seen = 1;
214  }
215  }
216  else if (white_seen) {
217  done= 1;
218  }
219  }
220  }
221  }
222  else {
223  chaco_break_pnt = LINE_LENGTH;
224  }
225 
226  chaco_offset = 0;
227  }
228 
229  while (isspace((int)(chaco_line[chaco_offset])) && chaco_offset < LINE_LENGTH) chaco_offset++;
230  if (chaco_line[chaco_offset] == '%' || chaco_line[chaco_offset] == '#') {
231  *end_flag = 1;
232  if (chaco_break_pnt < LINE_LENGTH) {
233  chaco_flush_line(infile);
234  }
235  return(0);
236  }
237 
238  ptr = &(chaco_line[chaco_offset]);
239  val = (int) strtol(ptr, &ptr2, 10);
240 
241  if (ptr2 == ptr) { /* End of input chaco_line. */
242  chaco_offset = 0;
243  *end_flag = 1;
244  return(0);
245  }
246  else {
247  chaco_offset = (int) (ptr2 - chaco_line) / sizeof(char);
248  }
249 
250  return(val);
251 }
252 
253 
259 FILE *infile /* file to read value from */
260 )
261 {
262  char c; /* character being read */
263 
264  c = fgetc(infile);
265  while (c != '\n' && c != '\f')
266  c = fgetc(infile);
267 }
268 
276 FILE *fin, /* input file */
277 char *inname, /* name of input file */
278 int **start, /* start of edge list for each vertex */
279 int **adjacency, /* edge list data */
280 int *nvtxs, /* number of vertices in graph */
281 int *vwgt_dim, /* # of vertex weights per node */
282 float **vweights, /* vertex weight list data */
283 int *ewgt_dim, /* # of edge weights per edge */
284 float **eweights /* edge weight list data */
285 )
286 {
287  int *adjptr; /* loops through adjacency data */
288  float *ewptr; /* loops through edge weight data */
289  int narcs; /* number of edges expected in graph */
290  int nedges; /* twice number of edges really in graph */
291  int nedge; /* loops through edges for each vertex */
292  int flag; /* condition indicator */
293  int skip_flag; /* should this edge be ignored? */
294  int end_flag; /* indicates end of line or file */
295  int vtx; /* vertex in graph */
296  int line_num; /* line number in input file */
297  int sum_edges; /* total number of edges read so far */
298  int option = 0; /* input option */
299  int using_ewgts; /* are edge weights in input file? */
300  int using_vwgts; /* are vertex weights in input file? */
301  int vtxnums; /* are vertex numbers in input file? */
302  int vertex; /* current vertex being read */
303  int new_vertex; /* new vertex being read */
304  float weight; /* weight being read */
305  float eweight; /* edge weight being read */
306  int neighbor; /* neighbor of current vertex */
307  int self_edge; /* is a self edge encountered? */
308  int ignore_me; /* is this edge being ignored? */
309  int ignored; /* how many edges are ignored? */
310  int error_flag; /* error reading input? */
311  int j; /* loop counters */
312 
313  /* Read first line of input (= nvtxs, narcs, option). */
314  /* The (decimal) digits of the option variable mean: 1's digit not zero => input
315  edge weights 10's digit not zero => input vertex weights 100's digit not zero
316  => include vertex numbers */
317 
318  *start = NULL;
319  *adjacency = NULL;
320  *vweights = NULL;
321  *eweights = NULL;
322 
323  error_flag = 0;
324  line_num = 0;
325 
326  /* Read any leading comment lines */
327  end_flag = 1;
328  while (end_flag == 1) {
329  *nvtxs = chaco_read_int(fin, &end_flag);
330  ++line_num;
331  }
332  if (*nvtxs <= 0) {
333  printf("ERROR in graph file `%s':", inname);
334  printf(" Invalid number of vertices (%d).\n", *nvtxs);
335  fclose(fin);
336  return(1);
337  }
338 
339  narcs = chaco_read_int(fin, &end_flag);
340  if (narcs < 0) {
341  printf("ERROR in graph file `%s':", inname);
342  printf(" Invalid number of expected edges (%d).\n", narcs);
343  fclose(fin);
344  return(1);
345  }
346 
347  /* Check if vertex or edge weights are used */
348  if (!end_flag) {
349  option = chaco_read_int(fin, &end_flag);
350  }
351  using_ewgts = option - 10 * (option / 10);
352  option /= 10;
353  using_vwgts = option - 10 * (option / 10);
354  option /= 10;
355  vtxnums = option - 10 * (option / 10);
356 
357  /* Get weight dimensions from Chaco option */
358  (*vwgt_dim) = using_vwgts;
359  (*ewgt_dim) = using_ewgts;
360 
361  /* Read weight dimensions if they are specified separately */
362  if (!end_flag && using_vwgts==1){
363  j = chaco_read_int(fin, &end_flag);
364  if (!end_flag) (*vwgt_dim) = j;
365  }
366  if (!end_flag && using_ewgts==1){
367  j = chaco_read_int(fin, &end_flag);
368  if (!end_flag) (*ewgt_dim) = j;
369  }
370 
371  /* Discard rest of line */
372  while (!end_flag)
373  j = chaco_read_int(fin, &end_flag);
374 
375  /* Allocate space for rows and columns. */
376  *start = (int *) malloc((unsigned) (*nvtxs + 1) * sizeof(int));
377  if (narcs != 0)
378  *adjacency = (int *) malloc((unsigned) (2 * narcs + 1) * sizeof(int));
379  else
380  *adjacency = NULL;
381 
382  if (using_vwgts)
383  *vweights = (float *) malloc((unsigned) (*nvtxs) * (*vwgt_dim) * sizeof(float));
384  else
385  *vweights = NULL;
386 
387  if (using_ewgts)
388  *eweights = (float *)
389  malloc((unsigned) (2 * narcs + 1) * (*ewgt_dim) * sizeof(float));
390  else
391  *eweights = NULL;
392 
393  adjptr = *adjacency;
394  ewptr = *eweights;
395  self_edge = 0;
396  ignored = 0;
397 
398  sum_edges = 0;
399  nedges = 0;
400  (*start)[0] = 0;
401  vertex = 0;
402  vtx = 0;
403  new_vertex = 1;
404  while ((using_vwgts || vtxnums || narcs) && end_flag != -1) {
405  ++line_num;
406 
407  /* If multiple input lines per vertex, read vertex number. */
408  if (vtxnums) {
409  j = chaco_read_int(fin, &end_flag);
410  if (end_flag) {
411  if (vertex == *nvtxs)
412  break;
413  printf("ERROR in graph file `%s':", inname);
414  printf(" no vertex number in line %d.\n", line_num);
415  fclose(fin);
416  return (1);
417  }
418  if (j != vertex && j != vertex + 1) {
419  printf("ERROR in graph file `%s':", inname);
420  printf(" out-of-order vertex number in line %d.\n", line_num);
421  fclose(fin);
422  return (1);
423  }
424  if (j != vertex) {
425  new_vertex = 1;
426  vertex = j;
427  }
428  else
429  new_vertex = 0;
430  }
431  else
432  vertex = ++vtx;
433 
434  if (vertex > *nvtxs)
435  break;
436 
437  /* If vertices are weighted, read vertex weight. */
438  if (using_vwgts && new_vertex) {
439  for (j=0; j<(*vwgt_dim); j++){
440  weight = chaco_read_val(fin, &end_flag);
441  if (end_flag) {
442  printf("ERROR in graph file `%s':", inname);
443  printf(" not enough weights for vertex %d.\n", vertex);
444  fclose(fin);
445  return (1);
446  }
447  (*vweights)[(vertex-1)*(*vwgt_dim)+j] = weight;
448  }
449  }
450 
451  nedge = 0;
452 
453  /* Read number of adjacent vertex. */
454  neighbor = chaco_read_int(fin, &end_flag);
455 
456  while (!end_flag) {
457  skip_flag = 0;
458  ignore_me = 0;
459 
460  if (using_ewgts) { /* Read edge weight if it's being input. */
461  for (j=0; j<(*ewgt_dim); j++){
462  eweight = chaco_read_val(fin, &end_flag);
463 
464  if (end_flag) {
465  printf("ERROR in graph file `%s':", inname);
466  printf(" not enough weights for edge (%d,%d).\n", vertex, neighbor);
467  fclose(fin);
468  return (1);
469  }
470 
471  else {
472  *ewptr++ = eweight;
473  }
474  }
475  }
476 
477  /* Add edge to data structure. */
478  if (!skip_flag) {
479  if (++nedges > 2*narcs) {
480  printf("ERROR in graph file `%s':", inname);
481  printf(" at least %d adjacencies entered, but nedges = %d\n",
482  nedges, narcs);
483  fclose(fin);
484  return (1);
485  }
486  *adjptr++ = neighbor;
487  nedge++;
488  }
489 
490  /* Read number of next adjacent vertex. */
491  neighbor = chaco_read_int(fin, &end_flag);
492  }
493 
494  sum_edges += nedge;
495  (*start)[vertex] = sum_edges;
496  }
497 
498  /* Make sure there's nothing else in file. */
499  flag = 0;
500  while (!flag && end_flag != -1) {
501  chaco_read_int(fin, &end_flag);
502  if (!end_flag)
503  flag = 1;
504  }
505 
506  (*start)[*nvtxs] = sum_edges;
507 
508  if (vertex != 0) { /* Normal file was read. */
509  if (narcs) {
510  }
511  else { /* no edges, but did have vertex weights or vertex numbers */
512  free(*start);
513  *start = NULL;
514  if (*adjacency != NULL)
515  free(*adjacency);
516  *adjacency = NULL;
517  if (*eweights != NULL)
518  free(*eweights);
519  *eweights = NULL;
520  }
521  }
522 
523  else {
524  /* Graph was empty */
525  free(*start);
526  if (*adjacency != NULL)
527  free(*adjacency);
528  if (*vweights != NULL)
529  free(*vweights);
530  if (*eweights != NULL)
531  free(*eweights);
532  *start = NULL;
533  *adjacency = NULL;
534  }
535 
536  fclose(fin);
537 
538  return (error_flag);
539 }
540 
541 
549 FILE *fingeom, /* geometry input file */
550 char *geomname, /* name of geometry file */
551 int nvtxs, /* number of coordinates to read */
552 int *igeom, /* dimensionality of geometry */
553 float **x, /* coordinates of vertices */
554 float **y,
555 float **z
556 )
557 {
558  float xc, yc, zc =0; /* first x, y, z coordinate */
559  int nread; /* number of lines of coordinates read */
560  int flag; /* any bad data at end of file? */
561  int line_num; /* counts input lines in file */
562  int end_flag; /* return conditional */
563  int ndims; /* number of values in an input line */
564  int i=0; /* loop counter */
565 
566  *x = *y = *z = NULL;
567  line_num = 0;
568  end_flag = 1;
569  while (end_flag == 1) {
570  xc = chaco_read_val(fingeom, &end_flag);
571  ++line_num;
572  }
573 
574  if (end_flag == -1) {
575  printf("No values found in geometry file `%s'\n", geomname);
576  fclose(fingeom);
577  return (1);
578  }
579 
580  ndims = 1;
581  yc = chaco_read_val(fingeom, &end_flag);
582  if (end_flag == 0) {
583  ndims = 2;
584  zc = chaco_read_val(fingeom, &end_flag);
585  if (end_flag == 0) {
586  ndims = 3;
587  chaco_read_val(fingeom, &end_flag);
588  if (!end_flag) {
589  printf("Too many values on input line of geometry file `%s'\n",
590  geomname);
591 
592  printf(" Maximum dimensionality is 3\n");
593  fclose(fingeom);
594  return (1);
595  }
596  }
597  }
598 
599  *igeom = ndims;
600 
601  *x = (float *) malloc((unsigned) nvtxs * sizeof(float));
602  (*x)[0] = xc;
603  if (ndims > 1) {
604  *y = (float *) malloc((unsigned) nvtxs * sizeof(float));
605  (*y)[0] = yc;
606  }
607  if (ndims > 2) {
608  *z = (float *) malloc((unsigned) nvtxs * sizeof(float));
609  (*z)[0] = zc;
610  }
611 
612  for (nread = 1; nread < nvtxs; nread++) {
613  ++line_num;
614  if (ndims == 1) {
615  i = fscanf(fingeom, "%f", &((*x)[nread]));
616  }
617  else if (ndims == 2) {
618  i = fscanf(fingeom, "%f%f", &((*x)[nread]), &((*y)[nread]));
619  }
620  else if (ndims == 3) {
621  i = fscanf(fingeom, "%f%f%f", &((*x)[nread]), &((*y)[nread]),
622  &((*z)[nread]));
623  }
624 
625  if (i == EOF) {
626  printf("Too few lines of values in geometry file; nvtxs=%d, but only %d read\n",
627  nvtxs, nread);
628  fclose(fingeom);
629  return (1);
630  }
631  else if (i != ndims) {
632  printf("Wrong number of values in line %d of geometry file `%s'\n",
633  line_num, geomname);
634  fclose(fingeom);
635  return (1);
636  }
637  }
638 
639  /* Check for spurious extra stuff in file. */
640  flag = 0;
641  end_flag = 0;
642  while (!flag && end_flag != -1) {
643  chaco_read_val(fingeom, &end_flag);
644  if (!end_flag)
645  flag = 1;
646  }
647 
648  fclose(fingeom);
649 
650  return (0);
651 }
652 
653 } // namespace Zoltan2
static int chaco_save_pnt
static void chaco_flush_line(FILE *)
helper function for zoltan1 chaco reader code Reader code was copied from zoltan1 test driver code...
double chaco_read_val(FILE *infile, int *end_flag)
read a double from a file Reader code was copied from zoltan1 test driver code.
static int chaco_offset
int chaco_read_int(FILE *infile, int *end_flag)
read a int from a file Reader code was copied from zoltan1 test driver code.
static int chaco_break_pnt
static char chaco_line[LINE_LENGTH]
#define LINE_LENGTH
int chaco_input_graph(FILE *fin, char *inname, int **start, int **adjacency, int *nvtxs, int *vwgt_dim, float **vweights, int *ewgt_dim, float **eweights)
read a Chaco graph file Reader code was copied from zoltan1 test driver code. File is closed when rea...
int chaco_input_geom(FILE *fingeom, char *geomname, int nvtxs, int *igeom, float **x, float **y, float **z)
read a Chaco coordinates file Reader code was copied from zoltan1 test driver code. File is closed when read is completed.