Phoebe developer's documentation  1.1.0
Phonon and Electron Boltzmann Equations
Matrix.h
1 #ifndef MATRIX_H
2 #define MATRIX_H
3 
4 #include "PMatrix.h"
5 #include "SMatrix.h"
6 
22 template <typename T>
23 class Matrix {
24 
26  int global2Local(const int& row, const int& col);
27  std::tuple<int, int> local2Global(const int& k);
28 
32  bool isDistributed;
33 
36  ParallelMatrix<T>* pmat = nullptr;
37 
40  SerialMatrix<T>* mat = nullptr;
41 
42  public:
51  Matrix(const int& numRows, const int& numCols, const int& numBlocksRows = 0,
52  const int& numBlocksCols = 0, bool isDistributed_ = false);
53 
56  Matrix();
57 
61 
64  Matrix(const Matrix<T>& that);
65 
68  Matrix<T>& operator=(const Matrix<T>& that);
69 
73  std::vector<std::tuple<int, int>> getAllLocalStates();
74 
78  bool indicesAreLocal(const int& row, const int& col);
79 
82  int rows() const;
84  int localRows() const;
86  int cols() const;
88  int localCols() const;
90  int size() const;
92  double getMemory() const;
93 
96  T& operator()(const int &row, const int &col);
97 
100  const T& operator()(const int &row, const int &col) const;
101 
113  Matrix<T> prod(const Matrix<T>& that, const char& trans1,
114  const char& trans2);
115 
119  if(isDistributed) (*pmat) += (*m1.pmat);
120  else (*mat) += (*m1.mat);
121  return *this;
122  }
123 
127  if(isDistributed) (*pmat) -= (*m1.pmat);
128  else (*mat) -= (*m1.mat);
129  return *this;
130  }
131 
134  Matrix<T> operator*=(const T& that) {
135  if(isDistributed) (*pmat) *= that;
136  else (*mat) *= that;
137  return *this;
138  }
139 
142  Matrix<T> operator/=(const T& that) {
143  if(isDistributed) (*pmat) /= that;
144  else (*mat) /= that;
145  return *this;
146  }
147 
150  void eye();
151 
155  std::tuple<std::vector<double>, Matrix<T>> diagonalize();
156 
161  std::tuple<std::vector<double>, Matrix<T>> diagonalize(int numEigenvalues,
162  bool checkNegativeEigenvalues = true);
163 
167  double squaredNorm();
168 
172  double norm();
173 
178  T dot(const Matrix<T>& that);
179 
183 
186  void outputToHDF5();
187 
190  void symmetrize();
191 
192 };
193 
194 /* ------------------ constructor implementations -------------- */
195 
196 // A default constructor to build a dense matrix of zeros to be filled
197 template <typename T>
198 Matrix<T>::Matrix(const int& numRows, const int& numCols,
199  const int& numBlocksRows, const int& numBlocksCols, bool isDistributed_) {
200 
201  isDistributed = isDistributed_; // default to false if no value supplied
202 
203  if(isDistributed){
204  pmat = new ParallelMatrix<T>(numRows,numCols,numBlocksRows,numBlocksCols);
205  }
206  else {
207  mat = new SerialMatrix<T>(numRows,numCols);
208  }
209 }
210 
211 // default constructor
212 template <typename T>
214  isDistributed = false;
215  if (pmat!=nullptr) delete pmat;
216  if (mat!=nullptr) delete mat;
217  mat = new SerialMatrix<T>();
218 }
219 
220 // copy constructor
221 template <typename T>
223  isDistributed = that.isDistributed;
224 
225  // call SMatrix or PMatrix copy constructor
226  if(isDistributed) {
227  pmat = that.pmat;
228  }
229  else {
230  mat = that.mat;
231  }
232 }
233 
234 template <typename T>
236  if (this != &that) {
237  isDistributed = that.isDistributed;
238  // call SMatrix or PMatrix copy constructor
239  if(isDistributed) {
240  if (pmat!=nullptr) delete pmat;
241  pmat = new ParallelMatrix<T>(*that.pmat);
242  }
243  else {
244  if (mat!=nullptr) delete mat;
245  mat = new SerialMatrix<T>(*that.mat);
246  }
247  }
248  return *this;
249 }
250 
251 // destructor
252 template <typename T>
254  if (pmat!=nullptr) {
255  delete pmat;
256  }
257  if (mat!=nullptr) {
258  delete mat;
259  }
260 }
261 
262 /* ------------- Very basic operations -------------- */
263 template <typename T>
264 int Matrix<T>::rows() const {
265  if(isDistributed) return pmat->rows();
266  else{ return mat->rows(); }
267 }
268 template <typename T>
269 int Matrix<T>::localRows() const {
270  if(isDistributed) return pmat->localRows();
271  else{ return mat->rows(); }
272 }
273 template <typename T> int Matrix<T>::cols() const {
274  if(isDistributed) return pmat->cols();
275  else{ return mat->cols(); }
276 }
277 template <typename T>
278 int Matrix<T>::localCols() const {
279  if(isDistributed) return pmat->localCols();
280  else{ return mat->cols(); }
281 }
282 template <typename T>
283 int Matrix<T>::size() const {
284  if(isDistributed) return pmat->size();
285  else{ return mat->size(); }
286 }
287 
288 /* ------------- get-set operations -------------- */
289 template <typename T>
290 double Matrix<T>::getMemory() const{
291  // this is done in parts to avoid overflow;
292  // size in GB is size of type*rows()*cols()/(1024)^3
293  double temp = sizeof(T)*rows()/1024.;
294  temp = temp*cols()/1024.;
295  return temp/(1024.);
296 }
297 
298 template <typename T>
299 T& Matrix<T>::operator()(const int &row, const int &col) {
300  if(isDistributed) return (*pmat)(row,col);
301  else { return (*mat)(row,col); }
302 }
303 
304 template <typename T>
305 const T& Matrix<T>::operator()(const int &row, const int &col) const {
306  if(isDistributed) return (*pmat)(row,col);
307  else{ return (*mat)(row,col); }
308 }
309 
310 template <typename T>
311 bool Matrix<T>::indicesAreLocal(const int& row, const int& col) {
312  if(isDistributed) return pmat->indicesAreLocal(row,col);
313  else{ return true; }
314 }
315 
316 template <typename T>
317 std::tuple<int, int> Matrix<T>::local2Global(const int& k) {
318  if(isDistributed) return pmat->local2Global(k);
319  else{ return mat->local2Global(k); }
320 }
321 
322 // Indexing to set up the matrix in col major format
323 template <typename T>
324 int Matrix<T>::global2Local(const int& row, const int& col) {
325  if(isDistributed) return pmat->global2Local(row,col);
326  else{ return mat->global2Local(row,col); }
327 }
328 
329 template <typename T>
330 std::vector<std::tuple<int, int>> Matrix<T>::getAllLocalStates() {
331  if(isDistributed) return pmat->getAllLocalStates();
332  else{ return mat->getAllLocalStates(); }
333 }
334 
335 /* ------------- basic linear algebra ops -------------- */
336 // General unary negation
337 template <typename T>
339  Matrix<T> c(*this); // copy this matrix
340  if (isDistributed) *(c.pmat) = -*(c.pmat);
341  else *(c.mat) = -*(c.mat);
342  return c;
343 }
344 
345 // Sets the matrix to the identity matrix
346 template <typename T>
348  if(isDistributed) pmat->eye();
349  else{ mat->eye(); }
350 }
351 
352 template <typename T>
353 double Matrix<T>::norm() {
354  if(isDistributed) return pmat->norm();
355  else{ return mat->norm(); }
356 }
357 
358 template <typename T>
360  if(isDistributed) return pmat->squaredNorm();
361  else{ return mat->squaredNorm(); }
362 }
363 
364 template <typename T>
365 T Matrix<T>::dot(const Matrix<T>& that) {
366  if(isDistributed) return pmat->dot(that.pmat);
367  else{ return mat->dot(that.mat); }
368 }
369 
370 template <typename T>
372  if(isDistributed) pmat->outputToHDF5();
373  else{ Error("Write to HDF5 not implemented for SMatrix."); }
374 }
375 
376 #endif // MATRIX_H
Object used to print an error message, and stop the code.
Definition: exceptions.h:10
Container class which wraps an underlying serial or parallel matrix.
Definition: Matrix.h:23
std::tuple< std::vector< double >, Matrix< T > > diagonalize(int numEigenvalues, bool checkNegativeEigenvalues=true)
Diagonalize a complex-hermitian / real-symmetric matrix for only some of it's eigenvalues.
void symmetrize()
Symmetrize the matrix.
Matrix< T > operator-=(const Matrix< T > &m1)
Matrix-matrix subtraction.
Definition: Matrix.h:126
void outputToHDF5()
A function to write the matrix to HDF5.
Definition: Matrix.h:371
T & operator()(const int &row, const int &col)
Get and set operator.
Definition: Matrix.h:299
int rows() const
Find global number of rows.
Definition: Matrix.h:264
Matrix(const int &numRows, const int &numCols, const int &numBlocksRows=0, const int &numBlocksCols=0, bool isDistributed_=false)
Default Matrix constructor.
Definition: Matrix.h:198
int size() const
Find global number of matrix elements.
Definition: Matrix.h:283
T dot(const Matrix< T > &that)
Computes a "scalar product" between two matrices A and B, defined as \sum_ij A_ij * B_ij.
Definition: Matrix.h:365
Matrix(const Matrix< T > &that)
Copy constructor.
Definition: Matrix.h:222
Matrix< T > operator-() const
Unary negation.
Definition: Matrix.h:338
Matrix< T > operator/=(const T &that)
Matrix-scalar division.
Definition: Matrix.h:142
double squaredNorm()
Computes the squared Frobenius norm of the matrix (or Euclidean norm, or L2 norm of the matrix)
Definition: Matrix.h:359
const T & operator()(const int &row, const int &col) const
Const get and set operator.
Definition: Matrix.h:305
double norm()
Computes the Frobenius norm of the matrix (or Euclidean norm, or L2 norm of the matrix).
Definition: Matrix.h:353
double getMemory() const
Return the size of the matrix in GB.
Definition: Matrix.h:290
std::tuple< std::vector< double >, Matrix< T > > diagonalize()
Diagonalize a complex-hermitian / real-symmetric matrix.
bool indicesAreLocal(const int &row, const int &col)
Returns true if the global indices (row,col) identify a matrix element stored by the MPI process.
Definition: Matrix.h:311
Matrix< T > & operator=(const Matrix< T > &that)
Copy constructor.
Definition: Matrix.h:235
~Matrix()
Destructor, to delete raw buffer.
Definition: Matrix.h:253
Matrix()
Default constructor.
Definition: Matrix.h:213
Matrix< T > operator*=(const T &that)
Matrix-scalar multiplication.
Definition: Matrix.h:134
int localCols() const
Return local number of rows.
Definition: Matrix.h:278
Matrix< T > prod(const Matrix< T > &that, const char &trans1, const char &trans2)
Matrix-matrix multiplication.
int localRows() const
Return local number of rows.
Definition: Matrix.h:269
int cols() const
Find global number of columns.
Definition: Matrix.h:273
void eye()
Sets this matrix as the identity.
Definition: Matrix.h:347
std::vector< std::tuple< int, int > > getAllLocalStates()
Find the global indices of the matrix elements that are stored locally by the current MPI process.
Definition: Matrix.h:330
Matrix< T > operator+=(const Matrix< T > &m1)
Matrix-matrix addition.
Definition: Matrix.h:118
Class for managing a (serial) matrix stored in memory.
Definition: SMatrix.h:20