CSS product code  0.1
C++ library to estimate distance of CSS codes. Some particular construction of CSS codes are implemented.
mmio.c
Go to the documentation of this file.
1 /*
2 * Matrix Market I/O library for ANSI C
3 *
4 * See http://math.nist.gov/MatrixMarket for details.
5 *
6 *
7 */
8 
9 
10 
11 
12 #include "mmio.h"
13 /*hide by weilei on Dec 2019. reason: this function is not used anywhere.
14 int mm_read_unsymmetric_sparse(const char *fname, int *M_, int *N_, int *nz_,
15  double **val_, int **I_, int **J_)
16 {
17  FILE *f;
18  MM_typecode matcode;
19  int M, N, nz;
20  int i;
21  double *val;
22  int *I, *J;
23 
24  if ((f = fopen(fname, "r")) == NULL)
25  return -1;
26 
27 
28  if (mm_read_banner(f, &matcode) != 0)
29  {
30  printf("mm_read_unsymetric: Could not process Matrix Market banner ");
31  printf(" in file [%s]\n", fname);
32  return -1;
33  }
34 
35 
36 
37  if ( !(mm_is_real(matcode) && mm_is_matrix(matcode) &&
38  mm_is_sparse(matcode)))
39  {
40  fprintf(stderr, "Sorry, this application does not support ");
41  fprintf(stderr, "Market Market type: [%s]\n",
42  mm_typecode_to_str(matcode));
43  return -1;
44  }
45 
46  // find out size of sparse matrix: M, N, nz ....
47 
48  if (mm_read_mtx_crd_size(f, &M, &N, &nz) !=0)
49  {
50  fprintf(stderr, "read_unsymmetric_sparse(): could not parse matrix size.\n");
51  return -1;
52  }
53 
54  *M_ = M;
55  *N_ = N;
56  *nz_ = nz;
57 
58 // reseve memory for matrices
59 
60  I = (int *) malloc(nz * sizeof(int));
61  J = (int *) malloc(nz * sizeof(int));
62  val = (double *) malloc(nz * sizeof(double));
63 
64  *val_ = val;
65  *I_ = I;
66  *J_ = J;
67 
68  // NOTE: when reading in doubles, ANSI C requires the use of the "l"
69  // specifier as in "%lg", "%lf", "%le", otherwise errors will occur
70  // (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15)
71 
72  for (i=0; i<nz; i++)
73  {
74  fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
75  I[i]--; // adjust from 1-based to 0-based
76  J[i]--;
77  }
78  fclose(f);
79 
80  return 0;
81 }
82 */
83 
85 {
86  if (!mm_is_matrix(matcode)) return 0;
87  if (mm_is_dense(matcode) && mm_is_pattern(matcode)) return 0;
88  if (mm_is_real(matcode) && mm_is_hermitian(matcode)) return 0;
89  if (mm_is_pattern(matcode) && (mm_is_hermitian(matcode) ||
90  mm_is_skew(matcode))) return 0;
91  return 1;
92 }
93 
94 int mm_read_banner(FILE *f, MM_typecode *matcode)
95 {
96  char line[MM_MAX_LINE_LENGTH];
97  char banner[MM_MAX_TOKEN_LENGTH];
98  char mtx[MM_MAX_TOKEN_LENGTH];
99  char crd[MM_MAX_TOKEN_LENGTH];
100  char data_type[MM_MAX_TOKEN_LENGTH];
101  char storage_scheme[MM_MAX_TOKEN_LENGTH];
102  char *p;
103 
104 
105  mm_clear_typecode(matcode);
106 
107  if (fgets(line, MM_MAX_LINE_LENGTH, f) == NULL)
108  return MM_PREMATURE_EOF;
109 
110  if (sscanf(line, "%s %s %s %s %s", banner, mtx, crd, data_type,
111  storage_scheme) != 5)
112  return MM_PREMATURE_EOF;
113 
114  for (p=mtx; *p!='\0'; *p=tolower(*p),p++); /* convert to lower case */
115  for (p=crd; *p!='\0'; *p=tolower(*p),p++);
116  for (p=data_type; *p!='\0'; *p=tolower(*p),p++);
117  for (p=storage_scheme; *p!='\0'; *p=tolower(*p),p++);
118 
119  /* check for banner */
120  if (strncmp(banner, MatrixMarketBanner, strlen(MatrixMarketBanner)) != 0)
121  return MM_NO_HEADER;
122 
123  /* first field should be "mtx" */
124  if (strcmp(mtx, MM_MTX_STR) != 0)
125  return MM_UNSUPPORTED_TYPE;
126  mm_set_matrix(matcode);
127 
128 
129  /* second field describes whether this is a sparse matrix (in coordinate
130  storgae) or a dense array */
131 
132 
133  if (strcmp(crd, MM_SPARSE_STR) == 0)
134  mm_set_sparse(matcode);
135  else
136  if (strcmp(crd, MM_DENSE_STR) == 0)
137  mm_set_dense(matcode);
138  else
139  return MM_UNSUPPORTED_TYPE;
140 
141 
142  /* third field */
143 
144  if (strcmp(data_type, MM_REAL_STR) == 0)
145  mm_set_real(matcode);
146  else
147  if (strcmp(data_type, MM_COMPLEX_STR) == 0)
148  mm_set_complex(matcode);
149  else
150  if (strcmp(data_type, MM_PATTERN_STR) == 0)
151  mm_set_pattern(matcode);
152  else
153  if (strcmp(data_type, MM_INT_STR) == 0)
154  mm_set_integer(matcode);
155  else
156  return MM_UNSUPPORTED_TYPE;
157 
158 
159  /* fourth field */
160 
161  if (strcmp(storage_scheme, MM_GENERAL_STR) == 0)
162  mm_set_general(matcode);
163  else
164  if (strcmp(storage_scheme, MM_SYMM_STR) == 0)
165  mm_set_symmetric(matcode);
166  else
167  if (strcmp(storage_scheme, MM_HERM_STR) == 0)
168  mm_set_hermitian(matcode);
169  else
170  if (strcmp(storage_scheme, MM_SKEW_STR) == 0)
171  mm_set_skew(matcode);
172  else
173  return MM_UNSUPPORTED_TYPE;
174 
175 
176  return 0;
177 }
178 
179 int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
180 {
181  if (fprintf(f, "%d %d %d\n", M, N, nz) != 3)
183  else
184  return 0;
185 }
186 
187 int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz )
188 {
189  char line[MM_MAX_LINE_LENGTH];
190  int num_items_read;
191 
192  /* set return null parameter values, in case we exit with errors */
193  *M = *N = *nz = 0;
194 
195  /* now continue scanning until you reach the end-of-comments */
196  do
197  {
198  if (fgets(line,MM_MAX_LINE_LENGTH,f) == NULL)
199  return MM_PREMATURE_EOF;
200  }while (line[0] == '%');
201 
202  /* line[] is either blank or has M,N, nz */
203  if (sscanf(line, "%d %d %d", M, N, nz) == 3)
204  return 0;
205 
206  else
207  do
208  {
209  num_items_read = fscanf(f, "%d %d %d", M, N, nz);
210  if (num_items_read == EOF) return MM_PREMATURE_EOF;
211  }
212  while (num_items_read != 3);
213 
214  return 0;
215 }
216 
217 
218 int mm_read_mtx_array_size(FILE *f, int *M, int *N)
219 {
220  char line[MM_MAX_LINE_LENGTH];
221  int num_items_read;
222  /* set return null parameter values, in case we exit with errors */
223  *M = *N = 0;
224 
225  /* now continue scanning until you reach the end-of-comments */
226  do
227  {
228  if (fgets(line,MM_MAX_LINE_LENGTH,f) == NULL)
229  return MM_PREMATURE_EOF;
230  }while (line[0] == '%');
231 
232  /* line[] is either blank or has M,N, nz */
233  if (sscanf(line, "%d %d", M, N) == 2)
234  return 0;
235 
236  else /* we have a blank line */
237  do
238  {
239  num_items_read = fscanf(f, "%d %d", M, N);
240  if (num_items_read == EOF) return MM_PREMATURE_EOF;
241  }
242  while (num_items_read != 2);
243 
244  return 0;
245 }
246 
247 int mm_write_mtx_array_size(FILE *f, int M, int N)
248 {
249  if (fprintf(f, "%d %d\n", M, N) != 2)
251  else
252  return 0;
253 }
254 
255 
256 
257 /*-------------------------------------------------------------------------*/
258 
259 /******************************************************************/
260 /* use when I[], J[], and val[]J, and val[] are already allocated */
261 /******************************************************************/
262 
263 int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[],
264  double val[], MM_typecode matcode)
265 {
266  int i;
267  if (mm_is_complex(matcode))
268  {
269  for (i=0; i<nz; i++)
270  if (fscanf(f, "%d %d %lg %lg", &I[i], &J[i], &val[2*i], &val[2*i+1])
271  != 4) return MM_PREMATURE_EOF;
272  }
273  else if (mm_is_real(matcode))
274  {
275  for (i=0; i<nz; i++)
276  {
277  if (fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i])
278  != 3) return MM_PREMATURE_EOF;
279 
280  }
281  }
282 
283  else if (mm_is_pattern(matcode))
284  {
285  for (i=0; i<nz; i++)
286  if (fscanf(f, "%d %d", &I[i], &J[i])
287  != 2) return MM_PREMATURE_EOF;
288  }
289  else
290  return MM_UNSUPPORTED_TYPE;
291 
292  return 0;
293 
294 }
295 
296 int mm_read_mtx_crd_entry(FILE *f, int *I, int *J,
297  double *real, double *imag, MM_typecode matcode)
298 {
299  if (mm_is_complex(matcode))
300  {
301  if (fscanf(f, "%d %d %lg %lg", I, J, real, imag)
302  != 4) return MM_PREMATURE_EOF;
303  }
304  else if (mm_is_real(matcode))
305  {
306  if (fscanf(f, "%d %d %lg\n", I, J, real)
307  != 3) return MM_PREMATURE_EOF;
308 
309  }
310 
311  else if (mm_is_pattern(matcode))
312  {
313  if (fscanf(f, "%d %d", I, J) != 2) return MM_PREMATURE_EOF;
314  }
315  else
316  return MM_UNSUPPORTED_TYPE;
317 
318  return 0;
319 
320 }
321 
322 
323 /************************************************************************
324  mm_read_mtx_crd() fills M, N, nz, array of values, and return
325  type code, e.g. 'MCRS'
326 
327  if matrix is complex, values[] is of size 2*nz,
328  (nz pairs of real/imaginary values)
329 ************************************************************************/
330 
331 int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J,
332  double **val, MM_typecode *matcode)
333 {
334  int ret_code;
335  FILE *f;
336 
337  if (strcmp(fname, "stdin") == 0) f=stdin;
338  else
339  if ((f = fopen(fname, "r")) == NULL)
340  return MM_COULD_NOT_READ_FILE;
341 
342 
343  if ((ret_code = mm_read_banner(f, matcode)) != 0)
344  return ret_code;
345 
346  if (!(mm_is_valid(*matcode) && mm_is_sparse(*matcode) &&
347  mm_is_matrix(*matcode)))
348  return MM_UNSUPPORTED_TYPE;
349 
350  if ((ret_code = mm_read_mtx_crd_size(f, M, N, nz)) != 0)
351  return ret_code;
352 
353 
354  *I = (int *) malloc(*nz * sizeof(int));
355  *J = (int *) malloc(*nz * sizeof(int));
356  *val = NULL;
357 
358  if (mm_is_complex(*matcode))
359  {
360  *val = (double *) malloc(*nz * 2 * sizeof(double));
361  ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
362  *matcode);
363  if (ret_code != 0) return ret_code;
364  }
365  else if (mm_is_real(*matcode))
366  {
367  *val = (double *) malloc(*nz * sizeof(double));
368  ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
369  *matcode);
370  if (ret_code != 0) return ret_code;
371  }
372 
373  else if (mm_is_pattern(*matcode))
374  {
375  ret_code = mm_read_mtx_crd_data(f, *M, *N, *nz, *I, *J, *val,
376  *matcode);
377  if (ret_code != 0) return ret_code;
378  }
379 
380  if (f != stdin) fclose(f);
381  return 0;
382 }
383 
384 int mm_write_banner(FILE *f, MM_typecode matcode)
385 {
386  char *str = mm_typecode_to_str(matcode);
387  int ret_code;
388 
389  ret_code = fprintf(f, "%s %s\n", MatrixMarketBanner, str);
390  free(str);
391  if (ret_code !=2 )
393  else
394  return 0;
395 }
396 
397 int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[],
398  double val[], MM_typecode matcode)
399 {
400  FILE *f;
401  int i;
402 
403  if (strcmp(fname, "stdout") == 0)
404  f = stdout;
405  else
406  if ((f = fopen(fname, "w")) == NULL)
408 
409  /* print banner followed by typecode */
410  fprintf(f, "%s ", MatrixMarketBanner);
411  fprintf(f, "%s\n", mm_typecode_to_str(matcode));
412 
413  /* print matrix sizes and nonzeros */
414  fprintf(f, "%d %d %d\n", M, N, nz);
415 
416  /* print values */
417  if (mm_is_pattern(matcode))
418  for (i=0; i<nz; i++)
419  fprintf(f, "%d %d\n", I[i], J[i]);
420  else
421  if (mm_is_real(matcode))
422  for (i=0; i<nz; i++)
423  fprintf(f, "%d %d %20.16g\n", I[i], J[i], val[i]);
424  else
425  if (mm_is_complex(matcode))
426  for (i=0; i<nz; i++)
427  fprintf(f, "%d %d %20.16g %20.16g\n", I[i], J[i], val[2*i],
428  val[2*i+1]);
429  else
430  {
431  if (f != stdout) fclose(f);
432  return MM_UNSUPPORTED_TYPE;
433  }
434 
435  if (f !=stdout) fclose(f);
436 
437  return 0;
438 }
439 
440 
446 char *mm_strdup(const char *s)
447 {
448  int len = strlen(s);
449  char *s2 = (char *) malloc((len+1)*sizeof(char));
450  return strcpy(s2, s);
451 }
452 
454 {
455  char buffer[MM_MAX_LINE_LENGTH];
456  char *types[4];
457  char *mm_strdup(const char *);
458 
459  // int error =0; //weilei removed
460 
461  //weilei add (char *) to avoid warning in C++. deperacated conversoin from string to char
462  /* check for MTX type */
463  if (mm_is_matrix(matcode))
464  types[0] =(char *) MM_MTX_STR;
465  // else //weilei removed
466  // error=1;
467 
468  /* check for CRD or ARR matrix */
469  if (mm_is_sparse(matcode))
470  types[1] =(char *) MM_SPARSE_STR;
471  else
472  if (mm_is_dense(matcode))
473  types[1] =(char *) MM_DENSE_STR;
474  else
475  return NULL;
476 
477  /* check for element data type */
478  if (mm_is_real(matcode))
479  types[2] =(char *) MM_REAL_STR;
480  else
481  if (mm_is_complex(matcode))
482  types[2] =(char *) MM_COMPLEX_STR;
483  else
484  if (mm_is_pattern(matcode))
485  types[2] =(char *) MM_PATTERN_STR;
486  else
487  if (mm_is_integer(matcode))
488  types[2] =(char *) MM_INT_STR;
489  else
490  return NULL;
491 
492 
493  /* check for symmetry type */
494  if (mm_is_general(matcode))
495  types[3] = (char *) MM_GENERAL_STR;
496  else
497  if (mm_is_symmetric(matcode))
498  types[3] = (char *) MM_SYMM_STR;
499  else
500  if (mm_is_hermitian(matcode))
501  types[3] = (char *) MM_HERM_STR;
502  else
503  if (mm_is_skew(matcode))
504  types[3] =(char *) MM_SKEW_STR;
505  else
506  return NULL;
507 
508  sprintf(buffer,"%s %s %s %s", types[0], types[1], types[2], types[3]);
509  return mm_strdup(buffer);
510 
511 }
MM_HERM_STR
#define MM_HERM_STR
Definition: mmio.h:129
MM_MTX_STR
#define MM_MTX_STR
Definition: mmio.h:119
mm_typecode_to_str
char * mm_typecode_to_str(MM_typecode matcode)
Definition: mmio.c:453
MM_PREMATURE_EOF
#define MM_PREMATURE_EOF
Definition: mmio.h:95
mm_write_mtx_array_size
int mm_write_mtx_array_size(FILE *f, int M, int N)
Definition: mmio.c:247
mm_is_dense
#define mm_is_dense(typecode)
Definition: mmio.h:50
mm_set_sparse
#define mm_set_sparse(typecode)
Definition: mmio.h:72
MM_COULD_NOT_WRITE_FILE
#define MM_COULD_NOT_WRITE_FILE
Definition: mmio.h:100
mm_read_mtx_crd
int mm_read_mtx_crd(char *fname, int *M, int *N, int *nz, int **I, int **J, double **val, MM_typecode *matcode)
Definition: mmio.c:331
MM_NO_HEADER
#define MM_NO_HEADER
Definition: mmio.h:97
MM_SYMM_STR
#define MM_SYMM_STR
Definition: mmio.h:128
mm_set_real
#define mm_set_real(typecode)
Definition: mmio.h:75
mm_is_hermitian
#define mm_is_hermitian(typecode)
Definition: mmio.h:61
mm_read_mtx_crd_size
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
Definition: mmio.c:187
mmio.h
MM_MAX_TOKEN_LENGTH
#define MM_MAX_TOKEN_LENGTH
Definition: mmio.h:21
mm_is_general
#define mm_is_general(typecode)
Definition: mmio.h:59
mm_is_pattern
#define mm_is_pattern(typecode)
Definition: mmio.h:55
MM_UNSUPPORTED_TYPE
#define MM_UNSUPPORTED_TYPE
Definition: mmio.h:98
mm_set_skew
#define mm_set_skew(typecode)
Definition: mmio.h:82
mm_set_general
#define mm_set_general(typecode)
Definition: mmio.h:81
mm_clear_typecode
#define mm_clear_typecode(typecode)
Definition: mmio.h:85
mm_is_symmetric
#define mm_is_symmetric(typecode)
Definition: mmio.h:58
mm_read_mtx_crd_data
int mm_read_mtx_crd_data(FILE *f, int M, int N, int nz, int I[], int J[], double val[], MM_typecode matcode)
Definition: mmio.c:263
mm_is_integer
#define mm_is_integer(typecode)
Definition: mmio.h:56
mm_write_mtx_crd_size
int mm_write_mtx_crd_size(FILE *f, int M, int N, int nz)
Definition: mmio.c:179
mm_write_mtx_crd
int mm_write_mtx_crd(char fname[], int M, int N, int nz, int I[], int J[], double val[], MM_typecode matcode)
Definition: mmio.c:397
mm_read_mtx_array_size
int mm_read_mtx_array_size(FILE *f, int *M, int *N)
Definition: mmio.c:218
MM_MAX_LINE_LENGTH
#define MM_MAX_LINE_LENGTH
Definition: mmio.h:19
mm_set_complex
#define mm_set_complex(typecode)
Definition: mmio.h:74
mm_set_pattern
#define mm_set_pattern(typecode)
Definition: mmio.h:76
MM_REAL_STR
#define MM_REAL_STR
Definition: mmio.h:125
MM_SKEW_STR
#define MM_SKEW_STR
Definition: mmio.h:130
mm_set_matrix
#define mm_set_matrix(typecode)
Definition: mmio.h:68
MM_typecode
char MM_typecode[4]
Definition: mmio.h:23
mm_set_dense
#define mm_set_dense(typecode)
Definition: mmio.h:71
mm_is_valid
int mm_is_valid(MM_typecode matcode)
Definition: mmio.c:84
MM_SPARSE_STR
#define MM_SPARSE_STR
Definition: mmio.h:123
MM_COMPLEX_STR
#define MM_COMPLEX_STR
Definition: mmio.h:124
mm_is_matrix
#define mm_is_matrix(typecode)
Definition: mmio.h:46
mm_is_skew
#define mm_is_skew(typecode)
Definition: mmio.h:60
mm_write_banner
int mm_write_banner(FILE *f, MM_typecode matcode)
Definition: mmio.c:384
mm_set_integer
#define mm_set_integer(typecode)
Definition: mmio.h:77
mm_set_symmetric
#define mm_set_symmetric(typecode)
Definition: mmio.h:80
mm_is_complex
#define mm_is_complex(typecode)
Definition: mmio.h:53
MatrixMarketBanner
#define MatrixMarketBanner
Definition: mmio.h:20
mm_set_hermitian
#define mm_set_hermitian(typecode)
Definition: mmio.h:83
mm_strdup
char * mm_strdup(const char *s)
Definition: mmio.c:446
mm_is_real
#define mm_is_real(typecode)
Definition: mmio.h:54
MM_PATTERN_STR
#define MM_PATTERN_STR
Definition: mmio.h:131
MM_COULD_NOT_READ_FILE
#define MM_COULD_NOT_READ_FILE
Definition: mmio.h:94
mm_read_banner
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition: mmio.c:94
MM_INT_STR
#define MM_INT_STR
Definition: mmio.h:126
MM_GENERAL_STR
#define MM_GENERAL_STR
Definition: mmio.h:127
MM_DENSE_STR
#define MM_DENSE_STR
Definition: mmio.h:121
mm_read_mtx_crd_entry
int mm_read_mtx_crd_entry(FILE *f, int *I, int *J, double *real, double *imag, MM_typecode matcode)
Definition: mmio.c:296
mm_is_sparse
#define mm_is_sparse(typecode)
Definition: mmio.h:48