ParGeMSLR
csr_matrix.hpp
Go to the documentation of this file.
1 #ifndef PARGEMSLR_CSR_MATRIX_H
2 #define PARGEMSLR_CSR_MATRIX_H
3 
8 #include <iostream>
9 
10 #include "../utils/utils.hpp"
11 #include "../vectors/int_vector.hpp"
12 #include "../vectors/sequential_vector.hpp"
13 #include "matrix.hpp"
14 
15 #ifdef PARGEMSLR_CUDA
16 #include "cusparse.h"
17 #endif
18 
19 namespace pargemslr
20 {
21 
26  template <typename T>
27  class CsrMatrixClass: public MatrixClass<T>
28  {
29  private:
30 
31  /* variables */
32 
37  IntVectorClass<int> _i_vec;
38 
43  IntVectorClass<int> _j_vec;
44 
49  SequentialVectorClass<T> _a_vec;
50 
55  int _nrows;
56 
61  int _ncols;
62 
67  int _nnz;
68 
73  bool _isrowsorted;
74 
79  bool _isholdingdata;
80 
85  bool _iscsr;
86 
87 #ifdef PARGEMSLR_CUDA
88 #if (PARGEMSLR_CUDA_VERSION == 11)
89 
93  cusparseSpMatDescr_t _cusparse_mat;
94 #endif
95 #endif
96 
97  public:
98 
104  typename std::conditional<PargemslrIsDoublePrecision<T>::value,
105  double,
106  float>::type _diagonal_shift;
107 
112  CsrMatrixClass();
113 
119  CsrMatrixClass(const CsrMatrixClass<T> &mat);
120 
127 
135 
143 
148  virtual ~CsrMatrixClass();
149 
159  int Setup(int nrows, int ncols, int nnz);
160 
170  int Setup(int nrows, int ncols, int nnz, int location);
171 
182  int Setup(int nrows, int ncols, int nnz, bool setzero);
183 
195  int Setup(int nrows, int ncols, int nnz, bool holdvalue, bool setzero);
196 
208  int Setup(int nrows, int ncols, int nnz, int location, bool setzero);
209 
222  int Setup(int nrows, int ncols, int nnz, int location, bool holdvalue, bool setzero);
223 
237  int Setup(int nrows, int ncols, int nnz, int location, bool holdvalue, bool setzero, bool iscsr);
238 
247  int PushBack( int col, T v);
248 
260  int SubMatrix( int row_start, int col_start, int num_rows, int num_cols, int location, CsrMatrixClass<T> &csrmat_out);
261 
275  int SubMatrix( vector_int &row_starts, vector_int &col_starts, vector_int &row_ends, vector_int &col_ends, int location, CsrMatrixClass<T> &csrmat_out);
276 
287  int SubMatrix( vector_int &rows, vector_int &cols, int location, CsrMatrixClass<T> &csrmat_out);
288 
303  int SubMatrixNoPerm( vector_int &rows, vector_int &cols, vector_int &row_perm, vector_int &col_perm, bool complement, int location, CsrMatrixClass<T> &csrmat_out);
304 
314  int GetConnectedComponents(std::vector<vector_int> &comp_indices, int &ncomps);
315 
323 
329  virtual int Clear();
330 
336  virtual int GetDataLocation() const;
337 
343  virtual int GetNumRowsLocal() const;
344 
350  virtual int GetNumColsLocal() const;
351 
357  virtual long int GetNumNonzeros() const;
358 
364  int SetNumNonzeros();
365 
371  int* GetI() const;
372 
378  int* GetJ() const;
379 
385  T* GetData() const;
386 
393 
399  const IntVectorClass<int>& GetIVector() const;
400 
407 
413  const IntVectorClass<int>& GetJVector() const;
414 
421 
428 
429 #ifdef PARGEMSLR_CUDA
430 #if (PARGEMSLR_CUDA_VERSION == 11)
431 
436  cusparseSpMatDescr_t GetCusparseMat() const;
437 
444  int SetCusparseMat(cusparseSpMatDescr_t cusparse_mat);
445 #endif
446 #endif
447 
453  bool IsHoldingData() const;
454 
460  bool& IsCsr();
461 
467  const bool& IsCsr() const;
468 
474  bool& IsRowSorted();
475 
481  const bool& IsRowSorted() const;
482 
488  int SortRow();
489 
495  virtual int Eye();
496 
503  virtual int Fill(const T &v);
504 
511  virtual int MoveData( const int &location);
512 
519  virtual int Scale(const T &alpha);
520 
527  int GetDiagScale(T &scale);
528 
535  int Convert(bool csr);
536 
542  int Transpose();
543 
550  int Transpose(CsrMatrixClass<T> &AT);
551 
562  virtual int MatVec( char trans, const T &alpha, const VectorClass<T> &x, const T &beta, VectorClass<T> &y);
563 
575  virtual int MatVec( char trans, const T &alpha, const VectorClass<T> &x, const T &beta, const VectorClass<T> &y, VectorClass<T> &z);
576 
589  int MatMat( const T &alpha, const CsrMatrixClass<T> &A, char transa, const CsrMatrixClass<T> &B, char transb, const T &beta);
590 
600  int PlotPattern( int *rperm, int *cperm, int conditiona, int conditionb);
601 
612  int PlotPatternGnuPlot( const char *datafilename, int *rperm, int *cperm, int conditiona, int conditionb);
613 
623  int Plot( int *perm, int conditiona, int conditionb, int width);
624 
639  int Laplacian(int nx, int ny, int nz, T alphax, T alphay, T alphaz, T shift, bool rand_perturb = false);
640 
648  int Helmholtz(int n, T w);
649 
657  int ReadFromMMFile(const char *matfile, int idxin);
658 
665  template <typename T1>
666  int SetComplexShift(T1 diagonal_shift)
667  {
668  this->_diagonal_shift = diagonal_shift;
669  return PARGEMSLR_SUCCESS;
670  }
671 
678  template <typename T1>
679  int GetComplexShift(T1 &diagonal_shift);
680 
687  template <typename T1>
688  int GetComplexShift(ComplexValueClass<T1> &diagonal_shift);
689 
698  int GetMpiInfo(int &np, int &myid, MPI_Comm &comm) const
699  {
700  comm = *parallel_log::_lcomm;
701  np = 1;
702  myid = 0;
703  return PARGEMSLR_SUCCESS;
704  }
705 
711  MPI_Comm GetComm() const
712  {
713  return *parallel_log::_lcomm;
714  }
715 
716  };
717 
718  typedef CsrMatrixClass<float> matrix_csr_float;
719  typedef CsrMatrixClass<double> matrix_csr_double;
720  typedef CsrMatrixClass<complexs> matrix_csr_complexs;
721  typedef CsrMatrixClass<complexd> matrix_csr_complexd;
722  template<> struct PargemslrIsComplex<CsrMatrixClass<complexs> > : public std::true_type {};
723  template<> struct PargemslrIsComplex<CsrMatrixClass<complexd> > : public std::true_type {};
724 
725 }
726 
727 #endif
pargemslr::CsrMatrixClass::Scale
virtual int Scale(const T &alpha)
Scale the current csr matrix.
Definition: csr_matrix.cpp:1262
pargemslr::CsrMatrixClass::IsCsr
bool & IsCsr()
Csr or csc.
Definition: csr_matrix.cpp:1144
pargemslr::CsrMatrixClass::Fill
virtual int Fill(const T &v)
Fill the matrix with constant value.
Definition: csr_matrix.cpp:1248
pargemslr::CsrMatrixClass::Transpose
int Transpose()
Transpost the current matrix.
Definition: csr_matrix.cpp:1455
pargemslr::CsrMatrixClass::GetComplexShift
int GetComplexShift(T1 &diagonal_shift)
Get the diagonal complex shift for some preconditioner options.
Definition: csr_matrix.cpp:1723
pargemslr::CsrMatrixClass::Plot
int Plot(int *perm, int conditiona, int conditionb, int width)
Plot the csr matrix to the terminal output. Function for testing purpose.
Definition: csr_matrix.cpp:1658
pargemslr::CsrMatrixClass::MatMat
int MatMat(const T &alpha, const CsrMatrixClass< T > &A, char transa, const CsrMatrixClass< T > &B, char transb, const T &beta)
Dense Matrix-Matrix multiplication, C = alpha*transa(A)*transb(B)+beta*C where C is this matrix.
Definition: csr_matrix.cpp:1372
pargemslr::CsrMatrixClass::IsRowSorted
bool & IsRowSorted()
Tell if the column/row indices in each row/column is sorted.
Definition: csr_matrix.cpp:1164
pargemslr::CsrMatrixClass::CsrMatrixClass
CsrMatrixClass()
The constructor of CsrMatrixClass.
Definition: csr_matrix.cpp:23
pargemslr::CsrMatrixClass::SortRow
int SortRow()
Sort the column/row indices in each row/column.
Definition: csr_matrix.cpp:1189
pargemslr::CsrMatrixClass::GetDataVector
SequentialVectorClass< T > & GetDataVector()
Get the reference to the data vector.
Definition: csr_matrix.cpp:1085
pargemslr::CsrMatrixClass::Eye
virtual int Eye()
Create an indentity matrix.
Definition: csr_matrix.cpp:1201
pargemslr::CsrMatrixClass::MatVec
virtual int MatVec(char trans, const T &alpha, const VectorClass< T > &x, const T &beta, VectorClass< T > &y)
In place csr Matrix-Vector product ==> y := alpha*A*x + beta*y, or y := alpha*A'*x + beta*y.
Definition: csr_matrix.cpp:1340
pargemslr::CsrMatrixClass::GetIVector
IntVectorClass< int > & GetIVector()
Get the reference to the I vector.
Definition: csr_matrix.cpp:1043
pargemslr::CsrMatrixClass::GetDataLocation
virtual int GetDataLocation() const
Get the data location of the matrix.
Definition: csr_matrix.cpp:938
pargemslr::CsrMatrixClass::GetNumColsLocal
virtual int GetNumColsLocal() const
Get the local number of columns of the matrix.
Definition: csr_matrix.cpp:965
pargemslr::CsrMatrixClass::SubMatrix
int SubMatrix(int row_start, int col_start, int num_rows, int num_cols, int location, CsrMatrixClass< T > &csrmat_out)
Copy data to extract a submatrix of the current matrix.
Definition: csr_matrix.cpp:398
pargemslr::CsrMatrixClass::Helmholtz
int Helmholtz(int n, T w)
Generate 3D Helmholtz matrix with 7-pt on [0,1]^3. -\Delta u - w^2 u = 0.
Definition: csr_matrix.cpp:1692
pargemslr::CsrMatrixClass::Convert
int Convert(bool csr)
Convert between csr and csc.
Definition: csr_matrix.cpp:1383
pargemslr::CsrMatrixClass::operator=
CsrMatrixClass< T > & operator=(const CsrMatrixClass< T > &mat)
The = operator of CsrMatrixClass.
Definition: csr_matrix.cpp:111
pargemslr::CsrMatrixClass::IsHoldingData
bool IsHoldingData() const
Check if the matrix holding its own data.
Definition: csr_matrix.cpp:1134
pargemslr::CsrMatrixClass::GetConnectedComponents
int GetConnectedComponents(std::vector< vector_int > &comp_indices, int &ncomps)
Get the connected components of A + AT, where A is the current matrix.
Definition: csr_matrix.cpp:729
pargemslr::CsrMatrixClass::SetComplexShift
int SetComplexShift(T1 diagonal_shift)
Set the diagonal complex shift for some preconditioner options.
Definition: csr_matrix.hpp:666
pargemslr::CsrMatrixClass::GetI
int * GetI() const
Get the I pointer of the matrix.
Definition: csr_matrix.cpp:1013
pargemslr::VectorClass
The virtual class of real/complex vector class.
Definition: vector.hpp:126
pargemslr::CsrMatrixClass::ReadFromMMFile
int ReadFromMMFile(const char *matfile, int idxin)
Read matrix on the host memory, matrix market format.
Definition: csr_matrix.cpp:1706
pargemslr::SequentialVectorClass
The class of sequential real/complex vector.
Definition: structs.hpp:12
pargemslr::CsrMatrixClass::GetComm
MPI_Comm GetComm() const
Get the MPI_comm.
Definition: csr_matrix.hpp:711
pargemslr::CsrMatrixClass::SubMatrixNoPerm
int SubMatrixNoPerm(vector_int &rows, vector_int &cols, vector_int &row_perm, vector_int &col_perm, bool complement, int location, CsrMatrixClass< T > &csrmat_out)
Copy data to extract a submatrix of the current matrix.
Definition: csr_matrix.cpp:668
pargemslr::CsrMatrixClass::Laplacian
int Laplacian(int nx, int ny, int nz, T alphax, T alphay, T alphaz, T shift, bool rand_perturb=false)
Generate Laplacian matrix on the host memory, 5-pt for 2D problem and 7-pt for 3D problem.
Definition: csr_matrix.cpp:1676
matrix.hpp
The virtual matrix classes.
pargemslr::ComplexValueClass
The template class complex.
Definition: complex.hpp:24
pargemslr::CsrMatrixClass::GetDiagScale
int GetDiagScale(T &scale)
Get the value leads to max diagonal no.
Definition: csr_matrix.cpp:1275
pargemslr::IntVectorClass< int >
pargemslr::CsrMatrixClass::GetMpiInfo
int GetMpiInfo(int &np, int &myid, MPI_Comm &comm) const
Get comm, np, and myid. Get the sequential comm.
Definition: csr_matrix.hpp:698
pargemslr::CsrMatrixClass::GetJVector
IntVectorClass< int > & GetJVector()
Get the reference to the J vector.
Definition: csr_matrix.cpp:1064
pargemslr::CsrMatrixClass::SetNumNonzeros
int SetNumNonzeros()
Set the number of nonzeros in this matrix.
Definition: csr_matrix.cpp:985
pargemslr::CsrMatrixClass::GetNumNonzeros
virtual long int GetNumNonzeros() const
Get the number of nonzeros in this matrix.
Definition: csr_matrix.cpp:975
pargemslr::CsrMatrixClass::MoveData
virtual int MoveData(const int &location)
Move the data to another memory location.
Definition: csr_matrix.cpp:1236
pargemslr::CsrMatrixClass::PushBack
int PushBack(int col, T v)
Insert value at the end of the J and A vector, user need to update the I vector.
Definition: csr_matrix.cpp:373
pargemslr::CsrMatrixClass::SetupVectorPtrStr
int SetupVectorPtrStr(SequentialVectorClass< T > &vec)
Update the structure of a vector to have same row permutation.
Definition: csr_matrix.cpp:928
pargemslr::CsrMatrixClass::GetData
T * GetData() const
Get the data pointer of the matrix.
Definition: csr_matrix.cpp:1033
pargemslr::CsrMatrixClass::PlotPattern
int PlotPattern(int *rperm, int *cperm, int conditiona, int conditionb)
Plot the pattern of the csr matrix to the terminal output. Similar to spy in the MATLAB....
Definition: csr_matrix.cpp:1493
pargemslr::CsrMatrixClass::Clear
virtual int Clear()
Free the current matrix.
Definition: csr_matrix.cpp:191
pargemslr::PargemslrIsComplex
Tell if a value is a complex value.
Definition: complex.hpp:684
pargemslr::CsrMatrixClass::GetNumRowsLocal
virtual int GetNumRowsLocal() const
Get the local number of rows of the matrix.
Definition: csr_matrix.cpp:955
pargemslr::CsrMatrixClass::Setup
int Setup(int nrows, int ncols, int nnz)
Free the current matrix, and allocate memory to create a new matrix.
Definition: csr_matrix.cpp:223
pargemslr::CsrMatrixClass::GetJ
int * GetJ() const
Get the J pointer of the matrix.
Definition: csr_matrix.cpp:1023
pargemslr::CsrMatrixClass::PlotPatternGnuPlot
int PlotPatternGnuPlot(const char *datafilename, int *rperm, int *cperm, int conditiona, int conditionb)
Plot the pattern of the csr matrix using gnuplot. Similar to spy in the MATLAB. Function for testing ...
Definition: csr_matrix.cpp:1574
pargemslr::CsrMatrixClass
Class of CSR/CSC matrices.
Definition: structs.hpp:16
pargemslr::CsrMatrixClass::~CsrMatrixClass
virtual ~CsrMatrixClass()
The destructor of CsrMatrixClass.
Definition: csr_matrix.cpp:181
pargemslr::CsrMatrixClass::_diagonal_shift
std::conditional< PargemslrIsDoublePrecision< T >::value, double, float >::type _diagonal_shift
The diagonal complex shift in some preconditioners. The preconditioner would be built on A+_diagonal_...
Definition: csr_matrix.hpp:106