42class MatrixMarketIterator {
50 MatrixMarketIterator(
const std::string& folder)
51 : m_sym(0), m_isvalid(
false), m_matIsLoaded(
false), m_hasRhs(
false), m_hasrefX(
false), m_folder(folder) {
52 m_folder_id = opendir(folder.c_str());
53 if (m_folder_id) Getnextvalidmatrix();
56 ~MatrixMarketIterator() {
57 if (m_folder_id) closedir(m_folder_id);
60 inline MatrixMarketIterator& operator++() {
61 m_matIsLoaded =
false;
67 inline operator bool()
const {
return m_isvalid; }
72 if (m_matIsLoaded)
return m_mat;
74 std::string matrix_file = m_folder +
"/" + m_matname +
".mtx";
76 std::cerr <<
"Warning loadMarket failed when loading \"" << matrix_file <<
"\"" << std::endl;
77 m_matIsLoaded =
false;
82 if (m_sym != NonSymmetric) {
84 RealScalar diag_norm = m_mat.diagonal().norm();
85 RealScalar lower_norm = m_mat.template triangularView<Lower>().norm();
86 RealScalar upper_norm = m_mat.template triangularView<Upper>().norm();
87 if (lower_norm > diag_norm && upper_norm == diag_norm) {
89 MatrixType tmp(m_mat);
90 m_mat = tmp.template selfadjointView<Lower>();
91 }
else if (upper_norm > diag_norm && lower_norm == diag_norm) {
93 MatrixType tmp(m_mat);
94 m_mat = tmp.template selfadjointView<Upper>();
103 inline VectorType&
rhs() {
105 if (m_hasRhs)
return m_rhs;
107 std::string rhs_file;
108 rhs_file = m_folder +
"/" + m_matname +
"_b.mtx";
109 m_hasRhs = Fileexists(rhs_file);
111 m_rhs.resize(m_mat.cols());
116 if (!m_matIsLoaded) this->
matrix();
117 m_refX.resize(m_mat.cols());
119 m_rhs = m_mat * m_refX;
134 if (m_hasrefX)
return m_refX;
136 std::string lhs_file;
137 lhs_file = m_folder +
"/" + m_matname +
"_x.mtx";
138 m_hasrefX = Fileexists(lhs_file);
140 m_refX.resize(m_mat.cols());
147 inline std::string& matname() {
return m_matname; }
149 inline int sym() {
return m_sym; }
151 bool hasRhs() {
return m_hasRhs; }
152 bool hasrefX() {
return m_hasrefX; }
153 bool isFolderValid() {
return bool(m_folder_id); }
156 inline bool Fileexists(std::string file) {
157 std::ifstream file_id(file.c_str());
158 if (!file_id.good()) {
166 void Getnextvalidmatrix() {
169 while ((m_curs_id = readdir(m_folder_id)) != NULL) {
172 curfile = m_folder +
"/" + m_curs_id->d_name;
174 if (m_curs_id->d_type == DT_DIR)
continue;
180 bool isvector, iscomplex =
false;
182 if (isvector)
continue;
184 if (internal::is_same<Scalar, std::complex<float> >::value ||
185 internal::is_same<Scalar, std::complex<double> >::value)
189 if (internal::is_same<Scalar, float>::value || internal::is_same<Scalar, double>::value)
continue;
193 std::string filename = m_curs_id->d_name;
194 m_matname = filename.substr(0, filename.length() - 4);
197 size_t found = m_matname.find(
"SPD");
198 if ((found != std::string::npos) && (m_sym != NonSymmetric)) m_sym = SPD;
208 std::string m_matname;
213 std::string m_folder;
215 struct dirent* m_curs_id;