Ginkgo  Generated from pipelines/2721898507 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
matrix.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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 #include <ginkgo/core/distributed/row_gatherer.hpp>
22 #include <ginkgo/core/distributed/vector_cache.hpp>
23 
24 
25 namespace gko {
26 namespace matrix {
27 
28 
29 template <typename ValueType, typename IndexType>
30 class Csr;
31 
32 
33 }
34 
35 
36 namespace multigrid {
37 
38 
39 template <typename ValueType, typename IndexType>
40 class Pgm;
41 
42 
43 }
44 
45 
46 namespace detail {
47 
48 
53 template <typename Builder, typename ValueType, typename IndexType,
54  typename = void>
55 struct is_matrix_type_builder : std::false_type {};
56 
57 
58 template <typename Builder, typename ValueType, typename IndexType>
59 struct is_matrix_type_builder<
60  Builder, ValueType, IndexType,
61  xstd::void_t<
62  decltype(std::declval<Builder>().template create<ValueType, IndexType>(
63  std::declval<std::shared_ptr<const Executor>>()))>>
64  : std::true_type {};
65 
66 
67 template <template <typename, typename> class MatrixType,
68  typename... CreateArgs>
69 struct MatrixTypeBuilderFromValueAndIndex {
70  template <typename ValueType, typename IndexType, std::size_t... I>
71  auto create_impl(std::shared_ptr<const Executor> exec,
72  std::index_sequence<I...>)
73  {
74  return MatrixType<ValueType, IndexType>::create(
75  exec, std::get<I>(create_args)...);
76  }
77 
78 
79  template <typename ValueType, typename IndexType>
80  auto create(std::shared_ptr<const Executor> exec)
81  {
82  // with c++17 we could use std::apply
83  static constexpr auto size = sizeof...(CreateArgs);
84  return create_impl<ValueType, IndexType>(
85  std::move(exec), std::make_index_sequence<size>{});
86  }
87 
88  std::tuple<CreateArgs...> create_args;
89 };
90 
91 
92 } // namespace detail
93 
94 
126 template <template <typename, typename> class MatrixType, typename... Args>
127 auto with_matrix_type(Args&&... create_args)
128 {
129  return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
130  std::forward_as_tuple(create_args...)};
131 }
132 
133 
134 namespace experimental {
135 namespace distributed {
136 
137 
147 enum class assembly_mode { communicate, local_only };
148 
149 
150 template <typename LocalIndexType, typename GlobalIndexType>
151 class Partition;
152 template <typename ValueType>
153 class Vector;
154 
155 
260 template <typename ValueType = default_precision,
261  typename LocalIndexType = int32, typename GlobalIndexType = int64>
262 class Matrix
263  : public LinOp,
264  public EnableCloneable<
265  Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
266  public ConvertibleTo<
267  Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
268 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
269  public ConvertibleTo<Matrix<next_precision<ValueType, 2>, LocalIndexType,
270  GlobalIndexType>>,
271 #endif
272 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
273  public ConvertibleTo<Matrix<next_precision<ValueType, 3>, LocalIndexType,
274  GlobalIndexType>>,
275 #endif
276  public WritableToMatrixData<ValueType, GlobalIndexType>,
277  public DistributedBase {
278  friend class EnableCloneable<Matrix>;
279  friend class Matrix<previous_precision<ValueType>, LocalIndexType,
280  GlobalIndexType>;
281 
282  friend class multigrid::Pgm<ValueType, LocalIndexType>;
283  GKO_ASSERT_SUPPORTED_VALUE_AND_DIST_INDEX_TYPE;
284 
285 public:
286  using value_type = ValueType;
287  using index_type = GlobalIndexType;
288  using local_index_type = LocalIndexType;
289  using global_index_type = GlobalIndexType;
290  using global_vector_type =
292  using local_vector_type = typename global_vector_type::local_vector_type;
293 
297  GlobalIndexType>>::convert_to;
299  GlobalIndexType>>::move_to;
300 
301  void convert_to(Matrix<next_precision<value_type>, local_index_type,
302  global_index_type>* result) const override;
303 
304  void move_to(Matrix<next_precision<value_type>, local_index_type,
305  global_index_type>* result) override;
306 
307 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
308  friend class Matrix<previous_precision<ValueType, 2>, LocalIndexType,
309  GlobalIndexType>;
311  global_index_type>>::convert_to;
313  global_index_type>>::move_to;
314 
315  void convert_to(Matrix<next_precision<value_type, 2>, local_index_type,
316  global_index_type>* result) const override;
317 
318  void move_to(Matrix<next_precision<value_type, 2>, local_index_type,
319  global_index_type>* result) override;
320 #endif
321 
322 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
323  friend class Matrix<previous_precision<ValueType, 3>, LocalIndexType,
324  GlobalIndexType>;
326  global_index_type>>::convert_to;
328  global_index_type>>::move_to;
329 
330  void convert_to(Matrix<next_precision<value_type, 3>, local_index_type,
331  global_index_type>* result) const override;
332 
333  void move_to(Matrix<next_precision<value_type, 3>, local_index_type,
334  global_index_type>* result) override;
335 #endif
336 
353  void read_distributed(
356  partition,
357  assembly_mode assembly_type = assembly_mode::local_only);
358 
368  void read_distributed(
371  partition,
372  assembly_mode assembly_type = assembly_mode::local_only);
373 
392  void read_distributed(
395  row_partition,
397  col_partition,
398  assembly_mode assembly_type = assembly_mode::local_only);
399 
409  void read_distributed(
412  row_partition,
414  col_partition,
415  assembly_mode assembly_type = assembly_mode::local_only);
416 
426  void write(matrix_data<value_type, global_index_type>& data) const override;
427 
436  std::shared_ptr<const LinOp> get_diag_matrix() const { return diag_mtx_; }
437 
446  std::shared_ptr<const LinOp> get_off_diag_matrix() const
447  {
448  return off_diag_mtx_;
449  }
450 
454  GKO_DEPRECATED("use get_diag_matrix() instead")
455  std::shared_ptr<const LinOp> get_local_matrix() const
456  {
457  return get_diag_matrix();
458  }
459 
463  GKO_DEPRECATED("use get_off_diag_matrix() instead")
464  std::shared_ptr<const LinOp> get_non_local_matrix() const
465  {
466  return get_off_diag_matrix();
467  }
468 
474  Matrix(const Matrix& other);
475 
481  Matrix(Matrix&& other) noexcept;
482 
491  Matrix& operator=(const Matrix& other);
492 
501  Matrix& operator=(Matrix&& other);
502 
512  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
513  mpi::communicator comm);
514 
528  static std::unique_ptr<Matrix> create(
529  std::shared_ptr<const Executor> exec,
530  std::shared_ptr<const RowGatherer<LocalIndexType>>
531  row_gatherer_template);
532 
553  template <typename MatrixType,
554  typename = std::enable_if_t<gko::detail::is_matrix_type_builder<
555  MatrixType, ValueType, LocalIndexType>::value>>
556  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
557  mpi::communicator comm,
558  MatrixType matrix_template)
559  {
560  return create(
561  exec, comm,
562  matrix_template.template create<ValueType, LocalIndexType>(exec));
563  }
564 
593  template <typename DiagMatrixType, typename OffDiagMatrixType,
594  typename = std::enable_if_t<
595  gko::detail::is_matrix_type_builder<DiagMatrixType, ValueType,
596  LocalIndexType>::value &&
597  gko::detail::is_matrix_type_builder<
598  OffDiagMatrixType, ValueType, LocalIndexType>::value>>
599  static std::unique_ptr<Matrix> create(
600  std::shared_ptr<const Executor> exec, mpi::communicator comm,
601  DiagMatrixType diag_matrix_template,
602  OffDiagMatrixType off_diag_matrix_template)
603  {
604  return create(
605  exec, comm,
606  diag_matrix_template.template create<ValueType, LocalIndexType>(
607  exec),
608  off_diag_matrix_template.template create<ValueType, LocalIndexType>(
609  exec));
610  }
611 
626  static std::unique_ptr<Matrix> create(
627  std::shared_ptr<const Executor> exec, mpi::communicator comm,
628  ptr_param<const LinOp> matrix_template);
629 
646  static std::unique_ptr<Matrix> create(
647  std::shared_ptr<const Executor> exec, mpi::communicator comm,
648  ptr_param<const LinOp> diag_matrix_template,
649  ptr_param<const LinOp> off_diag_matrix_template);
650 
663  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
664  mpi::communicator comm, dim<2> size,
665  std::shared_ptr<LinOp> diag_linop);
666 
685  [[deprecated(
686  "Please use the overload with an index_map instead.")]] static std::
687  unique_ptr<Matrix>
688  create(std::shared_ptr<const Executor> exec, mpi::communicator comm,
689  dim<2> size, std::shared_ptr<LinOp> diag_linop,
690  std::shared_ptr<LinOp> off_diag_linop,
691  std::vector<comm_index_type> recv_sizes,
692  std::vector<comm_index_type> recv_offsets,
693  array<local_index_type> recv_gather_idxs);
694 
708  static std::unique_ptr<Matrix> create(
709  std::shared_ptr<const Executor> exec, mpi::communicator comm,
711  std::shared_ptr<LinOp> diag_linop,
712  std::shared_ptr<LinOp> off_diag_linop);
713 
721  void col_scale(ptr_param<const global_vector_type> scaling_factors);
722 
730  void row_scale(ptr_param<const global_vector_type> scaling_factors);
731 
732 protected:
733  explicit Matrix(std::shared_ptr<const Executor> exec,
734  mpi::communicator comm);
735 
736  explicit Matrix(std::shared_ptr<const Executor> exec,
737  std::shared_ptr<const RowGatherer<LocalIndexType>>
738  row_gatherer_template,
739  ptr_param<const LinOp> diag_matrix_template,
740  ptr_param<const LinOp> off_diag_matrix_template);
741 
742  explicit Matrix(std::shared_ptr<const Executor> exec,
743  mpi::communicator comm, dim<2> size,
744  std::shared_ptr<LinOp> diag_linop);
745 
746  explicit Matrix(std::shared_ptr<const Executor> exec,
747  mpi::communicator comm,
749  std::shared_ptr<LinOp> diag_linop,
750  std::shared_ptr<LinOp> off_diag_linop);
751 
752  void apply_impl(const LinOp* b, LinOp* x) const override;
753 
754  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
755  LinOp* x) const override;
756 
757 private:
758  std::shared_ptr<RowGatherer<LocalIndexType>> row_gatherer_;
760  gko::detail::ScalarCache one_scalar_;
761  detail::GenericVectorCache recv_buffer_;
762  detail::GenericVectorCache host_recv_buffer_;
763  std::shared_ptr<LinOp> diag_mtx_;
764  std::shared_ptr<LinOp> off_diag_mtx_;
765 };
766 
767 
768 } // namespace distributed
769 } // namespace experimental
770 } // namespace gko
771 
772 
773 #endif
774 
775 
776 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
gko::experimental::distributed::Matrix::write
void write(matrix_data< value_type, global_index_type > &data) const override
Writes the locally stored matrix data into a matrix_data structure using global row and column indice...
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:127
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
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:147
gko::LinOp
Definition: lin_op.hpp:117
gko::matrix::Dense< value_type >
gko::experimental::distributed::RowGatherer
The distributed::RowGatherer gathers the rows of distributed::Vector that are located on other proces...
Definition: row_gatherer.hpp:30
gko::experimental::distributed::Matrix
The Matrix class defines a (MPI-)distributed matrix.
Definition: matrix.hpp:262
gko::experimental::distributed::Vector
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition: matrix.hpp:153
gko::experimental::distributed::Matrix::get_local_matrix
std::shared_ptr< const LinOp > get_local_matrix() const
Definition: matrix.hpp:455
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
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
Definition: matrix.hpp:464
gko::array< local_index_type >
gko::multigrid::Pgm
Parallel graph match (Pgm) is the aggregate method introduced in the paper M.
Definition: matrix.hpp:40
gko::experimental::mpi::communicator
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition: mpi.hpp:419
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::experimental::distributed::Matrix::get_diag_matrix
std::shared_ptr< const LinOp > get_diag_matrix() const
Get read access to the stored diagonal matrix block.
Definition: matrix.hpp:436
gko::experimental::distributed::Matrix::create
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, DiagMatrixType diag_matrix_template, OffDiagMatrixType off_diag_matrix_template)
Creates an empty distributed matrix with specified types for the diagonal matrix and the off-diagonal...
Definition: matrix.hpp:599
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::WritableToMatrixData
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:619
gko::next_precision
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:466
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::previous_precision
typename detail::find_precision_impl< T, -step >::type previous_precision
Obtains the previous move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:473
gko::experimental::distributed::Matrix::operator=
Matrix & operator=(const Matrix &other)
Copy assigns a Matrix.
gko::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:369
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:113
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:172
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:140
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:107
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:556
gko::experimental::distributed::DistributedBase
A base class for distributed objects.
Definition: base.hpp:32
gko::experimental::distributed::index_map< local_index_type, global_index_type >
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::experimental::distributed::Matrix::get_off_diag_matrix
std::shared_ptr< const LinOp > get_off_diag_matrix() const
Get read access to the stored off-diagonal matrix block.
Definition: matrix.hpp:446