Ginkgo  Generated from pipelines/1556235455 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
matrix.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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/mpi.hpp>
17 #include <ginkgo/core/base/std_extensions.hpp>
18 #include <ginkgo/core/distributed/base.hpp>
19 #include <ginkgo/core/distributed/index_map.hpp>
20 #include <ginkgo/core/distributed/lin_op.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 
136 template <typename LocalIndexType, typename GlobalIndexType>
137 class Partition;
138 template <typename ValueType>
139 class Vector;
140 
141 
246 template <typename ValueType = default_precision,
247  typename LocalIndexType = int32, typename GlobalIndexType = int64>
248 class Matrix
249  : public EnableDistributedLinOp<
250  Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
251  public ConvertibleTo<
252  Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
253  public DistributedBase {
255  friend class Matrix<next_precision<ValueType>, LocalIndexType,
256  GlobalIndexType>;
257  friend class multigrid::Pgm<ValueType, LocalIndexType>;
258 
259 public:
260  using value_type = ValueType;
261  using index_type = GlobalIndexType;
262  using local_index_type = LocalIndexType;
263  using global_index_type = GlobalIndexType;
264  using global_vector_type =
266  using local_vector_type = typename global_vector_type::local_vector_type;
267 
271  GlobalIndexType>>::convert_to;
273  GlobalIndexType>>::move_to;
274 
275  void convert_to(Matrix<next_precision<value_type>, local_index_type,
276  global_index_type>* result) const override;
277 
278  void move_to(Matrix<next_precision<value_type>, local_index_type,
279  global_index_type>* result) override;
280 
297  void read_distributed(
300  partition);
301 
311  void read_distributed(
314  partition);
315 
333  void read_distributed(
336  row_partition,
338  col_partition);
339 
349  void read_distributed(
352  row_partition,
354  col_partition);
355 
361  std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
362 
368  std::shared_ptr<const LinOp> get_non_local_matrix() const
369  {
370  return non_local_mtx_;
371  }
372 
378  Matrix(const Matrix& other);
379 
385  Matrix(Matrix&& other) noexcept;
386 
395  Matrix& operator=(const Matrix& other);
396 
405  Matrix& operator=(Matrix&& other);
406 
416  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
417  mpi::communicator comm);
418 
439  template <typename MatrixType,
440  typename = std::enable_if_t<gko::detail::is_matrix_type_builder<
441  MatrixType, ValueType, LocalIndexType>::value>>
442  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
443  mpi::communicator comm,
444  MatrixType matrix_template)
445  {
446  return create(
447  exec, comm,
448  matrix_template.template create<ValueType, LocalIndexType>(exec));
449  }
450 
479  template <typename LocalMatrixType, typename NonLocalMatrixType,
480  typename = std::enable_if_t<
481  gko::detail::is_matrix_type_builder<
482  LocalMatrixType, ValueType, LocalIndexType>::value &&
483  gko::detail::is_matrix_type_builder<
484  NonLocalMatrixType, ValueType, LocalIndexType>::value>>
485  static std::unique_ptr<Matrix> create(
486  std::shared_ptr<const Executor> exec, mpi::communicator comm,
487  LocalMatrixType local_matrix_template,
488  NonLocalMatrixType non_local_matrix_template)
489  {
490  return create(
491  exec, comm,
492  local_matrix_template.template create<ValueType, LocalIndexType>(
493  exec),
494  non_local_matrix_template
495  .template create<ValueType, LocalIndexType>(exec));
496  }
497 
512  static std::unique_ptr<Matrix> create(
513  std::shared_ptr<const Executor> exec, mpi::communicator comm,
514  ptr_param<const LinOp> matrix_template);
515 
532  static std::unique_ptr<Matrix> create(
533  std::shared_ptr<const Executor> exec, mpi::communicator comm,
534  ptr_param<const LinOp> local_matrix_template,
535  ptr_param<const LinOp> non_local_matrix_template);
536 
549  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
550  mpi::communicator comm, dim<2> size,
551  std::shared_ptr<LinOp> local_linop);
552 
571  static std::unique_ptr<Matrix> create(
572  std::shared_ptr<const Executor> exec, mpi::communicator comm,
573  dim<2> size, std::shared_ptr<LinOp> local_linop,
574  std::shared_ptr<LinOp> non_local_linop,
575  std::vector<comm_index_type> recv_sizes,
576  std::vector<comm_index_type> recv_offsets,
577  array<local_index_type> recv_gather_idxs);
578 
586  void col_scale(ptr_param<const global_vector_type> scaling_factors);
587 
595  void row_scale(ptr_param<const global_vector_type> scaling_factors);
596 
597 protected:
598  explicit Matrix(std::shared_ptr<const Executor> exec,
599  mpi::communicator comm);
600 
601  explicit Matrix(std::shared_ptr<const Executor> exec,
602  mpi::communicator comm,
603  ptr_param<const LinOp> local_matrix_template,
604  ptr_param<const LinOp> non_local_matrix_template);
605 
606  explicit Matrix(std::shared_ptr<const Executor> exec,
607  mpi::communicator comm, dim<2> size,
608  std::shared_ptr<LinOp> local_linop);
609 
610  explicit Matrix(std::shared_ptr<const Executor> exec,
611  mpi::communicator comm, dim<2> size,
612  std::shared_ptr<LinOp> local_linop,
613  std::shared_ptr<LinOp> non_local_linop,
614  std::vector<comm_index_type> recv_sizes,
615  std::vector<comm_index_type> recv_offsets,
616  array<local_index_type> recv_gather_idxs);
617 
626  mpi::request communicate(const local_vector_type* local_b) const;
627 
628  void apply_impl(const LinOp* b, LinOp* x) const override;
629 
630  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
631  LinOp* x) const override;
632 
633 private:
634  std::vector<comm_index_type> send_offsets_;
635  std::vector<comm_index_type> send_sizes_;
636  std::vector<comm_index_type> recv_offsets_;
637  std::vector<comm_index_type> recv_sizes_;
638  array<local_index_type> gather_idxs_;
639  array<global_index_type> non_local_to_global_;
640  gko::detail::DenseCache<value_type> one_scalar_;
641  gko::detail::DenseCache<value_type> host_send_buffer_;
642  gko::detail::DenseCache<value_type> host_recv_buffer_;
643  gko::detail::DenseCache<value_type> send_buffer_;
644  gko::detail::DenseCache<value_type> recv_buffer_;
645  std::shared_ptr<LinOp> local_mtx_;
646  std::shared_ptr<LinOp> non_local_mtx_;
647 };
648 
649 
650 } // namespace distributed
651 } // namespace experimental
652 } // namespace gko
653 
654 
655 #endif
656 
657 
658 #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::EnableDistributedPolymorphicObject
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition: polymorphic_object.hpp:52
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:248
gko::experimental::distributed::Vector
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition: matrix.hpp:139
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:361
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:485
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:319
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::experimental::EnableDistributedLinOp
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition: lin_op.hpp:42
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:368
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:408
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::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)
Reads a square matrix from the device_matrix_data structure and a global partition.
gko::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:461
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: matrix.hpp:137
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:109
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:470
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:103
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:442
gko::experimental::distributed::DistributedBase
A base class for distributed objects.
Definition: base.hpp:32
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.