Eigen-Contrib  5.0.1
 
Loading...
Searching...
No Matches
MatrixMarketIterator.h
1// SPDX-License-Identifier: MPL-2.0
2
3// This file is part of Eigen, a lightweight C++ template library
4// for linear algebra.
5//
6// Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
12#ifndef EIGEN_BROWSE_MATRICES_H
13#define EIGEN_BROWSE_MATRICES_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20enum { SPD = 0x100, NonSymmetric = 0x0 };
21
42template <typename Scalar>
43class MatrixMarketIterator {
44 typedef typename NumTraits<Scalar>::Real RealScalar;
45
46 public:
47 typedef Matrix<Scalar, Dynamic, 1> VectorType;
48 typedef SparseMatrix<Scalar, ColMajor> MatrixType;
49
50 public:
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();
55 }
56
57 ~MatrixMarketIterator() {
58 if (m_folder_id) closedir(m_folder_id);
59 }
60
61 inline MatrixMarketIterator& operator++() {
62 m_matIsLoaded = false;
63 m_hasrefX = false;
64 m_hasRhs = false;
65 Getnextvalidmatrix();
66 return *this;
67 }
68 inline operator bool() const { return m_isvalid; }
69
71 inline MatrixType& matrix() {
72 // Read the matrix
73 if (m_matIsLoaded) return m_mat;
74
75 std::string matrix_file = m_folder + "/" + m_matname + ".mtx";
76 if (!loadMarket(m_mat, matrix_file)) {
77 std::cerr << "Warning loadMarket failed when loading \"" << matrix_file << "\"" << std::endl;
78 m_matIsLoaded = false;
79 return m_mat;
80 }
81 m_matIsLoaded = true;
82
83 if (m_sym != NonSymmetric) {
84 // Check whether we need to restore a full matrix:
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) {
89 // only the lower part is stored
90 MatrixType tmp(m_mat);
91 m_mat = tmp.template selfadjointView<Lower>();
92 } else if (upper_norm > diag_norm && lower_norm == diag_norm) {
93 // only the upper part is stored
94 MatrixType tmp(m_mat);
95 m_mat = tmp.template selfadjointView<Upper>();
96 }
97 }
98 return m_mat;
99 }
100
104 inline VectorType& rhs() {
105 // Get the right hand side
106 if (m_hasRhs) return m_rhs;
107
108 std::string rhs_file;
109 rhs_file = m_folder + "/" + m_matname + "_b.mtx"; // The pattern is matname_b.mtx
110 m_hasRhs = Fileexists(rhs_file);
111 if (m_hasRhs) {
112 m_rhs.resize(m_mat.cols());
113 m_hasRhs = loadMarketVector(m_rhs, rhs_file);
114 }
115 if (!m_hasRhs) {
116 // Generate a random right hand side
117 if (!m_matIsLoaded) this->matrix();
118 m_refX.resize(m_mat.cols());
119 m_refX.setRandom();
120 m_rhs = m_mat * m_refX;
121 m_hasrefX = true;
122 m_hasRhs = true;
123 }
124 return m_rhs;
125 }
126
133 inline VectorType& refX() {
134 // Check if a reference solution is provided
135 if (m_hasrefX) return m_refX;
136
137 std::string lhs_file;
138 lhs_file = m_folder + "/" + m_matname + "_x.mtx";
139 m_hasrefX = Fileexists(lhs_file);
140 if (m_hasrefX) {
141 m_refX.resize(m_mat.cols());
142 m_hasrefX = loadMarketVector(m_refX, lhs_file);
143 } else
144 m_refX.resize(0);
145 return m_refX;
146 }
147
148 inline std::string& matname() { return m_matname; }
149
150 inline int sym() const { return m_sym; }
151
152 bool hasRhs() const { return m_hasRhs; }
153 bool hasrefX() const { return m_hasrefX; }
154 bool isFolderValid() const { return bool(m_folder_id); }
155
156 protected:
157 inline bool Fileexists(std::string file) const {
158 std::ifstream file_id(file.c_str());
159 if (!file_id.good()) {
160 return false;
161 } else {
162 file_id.close();
163 return true;
164 }
165 }
166
167 void Getnextvalidmatrix() {
168 m_isvalid = false;
169 // Here, we return with the next valid matrix in the folder
170 while ((m_curs_id = readdir(m_folder_id)) != nullptr) {
171 m_isvalid = false;
172 std::string curfile;
173 curfile = m_folder + "/" + m_curs_id->d_name;
174 // Discard if it is a folder
175 if (m_curs_id->d_type == DT_DIR) continue; // FIXME: This may not be available on non-BSD systems.
176
177 // Determine from the header if it is a matrix or a right hand side
178 bool isvector, iscomplex = false;
179 if (!getMarketHeader(curfile, m_sym, iscomplex, isvector)) continue;
180 if (isvector) continue;
181 if (!iscomplex) {
182 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, std::complex<float> >::value ||
183 std::is_same<Scalar, std::complex<double> >::value))
184 continue;
185 }
186 if (iscomplex) {
187 EIGEN_IF_CONSTEXPR ((std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value)) {
188 continue;
189 }
190 }
191
192 // Get the matrix name
193 std::string filename = m_curs_id->d_name;
194 m_matname = filename.substr(0, filename.length() - 4);
195
196 // Find if the matrix is SPD
197 size_t found = m_matname.find("SPD");
198 if ((found != std::string::npos) && (m_sym != NonSymmetric)) m_sym = SPD;
199
200 m_isvalid = true;
201 break;
202 }
203 }
204 int m_sym; // Symmetry of the matrix
205 MatrixType m_mat; // Current matrix
206 VectorType m_rhs; // Current vector
207 VectorType m_refX; // The reference solution, if exists
208 std::string m_matname; // Matrix Name
209 bool m_isvalid;
210 bool m_matIsLoaded; // Determine if the matrix has already been loaded from the file
211 bool m_hasRhs; // The right hand side exists
212 bool m_hasrefX; // A reference solution is provided
213 std::string m_folder;
214 DIR* m_folder_id;
215 struct dirent* m_curs_id;
216};
217
218} // end namespace Eigen
219
220#endif
MatrixType & matrix()
Definition MatrixMarketIterator.h:71
VectorType & refX()
Definition MatrixMarketIterator.h:133
VectorType & rhs()
Definition MatrixMarketIterator.h:104
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition MarketIO.h:285
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Loads a sparse matrix from a matrixmarket format file.
Definition MarketIO.h:157
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isdense)
Reads the header of a matrixmarket file and determines the properties of a matrix.
Definition MarketIO.h:123
Namespace containing all symbols from the Eigen library.