EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
io.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "mmio.h"
6 #include "io.h"
7 #include "evsl.h"
8 
9 #define ERR_IO 10
10 
11 char *mybasename (const char *name) {
12  const char *base = name;
13  while (*name) {
14  if (*name++ == '/') {
15  base = name;
16  }
17  }
18  return (char *) base;
19 }
20 
21 /*-----------------------------------------------*/
22 int get_matrix_info( FILE *fmat, io_t *pio ){
23 // char path[MAX_LINE], MatNam[MaxNamLen], Fmt[4], ca[2], cb[2], cn_intv[2]; eugene comment
24  char path1[MAX_LINE], path2[MAX_LINE], MatNam1[MaxNamLen], MatNam2[MaxNamLen], Fmt[4], ca[100], cb[100], cn_intv[100];
25  int count, n_intv;
26  double a, b;
27  /*-------------------- READ LINE */
28  if (8 != fscanf(fmat,"%s %s %s %s %s %s %s %s\n",path1, path2, MatNam1, MatNam2, Fmt, ca, cb, cn_intv) ) {
29  printf("warning: fscanf may not be successfully done\n");
30  }
31  /*-------------------- file pathname for stiffness and mass matrices*/
32  count = strlen(path1);
33  memset(pio->Fname1,0,MAX_LINE*sizeof(char));
34  memcpy(pio->Fname1,path1,count*sizeof(char));
35  count = strlen(path2);
36  memset(pio->Fname2,0,MAX_LINE*sizeof(char));
37  memcpy(pio->Fname2,path2,count*sizeof(char));
38  /*-------------------- file short names for stiffness and mass matrices */
39  count = strlen(MatNam1);
40  memset(pio->MatNam1,0,MaxNamLen*sizeof(char));
41  memcpy(pio->MatNam1,MatNam1,count*sizeof(char));
42  count = strlen(MatNam2);
43  memset(pio->MatNam2,0,MaxNamLen*sizeof(char));
44  memcpy(pio->MatNam2,MatNam2,count*sizeof(char));
45  /*-------------------- matrix format */
46  if (strcmp(Fmt,"HB")==0)
47  pio->Fmt = 1;
48  else
49  if (strcmp(Fmt,"MM0")==0)
50  pio->Fmt = MM0;
51  else
52  if (strcmp(Fmt,"MM1")==0)
53  pio->Fmt = MM1;
54  else
55  /*-------------------- UNKNOWN_FORMAT */
56  return(ERR_IO+2);
57  /*-------------------- interval information */
58  a = atof(ca);
59  b = atof(cb);
60  pio->a = a;
61  pio->b = b;
62  n_intv = atoi(cn_intv);
63  pio->n_intv = n_intv;
64  /* debug printf(" Echo: %s %s %s \n", pio->Fname, pio->MatNam, Fmt); */
65  return(0);
66 }
67 
68 /*---------------------------------------------*
69  * READ COO Matrix Market *
70  *---------------------------------------------*/
71 
72 int read_coo_MM(const char *matfile, int idxin, int idxout, cooMat *Acoo) {
73  MM_typecode matcode;
74  FILE *p = fopen(matfile,"r");
75  int i;
76  if (p == NULL) {
77  printf("Unable to open mat file %s\n", matfile);
78  exit(-1);
79  }
80  /*----------- READ MM banner */
81  if (mm_read_banner(p, &matcode) != 0){
82  printf("Could not process Matrix Market banner.\n");
83  return 1;
84  }
85  if (!mm_is_valid(matcode)){
86  printf("Invalid Matrix Market file.\n");
87  return 1;
88  }
89  if ( !( (mm_is_real(matcode) || mm_is_integer(matcode)) && mm_is_coordinate(matcode)
90  && mm_is_sparse(matcode) ) ) {
91  printf("Only sparse real-valued/integer coordinate \
92  matrices are supported\n");
93  return 1;
94  }
95  int nrow, ncol, nnz, nnz2, k, j;
96  char line[MAX_LINE];
97  /*------------- Read size */
98  if (mm_read_mtx_crd_size(p, &nrow, &ncol, &nnz) !=0) {
99  printf("MM read size error !\n");
100  return 1;
101  }
102  if (nrow != ncol) {
103  fprintf(stdout,"This is not a square matrix!\n");
104  return 1;
105  }
106  /*--------------------------------------
107  * symmetric case : only L part stored,
108  * so nnz2 := 2*nnz - nnz of diag,
109  * so nnz2 <= 2*nnz
110  *-------------------------------------*/
111  if (mm_is_symmetric(matcode)){
112  /*printf(" * * * * matrix is symmetric * * * * \n");*/
113  nnz2 = 2*nnz;
114  } else {
115  nnz2 = nnz;
116  }
117  /*-------- Allocate mem for COO */
118  int* IR = (int *)malloc(nnz2*sizeof(int));
119  int* JC = (int *)malloc(nnz2*sizeof(int));
120  double* VAL = (double *)malloc(nnz2*sizeof(double));
121  /*-------- read line by line */
122  char *p1, *p2;
123  for (k=0; k<nnz; k++) {
124  if (fgets(line, MAX_LINE, p) == NULL) {return -1;};
125  for( p1 = line; ' ' == *p1; p1++ );
126  /*----------------- 1st entry - row index */
127  for( p2 = p1; ' ' != *p2; p2++ );
128  *p2 = '\0';
129  float tmp1 = atof(p1);
130  //coo.ir[k] = atoi(p1);
131  IR[k] = (int) tmp1;
132  /*-------------- 2nd entry - column index */
133  for( p1 = p2+1; ' ' == *p1; p1++ );
134  for( p2 = p1; ' ' != *p2; p2++ );
135  *p2 = '\0';
136  float tmp2 = atof(p1);
137  JC[k] = (int) tmp2;
138  //coo.jc[k] = atoi(p1);
139  /*------------- 3rd entry - nonzero entry */
140  p1 = p2+1;
141  VAL[k] = atof(p1);
142  }
143  /*------------------ Symmetric case */
144  j = nnz;
145  if (mm_is_symmetric(matcode)) {
146  for (k=0; k<nnz; k++)
147  if (IR[k] != JC[k]) {
148  /*------------------ off-diag entry */
149  IR[j] = JC[k];
150  JC[j] = IR[k];
151  VAL[j] = VAL[k];
152  j++;
153  }
154  if (j != nnz2) {
155  nnz2 = j;
156  }
157  }
158  int offset = idxout - idxin;
159  if (offset) {
160  for (i=0; i<nnz2; i++) {
161  IR[i] += offset;
162  JC[i] += offset;
163  }
164  }
165  // printf("nrow = %d, ncol = %d, nnz = %d\n", nrow, ncol, j);
166  fclose(p);
167  Acoo->nrows = nrow;
168  Acoo->ncols = ncol;
169  Acoo->nnz = nnz2;
170  Acoo->ir = IR;
171  Acoo->jc = JC;
172  Acoo->vv = VAL;
173  return 0;
174 }
175 
176 // parse command-line input parameters
177 int findarg(const char *argname, ARG_TYPE type, void *val, int argc, char **argv) {
178  int *outint;
179  double *outdouble;
180  char *outchar;
181  int i;
182  for (i=0; i<argc; i++) {
183  if (argv[i][0] != '-') {
184  continue;
185  }
186  if (!strcmp(argname, argv[i]+1)) {
187  if (type == NA) {
188  return 1;
189  } else {
190  if (i+1 >= argc /*|| argv[i+1][0] == '-'*/) {
191  return 0;
192  }
193  switch (type) {
194  case INT:
195  outint = (int *) val;
196  *outint = atoi(argv[i+1]);
197  return 1;
198  break;
199  case DOUBLE:
200  outdouble = (double *) val;
201  *outdouble = atof(argv[i+1]);
202  return 1;
203  break;
204  case STR:
205  outchar = (char *) val;
206  sprintf(outchar, "%s", argv[i+1]);
207  return 1;
208  break;
209  default:
210  printf("unknown arg type\n");
211  }
212  }
213  }
214  }
215  return 0;
216 }
217 
int findarg(const char *argname, ARG_TYPE type, void *val, int argc, char **argv)
Definition: io.c:171
double b
Definition: io.h:25
#define MAX_LINE
Definition: io.h:5
ARG_TYPE
Definition: io.h:33
int nnz
Definition: struct.h:17
Definition: io.h:35
double * vv
Definition: struct.h:22
int Fmt
Definition: io.h:20
Definition: io.h:34
int * jc
Definition: struct.h:17
Definition: io.h:36
int * ir
Definition: struct.h:17
char Fname1[MAX_LINE]
Definition: io.h:17
int mm_read_mtx_crd_size(FILE *f, int *M, int *N, int *nz)
Definition: mmio.c:184
char Fname2[MAX_LINE]
Definition: io.h:19
char MatNam2[MaxNamLen]
Definition: io.h:20
#define MaxNamLen
Definition: io.h:6
int mm_read_banner(FILE *f, MM_typecode *matcode)
Definition: mmio.c:92
#define mm_is_real(typecode)
Definition: mmio.h:39
#define mm_is_coordinate(typecode)
Definition: mmio.h:34
#define mm_is_sparse(typecode)
Definition: mmio.h:33
int ncols
Definition: struct.h:17
This file contains function prototypes and constant definitions for EVSL.
Definition: io.h:14
int mm_is_valid(MM_typecode matcode)
Definition: mmio.c:82
char * mybasename(const char *name)
Definition: io.c:11
double a
Definition: io.h:24
#define mm_is_integer(typecode)
Definition: mmio.h:41
#define ERR_IO
Definition: io.c:9
char MM_typecode[4]
Definition: mmio.h:16
int read_coo_MM(const char *matfile, int idxin, int idxout, cooMat *Acoo)
Definition: io.c:66
int nrows
Definition: struct.h:17
int get_matrix_info(FILE *fmat, io_t *pio)
Definition: io.c:22
sparse matrix format: the coordinate (COO) format, 0-based
Definition: struct.h:16
#define mm_is_symmetric(typecode)
Definition: mmio.h:43
int n_intv
Definition: io.h:23
Definition: io.h:37
char MatNam1[MaxNamLen]
Definition: io.h:18
#define MM1
Definition: io.h:9
#define MM0
Definition: io.h:8