XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrix.hpp
Go to the documentation of this file.
1 #ifndef MATRIX_HPP
2 #define MATRIX_HPP
3 
4 #include "space_settings.hpp"
5 #include "my_mirror_view.hpp"
6 
7 /* The Matrix class stores a sparse matrix. It is initialized in (?) format but can be converted to CSR (Compressed Sparse Row) with the convert_org_to_csr
8  * method.
9  * */
10 template<class Device>
11 class Matrix{
12 
13  using exspace = typename Device::execution_space;
14  public: // temporarily public, for testing
17  Add=1
18  };
19 
20  enum Type{
22  };
23 
24  int m;
25  int n;
26  int width; //< max of non-zero element of each row
27  View<double**,CLayout, Device> value;
28  View<int**,CLayout, Device> eindex;
29  View<int*,CLayout, Device> nelement;
30 
31  // use compressed sparse row matrix data structure
32  bool is_csr;
33  int nnz;
34  View<int*,CLayout, Device> csr_ridx;
35  View<double*,CLayout, Device> csr_v;
36  View<int*,CLayout, Device> csr_cidx;
37 
38  public:
39 
40  Matrix(){}
41 
42  Matrix(int m_in,int n_in,int w_in);
43 
44  Matrix(Type matrix_type, int m_in);
45 
46  Matrix(int m_in, int n_in, int w_in, int nnz_in, bool is_csr_in, int* csr_ridx_or_eindex, int* csr_cidx_or_nelement, double* values);
47 
48  // Create a mirror with a different device type
49  template<class Device2>
50  inline Matrix<Device2> mirror() const{
51  Matrix<Device2> mtx;
52 
53  mtx.m = m;
54  mtx.n = n;
55  mtx.width = width;
56  mtx.value = my_mirror_view(value, Device2());
57  mirror_copy(mtx.value, value);
58  mtx.eindex = my_mirror_view(eindex, Device2());
60  mtx.nelement = my_mirror_view(nelement, Device2());
62 
63  mtx.is_csr = is_csr;
64  mtx.nnz = nnz;
65  mtx.csr_ridx = my_mirror_view(csr_ridx, Device2());
67  mtx.csr_v = my_mirror_view(csr_v, Device2());
68  mirror_copy(mtx.csr_v, csr_v);
69  mtx.csr_cidx = my_mirror_view(csr_cidx, Device2());
71  return mtx;
72  }
73 
74  void convert_org_to_csr();
75 
76  // i row index
77  // j column index
78  // value_in is value
79  // flag is whether to replace or add
89  KOKKOS_INLINE_FUNCTION void set_value(int i, int j, double value_in, SetValueOpt flag) const{
90  if(is_csr){
91  DEVICE_PRINTF("\n Error: set_value is only allowed when matrix is not in csr format");
92  return;
93  }
94 
95  // Check matrix indices are valid
96  if(i>=m || j>=n || i<0 || j<0){
97  DEVICE_PRINTF("\n Error: Out of bounds access in set_value");
98  return;
99  }
100 
101  // Loop through indices that have already been assigned
102  for (int l=0; l<nelement(i); l++){
103  if(j==eindex(i,l)-1){ // If this index has already been assigned, replace or add the value (eindex is 1-indexed)
104  if(flag==Replace){
105  value(i,l) = value_in;
106  } else {
107  value(i,l) += value_in;
108  }
109  return; //DONE already -- exit the function
110  }
111  }
112 
113  // If the index has not already been assigned, then we need to add the index
114  // First, check that we have room in our mapping to add another index
115  if(nelement(i) < width){
116  int l=nelement(i);
117  eindex(i,l)=j+1; // 1-indexed
118  value(i,l)=value_in;
119  nelement(i) += 1;
120  }else{
121  DEVICE_PRINTF("\nError in set_value: Not enough memory space for matrix.");
122  DEVICE_PRINTF("\nIncrease width. width=%d but nelement(%d)=%d",width,i,nelement(i));
123  }
124  }
125 
126 // private: // Can't be private because there is this cuda error:
127 // The enclosing parent function for an extended __host__ __device__ lambda cannot have private or protected access within its class
128 // There should be some workarounds available if this is a problem.
129 
130  void mult_org(const View<double*,CLayout,Device>& x, const View<double*,Kokkos::LayoutRight,Device>& y) const;
131 
132  void mult_csr(const View<double*,CLayout,Device>& x, const View<double*,Kokkos::LayoutRight,Device>& y) const;
133 
134  void mult_tensor_org(const View<double**,CLayout,Device>& x, const View<double**,Kokkos::LayoutRight,Device>& y) const;
135 
136  void mult_tensor_csr(const View<double**,CLayout,Device>& x, const View<double**,Kokkos::LayoutRight,Device>& y) const;
137 
138  void transpose_mult_org(const View<double*,CLayout,Device>& x, const View<double*,Kokkos::LayoutRight,Device>& y) const;
139 
140  void transpose_mult_csr(const View<double*,CLayout,Device>& x, const View<double*,Kokkos::LayoutRight,Device>& y) const;
141 
142  public:
143 
144  void mult(const View<double*,CLayout,Device>& x,const View<double*,Kokkos::LayoutRight,Device>& y) const;
145 
146  void mult_tensor(const View<double**,CLayout,Device>& x, const View<double**,Kokkos::LayoutRight,Device>& y) const;
147 
148  void transpose_mult(const View<double*,CLayout,Device>& x, const View<double*,Kokkos::LayoutRight,Device>& y) const;
149 
150  void write(const std::string stream_name, const std::string filename) const;
151 };
152 
153 #endif
View< int **, CLayout, Device > eindex
column index - 1-indexed!!
Definition: matrix.hpp:28
void transpose_mult_org(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:178
void mirror_copy(T1 &view_dest, const T2 &view_src)
Definition: my_mirror_view.hpp:122
#define DEVICE_PRINTF(...)
Definition: space_settings.hpp:86
void mult_tensor(const View< double **, CLayout, Device > &x, const View< double **, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:290
Matrix< Device2 > mirror() const
Definition: matrix.hpp:50
int nnz
if in CSR format, number of (nonzero) values
Definition: matrix.hpp:33
View< double *, CLayout, Device > csr_v
value of CSR - nnz
Definition: matrix.hpp:35
Definition: matrix.hpp:16
typename HostType::execution_space exspace
Use execution space where matrix views are allocated.
Definition: matrix.hpp:13
Definition: matrix.hpp:17
int n
of columns (size of each row)
Definition: matrix.hpp:25
void transpose_mult(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:306
View< int *, CLayout, Device > csr_cidx
columun index - nnz
Definition: matrix.hpp:36
void mult_tensor_csr(const View< double **, CLayout, Device > &x, const View< double **, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:237
View< int *, CLayout, Device > csr_ridx
row index for CSR - m+1
Definition: matrix.hpp:34
int m
of rows (size of each column)
Definition: matrix.hpp:24
int width
Definition: matrix.hpp:26
Definition: matrix.hpp:11
bool is_csr
Whether the matrix is in CSR format.
Definition: matrix.hpp:32
Matrix()
Definition: matrix.hpp:40
void write(const std::string stream_name, const std::string filename) const
Definition: matrix.cpp:315
View< T *, CLayout, Device > my_mirror_view(const View< T *, CLayout, Device > &view, Device nd)
Definition: my_mirror_view.hpp:14
void convert_org_to_csr()
Definition: matrix.cpp:94
KOKKOS_INLINE_FUNCTION void set_value(int i, int j, double value_in, SetValueOpt flag) const
Definition: matrix.hpp:89
Type
Definition: matrix.hpp:20
SetValueOpt
Definition: matrix.hpp:15
void mult(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:259
View< int *, CLayout, Device > nelement
of non-zero element of each row
Definition: matrix.hpp:29
Definition: matrix.hpp:21
void transpose_mult_csr(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:197
void mult_org(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:141
View< double **, CLayout, Device > value
matrix value
Definition: matrix.hpp:27
void mult_tensor_org(const View< double **, CLayout, Device > &x, const View< double **, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:216
void mult_csr(const View< double *, CLayout, Device > &x, const View< double *, Kokkos::LayoutRight, Device > &y) const
Definition: matrix.cpp:159