sym-ildl  1.2
Incomplete LDL' factorizations of indefinite symmetric and skew-symmetric matrices.
lilc_matrix_load.h
1 //-*-mode:c++-*-
2 #ifndef _LILC_MATRIX_LOAD_H_
3 #define _LILC_MATRIX_LOAD_H_
4 
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 #include <string>
9 #include <vector>
10 
11 template <class el_type>
12 inline bool readline (std::stringstream& line, int& n_rows, int& n_cols, int& i, int& j, el_type& value) {
13  line >> i >> j >> value;
14  i--;
15  j--;
16  if(i>=0 && j>=0 && i<n_rows && j< n_cols) {
17  return true;
18  }
19  else
20  return false;
21 }
22 
23 template <class el_type>
24 bool lilc_matrix<el_type> :: load (std::string filename)
25 {
26  std::ifstream input(filename.c_str(), std::ios::in);
27  //input.sync_with_stdio(0);
28 
29  if(!input) return false;
30 
31  const int maxBuffersize = 2048;
32  char buffer[maxBuffersize];
33 
34  bool readsizes = false;
35 
36  int n_rows(-1), n_cols(-1), n_nzs(-1), i(-1), j(-1);
37  int count = 0;
38  el_type value;
39 
40  bool full_detected = false;
41  while(input.getline(buffer, maxBuffersize))
42  {
43  // skip comments
44  //NOTE An appropriate test should be done on the header to get the symmetry
45  if(buffer[0]=='%')
46  continue;
47 
48  std::stringstream line(buffer);
49  //line.sync_with_stdio(0);
50  if(!readsizes)
51  {
52  line >> n_rows >> n_cols >> n_nzs;
53  if(n_rows > 0 && n_cols > 0 && n_nzs > 0)
54  {
55  readsizes = true;
56 
57  resize(n_rows, n_cols);
58  std::fill(row_first.begin(), row_first.end(), 0); //a bit of optimization could be used here since resize sets all elem in first to 1
59  std::fill(col_first.begin(), col_first.end(), 0); //a bit of optimization could be used here since resize sets all elem in first to 1
60  }
61  }
62  else
63  {
64  i = -1;
65  j = -1;
66  if( readline(line, n_rows, n_cols, i, j, value) )
67  {
68  if (j > i) {
69  full_detected = true;
70  continue;
71  }
72  m_idx[j].push_back(i);
73  m_x[j].push_back(value);
74  ++count;
75  assert(i >= j);
76  if (i != j) list[i].push_back(j);
77 
78  }
79  else
80  std::cerr << "Invalid read: " << i << "," << j << "\n";
81  }
82 
83  }
84 
85  if (!full_detected && count != n_nzs) std::cout << "Expected " << n_nzs << " elems but read " << count << "." << std::endl;
86 
87  if (full_detected) {
88  std::cout << "Full matrix detected, assuming matrix is symmetric and loading lower-half of the matrix only." << std::endl;
89  }
90  nnz_count = count;
91  std::cout << "Load succeeded. " << "File " << filename << " was loaded." << std::endl;
92  input.close();
93  return true;
94 }
95 
96 template<class el_type>
97 bool lilc_matrix<el_type> :: load (const std::vector<int>& ptr, const std::vector<int>& row, const std::vector<el_type>& val) {
98  if (ptr.size() == 0 || ptr.back() != row.size() || val.size() != ptr.back()) {
99  std::cout << "Error in CSC format detected. Matrix failed to load." << std::endl;
100  return false;
101  }
102  return load(ptr.data(), row.data(), val.data(), ptr.size()-1);
103 }
104 
105 template<class el_type>
106 bool lilc_matrix<el_type> :: load (const int* ptr, const int* row, const el_type* val, int dim) {
107  bool full_detected = false;
108  int n_rows = dim, n_cols = dim;
109 
110  resize(n_rows, n_cols);
111  std::fill(row_first.begin(), row_first.end(), 0); //a bit of optimization could be used here since resize sets all elem in first to 1
112  std::fill(col_first.begin(), col_first.end(), 0); //a bit of optimization could be used here since resize sets all elem in first to 1
113 
114  int count = 0;
115  for (int i = 0; i < dim; i++) {
116  for (int j = ptr[i]; j < ptr[i+1]; j++) {
117  if (i > row[j]) {
118  full_detected = true;
119  continue;
120  }
121  m_idx[i].push_back(row[j]);
122  m_x[i].push_back(val[j]);
123  if (i != row[j]) list[row[j]].push_back(i);
124  ++count;
125  }
126  }
127 
128  if (full_detected) {
129  std::cout << "Full matrix detected, assuming matrix is symmetric and loading lower-half of the matrix only." << std::endl;
130  }
131 
132  nnz_count = count;
133  return true;
134 }
135 
136 #endif
bool load(std::string filename)
Loads a matrix in matrix market format.
Definition: lilc_matrix_load.h:24