Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.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/distributed/base.hpp>
18 #include <ginkgo/core/distributed/index_map.hpp>
19 #include <ginkgo/core/distributed/lin_op.hpp>
20 
21 
22 namespace gko {
23 namespace matrix {
24 
25 
26 template <typename ValueType, typename IndexType>
27 class Csr;
28 
29 
30 }
31 
32 
33 namespace multigrid {
34 
35 
36 template <typename ValueType, typename IndexType>
37 class Pgm;
38 
39 
40 }
41 
42 
43 namespace detail {
44 
45 
50 template <typename Builder, typename ValueType, typename IndexType,
51  typename = void>
52 struct is_matrix_type_builder : std::false_type {};
53 
54 
55 template <typename Builder, typename ValueType, typename IndexType>
56 struct is_matrix_type_builder<
57  Builder, ValueType, IndexType,
58  gko::xstd::void_t<
59  decltype(std::declval<Builder>().template create<ValueType, IndexType>(
60  std::declval<std::shared_ptr<const Executor>>()))>>
61  : std::true_type {};
62 
63 
64 template <template <typename, typename> class MatrixType,
65  typename... CreateArgs>
66 struct MatrixTypeBuilderFromValueAndIndex {
67  template <typename ValueType, typename IndexType, std::size_t... I>
68  auto create_impl(std::shared_ptr<const Executor> exec,
69  std::index_sequence<I...>)
70  {
71  return MatrixType<ValueType, IndexType>::create(
72  exec, std::get<I>(create_args)...);
73  }
74 
75 
76  template <typename ValueType, typename IndexType>
77  auto create(std::shared_ptr<const Executor> exec)
78  {
79  // with c++17 we could use std::apply
80  static constexpr auto size = sizeof...(CreateArgs);
81  return create_impl<ValueType, IndexType>(
82  std::move(exec), std::make_index_sequence<size>{});
83  }
84 
85  std::tuple<CreateArgs...> create_args;
86 };
87 
88 
89 } // namespace detail
90 
91 
123 template <template <typename, typename> class MatrixType, typename... Args>
124 auto with_matrix_type(Args&&... create_args)
125 {
126  return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
127  std::forward_as_tuple(create_args...)};
128 }
129 
130 
131 namespace experimental {
132 namespace distributed {
133 
134 
135 template <typename LocalIndexType, typename GlobalIndexType>
136 class Partition;
137 template <typename ValueType>
138 class Vector;
139 
140 
245 template <typename ValueType = default_precision,
246  typename LocalIndexType = int32, typename GlobalIndexType = int64>
247 class Matrix
248  : public EnableDistributedLinOp<
249  Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
250  public ConvertibleTo<
251  Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
252  public DistributedBase {
254  friend class Matrix<next_precision<ValueType>, LocalIndexType,
255  GlobalIndexType>;
256  friend class multigrid::Pgm<ValueType, LocalIndexType>;
257 
258 public:
259  using value_type = ValueType;
260  using index_type = GlobalIndexType;
261  using local_index_type = LocalIndexType;
262  using global_index_type = GlobalIndexType;
263  using global_vector_type =
265  using local_vector_type = typename global_vector_type::local_vector_type;
266 
270  GlobalIndexType>>::convert_to;
272  GlobalIndexType>>::move_to;
273 
274  void convert_to(Matrix<next_precision<value_type>, local_index_type,
275  global_index_type>* result) const override;
276 
277  void move_to(Matrix<next_precision<value_type>, local_index_type,
278  global_index_type>* result) override;
279 
296  void read_distributed(
299  partition);
300 
310  void read_distributed(
313  partition);
314 
332  void read_distributed(
335  row_partition,
337  col_partition);
338 
348  void read_distributed(
351  row_partition,
353  col_partition);
354 
360  std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
361 
367  std::shared_ptr<const LinOp> get_non_local_matrix() const
368  {
369  return non_local_mtx_;
370  }
371 
377  Matrix(const Matrix& other);
378 
384  Matrix(Matrix&& other) noexcept;
385 
394  Matrix& operator=(const Matrix& other);
395 
404  Matrix& operator=(Matrix&& other);
405 
415  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
416  mpi::communicator comm);
417 
438  template <typename MatrixType,
439  typename = std::enable_if_t<detail::is_matrix_type_builder<
440  MatrixType, ValueType, LocalIndexType>::value>>
441  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
442  mpi::communicator comm,
443  MatrixType matrix_template)
444  {
445  return create(
446  exec, comm,
447  matrix_template.template create<ValueType, LocalIndexType>(exec));
448  }
449 
478  template <typename LocalMatrixType, typename NonLocalMatrixType,
479  typename = std::enable_if_t<
480  detail::is_matrix_type_builder<LocalMatrixType, ValueType,
481  LocalIndexType>::value &&
482  detail::is_matrix_type_builder<NonLocalMatrixType, ValueType,
483  LocalIndexType>::value>>
484  static std::unique_ptr<Matrix> create(
485  std::shared_ptr<const Executor> exec, mpi::communicator comm,
486  LocalMatrixType local_matrix_template,
487  NonLocalMatrixType non_local_matrix_template)
488  {
489  return create(
490  exec, comm,
491  local_matrix_template.template create<ValueType, LocalIndexType>(
492  exec),
493  non_local_matrix_template
494  .template create<ValueType, LocalIndexType>(exec));
495  }
496 
511  static std::unique_ptr<Matrix> create(
512  std::shared_ptr<const Executor> exec, mpi::communicator comm,
513  ptr_param<const LinOp> matrix_template);
514 
531  static std::unique_ptr<Matrix> create(
532  std::shared_ptr<const Executor> exec, mpi::communicator comm,
533  ptr_param<const LinOp> local_matrix_template,
534  ptr_param<const LinOp> non_local_matrix_template);
535 
548  static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
549  mpi::communicator comm, dim<2> size,
550  std::shared_ptr<LinOp> local_linop);
551 
570  static std::unique_ptr<Matrix> create(
571  std::shared_ptr<const Executor> exec, mpi::communicator comm,
572  dim<2> size, std::shared_ptr<LinOp> local_linop,
573  std::shared_ptr<LinOp> non_local_linop,
574  std::vector<comm_index_type> recv_sizes,
575  std::vector<comm_index_type> recv_offsets,
576  array<local_index_type> recv_gather_idxs);
577 
578 protected:
579  explicit Matrix(std::shared_ptr<const Executor> exec,
580  mpi::communicator comm);
581 
582  explicit Matrix(std::shared_ptr<const Executor> exec,
583  mpi::communicator comm,
584  ptr_param<const LinOp> local_matrix_template,
585  ptr_param<const LinOp> non_local_matrix_template);
586 
587  explicit Matrix(std::shared_ptr<const Executor> exec,
588  mpi::communicator comm, dim<2> size,
589  std::shared_ptr<LinOp> local_linop);
590 
591  explicit Matrix(std::shared_ptr<const Executor> exec,
592  mpi::communicator comm, dim<2> size,
593  std::shared_ptr<LinOp> local_linop,
594  std::shared_ptr<LinOp> non_local_linop,
595  std::vector<comm_index_type> recv_sizes,
596  std::vector<comm_index_type> recv_offsets,
597  array<local_index_type> recv_gather_idxs);
598 
607  mpi::request communicate(const local_vector_type* local_b) const;
608 
609  void apply_impl(const LinOp* b, LinOp* x) const override;
610 
611  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
612  LinOp* x) const override;
613 
614 private:
615  std::vector<comm_index_type> send_offsets_;
616  std::vector<comm_index_type> send_sizes_;
617  std::vector<comm_index_type> recv_offsets_;
618  std::vector<comm_index_type> recv_sizes_;
619  array<local_index_type> gather_idxs_;
620  array<global_index_type> non_local_to_global_;
621  gko::detail::DenseCache<value_type> one_scalar_;
622  gko::detail::DenseCache<value_type> host_send_buffer_;
623  gko::detail::DenseCache<value_type> host_recv_buffer_;
624  gko::detail::DenseCache<value_type> send_buffer_;
625  gko::detail::DenseCache<value_type> recv_buffer_;
626  std::shared_ptr<LinOp> local_mtx_;
627  std::shared_ptr<LinOp> non_local_mtx_;
628 };
629 
630 
631 } // namespace distributed
632 } // namespace experimental
633 } // namespace gko
634 
635 
636 #endif
637 
638 
639 #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:124
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:27
gko::experimental::EnableDistributedPolymorphicObject
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition: polymorphic_object.hpp:53
gko::LinOp
Definition: lin_op.hpp:118
gko::matrix::Dense< value_type >
gko::experimental::distributed::Matrix
The Matrix class defines a (MPI-)distributed matrix.
Definition: matrix.hpp:247
gko::experimental::distributed::Vector
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition: matrix.hpp:138
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:360
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:484
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:320
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:43
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:367
gko::array< local_index_type >
gko::multigrid::Pgm
Parallel graph match (Pgm) is the aggregate method introduced in the paper M.
Definition: matrix.hpp:37
gko::experimental::mpi::communicator
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition: mpi.hpp:409
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:127
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:462
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::experimental::distributed::Partition
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition: matrix.hpp:136
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:131
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:193
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:471
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:125
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:441
gko::experimental::distributed::DistributedBase
A base class for distributed objects.
Definition: base.hpp:32
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.