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