monolish  0.14.0
MONOlithic LIner equation Solvers for Highly-parallel architecture
IO_coo.cpp
Go to the documentation of this file.
1 #include "../../../include/common/monolish_dense.hpp"
2 #include "../../../include/common/monolish_logger.hpp"
3 #include "../../../include/common/monolish_matrix.hpp"
4 #include "../../internal/monolish_internal.hpp"
5 
6 #include <cassert>
7 #include <fstream>
8 #include <iomanip>
9 #include <limits>
10 #include <sstream>
11 #include <stdexcept>
12 #include <string>
13 
14 namespace monolish {
15 namespace matrix {
16 
17 template <typename T> void COO<T>::print_all(bool force_cpu) const {
18  Logger &logger = Logger::get_instance();
19  logger.util_in(monolish_func);
20  std::cout << std::scientific;
21  std::cout << std::setprecision(std::numeric_limits<T>::max_digits10);
22 
23  for (size_t i = 0; i < nnz; i++) {
24  std::cout << row_index[i] + 1 << " " << col_index[i] + 1 << " " << val[i]
25  << std::endl;
26  }
27  logger.util_out();
28 }
29 template void COO<double>::print_all(bool force_cpu) const;
30 template void COO<float>::print_all(bool force_cpu) const;
31 
32 template <typename T> void COO<T>::print_all(std::string filename) const {
33  Logger &logger = Logger::get_instance();
34  logger.util_in(monolish_func);
35  std::ofstream out(filename);
36  out << std::scientific;
37  out << std::setprecision(std::numeric_limits<T>::max_digits10);
38 
39  for (size_t i = 0; i < nnz; i++) {
40  out << row_index[i] + 1 << " " << col_index[i] + 1 << " " << val[i]
41  << std::endl;
42  }
43  logger.util_out();
44 }
45 template void COO<double>::print_all(std::string filename) const;
46 template void COO<float>::print_all(std::string filename) const;
47 
48 template <typename T> void COO<T>::output_mm(const std::string filename) const {
49  Logger &logger = Logger::get_instance();
50  logger.util_in(monolish_func);
51  std::ofstream out(filename);
52  out << std::scientific;
53  out << std::setprecision(std::numeric_limits<T>::max_digits10);
54 
55  out << (MM_BANNER " " MM_MAT " " MM_FMT " " MM_TYPE_REAL " " MM_TYPE_GENERAL)
56  << std::endl;
57  out << rowN << " " << colN << " " << nnz << std::endl;
58 
59  for (size_t i = 0; i < nnz; i++) {
60  out << row_index[i] + 1 << " " << col_index[i] + 1 << " " << val[i]
61  << std::endl;
62  }
63  logger.util_out();
64 }
65 template void COO<double>::output_mm(const std::string filename) const;
66 template void COO<float>::output_mm(const std::string filename) const;
67 
68 template <typename T> void COO<T>::input_mm(const std::string filename) {
69  Logger &logger = Logger::get_instance();
70  logger.util_in(monolish_func);
71 
72  std::string banner, buf;
73  std::string mm, mat, fmt, dtype, dstruct;
74 
75  // file open
76  std::ifstream ifs(filename);
77  if (!ifs) {
78  std::cerr << "Matrix.input: cannot open file " << filename << std::endl;
79  std::exit(1);
80  }
81 
82  // check Matrix Market bannner
83  getline(ifs, banner);
84  std::istringstream bn(banner);
85  bn >> mm >> mat >> fmt >> dtype >> dstruct;
86 
87  if (mm != std::string(MM_BANNER)) {
88  std::cerr << "Matrix.input: This matrix is not MM format:" << mm
89  << std::endl;
90  exit(-1);
91  }
92  if (mat != std::string(MM_MAT)) {
93  std::cerr << "Matrix.input: This matrix is not matrix type:" << mat
94  << std::endl;
95  exit(-1);
96  }
97  if (fmt != std::string(MM_FMT)) {
98  std::cerr << "Matrix.input: This matrix is not coodinate format:" << fmt
99  << std::endl;
100  exit(-1);
101  }
102  if (dtype != std::string(MM_TYPE_REAL)) {
103  std::cerr << "Matrix.input: This matrix is not real:" << dtype << std::endl;
104  exit(-1);
105  }
106  if (dstruct != std::string(MM_TYPE_GENERAL)) {
107  std::cerr << "Matrix.input: This matrix is not general:" << dstruct
108  << std::endl;
109  exit(-1);
110  }
111 
112  // skip %
113  do {
114  getline(ifs, buf);
115  } while (buf[0] == '%');
116 
117  // check size
118  size_t rowNN, colNN, NNZ;
119 
120  std::istringstream data(buf);
121  data >> rowNN >> colNN >> NNZ;
122 
123  if (colNN <= 0 || NNZ < 0) {
124  std::cerr << "Matrix.input: Matrix size should be positive" << std::endl;
125  exit(-1);
126  }
127 
128  rowN = rowNN;
129  colN = colNN;
130  nnz = NNZ;
131 
132  // allocate
133  row_index.resize(nnz, 0.0);
134  col_index.resize(nnz, 0.0);
135  val.resize(nnz, 0.0);
136 
137  // set values
138  for (size_t i = 0; i < nnz; i++) {
139  size_t ix, jx;
140  T value;
141 
142  getline(ifs, buf);
143  std::istringstream data(buf);
144  data >> ix >> jx >> value;
145 
146  row_index[i] = ix - 1;
147  col_index[i] = jx - 1;
148  val[i] = value;
149  }
150  logger.util_out();
151 }
152 
153 template void COO<double>::input_mm(const std::string filename);
154 template void COO<float>::input_mm(const std::string filename);
155 
156 } // namespace matrix
157 } // namespace monolish
MM_BANNER
#define MM_BANNER
Definition: monolish_coo.hpp:16
monolish_func
#define monolish_func
Definition: monolish_logger.hpp:9
MM_TYPE_REAL
#define MM_TYPE_REAL
Definition: monolish_coo.hpp:20
monolish::matrix::COO::output_mm
void output_mm(const std::string filename) const
output matrix elements in MatrixMarket format (MatrixMarket format: https://math.nist....
Definition: IO_coo.cpp:48
MM_MAT
#define MM_MAT
Definition: monolish_coo.hpp:17
monolish::Logger
logger class (singleton, for developper class)
Definition: monolish_logger.hpp:19
MM_TYPE_GENERAL
#define MM_TYPE_GENERAL
Definition: monolish_coo.hpp:21
monolish::Logger::util_out
void util_out()
Definition: logger_utils.cpp:123
monolish::matrix::COO::input_mm
void input_mm(const std::string filename)
Create COO matrix from MatrixMatrket format file (only real general) (MatrixMarket format: https://ma...
Definition: IO_coo.cpp:68
monolish::matrix::COO::print_all
void print_all(bool force_cpu=false) const
print all elements to standard I/O
Definition: IO_coo.cpp:17
monolish::Logger::util_in
void util_in(const std::string func_name)
Definition: logger_utils.cpp:113
monolish
Definition: monolish_matrix_blas.hpp:9
MM_FMT
#define MM_FMT
Definition: monolish_coo.hpp:19
monolish::Logger::get_instance
static Logger & get_instance()
Definition: monolish_logger.hpp:42