XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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::batch::matrix::Ell<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  double ginkgo_residual_reduction, int ginkgo_max_iterations)
162  : exec_{exec}, GridMatrix<Device>(n_matrices, num_species), nvr_{nvr},
163  nvz_{nvz}, ginkgo_residual_reduction_{ginkgo_residual_reduction},
164  ginkgo_max_iterations_{ginkgo_max_iterations}
165  {
166  setup_sparsity();
167  }
168 
169  const_values_array_t get_const_values() const override;
170 
171  values_array_t get_values() override;
172 
173  void subtract_from_identity() override;
174 
175  void add_identity_multiply(const_vector_view_t dist_col,
176  vector_view_t dist_iter) const override;
177 
178  void apply_solve(const_vector_dualview_t b, vector_dualview_t x) const override;
179 
180 protected:
181  using GridMatrix<Device>::index_map_;
182  using GridMatrix<Device>::num_nodes_;
183  using GridMatrix<Device>::num_rows_;
184  using GridMatrix<Device>::nnz_;
185  using GridMatrix<Device>::batch_size_;
186  using GridMatrix<Device>::num_species_;
187  std::shared_ptr<const gko::Executor> exec_;
188  std::shared_ptr<mtx_type> mtx_;
189 
190  index_type nvz_;
191  index_type nvr_;
192 
193  double ginkgo_residual_reduction_;
194  int ginkgo_max_iterations_;
195 
196  void setup_sparsity();
197 };
198 
199 #endif
200 
201 template <typename Device>
202 class InvalidMatrixType : public std::exception
203 {
204 public:
206  {
207  std::string mtx_type = "unknown";
208  if(dynamic_cast<const CSCMatrix<Device>*>(mtx)) {
209  mtx_type = "CSC";
210  }
211  what_ = mtx_type + " matrix type not supported!";
212  }
213 
214  virtual const char* what() const noexcept override { return what_.c_str(); }
215 
216 private:
217  std::string what_;
218 };
219 
220 
221 enum class LinAlgBackend {
222  lapack,
223  ginkgo
224 };
225 
226 
227 template <typename Device>
228 std::unique_ptr<GridMatrix<Device>> create_matrix(size_type n_matrices,
229  int nvr, int nvz, int num_species, LinAlgBackend labackend,
230  double ginkgo_residual_reduction, int ginkgo_max_iterations,
231  std::string format = "default");
232 
233 
234 }
235 
236 
237 #include "col_grid_matrix.tpp"
238 
239 
240 #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:214
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:202
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
std::unique_ptr< GridMatrix< Device > > create_matrix(size_type n_matrices, int nvr, int nvz, int num_species, LinAlgBackend labackend, double ginkgo_residual_reduction, int ginkgo_max_iterations, std::string format="default")
Definition: col_grid_matrix.tpp:663
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:205
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:221
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
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:217
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