2 #ifndef _LILC_MATRIX_LOAD_H_ 3 #define _LILC_MATRIX_LOAD_H_ 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;
16 if(i>=0 && j>=0 && i<n_rows && j< n_cols) {
23 template <
class el_type>
26 std::ifstream input(filename.c_str(), std::ios::in);
29 if(!input)
return false;
31 const int maxBuffersize = 2048;
32 char buffer[maxBuffersize];
34 bool readsizes =
false;
36 int n_rows(-1), n_cols(-1), n_nzs(-1), i(-1), j(-1);
40 bool full_detected =
false;
41 while(input.getline(buffer, maxBuffersize))
48 std::stringstream line(buffer);
52 line >> n_rows >> n_cols >> n_nzs;
53 if(n_rows > 0 && n_cols > 0 && n_nzs > 0)
57 resize(n_rows, n_cols);
58 std::fill(row_first.begin(), row_first.end(), 0);
59 std::fill(col_first.begin(), col_first.end(), 0);
66 if( readline(line, n_rows, n_cols, i, j, value) )
72 m_idx[j].push_back(i);
73 m_x[j].push_back(value);
76 if (i != j) list[i].push_back(j);
80 std::cerr <<
"Invalid read: " << i <<
"," << j <<
"\n";
85 if (!full_detected && count != n_nzs) std::cout <<
"Expected " << n_nzs <<
" elems but read " << count <<
"." << std::endl;
88 std::cout <<
"Full matrix detected, assuming matrix is symmetric and loading lower-half of the matrix only." << std::endl;
91 std::cout <<
"Load succeeded. " <<
"File " << filename <<
" was loaded." << std::endl;
96 template<
class el_type>
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;
102 return load(ptr.data(), row.data(), val.data(), ptr.size()-1);
105 template<
class el_type>
107 bool full_detected =
false;
108 int n_rows = dim, n_cols = dim;
110 resize(n_rows, n_cols);
111 std::fill(row_first.begin(), row_first.end(), 0);
112 std::fill(col_first.begin(), col_first.end(), 0);
115 for (
int i = 0; i < dim; i++) {
116 for (
int j = ptr[i]; j < ptr[i+1]; j++) {
118 full_detected =
true;
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);
129 std::cout <<
"Full matrix detected, assuming matrix is symmetric and loading lower-half of the matrix only." << std::endl;
bool load(std::string filename)
Loads a matrix in matrix market format.
Definition: lilc_matrix_load.h:24