Eigen::MatrixMarketIterator< Scalar > Class Template Reference

Iterator to browse matrices from a specified folder. More...

Public Types

typedef SparseMatrix< Scalar, ColMajorMatrixType
 
typedef Matrix< Scalar, Dynamic, 1 > VectorType
 

Public Member Functions

bool hasrefX ()
 
bool hasRhs ()
 
bool isFolderValid ()
 
std::string & matname ()
 
MatrixTypematrix ()
 
 MatrixMarketIterator (const std::string &folder)
 
 operator bool () const
 
MatrixMarketIteratoroperator++ ()
 
VectorTyperefX ()
 
VectorTyperhs ()
 
int sym ()
 
 ~MatrixMarketIterator ()
 

Protected Member Functions

bool Fileexists (std::string file)
 
void Getnextvalidmatrix ()
 

Protected Attributes

struct dirent * m_curs_id
 
std::string m_folder
 
DIR * m_folder_id
 
bool m_hasrefX
 
bool m_hasRhs
 
bool m_isvalid
 
MatrixType m_mat
 
bool m_matIsLoaded
 
std::string m_matname
 
VectorType m_refX
 
VectorType m_rhs
 
int m_sym
 

Private Types

typedef NumTraits< Scalar >::Real RealScalar
 

Detailed Description

template<typename Scalar>
class Eigen::MatrixMarketIterator< Scalar >

Iterator to browse matrices from a specified folder.

This is used to load all the matrices from a folder. The matrices should be in Matrix Market format It is assumed that the matrices are named as matname.mtx and matname_SPD.mtx if the matrix is Symmetric and positive definite (or Hermitian) The right hand side vectors are loaded as well, if they exist. They should be named as matname_b.mtx. Note that the right hand side for a SPD matrix is named as matname_SPD_b.mtx

Sometimes a reference solution is available. In this case, it should be named as matname_x.mtx

Sample code

Template Parameters
ScalarThe scalar type

Definition at line 44 of file MatrixMarketIterator.h.

Member Typedef Documentation

◆ MatrixType

template<typename Scalar >
typedef SparseMatrix<Scalar,ColMajor> Eigen::MatrixMarketIterator< Scalar >::MatrixType

Definition at line 49 of file MatrixMarketIterator.h.

◆ RealScalar

template<typename Scalar >
typedef NumTraits<Scalar>::Real Eigen::MatrixMarketIterator< Scalar >::RealScalar
private

Definition at line 46 of file MatrixMarketIterator.h.

◆ VectorType

template<typename Scalar >
typedef Matrix<Scalar,Dynamic,1> Eigen::MatrixMarketIterator< Scalar >::VectorType

Definition at line 48 of file MatrixMarketIterator.h.

Constructor & Destructor Documentation

◆ MatrixMarketIterator()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::MatrixMarketIterator ( const std::string &  folder)
inline

◆ ~MatrixMarketIterator()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::~MatrixMarketIterator ( )
inline

Definition at line 60 of file MatrixMarketIterator.h.

61  {
62  if (m_folder_id) closedir(m_folder_id);
63  }

Member Function Documentation

◆ Fileexists()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::Fileexists ( std::string  file)
inlineprotected

Definition at line 175 of file MatrixMarketIterator.h.

176  {
177  std::ifstream file_id(file.c_str());
178  if (!file_id.good() )
179  {
180  return false;
181  }
182  else
183  {
184  file_id.close();
185  return true;
186  }
187  }

◆ Getnextvalidmatrix()

template<typename Scalar >
void Eigen::MatrixMarketIterator< Scalar >::Getnextvalidmatrix ( )
inlineprotected

Definition at line 189 of file MatrixMarketIterator.h.

190  {
191  m_isvalid = false;
192  // Here, we return with the next valid matrix in the folder
193  while ( (m_curs_id = readdir(m_folder_id)) != NULL) {
194  m_isvalid = false;
195  std::string curfile;
196  curfile = m_folder + "/" + m_curs_id->d_name;
197  // Discard if it is a folder
198  if (m_curs_id->d_type == DT_DIR) continue; //FIXME This may not be available on non BSD systems
199 // struct stat st_buf;
200 // stat (curfile.c_str(), &st_buf);
201 // if (S_ISDIR(st_buf.st_mode)) continue;
202 
203  // Determine from the header if it is a matrix or a right hand side
204  bool isvector,iscomplex=false;
205  if(!getMarketHeader(curfile,m_sym,iscomplex,isvector)) continue;
206  if(isvector) continue;
207  if (!iscomplex)
208  {
209  if(internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value)
210  continue;
211  }
212  if (iscomplex)
213  {
214  if(internal::is_same<Scalar, float>::value || internal::is_same<Scalar, double>::value)
215  continue;
216  }
217 
218 
219  // Get the matrix name
220  std::string filename = m_curs_id->d_name;
221  m_matname = filename.substr(0, filename.length()-4);
222 
223  // Find if the matrix is SPD
224  size_t found = m_matname.find("SPD");
225  if( (found!=std::string::npos) && (m_sym != NonSymmetric) )
226  m_sym = SPD;
227 
228  m_isvalid = true;
229  break;
230  }
231  }
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:122

◆ hasrefX()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::hasrefX ( )
inline

Definition at line 170 of file MatrixMarketIterator.h.

170 {return m_hasrefX; }

◆ hasRhs()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::hasRhs ( )
inline

Definition at line 169 of file MatrixMarketIterator.h.

169 {return m_hasRhs; }

◆ isFolderValid()

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::isFolderValid ( )
inline

Definition at line 171 of file MatrixMarketIterator.h.

171 { return bool(m_folder_id); }

◆ matname()

template<typename Scalar >
std::string& Eigen::MatrixMarketIterator< Scalar >::matname ( )
inline

Definition at line 165 of file MatrixMarketIterator.h.

165 { return m_matname; }

◆ matrix()

template<typename Scalar >
MatrixType& Eigen::MatrixMarketIterator< Scalar >::matrix ( )
inline

Return the sparse matrix corresponding to the current file

Definition at line 76 of file MatrixMarketIterator.h.

77  {
78  // Read the matrix
79  if (m_matIsLoaded) return m_mat;
80 
81  std::string matrix_file = m_folder + "/" + m_matname + ".mtx";
82  if ( !loadMarket(m_mat, matrix_file))
83  {
84  std::cerr << "Warning loadMarket failed when loading \"" << matrix_file << "\"" << std::endl;
85  m_matIsLoaded = false;
86  return m_mat;
87  }
88  m_matIsLoaded = true;
89 
90  if (m_sym != NonSymmetric)
91  {
92  // Check whether we need to restore a full matrix:
93  RealScalar diag_norm = m_mat.diagonal().norm();
94  RealScalar lower_norm = m_mat.template triangularView<Lower>().norm();
95  RealScalar upper_norm = m_mat.template triangularView<Upper>().norm();
96  if(lower_norm>diag_norm && upper_norm==diag_norm)
97  {
98  // only the lower part is stored
99  MatrixType tmp(m_mat);
100  m_mat = tmp.template selfadjointView<Lower>();
101  }
102  else if(upper_norm>diag_norm && lower_norm==diag_norm)
103  {
104  // only the upper part is stored
105  MatrixType tmp(m_mat);
106  m_mat = tmp.template selfadjointView<Upper>();
107  }
108  }
109  return m_mat;
110  }
Matrix< float, 1, Dynamic > MatrixType
NumTraits< Scalar >::Real RealScalar
RealScalar norm() const
DiagonalReturnType diagonal()
bool loadMarket(SparseMatrixType &mat, const std::string &filename)
Loads a sparse matrix from a matrixmarket format file.
Definition: MarketIO.h:155

◆ operator bool()

template<typename Scalar >
Eigen::MatrixMarketIterator< Scalar >::operator bool ( ) const
inline

Definition at line 73 of file MatrixMarketIterator.h.

73 { return m_isvalid;}

◆ operator++()

template<typename Scalar >
MatrixMarketIterator& Eigen::MatrixMarketIterator< Scalar >::operator++ ( )
inline

Definition at line 65 of file MatrixMarketIterator.h.

66  {
67  m_matIsLoaded = false;
68  m_hasrefX = false;
69  m_hasRhs = false;
71  return *this;
72  }

◆ refX()

template<typename Scalar >
VectorType& Eigen::MatrixMarketIterator< Scalar >::refX ( )
inline

Return a reference solution If it is not provided and if the right hand side is not available then refX is randomly generated such that A*refX = b where A and b are the matrix and the rhs. Note that when a rhs is provided, refX is not available

Definition at line 147 of file MatrixMarketIterator.h.

148  {
149  // Check if a reference solution is provided
150  if (m_hasrefX) return m_refX;
151 
152  std::string lhs_file;
153  lhs_file = m_folder + "/" + m_matname + "_x.mtx";
154  m_hasrefX = Fileexists(lhs_file);
155  if (m_hasrefX)
156  {
157  m_refX.resize(m_mat.cols());
158  m_hasrefX = loadMarketVector(m_refX, lhs_file);
159  }
160  else
161  m_refX.resize(0);
162  return m_refX;
163  }
bool Fileexists(std::string file)
constexpr void resize(Index rows, Index cols)
Index cols() const
bool loadMarketVector(VectorType &vec, const std::string &filename)
Same functionality as loadMarketDense, deprecated.
Definition: MarketIO.h:296

◆ rhs()

template<typename Scalar >
VectorType& Eigen::MatrixMarketIterator< Scalar >::rhs ( )
inline

Return the right hand side corresponding to the current matrix. If the rhs file is not provided, a random rhs is generated

Definition at line 115 of file MatrixMarketIterator.h.

116  {
117  // Get the right hand side
118  if (m_hasRhs) return m_rhs;
119 
120  std::string rhs_file;
121  rhs_file = m_folder + "/" + m_matname + "_b.mtx"; // The pattern is matname_b.mtx
122  m_hasRhs = Fileexists(rhs_file);
123  if (m_hasRhs)
124  {
125  m_rhs.resize(m_mat.cols());
126  m_hasRhs = loadMarketVector(m_rhs, rhs_file);
127  }
128  if (!m_hasRhs)
129  {
130  // Generate a random right hand side
131  if (!m_matIsLoaded) this->matrix();
132  m_refX.resize(m_mat.cols());
133  m_refX.setRandom();
134  m_rhs = m_mat * m_refX;
135  m_hasrefX = true;
136  m_hasRhs = true;
137  }
138  return m_rhs;
139  }
Derived & setRandom(Index rows, Index cols)

◆ sym()

template<typename Scalar >
int Eigen::MatrixMarketIterator< Scalar >::sym ( )
inline

Definition at line 167 of file MatrixMarketIterator.h.

167 { return m_sym; }

Member Data Documentation

◆ m_curs_id

template<typename Scalar >
struct dirent* Eigen::MatrixMarketIterator< Scalar >::m_curs_id
protected

Definition at line 243 of file MatrixMarketIterator.h.

◆ m_folder

template<typename Scalar >
std::string Eigen::MatrixMarketIterator< Scalar >::m_folder
protected

Definition at line 241 of file MatrixMarketIterator.h.

◆ m_folder_id

template<typename Scalar >
DIR* Eigen::MatrixMarketIterator< Scalar >::m_folder_id
protected

Definition at line 242 of file MatrixMarketIterator.h.

◆ m_hasrefX

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::m_hasrefX
protected

Definition at line 240 of file MatrixMarketIterator.h.

◆ m_hasRhs

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::m_hasRhs
protected

Definition at line 239 of file MatrixMarketIterator.h.

◆ m_isvalid

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::m_isvalid
protected

Definition at line 237 of file MatrixMarketIterator.h.

◆ m_mat

template<typename Scalar >
MatrixType Eigen::MatrixMarketIterator< Scalar >::m_mat
protected

Definition at line 233 of file MatrixMarketIterator.h.

◆ m_matIsLoaded

template<typename Scalar >
bool Eigen::MatrixMarketIterator< Scalar >::m_matIsLoaded
protected

Definition at line 238 of file MatrixMarketIterator.h.

◆ m_matname

template<typename Scalar >
std::string Eigen::MatrixMarketIterator< Scalar >::m_matname
protected

Definition at line 236 of file MatrixMarketIterator.h.

◆ m_refX

template<typename Scalar >
VectorType Eigen::MatrixMarketIterator< Scalar >::m_refX
protected

Definition at line 235 of file MatrixMarketIterator.h.

◆ m_rhs

template<typename Scalar >
VectorType Eigen::MatrixMarketIterator< Scalar >::m_rhs
protected

Definition at line 234 of file MatrixMarketIterator.h.

◆ m_sym

template<typename Scalar >
int Eigen::MatrixMarketIterator< Scalar >::m_sym
protected

Definition at line 232 of file MatrixMarketIterator.h.


The documentation for this class was generated from the following file: