XGC1
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>>;
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_h_view_t = Kokkos::View<value_type****, Kokkos::LayoutRight, host_type>;
38  using const_vector_h_view_t = Kokkos::View<const value_type****, Kokkos::LayoutRight, host_type>;
39  using vector_dualview_t = Kokkos::DualView<value_type****, Kokkos::LayoutRight, Device>;
40  using const_vector_dualview_t = Kokkos::DualView<const value_type****, Kokkos::LayoutRight, Device>;
41 
42  GridMatrix(size_type n_nodes, int n_species)
43  : num_nodes_{n_nodes}, num_species_{n_species}, batch_size_{n_nodes * n_species}
44  { }
45 
46  virtual ~GridMatrix() { }
47 
48  typename Kokkos::View<index_type**, device>::HostMirror host_index_map_LU() {
49  return index_map_.h_view;
50  }
51 
52  Kokkos::View<index_type**, device> device_index_map_LU() const {
53  return index_map_.d_view;
54  }
55 
57 
58  virtual values_array_t get_values() = 0;
59 
60  virtual void subtract_from_identity() = 0;
61 
62  // For picard_step_init
64  vector_view_t dist_iter) const = 0;
65 
67 
68  index_type get_num_nonzeros() const { return nnz_; }
69  index_type get_num_rows() const { return num_rows_; }
70 
71 protected:
72  Kokkos::DualView<index_type**,device> index_map_;
81 };
82 
83 
84 template <typename Device>
85 class CSCMatrix : public GridMatrix<Device>
86 {
87 public:
94  using internal_values_array_t = Kokkos::DualView<value_type***, Kokkos::LayoutRight, Device>;
101 
102  CSCMatrix(size_type n_nodes, index_type num_species, int nvr, int nvz)
103  : GridMatrix<Device>(n_nodes, num_species), nvr_{nvr},
104  nvz_{nvz}
105  {
106  setup_sparsity();
107  }
108 
109  const_values_array_t get_const_values() const override;
110 
111  values_array_t get_values() override;
112 
117  void subtract_from_identity() override;
118 
120  vector_view_t dist_iter) const override;
121 
123 
124 protected:
131 
134 
136  Kokkos::View<index_type*,device> central_locs_;
137  Kokkos::View<int*,host_type> LU_rowindx_;
138  Kokkos::View<int*,host_type> LU_colptr_;
139 
140  void setup_sparsity();
141 };
142 
143 #ifdef USE_GINKGO
144 
145 template <typename Device>
146 class ELLMatrix : public GridMatrix<Device>
147 {
148 public:
149  using index_type = typename GridMatrix<Device>::index_type;
150  using host_type = typename GridMatrix<Device>::host_type;
151  using device = typename GridMatrix<Device>::device;
152  using value_type = typename GridMatrix<Device>::value_type;
153  using values_array_t = typename GridMatrix<Device>::values_array_t;
154  using const_values_array_t = typename GridMatrix<Device>::const_values_array_t;
155  using vector_view_t = typename GridMatrix<Device>::vector_view_t;
156  using const_vector_view_t = typename GridMatrix<Device>::const_vector_view_t;
157  using vector_h_view_t = typename GridMatrix<Device>::vector_h_view_t;
158  using const_vector_h_view_t = typename GridMatrix<Device>::const_vector_h_view_t;
159  using vector_dualview_t = typename GridMatrix<Device>::vector_dualview_t;
160  using const_vector_dualview_t = typename GridMatrix<Device>::const_vector_dualview_t;
161  using mtx_type = gko::batch::matrix::Ell<value_type, index_type>;
162 
163  static constexpr int max_nnz_per_row = 9;
164 
165  ELLMatrix(std::shared_ptr<const gko::Executor> exec, size_type n_matrices,
166  index_type num_species, int nvr, int nvz,
167  double ginkgo_residual_reduction, int ginkgo_max_iterations)
168  : exec_{exec}, GridMatrix<Device>(n_matrices, num_species), nvr_{nvr},
169  nvz_{nvz}, ginkgo_residual_reduction_{ginkgo_residual_reduction},
170  ginkgo_max_iterations_{ginkgo_max_iterations}
171  {
172  setup_sparsity();
173  }
174 
175  const_values_array_t get_const_values() const override;
176 
177  values_array_t get_values() override;
178 
179  void subtract_from_identity() override;
180 
181  void add_identity_multiply(const_vector_view_t dist_col,
182  vector_view_t dist_iter) const override;
183 
184  void apply_solve(const_vector_h_view_t b_h, vector_h_view_t x_h, const_vector_view_t b, vector_view_t x) const override;
185 
186 protected:
193  std::shared_ptr<const gko::Executor> exec_;
194  std::shared_ptr<mtx_type> mtx_;
195 
196  index_type nvz_;
197  index_type nvr_;
198 
199  double ginkgo_residual_reduction_;
200  int ginkgo_max_iterations_;
201 
202  void setup_sparsity();
203 };
204 
205 #endif
206 
207 template <typename Device>
208 class InvalidMatrixType : public std::exception
209 {
210 public:
212  {
213  std::string mtx_type = "unknown";
214  if(dynamic_cast<const CSCMatrix<Device>*>(mtx)) {
215  mtx_type = "CSC";
216  }
217  what_ = mtx_type + " matrix type not supported!";
218  }
219 
220  virtual const char* what() const noexcept override { return what_.c_str(); }
221 
222 private:
223  std::string what_;
224 };
225 
226 
227 enum class LinAlgBackend {
228  lapack,
229  ginkgo
230 };
231 
232 
233 std::unique_ptr<GridMatrix<DeviceType>> create_matrix(int n_matrices,
234  int nvr, int nvz, int num_species, LinAlgBackend labackend,
235  double ginkgo_residual_reduction, int ginkgo_max_iterations,
236  std::string format = "default");
237 
238 
239 }
240 
241 #endif
Definition: col_grid_matrix.hpp:86
typename GridMatrix< Device >::vector_view_t vector_view_t
Definition: col_grid_matrix.hpp:95
typename GridMatrix< Device >::index_type index_type
Definition: col_grid_matrix.hpp:88
void setup_sparsity()
Definition: col_grid_matrix.cpp:7
Kokkos::View< int *, host_type > LU_colptr_
Definition: col_grid_matrix.hpp:138
typename GridMatrix< Device >::vector_h_view_t vector_h_view_t
Definition: col_grid_matrix.hpp:97
typename GridMatrix< Device >::values_array_t values_array_t
Definition: col_grid_matrix.hpp:92
Kokkos::DualView< value_type ***, Kokkos::LayoutRight, Device > internal_values_array_t
Definition: col_grid_matrix.hpp:94
const_values_array_t get_const_values() const override
Definition: col_grid_matrix.cpp:226
CSCMatrix(size_type n_nodes, index_type num_species, int nvr, int nvz)
Definition: col_grid_matrix.hpp:102
values_array_t get_values() override
Definition: col_grid_matrix.cpp:216
internal_values_array_t values_
Definition: col_grid_matrix.hpp:135
Kokkos::View< index_type *, device > central_locs_
Definition: col_grid_matrix.hpp:136
void subtract_from_identity() override
Definition: col_grid_matrix.cpp:239
index_type nvz_
Definition: col_grid_matrix.hpp:132
typename GridMatrix< Device >::const_vector_h_view_t const_vector_h_view_t
Definition: col_grid_matrix.hpp:98
typename GridMatrix< Device >::const_vector_view_t const_vector_view_t
Definition: col_grid_matrix.hpp:96
typename GridMatrix< Device >::const_values_array_t const_values_array_t
Definition: col_grid_matrix.hpp:93
Kokkos::View< int *, host_type > LU_rowindx_
Definition: col_grid_matrix.hpp:137
index_type nvr_
Definition: col_grid_matrix.hpp:133
void apply_solve(const_vector_h_view_t b_h, vector_h_view_t x_h, const_vector_view_t b, vector_view_t x) const override
Definition: col_grid_matrix.cpp:321
void add_identity_multiply(const_vector_view_t dist_col, vector_view_t dist_iter) const override
Definition: col_grid_matrix.cpp:264
Definition: col_grid_matrix.hpp:24
virtual const_values_array_t get_const_values() const =0
Kokkos::View< index_type **, device >::HostMirror host_index_map_LU()
Definition: col_grid_matrix.hpp:48
Kokkos::View< const value_type ****, Kokkos::LayoutRight, Device > const_vector_view_t
Definition: col_grid_matrix.hpp:36
virtual void add_identity_multiply(const_vector_view_t dist_col, vector_view_t dist_iter) const =0
Kokkos::DualView< value_type ****, Kokkos::LayoutRight, Device > vector_dualview_t
Definition: col_grid_matrix.hpp:39
Kokkos::View< const value_type ***, Kokkos::LayoutRight, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged > > const_values_array_t
Definition: col_grid_matrix.hpp:34
const size_type batch_size_
Definition: col_grid_matrix.hpp:78
int index_type
Definition: col_grid_matrix.hpp:26
virtual void apply_solve(const_vector_h_view_t b_h, vector_h_view_t x_h, const_vector_view_t b, vector_view_t x) const =0
Kokkos::View< value_type ***, Kokkos::LayoutRight, Device, Kokkos::MemoryTraits< Kokkos::Unmanaged > > values_array_t
Definition: col_grid_matrix.hpp:31
Kokkos::DualView< index_type **, device > index_map_
Definition: col_grid_matrix.hpp:72
Kokkos::View< const value_type ****, Kokkos::LayoutRight, host_type > const_vector_h_view_t
Definition: col_grid_matrix.hpp:38
Kokkos::DualView< const value_type ****, Kokkos::LayoutRight, Device > const_vector_dualview_t
Definition: col_grid_matrix.hpp:40
Kokkos::View< value_type ****, Kokkos::LayoutRight, host_type > vector_h_view_t
Definition: col_grid_matrix.hpp:37
GridMatrix(size_type n_nodes, int n_species)
Definition: col_grid_matrix.hpp:42
virtual values_array_t get_values()=0
index_type num_rows_
Definition: col_grid_matrix.hpp:79
Kokkos::View< value_type ****, Kokkos::LayoutRight, Device > vector_view_t
Definition: col_grid_matrix.hpp:35
index_type get_num_nonzeros() const
Definition: col_grid_matrix.hpp:68
Device device
Definition: col_grid_matrix.hpp:28
index_type get_num_rows() const
Definition: col_grid_matrix.hpp:69
Kokkos::View< index_type **, device > device_index_map_LU() const
Definition: col_grid_matrix.hpp:52
HostType host_type
Definition: col_grid_matrix.hpp:27
double value_type
Definition: col_grid_matrix.hpp:29
index_type nnz_
Definition: col_grid_matrix.hpp:80
virtual ~GridMatrix()
Definition: col_grid_matrix.hpp:46
virtual void subtract_from_identity()=0
const index_type num_nodes_
Definition: col_grid_matrix.hpp:77
const index_type num_species_
Definition: col_grid_matrix.hpp:76
Definition: col_grid_matrix.hpp:209
virtual const char * what() const noexcept override
Definition: col_grid_matrix.hpp:220
std::string what_
Definition: col_grid_matrix.hpp:223
InvalidMatrixType(const GridMatrix< Device > *const mtx)
Definition: col_grid_matrix.hpp:211
Definition: col_grid_matrix.cpp:4
LinAlgBackend
Definition: col_grid_matrix.hpp:227
int size_type
Definition: col_grid_matrix.hpp:19
std::unique_ptr< GridMatrix< DeviceType > > create_matrix(const int n_matrices_in, int nvr, int nvz, const int num_species, const LinAlgBackend labackend, const double ginkgo_residual_reduction, const int ginkgo_max_iterations, const std::string format)
Definition: col_grid_matrix.cpp:652
Kokkos::Device< HostExSpace, HostMemSpace > HostType
Definition: space_settings.hpp:57