MatrixMarketIterator.h
Go to the documentation of this file.
1 
2 // This file is part of Eigen, a lightweight C++ template library
3 // for linear algebra.
4 //
5 // Copyright (C) 2012 Desire NUENTSA WAKAM <desire.nuentsa_wakam@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #ifndef EIGEN_BROWSE_MATRICES_H
12 #define EIGEN_BROWSE_MATRICES_H
13 
14 namespace Eigen {
15 
16 enum {
17  SPD = 0x100,
19 };
20 
41 template <typename Scalar>
43 {
45  public:
48 
49  public:
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  {
53  m_folder_id = opendir(folder.c_str());
54  if(m_folder_id)
56  }
57 
59  {
60  if (m_folder_id) closedir(m_folder_id);
61  }
62 
64  {
65  m_matIsLoaded = false;
66  m_hasrefX = false;
67  m_hasRhs = false;
69  return *this;
70  }
71  inline operator bool() const { return m_isvalid;}
72 
74  inline MatrixType& matrix()
75  {
76  // Read the matrix
77  if (m_matIsLoaded) return m_mat;
78 
79  std::string matrix_file = m_folder + "/" + m_matname + ".mtx";
80  if ( !loadMarket(m_mat, matrix_file))
81  {
82  std::cerr << "Warning loadMarket failed when loading \"" << matrix_file << "\"" << std::endl;
83  m_matIsLoaded = false;
84  return m_mat;
85  }
86  m_matIsLoaded = true;
87 
88  if (m_sym != NonSymmetric)
89  {
90  // Check whether we need to restore a full matrix:
91  RealScalar diag_norm = m_mat.diagonal().norm();
92  RealScalar lower_norm = m_mat.template triangularView<Lower>().norm();
93  RealScalar upper_norm = m_mat.template triangularView<Upper>().norm();
94  if(lower_norm>diag_norm && upper_norm==diag_norm)
95  {
96  // only the lower part is stored
97  MatrixType tmp(m_mat);
98  m_mat = tmp.template selfadjointView<Lower>();
99  }
100  else if(upper_norm>diag_norm && lower_norm==diag_norm)
101  {
102  // only the upper part is stored
103  MatrixType tmp(m_mat);
104  m_mat = tmp.template selfadjointView<Upper>();
105  }
106  }
107  return m_mat;
108  }
109 
113  inline VectorType& rhs()
114  {
115  // Get the right hand side
116  if (m_hasRhs) return m_rhs;
117 
118  std::string rhs_file;
119  rhs_file = m_folder + "/" + m_matname + "_b.mtx"; // The pattern is matname_b.mtx
120  m_hasRhs = Fileexists(rhs_file);
121  if (m_hasRhs)
122  {
123  m_rhs.resize(m_mat.cols());
124  m_hasRhs = loadMarketVector(m_rhs, rhs_file);
125  }
126  if (!m_hasRhs)
127  {
128  // Generate a random right hand side
129  if (!m_matIsLoaded) this->matrix();
130  m_refX.resize(m_mat.cols());
131  m_refX.setRandom();
132  m_rhs = m_mat * m_refX;
133  m_hasrefX = true;
134  m_hasRhs = true;
135  }
136  return m_rhs;
137  }
138 
145  inline VectorType& refX()
146  {
147  // Check if a reference solution is provided
148  if (m_hasrefX) return m_refX;
149 
150  std::string lhs_file;
151  lhs_file = m_folder + "/" + m_matname + "_x.mtx";
152  m_hasrefX = Fileexists(lhs_file);
153  if (m_hasrefX)
154  {
155  m_refX.resize(m_mat.cols());
156  m_hasrefX = loadMarketVector(m_refX, lhs_file);
157  }
158  else
159  m_refX.resize(0);
160  return m_refX;
161  }
162 
163  inline std::string& matname() { return m_matname; }
164 
165  inline int sym() { return m_sym; }
166 
167  bool hasRhs() {return m_hasRhs; }
168  bool hasrefX() {return m_hasrefX; }
169  bool isFolderValid() { return bool(m_folder_id); }
170 
171  protected:
172 
173  inline bool Fileexists(std::string file)
174  {
175  std::ifstream file_id(file.c_str());
176  if (!file_id.good() )
177  {
178  return false;
179  }
180  else
181  {
182  file_id.close();
183  return true;
184  }
185  }
186 
188  {
189  m_isvalid = false;
190  // Here, we return with the next valid matrix in the folder
191  while ( (m_curs_id = readdir(m_folder_id)) != NULL) {
192  m_isvalid = false;
193  std::string curfile;
194  curfile = m_folder + "/" + m_curs_id->d_name;
195  // Discard if it is a folder
196  if (m_curs_id->d_type == DT_DIR) continue; //FIXME This may not be available on non BSD systems
197 // struct stat st_buf;
198 // stat (curfile.c_str(), &st_buf);
199 // if (S_ISDIR(st_buf.st_mode)) continue;
200 
201  // Determine from the header if it is a matrix or a right hand side
202  bool isvector,iscomplex=false;
203  if(!getMarketHeader(curfile,m_sym,iscomplex,isvector)) continue;
204  if(isvector) continue;
205  if (!iscomplex)
206  {
207  if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
208  continue;
209  }
210  if (iscomplex)
211  {
213  continue;
214  }
215 
216 
217  // Get the matrix name
218  std::string filename = m_curs_id->d_name;
219  m_matname = filename.substr(0, filename.length()-4);
220 
221  // Find if the matrix is SPD
222  size_t found = m_matname.find("SPD");
223  if( (found!=std::string::npos) && (m_sym != NonSymmetric) )
224  m_sym = SPD;
225 
226  m_isvalid = true;
227  break;
228  }
229  }
230  int m_sym; // Symmetry of the matrix
231  MatrixType m_mat; // Current matrix
232  VectorType m_rhs; // Current vector
233  VectorType m_refX; // The reference solution, if exists
234  std::string m_matname; // Matrix Name
235  bool m_isvalid;
236  bool m_matIsLoaded; // Determine if the matrix has already been loaded from the file
237  bool m_hasRhs; // The right hand side exists
238  bool m_hasrefX; // A reference solution is provided
239  std::string m_folder;
241  struct dirent *m_curs_id;
242 
243 };
244 
245 } // end namespace Eigen
246 
247 #endif
Eigen::NonSymmetric
@ NonSymmetric
Definition: MatrixMarketIterator.h:18
Eigen::MatrixMarketIterator::Fileexists
bool Fileexists(std::string file)
Definition: MatrixMarketIterator.h:173
Eigen::SparseMatrix::cols
Index cols() const
Definition: SparseMatrix.h:140
Eigen::SPD
@ SPD
Definition: MatrixMarketIterator.h:17
Eigen::MatrixMarketIterator::matrix
MatrixType & matrix()
Definition: MatrixMarketIterator.h:74
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::MatrixMarketIterator::isFolderValid
bool isFolderValid()
Definition: MatrixMarketIterator.h:169
Eigen::SparseMatrix< Scalar, ColMajor >
Eigen::MatrixMarketIterator::m_mat
MatrixType m_mat
Definition: MatrixMarketIterator.h:231
Eigen::PlainObjectBase::setRandom
Derived & setRandom(Index size)
Definition: Random.h:151
Eigen::MatrixMarketIterator::~MatrixMarketIterator
~MatrixMarketIterator()
Definition: MatrixMarketIterator.h:58
Eigen::MatrixMarketIterator::hasRhs
bool hasRhs()
Definition: MatrixMarketIterator.h:167
Eigen::MatrixMarketIterator::m_rhs
VectorType m_rhs
Definition: MatrixMarketIterator.h:232
Eigen::MatrixMarketIterator::sym
int sym()
Definition: MatrixMarketIterator.h:165
Eigen::MatrixMarketIterator::m_curs_id
struct dirent * m_curs_id
Definition: MatrixMarketIterator.h:241
Eigen::MatrixMarketIterator::rhs
VectorType & rhs()
Definition: MatrixMarketIterator.h:113
Eigen::MatrixMarketIterator::MatrixType
SparseMatrix< Scalar, ColMajor > MatrixType
Definition: MatrixMarketIterator.h:47
Eigen::PlainObjectBase::resize
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:271
Eigen::MatrixMarketIterator::m_matname
std::string m_matname
Definition: MatrixMarketIterator.h:234
conf.DIR
DIR
Definition: wrap/pybind11/docs/conf.py:22
Eigen::MatrixMarketIterator::m_matIsLoaded
bool m_matIsLoaded
Definition: MatrixMarketIterator.h:236
Eigen::MatrixMarketIterator::operator++
MatrixMarketIterator & operator++()
Definition: MatrixMarketIterator.h:63
Eigen::loadMarketVector
bool loadMarketVector(VectorType &vec, const std::string &filename)
Definition: MarketIO.h:200
Eigen::SparseMatrix::diagonal
const ConstDiagonalReturnType diagonal() const
Definition: SparseMatrix.h:655
Eigen::MatrixMarketIterator::m_folder
std::string m_folder
Definition: MatrixMarketIterator.h:239
Eigen::MatrixMarketIterator::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: MatrixMarketIterator.h:44
relicense.filename
filename
Definition: relicense.py:57
Eigen::MatrixMarketIterator::Getnextvalidmatrix
void Getnextvalidmatrix()
Definition: MatrixMarketIterator.h:187
Eigen::MatrixMarketIterator::hasrefX
bool hasrefX()
Definition: MatrixMarketIterator.h:168
Eigen::MatrixMarketIterator::m_isvalid
bool m_isvalid
Definition: MatrixMarketIterator.h:235
Eigen::MatrixMarketIterator::m_sym
int m_sym
Definition: MatrixMarketIterator.h:230
Eigen::MatrixMarketIterator::m_refX
VectorType m_refX
Definition: MatrixMarketIterator.h:233
Eigen::MatrixMarketIterator::m_hasrefX
bool m_hasrefX
Definition: MatrixMarketIterator.h:238
Eigen::MatrixMarketIterator::matname
std::string & matname()
Definition: MatrixMarketIterator.h:163
Eigen::MatrixMarketIterator::refX
VectorType & refX()
Definition: MatrixMarketIterator.h:145
Eigen::getMarketHeader
bool getMarketHeader(const std::string &filename, int &sym, bool &iscomplex, bool &isvector)
Definition: MarketIO.h:109
Eigen::MatrixMarketIterator::m_folder_id
DIR * m_folder_id
Definition: MatrixMarketIterator.h:240
Eigen::internal::is_same
Definition: Meta.h:148
Eigen::Matrix< Scalar, Dynamic, 1 >
Eigen::MatrixMarketIterator::MatrixMarketIterator
MatrixMarketIterator(const std::string &folder)
Definition: MatrixMarketIterator.h:50
NULL
#define NULL
Definition: ccolamd.c:609
Eigen::MatrixMarketIterator
Iterator to browse matrices from a specified folder.
Definition: MatrixMarketIterator.h:42
Eigen::loadMarket
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Definition: MarketIO.h:134
triangularView< Lower >
A triangularView< Lower >().adjoint().solveInPlace(B)
matlab_wrap.file
file
Definition: matlab_wrap.py:57
Eigen::MatrixMarketIterator::m_hasRhs
bool m_hasRhs
Definition: MatrixMarketIterator.h:237
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
test_callbacks.value
value
Definition: test_callbacks.py:160
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::MatrixMarketIterator::VectorType
Matrix< Scalar, Dynamic, 1 > VectorType
Definition: MatrixMarketIterator.h:46


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:03:07