Ginkgo  Generated from pipelines/2897303314 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 template <typename ValueType, typename IndexType>
44 
45 
46 } // namespace multigrid
47 
48 
49 namespace detail {
50 
51 
56 template <typename Builder, typename ValueType, typename IndexType,
57  typename = void>
58 struct is_matrix_type_builder : std::false_type {};
59 
60 
61 template <typename Builder, typename ValueType, typename IndexType>
62 struct is_matrix_type_builder<
63  Builder, ValueType, IndexType,
64  xstd::void_t<
65  decltype(std::declval<Builder>().template create<ValueType, IndexType>(
66  std::declval<std::shared_ptr<const Executor>>()))>>
67  : std::true_type {};
68 
69 
70 template <template <typename, typename> class MatrixType,
71  typename... CreateArgs>
72 struct MatrixTypeBuilderFromValueAndIndex {
73  template <typename ValueType, typename IndexType, std::size_t... I>
74  auto create_impl(std::shared_ptr<const Executor> exec,
75  std::index_sequence<I...>)
76  {
77  return MatrixType<ValueType, IndexType>::create(
78  exec, std::get<I>(create_args)...);
79  }
80 
81 
82  template <typename ValueType, typename IndexType>
83  auto create(std::shared_ptr<const Executor> exec)
84  {
85  // with c++17 we could use std::apply
86  static constexpr auto size = sizeof...(CreateArgs);
87  return create_impl<ValueType, IndexType>(
88  std::move(exec), std::make_index_sequence<size>{});
89  }
90 
91  std::tuple<CreateArgs...> create_args;
92 };
93 
94 
95 } // namespace detail
96 
97 
129 template <template <typename, typename> class MatrixType, typename... Args>
130 auto with_matrix_type(Args&&... create_args)
131 {
132  return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
133  std::forward_as_tuple(create_args...)};
134 }
135 
136 
137 namespace experimental {
138 namespace distributed {
139 
140 
150 enum class assembly_mode { communicate, local_only };
151 
152 
153 template <typename LocalIndexType, typename GlobalIndexType>
154 class Partition;
155 template <typename ValueType>
156 class Vector;
157 
158 
263 template <typename ValueType = default_precision,
264  typename LocalIndexType = int32, typename GlobalIndexType = int64>
265 class Matrix
266  : public LinOp,
267  public EnableCloneable<
268  Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
269  public ConvertibleTo<
270  Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
271 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
272  public ConvertibleTo<Matrix<next_precision<ValueType, 2>, LocalIndexType,
273  GlobalIndexType>>,
274 #endif
275 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
276  public ConvertibleTo<Matrix<next_precision<ValueType, 3>, LocalIndexType,
277  GlobalIndexType>>,
278 #endif
279  public WritableToMatrixData<ValueType, GlobalIndexType>,
280  public DistributedBase {
281  friend class EnableCloneable<Matrix>;
282  friend class Matrix<previous_precision<ValueType>, LocalIndexType,
283  GlobalIndexType>;
284 
285  friend class multigrid::Pgm<ValueType, LocalIndexType>;
286  friend class multigrid::UniformCoarsening<ValueType, LocalIndexType>;
287  GKO_ASSERT_SUPPORTED_VALUE_AND_DIST_INDEX_TYPE;
288 
289 public:
290  using value_type = ValueType;
291  using index_type = GlobalIndexType;
292  using local_index_type = LocalIndexType;
293  using global_index_type = GlobalIndexType;
294  using global_vector_type =
296  using local_vector_type = typename global_vector_type::local_vector_type;
297 
301  GlobalIndexType>>::convert_to;
303  GlobalIndexType>>::move_to;
304 
305  void convert_to(Matrix<next_precision<value_type>, local_index_type,
306  global_index_type>* result) const override;
307 
308  void move_to(Matrix<next_precision<value_type>, local_index_type,
309  global_index_type>* result) override;
310 
311 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
312  friend class Matrix<previous_precision<ValueType, 2>, LocalIndexType,
313  GlobalIndexType>;
315  global_index_type>>::convert_to;
317  global_index_type>>::move_to;
318 
319  void convert_to(Matrix<next_precision<value_type, 2>, local_index_type,
320  global_index_type>* result) const override;
321 
322  void move_to(Matrix<next_precision<value_type, 2>, local_index_type,
323  global_index_type>* result) override;
324 #endif
325 
326 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
327  friend class Matrix<previous_precision<ValueType, 3>, LocalIndexType,
328  GlobalIndexType>;
330  global_index_type>>::convert_to;
332  global_index_type>>::move_to;
333 
334  void convert_to(Matrix<next_precision<value_type, 3>, local_index_type,
335  global_index_type>* result) const override;
336 
337  void move_to(Matrix<next_precision<value_type, 3>, local_index_type,
338  global_index_type>* result) override;
339 #endif
340 
357  void read_distributed(
360  partition,
361  assembly_mode assembly_type = assembly_mode::local_only);
362 
372  void read_distributed(
375  partition,
376  assembly_mode assembly_type = assembly_mode::local_only);
377 
396  void read_distributed(
399  row_partition,
401  col_partition,
402  assembly_mode assembly_type = assembly_mode::local_only);
403 
413  void read_distributed(
416  row_partition,
418  col_partition,
419  assembly_mode assembly_type = assembly_mode::local_only);
420 
431 
432  void write(matrix_data<value_type, global_index_type>& data) const override;
433 
442  std::shared_ptr<const LinOp> get_diag_matrix() const { return diag_mtx_; }
443 
452  std::shared_ptr<const LinOp> get_off_diag_matrix() const
453  {
454  return off_diag_mtx_;
455  }
456 
468  std::shared_ptr<const RowGatherer<LocalIndexType>> get_row_gatherer() const
469  {
470  return row_gatherer_;
471  }
472 
476  GKO_DEPRECATED("use get_diag_matrix() instead")
477  std::shared_ptr<const LinOp> get_local_matrix() const
478  {
479  return get_diag_matrix();
480  }
481 
485  GKO_DEPRECATED("use get_off_diag_matrix() instead")
486  std::shared_ptr<const LinOp> get_non_local_matrix() const
487  {
488  return get_off_diag_matrix();
489  }
490 
495  std::shared_ptr<const Partition<local_index_type, global_index_type>>
496  get_row_partition() const;
497 
514 
530  void transpose(ptr_param<Matrix> result) const;
531 
537  Matrix(const Matrix& other);
538 
544  Matrix(Matrix&& other) noexcept;
545 
554  Matrix& operator=(const Matrix& other);
555 
564  Matrix& operator=(Matrix&& other);
565 
575  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
576  mpi::communicator comm);
577 
591  static std::unique_ptr<Matrix> create(
592  std::shared_ptr<const Executor> exec,
593  std::shared_ptr<const RowGatherer<LocalIndexType>>
594  row_gatherer_template);
595 
616  template <typename MatrixType,
617  typename = std::enable_if_t<gko::detail::is_matrix_type_builder<
618  MatrixType, ValueType, LocalIndexType>::value>>
619  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
620  mpi::communicator comm,
621  MatrixType matrix_template)
622  {
623  return create(
624  exec, comm,
625  matrix_template.template create<ValueType, LocalIndexType>(exec));
626  }
627 
657  template <typename DiagMatrixType, typename OffDiagMatrixType,
658  typename = std::enable_if_t<
659  gko::detail::is_matrix_type_builder<DiagMatrixType, ValueType,
660  LocalIndexType>::value &&
661  gko::detail::is_matrix_type_builder<
662  OffDiagMatrixType, ValueType, LocalIndexType>::value>>
663  static std::unique_ptr<Matrix> create(
664  std::shared_ptr<const Executor> exec, mpi::communicator comm,
665  DiagMatrixType diag_matrix_template,
666  OffDiagMatrixType off_diag_matrix_template)
667  {
668  return create(
669  exec, comm,
670  diag_matrix_template.template create<ValueType, LocalIndexType>(
671  exec),
672  off_diag_matrix_template.template create<ValueType, LocalIndexType>(
673  exec));
674  }
675 
690  static std::unique_ptr<Matrix> create(
691  std::shared_ptr<const Executor> exec, mpi::communicator comm,
692  ptr_param<const LinOp> matrix_template);
693 
710  static std::unique_ptr<Matrix> create(
711  std::shared_ptr<const Executor> exec, mpi::communicator comm,
712  ptr_param<const LinOp> diag_matrix_template,
713  ptr_param<const LinOp> off_diag_matrix_template);
714 
727  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
728  mpi::communicator comm, dim<2> size,
729  std::shared_ptr<LinOp> diag_linop);
730 
749  [[deprecated(
750  "Please use the overload with an index_map instead.")]] static std::
751  unique_ptr<Matrix>
752  create(std::shared_ptr<const Executor> exec, mpi::communicator comm,
753  dim<2> size, std::shared_ptr<LinOp> diag_linop,
754  std::shared_ptr<LinOp> off_diag_linop,
755  std::vector<comm_index_type> recv_sizes,
756  std::vector<comm_index_type> recv_offsets,
757  array<local_index_type> recv_gather_idxs);
758 
772  static std::unique_ptr<Matrix> create(
773  std::shared_ptr<const Executor> exec, mpi::communicator comm,
775  std::shared_ptr<LinOp> diag_linop,
776  std::shared_ptr<LinOp> off_diag_linop);
777 
785  void col_scale(ptr_param<const global_vector_type> scaling_factors);
786 
794  void row_scale(ptr_param<const global_vector_type> scaling_factors);
795 
796 protected:
797  explicit Matrix(std::shared_ptr<const Executor> exec,
798  mpi::communicator comm);
799 
800  explicit Matrix(std::shared_ptr<const Executor> exec,
801  std::shared_ptr<const RowGatherer<LocalIndexType>>
802  row_gatherer_template,
803  ptr_param<const LinOp> diag_matrix_template,
804  ptr_param<const LinOp> off_diag_matrix_template);
805 
806  explicit Matrix(std::shared_ptr<const Executor> exec,
807  mpi::communicator comm, dim<2> size,
808  std::shared_ptr<LinOp> diag_linop);
809 
810  explicit Matrix(std::shared_ptr<const Executor> exec,
811  mpi::communicator comm,
813  std::shared_ptr<LinOp> diag_linop,
814  std::shared_ptr<LinOp> off_diag_linop);
815 
816  void apply_impl(const LinOp* b, LinOp* x) const override;
817 
818  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
819  LinOp* x) const override;
820 
821 private:
822  std::shared_ptr<RowGatherer<LocalIndexType>> row_gatherer_;
824  std::shared_ptr<const Partition<local_index_type, global_index_type>>
825  row_partition_;
826  gko::detail::ScalarCache one_scalar_;
827  detail::GenericVectorCache recv_buffer_;
828  detail::GenericVectorCache host_recv_buffer_;
829  std::shared_ptr<LinOp> diag_mtx_;
830  std::shared_ptr<LinOp> off_diag_mtx_;
831 };
832 
833 
834 } // namespace distributed
835 } // namespace experimental
836 } // namespace gko
837 
838 
839 #endif
840 
841 
842 #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 a matrix to a matrix_data structure.
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:130
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:150
gko::experimental::distributed::Matrix::multiply
void multiply(ptr_param< const Matrix > b, ptr_param< Matrix > c) const
Computes the distributed sparse matrix-matrix product C = A * B, where A is this matrix.
gko::LinOp
Definition: lin_op.hpp:117
gko::multigrid::UniformCoarsening
UniformCoarsening is a simple coarse grid generation algorithm.
Definition: matrix.hpp:43
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:265
gko::experimental::distributed::Vector
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition: matrix.hpp:156
gko::experimental::distributed::Matrix::get_local_matrix
std::shared_ptr< const LinOp > get_local_matrix() const
Definition: matrix.hpp:477
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:19
gko::experimental::distributed::Matrix::get_row_partition
std::shared_ptr< const Partition< local_index_type, global_index_type > > get_row_partition() const
Get the row partition this matrix was constructed with via read_distributed.
gko::experimental::distributed::Matrix::get_non_local_matrix
std::shared_ptr< const LinOp > get_non_local_matrix() const
Definition: matrix.hpp:486
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:418
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:442
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:663
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::experimental::distributed::Matrix::transpose
void transpose(ptr_param< Matrix > result) const
Computes the transpose of this matrix into result.
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:465
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:472
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:376
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:124
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:183
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:118
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:619
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::get_row_gatherer
std::shared_ptr< const RowGatherer< LocalIndexType > > get_row_gatherer() const
Get read access to the row gatherer used to fetch the non-local rows during an apply.
Definition: matrix.hpp:468
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:452