Ginkgo  Generated from pipelines/2969282958 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
pgm.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MULTIGRID_PGM_HPP_
6 #define GKO_PUBLIC_CORE_MULTIGRID_PGM_HPP_
7 
8 
9 #include <vector>
10 
11 #include <ginkgo/core/base/composition.hpp>
12 #include <ginkgo/core/base/exception_helpers.hpp>
13 #include <ginkgo/core/base/lin_op.hpp>
14 #include <ginkgo/core/base/types.hpp>
15 #include <ginkgo/core/config/config.hpp>
16 #include <ginkgo/core/config/registry.hpp>
17 #include <ginkgo/core/config/type_descriptor.hpp>
18 #include <ginkgo/core/distributed/matrix.hpp>
19 #include <ginkgo/core/matrix/csr.hpp>
20 #include <ginkgo/core/matrix/dense.hpp>
21 #include <ginkgo/core/matrix/sparsity_csr.hpp>
22 #include <ginkgo/core/multigrid/multigrid_level.hpp>
23 
24 
25 namespace gko {
26 namespace multigrid {
27 
28 
57 template <typename ValueType = default_precision, typename IndexType = int32>
58 class Pgm : public LinOp, public EnableMultigridLevel<ValueType> {
59  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
60 
61 public:
62  using value_type = ValueType;
63  using index_type = IndexType;
64 
70  std::shared_ptr<const LinOp> get_system_matrix() const
71  {
72  return system_matrix_;
73  }
74 
84  IndexType* get_agg() noexcept { return agg_.get_data(); }
85 
93  const IndexType* get_const_agg() const noexcept
94  {
95  return agg_.get_const_data();
96  }
97 
99  {
105  unsigned GKO_FACTORY_PARAMETER_SCALAR(max_iterations, 15u);
106 
113  double GKO_FACTORY_PARAMETER_SCALAR(max_unassigned_ratio, 0.05);
114 
122  bool GKO_FACTORY_PARAMETER_SCALAR(deterministic, false);
123 
134  bool GKO_FACTORY_PARAMETER_SCALAR(skip_sorting, false);
135  };
136  class reuse_data_type;
140 
146  friend class Pgm;
147 
148  bool initialized_ = false;
149  bool distributed_ = false;
150  dim<2> size_{};
151  array<IndexType> agg_;
152  std::shared_ptr<const LinOp> prolong_op_;
153  std::shared_ptr<const LinOp> restrict_op_;
154  std::shared_ptr<const LinOp> coarse_op_;
155  std::shared_ptr<const matrix::SparsityCsr<ValueType, IndexType>>
156  mapping_local_;
157  std::shared_ptr<const matrix::SparsityCsr<ValueType, IndexType>>
158  mapping_off_diag_;
159  };
160 
174  static parameters_type parse(
175  const config::pnode& config, const config::registry& context,
176  const config::type_descriptor& td_for_child =
177  config::make_type_descriptor<ValueType, IndexType>());
178 
179 protected:
180  void apply_impl(const LinOp* b, LinOp* x) const override
181  {
182  this->get_composition()->apply(b, x);
183  }
184 
185  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
186  LinOp* x) const override
187  {
188  this->get_composition()->apply(alpha, b, beta, x);
189  }
190 
191  explicit Pgm(std::shared_ptr<const Executor> exec) : LinOp(std::move(exec))
192  {}
193 
194  explicit Pgm(const Factory* factory,
195  std::shared_ptr<const LinOp> system_matrix)
196  : Pgm(factory, std::move(system_matrix), nullptr)
197  {}
198 
199  Pgm(const Factory* factory, std::shared_ptr<const LinOp> system_matrix,
200  reuse_data_type& reuse_data)
201  : Pgm(factory, std::move(system_matrix), &reuse_data)
202  {}
203 
204  Pgm(const Factory* factory, std::shared_ptr<const LinOp> system_matrix,
205  reuse_data_type* reuse_data)
206  : LinOp(factory->get_executor(), system_matrix->get_size()),
207  EnableMultigridLevel<ValueType>(system_matrix),
208  parameters_{factory->get_parameters()},
209  system_matrix_{system_matrix},
210  agg_(factory->get_executor(), system_matrix_->get_size()[0])
211  {
212  GKO_ASSERT(parameters_.max_unassigned_ratio <= 1.0);
213  GKO_ASSERT(parameters_.max_unassigned_ratio >= 0.0);
214  if (system_matrix_->get_size()[0] != 0) {
215  // generate on the existed matrix
216  if (reuse_data && reuse_data->initialized_) {
217  this->generate_from(*reuse_data);
218  } else {
219  this->generate(reuse_data);
220  }
221  }
222  }
223 
230  void generate(reuse_data_type* reuse_data);
231 
236  void generate_from(const reuse_data_type& reuse_data);
237 
239  static void check_reuse_consistent(const Factory* factory,
240  const LinOp* input,
241  const reuse_data_type& reuse_data);
242 
250  std::tuple<std::shared_ptr<LinOp>, std::shared_ptr<LinOp>,
251  std::shared_ptr<LinOp>>
252  generate_local(
253  std::shared_ptr<const matrix::Csr<ValueType, IndexType>> local_matrix,
254  std::shared_ptr<const matrix::SparsityCsr<ValueType, IndexType>>*
255  mapping = nullptr);
256 
268  std::shared_ptr<const matrix::Csr<ValueType, IndexType>>
269  convert_local_fine_op(std::shared_ptr<const LinOp> system_matrix) const;
270 
271 #if GINKGO_BUILD_MPI
272 
280  std::shared_ptr<const LinOp> convert_distributed_fine_op(
281  std::shared_ptr<const LinOp> system_matrix) const;
282 
296  template <typename GlobalIndexType>
297  array<GlobalIndexType> communicate_off_diag_agg(
298  std::shared_ptr<const experimental::distributed::Matrix<
299  ValueType, IndexType, GlobalIndexType>>
300  matrix,
301  std::shared_ptr<
302  experimental::distributed::Partition<IndexType, GlobalIndexType>>
303  coarse_partition,
304  const array<IndexType>& local_agg);
305 #endif
306 
307 private:
308  std::shared_ptr<const LinOp> system_matrix_{};
309  array<IndexType> agg_;
310 };
311 
312 
313 template <typename ValueType = default_precision, typename IndexType = int32>
314 using AmgxPgm GKO_DEPRECATED(
315  "This class is deprecated and will be removed in the next "
316  "major release. Please use Pgm instead.") = Pgm<ValueType, IndexType>;
317 
318 
319 } // namespace multigrid
320 } // namespace gko
321 
322 
323 #endif // GKO_PUBLIC_CORE_MULTIGRID_PGM_HPP_
gko::config::pnode
pnode describes a tree of properties.
Definition: property_tree.hpp:28
gko::LinOp
Definition: lin_op.hpp:117
gko::log::profile_event_category::factory
LinOpFactory events.
gko::UseComposition::get_composition
std::shared_ptr< Composition< ValueType > > get_composition() const
Returns the composition operators.
Definition: composition.hpp:186
GKO_FACTORY_PARAMETER_SCALAR
#define GKO_FACTORY_PARAMETER_SCALAR(_name, _default)
Creates a scalar factory parameter in the factory parameters structure.
Definition: abstract_factory.hpp:469
gko::config::type_descriptor
This class describes the value and index types to be used when building a Ginkgo type from a configur...
Definition: type_descriptor.hpp:39
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::multigrid::Pgm::get_agg
IndexType * get_agg() noexcept
Returns the aggregate group.
Definition: pgm.hpp:84
gko::array< IndexType >
gko::multigrid::Pgm
Parallel graph match (Pgm) is the aggregate method introduced in the paper M.
Definition: matrix.hpp:40
gko::dim< 2 >
gko::multigrid::Pgm::Factory
Definition: pgm.hpp:138
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:686
gko::multigrid::Pgm::parameters_type
Definition: pgm.hpp:98
gko::LinOpFactory::ReuseData
Opaque base for the data a factory records in generate_reuse() to speed up later generations.
Definition: lin_op.hpp:371
gko::multigrid::Pgm::get_const_agg
const IndexType * get_const_agg() const noexcept
Returns the aggregate group.
Definition: pgm.hpp:93
gko::config::registry
This class stores additional context for creating Ginkgo objects from configuration files.
Definition: registry.hpp:167
GKO_ENABLE_BUILD_METHOD
#define GKO_ENABLE_BUILD_METHOD(_factory_name)
Defines a build method for the factory, simplifying its construction by removing the repetitive typin...
Definition: abstract_factory.hpp:418
GKO_CREATE_FACTORY_PARAMETERS
#define GKO_CREATE_FACTORY_PARAMETERS(_parameters_name, _factory_name)
This Macro will generate a new type containing the parameters for the factory _factory_name.
Definition: abstract_factory.hpp:304
gko::array::get_const_data
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:695
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
gko::multigrid::Pgm::get_system_matrix
std::shared_ptr< const LinOp > get_system_matrix() const
Returns the system operator (matrix) of the linear system.
Definition: pgm.hpp:70
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:169
gko::multigrid::Pgm::parse
static parameters_type parse(const config::pnode &config, const config::registry &context, const config::type_descriptor &td_for_child=config::make_type_descriptor< ValueType, IndexType >())
Create the parameters from the property_tree.
gko::multigrid::Pgm::parameters_type::max_unassigned_ratio
double max_unassigned_ratio
The maximum ratio of unassigned number, which is valid in the interval 0.0 ~ 1.0.
Definition: pgm.hpp:113
GKO_ENABLE_LIN_OP_FACTORY_WITH_REUSE
#define GKO_ENABLE_LIN_OP_FACTORY_WITH_REUSE(_lin_op, _parameters_name, _factory_name, _reuse_data_type)
Like GKO_ENABLE_LIN_OP_FACTORY, but the factory also supports LinOpFactory::generate_reuse(),...
Definition: lin_op.hpp:1167
gko::multigrid::Pgm::reuse_data_type
Records the aggregates, the transfer operators and the coarse-to-fine mappings of the first generate_...
Definition: pgm.hpp:145
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.