XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
col_grid_matrix.hpp
Go to the documentation of this file.
1 #ifndef XGC_CORE_CPP_COL_GRID_MATRIX_HPP_
2 #define XGC_CORE_CPP_COL_GRID_MATRIX_HPP_
3 
4 #include <stdexcept>
5 #include <memory>
6 
7 #include <Kokkos_DualView.hpp>
8 
9 #include "space_settings.hpp"
10 
11 #ifdef USE_GINKGO
12 #include <ginkgo/ginkgo.hpp>
13 #endif
14 
15 
16 namespace Collisions {
17 
18 
19 using size_type = int;
20 
21 
22 template <typename Device>
24 {
25 public:
26  using index_type = int;
27  using host_type = HostType; //Kokkos::HostSpace;
28  using device = Device;
29  using value_type = double;
30  using values_array_t = Kokkos::View<value_type***, Kokkos::LayoutRight, Device,
31  Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
32  using const_values_array_t =
33  Kokkos::View<const value_type***, Kokkos::LayoutRight, Device,
34  Kokkos::MemoryTraits<Kokkos::Unmanaged>>;
35  using vector_view_t = Kokkos::View<value_type****, Kokkos::LayoutRight, Device>;
36  using const_vector_view_t = Kokkos::View<const value_type****, Kokkos::LayoutRight, Device>;
37  using vector_dualview_t = Kokkos::DualView<value_type****, Kokkos::LayoutRight, Device>;
38  using const_vector_dualview_t = Kokkos::DualView<const value_type****, Kokkos::LayoutRight, Device>;
39 
40  GridMatrix(size_type n_nodes, int n_species)
41  : num_nodes_{n_nodes}, num_species_{n_species}, batch_size_{n_nodes * n_species}
42  { }
43 
44  virtual ~GridMatrix() { }
45 
46  typename Kokkos::View<index_type**, device>::HostMirror host_index_map_LU() {
47  return index_map_.h_view;
48  }
49 
50  Kokkos::View<index_type**, device> device_index_map_LU() const {
51  return index_map_.d_view;
52  }
53 
54  virtual const_values_array_t get_const_values() const = 0;
55 
56  virtual values_array_t get_values() = 0;
57 
58  virtual void subtract_from_identity() = 0;
59 
60  // For picard_step_init
61  virtual void add_identity_multiply(const_vector_view_t dist_col,
62  vector_view_t dist_iter) const = 0;
63 
64  virtual void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const = 0;
65 
66  index_type get_num_nonzeros() const { return nnz_; }
67  index_type get_num_rows() const { return num_rows_; }
68 
69 protected:
70  Kokkos::DualView<index_type**,device> index_map_;
79 };
80 
81 
82 template <typename Device>
83 class CSCMatrix : public GridMatrix<Device>
84 {
85 public:
92  using internal_values_array_t = Kokkos::DualView<value_type***, Kokkos::LayoutRight, Device>;
97 
98  CSCMatrix(size_type n_nodes, index_type num_species, int nvr, int nvz)
99  : GridMatrix<Device>(n_nodes, num_species), nvr_{nvr},
100  nvz_{nvz}
101  {
102  setup_sparsity();
103  }
104 
105  const_values_array_t get_const_values() const override;
106 
107  values_array_t get_values() override;
108 
113  void subtract_from_identity() override;
114 
116  vector_view_t dist_iter) const override;
117 
118  void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const override;
119 
120 protected:
121  using GridMatrix<Device>::index_map_;
122  using GridMatrix<Device>::num_nodes_;
123  using GridMatrix<Device>::num_rows_;
124  using GridMatrix<Device>::nnz_;
125  using GridMatrix<Device>::batch_size_;
126  using GridMatrix<Device>::num_species_;
127 
130 
132  Kokkos::View<index_type*,device> central_locs_;
133  Kokkos::View<int*,host_type> LU_rowindx_;
134  Kokkos::View<int*,host_type> LU_colptr_;
135 
136  void setup_sparsity();
137 };
138 
139 #ifdef USE_GINKGO
140 
141 template <typename Device>
142 class ELLMatrix : public GridMatrix<Device>
143 {
144 public:
145  using index_type = typename GridMatrix<Device>::index_type;
146  using host_type = typename GridMatrix<Device>::host_type;
147  using device = typename GridMatrix<Device>::device;
148  using value_type = typename GridMatrix<Device>::value_type;
149  using values_array_t = typename GridMatrix<Device>::values_array_t;
150  using const_values_array_t = typename GridMatrix<Device>::const_values_array_t;
151  using vector_view_t = typename GridMatrix<Device>::vector_view_t;
152  using const_vector_view_t = typename GridMatrix<Device>::const_vector_view_t;
153  using vector_dualview_t = typename GridMatrix<Device>::vector_dualview_t;
154  using const_vector_dualview_t = typename GridMatrix<Device>::const_vector_dualview_t;
155  using mtx_type = gko::matrix::BatchEll<value_type, index_type>;
156 
157  static constexpr int max_nnz_per_row = 9;
158 
159  ELLMatrix(std::shared_ptr<const gko::Executor> exec, size_type n_matrices,
160  index_type num_species, int nvr, int nvz)
161  : exec_{exec}, GridMatrix<Device>(n_matrices, num_species), nvr_{nvr},
162  nvz_{nvz}
163  {
164  setup_sparsity();
165  }
166 
167  const_values_array_t get_const_values() const override;
168 
169  values_array_t get_values() override;
170 
171  void subtract_from_identity() override;
172 
173  void add_identity_multiply(const_vector_view_t dist_col,
174  vector_view_t dist_iter) const override;
175 
176  void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const override;
177 
178 protected:
179  using GridMatrix<Device>::index_map_;
180  using GridMatrix<Device>::num_nodes_;
181  using GridMatrix<Device>::num_rows_;
182  using GridMatrix<Device>::nnz_;
183  using GridMatrix<Device>::batch_size_;
184  using GridMatrix<Device>::num_species_;
185  std::shared_ptr<const gko::Executor> exec_;
186  std::shared_ptr<mtx_type> mtx_;
187 
188  index_type nvz_;
189  index_type nvr_;
190 
191  void setup_sparsity();
192 };
193 
194 #endif
195 
196 template <typename Device>
197 class InvalidMatrixType : public std::exception
198 {
199 public:
201  {
202  std::string mtx_type = "unknown";
203  if(dynamic_cast<const CSCMatrix<Device>*>(mtx)) {
204  mtx_type = "CSC";
205  }
206  what_ = mtx_type + " matrix type not supported!";
207  }
208 
209  virtual const char* what() const noexcept override { return what_.c_str(); }
210 
211 private:
212  std::string what_;
213 };
214 
215 
216 enum class LinAlgBackend {
217  lapack,
218  ginkgo
219 };
220 
221 
222 template <typename Device>
223 std::unique_ptr<GridMatrix<Device>> create_matrix(size_type n_matrices,
224  int nvr, int nvz, int num_species, LinAlgBackend labackend,
225  std::string format = "default");
226 
227 
228 }
229 
230 
231 #include "col_grid_matrix.tpp"
232 
233 
234 #endif
Definition: col_grid_matrix.hpp:23
Definition: col_grid_matrix.hpp:83
typename GridMatrix< Device >::const_vector_view_t const_vector_view_t
Definition: col_grid_matrix.hpp:94
index_type num_rows_
Definition: col_grid_matrix.hpp:77
typename GridMatrix< Device >::vector_view_t vector_view_t
Definition: col_grid_matrix.hpp:93
index_type nnz_
Definition: col_grid_matrix.hpp:78
Kokkos::DualView< const value_type ****, Kokkos::LayoutRight, Device > const_vector_dualview_t
Definition: col_grid_matrix.hpp:38
const_values_array_t get_const_values() const override
Definition: col_grid_matrix.tpp:224
Kokkos::Device< HostExSpace, HostMemSpace > HostType
Definition: space_settings.hpp:56
virtual void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const =0
Kokkos::View< index_type **, device > device_index_map_LU() const
Definition: col_grid_matrix.hpp:50
Kokkos::View< value_type ***, Kokkos::LayoutRight, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged >> values_array_t
Definition: col_grid_matrix.hpp:31
virtual const char * what() const noexceptoverride
Definition: col_grid_matrix.hpp:209
void subtract_from_identity() override
Definition: col_grid_matrix.tpp:237
Kokkos::View< int *, host_type > LU_rowindx_
Definition: col_grid_matrix.hpp:133
values_array_t get_values() override
Definition: col_grid_matrix.tpp:214
void setup_sparsity()
Definition: col_grid_matrix.tpp:5
Definition: col_grid_matrix.hpp:197
HostType host_type
Definition: col_grid_matrix.hpp:27
Kokkos::DualView< value_type ***, Kokkos::LayoutRight, Device > internal_values_array_t
Definition: col_grid_matrix.hpp:92
int size_type
Definition: col_grid_matrix.hpp:19
const index_type num_species_
Definition: col_grid_matrix.hpp:74
virtual ~GridMatrix()
Definition: col_grid_matrix.hpp:44
typename GridMatrix< Device >::values_array_t values_array_t
Definition: col_grid_matrix.hpp:90
Kokkos::View< const value_type ***, Kokkos::LayoutRight, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged >> const_values_array_t
Definition: col_grid_matrix.hpp:34
index_type nvr_
Definition: col_grid_matrix.hpp:129
GridMatrix(size_type n_nodes, int n_species)
Definition: col_grid_matrix.hpp:40
index_type nvz_
Definition: col_grid_matrix.hpp:128
Kokkos::View< index_type *, device > central_locs_
Definition: col_grid_matrix.hpp:132
Kokkos::DualView< index_type **, device > index_map_
Definition: col_grid_matrix.hpp:70
typename GridMatrix< Device >::const_values_array_t const_values_array_t
Definition: col_grid_matrix.hpp:91
index_type get_num_nonzeros() const
Definition: col_grid_matrix.hpp:66
virtual const_values_array_t get_const_values() const =0
double value_type
Definition: col_grid_matrix.hpp:29
Kokkos::DualView< value_type ****, Kokkos::LayoutRight, Device > vector_dualview_t
Definition: col_grid_matrix.hpp:37
internal_values_array_t values_
Definition: col_grid_matrix.hpp:131
void add_identity_multiply(const_vector_view_t dist_col, vector_view_t dist_iter) const override
Definition: col_grid_matrix.tpp:262
Kokkos::View< value_type ****, Kokkos::LayoutRight, Device > vector_view_t
Definition: col_grid_matrix.hpp:35
const size_type batch_size_
Definition: col_grid_matrix.hpp:76
InvalidMatrixType(const GridMatrix< Device > *const mtx)
Definition: col_grid_matrix.hpp:200
Kokkos::View< const value_type ****, Kokkos::LayoutRight, Device > const_vector_view_t
Definition: col_grid_matrix.hpp:36
int index_type
Definition: col_grid_matrix.hpp:26
typename GridMatrix< Device >::vector_dualview_t vector_dualview_t
Definition: col_grid_matrix.hpp:95
LinAlgBackend
Definition: col_grid_matrix.hpp:216
typename GridMatrix< Device >::const_vector_dualview_t const_vector_dualview_t
Definition: col_grid_matrix.hpp:96
Kokkos::View< index_type **, device >::HostMirror host_index_map_LU()
Definition: col_grid_matrix.hpp:46
index_type get_num_rows() const
Definition: col_grid_matrix.hpp:67
CSCMatrix(size_type n_nodes, index_type num_species, int nvr, int nvz)
Definition: col_grid_matrix.hpp:98
std::unique_ptr< GridMatrix< Device > > create_matrix(size_type n_matrices, int nvr, int nvz, int num_species, LinAlgBackend labackend, std::string format="default")
Definition: col_grid_matrix.tpp:663
const index_type num_nodes_
Definition: col_grid_matrix.hpp:75
virtual void add_identity_multiply(const_vector_view_t dist_col, vector_view_t dist_iter) const =0
typename GridMatrix< Device >::index_type index_type
Definition: col_grid_matrix.hpp:86
std::string what_
Definition: col_grid_matrix.hpp:212
Device device
Definition: col_grid_matrix.hpp:28
virtual values_array_t get_values()=0
Kokkos::View< int *, host_type > LU_colptr_
Definition: col_grid_matrix.hpp:134
void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const override
Definition: col_grid_matrix.tpp:319
virtual void subtract_from_identity()=0