DD-AVX  2.0.0
matrix_IO.cpp
Go to the documentation of this file.
1 #include<DD-AVX_internal.hpp>
2 #include <cassert>
3 #include <fstream>
4 #include <iomanip>
5 #include <limits>
6 #include <sstream>
7 
8 #define MM_BANNER "%%MatrixMarket"
9 #define MM_MAT "matrix"
10 #define MM_VEC "vector"
11 #define MM_FMT "coordinate"
12 #define MM_TYPE_REAL "real"
13 #define MM_TYPE_GENERAL "general"
14 #define MM_TYPE_SYMM "symmetric"
15 
16 void d_real_SpMat::input_mm(const char* filename){
17 
18  std::string banner, buf;
19  std::string mm, mat, fmt, dtype, dstruct;
20 
21  //file open
22  std::ifstream ifs(filename);
23  if (!ifs) {
24  std::cerr << "Matrix.input: cannot open file " << filename << std::endl;
25  std::exit(1);
26  }
27 
28  //check Matrix Market bannner
29  getline(ifs, banner);
30  std::istringstream bn(banner);
31  bn >> mm >> mat >> fmt >> dtype >> dstruct;
32 
33  if (mm != std::string(MM_BANNER)) {
34  std::cerr << "Matrix.input: This matrix is not MM format:" << mm << std::endl;
35  exit(-1);
36  }
37  if (mat != std::string(MM_MAT)) {
38  std::cerr << "Matrix.input: This matrix is not matrix type:" << mat << std::endl;
39  exit(-1);
40  }
41  if (fmt != std::string(MM_FMT)) {
42  std::cerr << "Matrix.input: This matrix is not coodinate format:" << fmt << std::endl;
43  exit(-1);
44  }
45  if (dtype != std::string(MM_TYPE_REAL)) {
46  std::cerr << "Matrix.input: This matrix is not real:" << dtype << std::endl;
47  exit(-1);
48  }
49  if (dstruct != std::string(MM_TYPE_GENERAL)) {
50  std::cerr << "Matrix.input: This matrix is not general:" << dstruct << std::endl;
51  exit(-1);
52  }
53 
54  //skip %
55  do {
56  getline(ifs, buf);
57  } while (buf[0] == '%');
58 
59  //check size
60  int rowN, colN, NNZ;
61 
62  std::istringstream data(buf);
63  data >> rowN >> colN >> NNZ;
64 
65  //symmetric check!
66  if (colN != rowN) {
67  std::cerr << "Matrix.input: Matrix is not square" << std::endl;
68  exit(-1);
69  }
70  if (colN <= 0 || NNZ < 0) {
71  std::cerr << "Matrix.input: Matrix size should be positive" << std::endl;
72  exit(-1);
73  }
74 
75  row = rowN;
76  nnz = NNZ;
77  val.resize(nnz, 0);
78  row_ptr.resize(row+1, 0);
79  col_ind.resize(nnz, 0);
80 
81  int c_row = 1;
82  row_ptr[0] = 0;
83  //input MM data as selected matrix storage format
84  for (int i = 0; i < NNZ; i++) {
85  int idx, jdx;
86  int value;
87 
88  getline(ifs, buf);
89  std::istringstream data(buf);
90  data >> idx >> jdx >> value;
91 
92  if (idx < 1 || idx > rowN || jdx < 1 || jdx > colN) {
93  std::cerr << "Matrix.input: Invalid index" << std::endl;
94  exit(1);
95  }
96 
97  val[i] = value;
98  col_ind[i] = jdx-1;
99 
100  if(c_row == idx){
101  row_ptr[c_row] = i+1;
102  }
103  else{
104  c_row = c_row + 1;
105  row_ptr[c_row] = i+1;
106  }
107  }
108 
109  ifs.close();
110 }
111 
113 
114  if(row == 0 || nnz == 0){
115  std::cerr << "This matrix is brank" << std::endl;
116  }
117 
118  for(int i = 0; i < row; i++){
119  for(int j = row_ptr[i]; j < row_ptr[i+1]; j++){
120  std::cout << i+1 << "\t" << col_ind[j]+1 << "\t" << val[j] << std::endl;
121  }
122  }
123 }
124 
125 void d_real_SpMat::output_mm(const char* filename){
126 
127  std::ofstream out(filename);
128  out << std::scientific;
129  out << std::setprecision(std::numeric_limits<double>::max_digits10);
130 
131  out << (MM_BANNER " " MM_MAT " " MM_FMT " " MM_TYPE_REAL " " MM_TYPE_GENERAL) << std::endl;
132  out << row << " " << row << " " << nnz << std::endl;
133 
134  for(int i = 0; i < row; i++){
135  for(int j = row_ptr[i]; j < row_ptr[i+1]; j++){
136  out << i+1 << " " << col_ind[j]+1 << " " << val[j] << std::endl;
137  }
138  }
139 }
std::vector< double >::reference data()
get CRS value array
std::vector< int > col_ind
void input_mm(const char *filename)
input matrix from matrix market format
Definition: matrix_IO.cpp:16
void output_mm(const char *filename)
output matrix to matrix market format
Definition: matrix_IO.cpp:125
void output()
output matrix to standard I/O
Definition: matrix_IO.cpp:112
std::vector< int > row_ptr
std::vector< double > val
#define MM_TYPE_REAL
Definition: matrix_IO.cpp:12
#define MM_TYPE_GENERAL
Definition: matrix_IO.cpp:13
#define MM_FMT
Definition: matrix_IO.cpp:11
#define MM_BANNER
Definition: matrix_IO.cpp:8
#define MM_MAT
Definition: matrix_IO.cpp:9