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