43class MatrixMarketIterator {
51 MatrixMarketIterator(
const std::string& folder)
52 : m_sym(0), m_isvalid(
false), m_matIsLoaded(
false), m_hasRhs(
false), m_hasrefX(
false), m_folder(folder) {
53 m_folder_id = opendir(folder.c_str());
54 if (m_folder_id) Getnextvalidmatrix();
57 ~MatrixMarketIterator() {
58 if (m_folder_id) closedir(m_folder_id);
61 inline MatrixMarketIterator& operator++() {
62 m_matIsLoaded =
false;
68 inline operator bool()
const {
return m_isvalid; }
73 if (m_matIsLoaded)
return m_mat;
75 std::string matrix_file = m_folder +
"/" + m_matname +
".mtx";
77 std::cerr <<
"Warning loadMarket failed when loading \"" << matrix_file <<
"\"" << std::endl;
78 m_matIsLoaded =
false;
83 if (m_sym != NonSymmetric) {
85 RealScalar diag_norm = m_mat.diagonal().norm();
86 RealScalar lower_norm = m_mat.template triangularView<Lower>().norm();
87 RealScalar upper_norm = m_mat.template triangularView<Upper>().norm();
88 if (lower_norm > diag_norm && upper_norm == diag_norm) {
90 MatrixType tmp(m_mat);
91 m_mat = tmp.template selfadjointView<Lower>();
92 }
else if (upper_norm > diag_norm && lower_norm == diag_norm) {
94 MatrixType tmp(m_mat);
95 m_mat = tmp.template selfadjointView<Upper>();
104 inline VectorType&
rhs() {
106 if (m_hasRhs)
return m_rhs;
108 std::string rhs_file;
109 rhs_file = m_folder +
"/" + m_matname +
"_b.mtx";
110 m_hasRhs = Fileexists(rhs_file);
112 m_rhs.resize(m_mat.cols());
117 if (!m_matIsLoaded) this->
matrix();
118 m_refX.resize(m_mat.cols());
120 m_rhs = m_mat * m_refX;
135 if (m_hasrefX)
return m_refX;
137 std::string lhs_file;
138 lhs_file = m_folder +
"/" + m_matname +
"_x.mtx";
139 m_hasrefX = Fileexists(lhs_file);
141 m_refX.resize(m_mat.cols());
148 inline std::string& matname() {
return m_matname; }
150 inline int sym()
const {
return m_sym; }
152 bool hasRhs()
const {
return m_hasRhs; }
153 bool hasrefX()
const {
return m_hasrefX; }
154 bool isFolderValid()
const {
return bool(m_folder_id); }
157 inline bool Fileexists(std::string file)
const {
158 std::ifstream file_id(file.c_str());
159 if (!file_id.good()) {
167 void Getnextvalidmatrix() {
170 while ((m_curs_id = readdir(m_folder_id)) !=
nullptr) {
173 curfile = m_folder +
"/" + m_curs_id->d_name;
175 if (m_curs_id->d_type == DT_DIR)
continue;
178 bool isvector, iscomplex =
false;
180 if (isvector)
continue;
182 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, std::complex<float> >::value ||
183 std::is_same<Scalar, std::complex<double> >::value))
187 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value)) {
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;