Ginkgo  Generated from pipelines/1680925034 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
matrix.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
6 #define GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
7 
8 
9 #include <ginkgo/config.hpp>
10 
11 
12 #if GINKGO_BUILD_MPI
13 
14 
15 #include <ginkgo/core/base/dense_cache.hpp>
16 #include <ginkgo/core/base/lin_op.hpp>
17 #include <ginkgo/core/base/mpi.hpp>
18 #include <ginkgo/core/base/std_extensions.hpp>
19 #include <ginkgo/core/distributed/base.hpp>
20 #include <ginkgo/core/distributed/index_map.hpp>
21 
22 
23 namespace gko {
24 namespace matrix {
25 
26 
27 template <typename ValueType, typename IndexType>
28 class Csr;
29 
30 
31 }
32 
33 
34 namespace multigrid {
35 
36 
37 template <typename ValueType, typename IndexType>
38 class Pgm;
39 
40 
41 }
42 
43 
44 namespace detail {
45 
46 
51 template <typename Builder, typename ValueType, typename IndexType,
52  typename = void>
53 struct is_matrix_type_builder : std::false_type {};
54 
55 
56 template <typename Builder, typename ValueType, typename IndexType>
57 struct is_matrix_type_builder<
58  Builder, ValueType, IndexType,
59  xstd::void_t<
60  decltype(std::declval<Builder>().template create<ValueType, IndexType>(
61  std::declval<std::shared_ptr<const Executor>>()))>>
62  : std::true_type {};
63 
64 
65 template <template <typename, typename> class MatrixType,
66  typename... CreateArgs>
67 struct MatrixTypeBuilderFromValueAndIndex {
68  template <typename ValueType, typename IndexType, std::size_t... I>
69  auto create_impl(std::shared_ptr<const Executor> exec,
70  std::index_sequence<I...>)
71  {
72  return MatrixType<ValueType, IndexType>::create(
73  exec, std::get<I>(create_args)...);
74  }
75 
76 
77  template <typename ValueType, typename IndexType>
78  auto create(std::shared_ptr<const Executor> exec)
79  {
80  // with c++17 we could use std::apply
81  static constexpr auto size = sizeof...(CreateArgs);
82  return create_impl<ValueType, IndexType>(
83  std::move(exec), std::make_index_sequence<size>{});
84  }
85 
86  std::tuple<CreateArgs...> create_args;
87 };
88 
89 
90 } // namespace detail
91 
92 
124 template <template <typename, typename> class MatrixType, typename... Args>
125 auto with_matrix_type(Args&&... create_args)
126 {
127  return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
128  std::forward_as_tuple(create_args...)};
129 }
130 
131 
132 namespace experimental {
133 namespace distributed {
134 
135 
145 enum class assembly_mode { communicate, local_only };
146 
147 
148 template <typename LocalIndexType, typename GlobalIndexType>
149 class Partition;
150 template <typename ValueType>
151 class Vector;
152 
153 
258 template <typename ValueType = default_precision,
259  typename LocalIndexType = int32, typename GlobalIndexType = int64>
260 class Matrix
261  : public EnableLinOp<Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
262  public ConvertibleTo<
263  Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
264 #if GINKGO_ENABLE_HALF
265  public ConvertibleTo<Matrix<next_precision<next_precision<ValueType>>,
266  LocalIndexType, GlobalIndexType>>,
267 #endif
268  public DistributedBase {
269  friend class EnablePolymorphicObject<Matrix, LinOp>;
270  friend class Matrix<previous_precision<ValueType>, LocalIndexType,
271  GlobalIndexType>;
272  friend class multigrid::Pgm<ValueType, LocalIndexType>;
273 
274 
275 public:
276  using value_type = ValueType;
277  using index_type = GlobalIndexType;
278  using local_index_type = LocalIndexType;
279  using global_index_type = GlobalIndexType;
280  using global_vector_type =
282  using local_vector_type = typename global_vector_type::local_vector_type;
283 
287  GlobalIndexType>>::convert_to;
289  GlobalIndexType>>::move_to;
290 
291  void convert_to(Matrix<next_precision<value_type>, local_index_type,
292  global_index_type>* result) const override;
293 
294  void move_to(Matrix<next_precision<value_type>, local_index_type,
295  global_index_type>* result) override;
296 #if GINKGO_ENABLE_HALF
297  friend class Matrix<previous_precision<previous_precision<ValueType>>,
298  LocalIndexType, GlobalIndexType>;
299  using ConvertibleTo<
301  global_index_type>>::convert_to;
303  local_index_type, global_index_type>>::move_to;
304 
305  void convert_to(
307  global_index_type>* result) const override;
308 
310  local_index_type, global_index_type>* result) override;
311 
312 #endif
313 
330  void read_distributed(
333  partition,
334  assembly_mode assembly_type = assembly_mode::local_only);
335 
345  void read_distributed(
348  partition,
349  assembly_mode assembly_type = assembly_mode::local_only);
350 
369  void read_distributed(
372  row_partition,
374  col_partition,
375  assembly_mode assembly_type = assembly_mode::local_only);
376 
386  void read_distributed(
389  row_partition,
391  col_partition,
392  assembly_mode assembly_type = assembly_mode::local_only);
393 
399  std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
400 
406  std::shared_ptr<const LinOp> get_non_local_matrix() const
407  {
408  return non_local_mtx_;
409  }
410 
416  Matrix(const Matrix& other);
417 
423  Matrix(Matrix&& other) noexcept;
424 
433  Matrix& operator=(const Matrix& other);
434 
443  Matrix& operator=(Matrix&& other);
444 
454  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
455  mpi::communicator comm);
456 
477  template <typename MatrixType,
478  typename = std::enable_if_t<gko::detail::is_matrix_type_builder<
479  MatrixType, ValueType, LocalIndexType>::value>>
480  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
481  mpi::communicator comm,
482  MatrixType matrix_template)
483  {
484  return create(
485  exec, comm,
486  matrix_template.template create<ValueType, LocalIndexType>(exec));
487  }
488 
517  template <typename LocalMatrixType, typename NonLocalMatrixType,
518  typename = std::enable_if_t<
519  gko::detail::is_matrix_type_builder<
520  LocalMatrixType, ValueType, LocalIndexType>::value &&
521  gko::detail::is_matrix_type_builder<
522  NonLocalMatrixType, ValueType, LocalIndexType>::value>>
523  static std::unique_ptr<Matrix> create(
524  std::shared_ptr<const Executor> exec, mpi::communicator comm,
525  LocalMatrixType local_matrix_template,
526  NonLocalMatrixType non_local_matrix_template)
527  {
528  return create(
529  exec, comm,
530  local_matrix_template.template create<ValueType, LocalIndexType>(
531  exec),
532  non_local_matrix_template
533  .template create<ValueType, LocalIndexType>(exec));
534  }
535 
550  static std::unique_ptr<Matrix> create(
551  std::shared_ptr<const Executor> exec, mpi::communicator comm,
552  ptr_param<const LinOp> matrix_template);
553 
570  static std::unique_ptr<Matrix> create(
571  std::shared_ptr<const Executor> exec, mpi::communicator comm,
572  ptr_param<const LinOp> local_matrix_template,
573  ptr_param<const LinOp> non_local_matrix_template);
574 
587  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
588  mpi::communicator comm, dim<2> size,
589  std::shared_ptr<LinOp> local_linop);
590 
609  static std::unique_ptr<Matrix> create(
610  std::shared_ptr<const Executor> exec, mpi::communicator comm,
611  dim<2> size, std::shared_ptr<LinOp> local_linop,
612  std::shared_ptr<LinOp> non_local_linop,
613  std::vector<comm_index_type> recv_sizes,
614  std::vector<comm_index_type> recv_offsets,
615  array<local_index_type> recv_gather_idxs);
616 
624  void col_scale(ptr_param<const global_vector_type> scaling_factors);
625 
633  void row_scale(ptr_param<const global_vector_type> scaling_factors);
634 
635 protected:
636  explicit Matrix(std::shared_ptr<const Executor> exec,
637  mpi::communicator comm);
638 
639  explicit Matrix(std::shared_ptr<const Executor> exec,
640  mpi::communicator comm,
641  ptr_param<const LinOp> local_matrix_template,
642  ptr_param<const LinOp> non_local_matrix_template);
643 
644  explicit Matrix(std::shared_ptr<const Executor> exec,
645  mpi::communicator comm, dim<2> size,
646  std::shared_ptr<LinOp> local_linop);
647 
648  explicit Matrix(std::shared_ptr<const Executor> exec,
649  mpi::communicator comm, dim<2> size,
650  std::shared_ptr<LinOp> local_linop,
651  std::shared_ptr<LinOp> non_local_linop,
652  std::vector<comm_index_type> recv_sizes,
653  std::vector<comm_index_type> recv_offsets,
654  array<local_index_type> recv_gather_idxs);
655 
664  mpi::request communicate(const local_vector_type* local_b) const;
665 
666  void apply_impl(const LinOp* b, LinOp* x) const override;
667 
668  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
669  LinOp* x) const override;
670 
671 private:
672  std::vector<comm_index_type> send_offsets_;
673  std::vector<comm_index_type> send_sizes_;
674  std::vector<comm_index_type> recv_offsets_;
675  std::vector<comm_index_type> recv_sizes_;
676  array<local_index_type> gather_idxs_;
677  array<global_index_type> non_local_to_global_;
678  gko::detail::DenseCache<value_type> one_scalar_;
679  gko::detail::DenseCache<value_type> host_send_buffer_;
680  gko::detail::DenseCache<value_type> host_recv_buffer_;
681  gko::detail::DenseCache<value_type> send_buffer_;
682  gko::detail::DenseCache<value_type> recv_buffer_;
683  std::shared_ptr<LinOp> local_mtx_;
684  std::shared_ptr<LinOp> non_local_mtx_;
685 };
686 
687 
688 } // namespace distributed
689 } // namespace experimental
690 } // namespace gko
691 
692 
693 #endif
694 
695 
696 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
gko::with_matrix_type
auto with_matrix_type(Args &&... create_args)
This function returns a type that delays a call to MatrixType::create.
Definition: matrix.hpp:125
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:28
gko::experimental::distributed::assembly_mode
assembly_mode
assembly_mode defines how the read_distributed function of the distributed matrix treats non-local in...
Definition: matrix.hpp:145
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense< value_type >
gko::experimental::distributed::Matrix
The Matrix class defines a (MPI-)distributed matrix.
Definition: matrix.hpp:260
gko::experimental::distributed::Vector
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition: matrix.hpp:151
gko::experimental::distributed::Matrix::get_local_matrix
std::shared_ptr< const LinOp > get_local_matrix() const
Get read access to the stored local matrix.
Definition: matrix.hpp:399
gko::experimental::distributed::Matrix::create
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, LocalMatrixType local_matrix_template, NonLocalMatrixType non_local_matrix_template)
Creates an empty distributed matrix with specified types for the local matrix and the non-local matri...
Definition: matrix.hpp:523
gko::experimental::distributed::Matrix::col_scale
void col_scale(ptr_param< const global_vector_type > scaling_factors)
Scales the columns of the matrix by the respective entries of the vector.
gko::experimental::distributed::Matrix::create
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm)
Creates an empty distributed matrix.
gko::experimental::mpi::request
The request class is a light, move-only wrapper around the MPI_Request handle.
Definition: mpi.hpp:327
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::experimental::distributed::Matrix::get_non_local_matrix
std::shared_ptr< const LinOp > get_non_local_matrix() const
Get read access to the stored non-local matrix.
Definition: matrix.hpp:406
gko::array< local_index_type >
gko::multigrid::Pgm
Parallel graph match (Pgm) is the aggregate method introduced in the paper M.
Definition: matrix.hpp:38
gko::experimental::mpi::communicator
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition: mpi.hpp:416
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:41
gko::experimental::distributed::Partition
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition: assembly.hpp:26
gko::next_precision
next_precision_base< T > next_precision
Obtains the next type in the singly-linked precision list with half.
Definition: math.hpp:445
gko::experimental::distributed::Matrix::operator=
Matrix & operator=(const Matrix &other)
Copy assigns a Matrix.
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:112
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:171
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:479
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:106
gko::experimental::distributed::Matrix::create
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, MatrixType matrix_template)
Creates an empty distributed matrix with specified type for local matrices.
Definition: matrix.hpp:480
gko::experimental::distributed::DistributedBase
A base class for distributed objects.
Definition: base.hpp:32
gko::experimental::distributed::Matrix::read_distributed
void read_distributed(const device_matrix_data< value_type, global_index_type > &data, std::shared_ptr< const Partition< local_index_type, global_index_type >> partition, assembly_mode assembly_type=assembly_mode::local_only)
Reads a square matrix from the device_matrix_data structure and a global partition.
gko::experimental::distributed::Matrix::row_scale
void row_scale(ptr_param< const global_vector_type > scaling_factors)
Scales the rows of the matrix by the respective entries of the vector.
gko::device_matrix_data
This type is a device-side equivalent to matrix_data.
Definition: device_matrix_data.hpp:36
gko::experimental::distributed::Matrix::Matrix
Matrix(const Matrix &other)
Copy constructs a Matrix.
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:877
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:667