Ginkgo Generated from branch based on main. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
 
Loading...
Searching...
No Matches
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
23namespace gko {
24namespace matrix {
25
26
27template <typename ValueType, typename IndexType>
28class Csr;
29
30
31}
32
33
34namespace multigrid {
35
36
37template <typename ValueType, typename IndexType>
38class Pgm;
39
40
41}
42
43
44namespace detail {
45
46
51template <typename Builder, typename ValueType, typename IndexType,
52 typename = void>
53struct is_matrix_type_builder : std::false_type {};
54
55
56template <typename Builder, typename ValueType, typename IndexType>
57struct 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
65template <template <typename, typename> class MatrixType,
66 typename... CreateArgs>
67struct 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
124template <template <typename, typename> class MatrixType, typename... Args>
125auto with_matrix_type(Args&&... create_args)
126{
127 return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
128 std::forward_as_tuple(create_args...)};
129}
130
131
132namespace experimental {
133namespace distributed {
134
135
145enum class assembly_mode { communicate, local_only };
146
147
148template <typename LocalIndexType, typename GlobalIndexType>
149class Partition;
150template <typename ValueType>
151class Vector;
152
153
258template <typename ValueType = default_precision,
259 typename LocalIndexType = int32, typename GlobalIndexType = int64>
261 Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
262 public ConvertibleTo<Matrix<next_precision_base<ValueType>,
263 LocalIndexType, GlobalIndexType>>,
264 public DistributedBase {
266 friend class Matrix<next_precision_base<ValueType>, LocalIndexType,
267 GlobalIndexType>;
268 friend class multigrid::Pgm<ValueType, LocalIndexType>;
269
270public:
271 using value_type = ValueType;
272 using index_type = GlobalIndexType;
273 using local_index_type = LocalIndexType;
274 using global_index_type = GlobalIndexType;
275 using global_vector_type =
277 using local_vector_type = typename global_vector_type::local_vector_type;
278
279 using EnableDistributedLinOp<Matrix>::convert_to;
280 using EnableDistributedLinOp<Matrix>::move_to;
282 GlobalIndexType>>::convert_to;
284 GlobalIndexType>>::move_to;
285
286 void convert_to(Matrix<next_precision_base<value_type>, local_index_type,
287 global_index_type>* result) const override;
288
289 void move_to(Matrix<next_precision_base<value_type>, local_index_type,
290 global_index_type>* result) override;
291
312 partition,
313 assembly_mode assembly_type = assembly_mode::local_only);
314
327 partition,
328 assembly_mode assembly_type = assembly_mode::local_only);
329
351 row_partition,
353 col_partition,
354 assembly_mode assembly_type = assembly_mode::local_only);
355
368 row_partition,
370 col_partition,
371 assembly_mode assembly_type = assembly_mode::local_only);
372
378 std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
379
385 std::shared_ptr<const LinOp> get_non_local_matrix() const
386 {
387 return non_local_mtx_;
388 }
389
395 Matrix(const Matrix& other);
396
402 Matrix(Matrix&& other) noexcept;
403
412 Matrix& operator=(const Matrix& other);
413
423
433 static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
434 mpi::communicator comm);
435
456 template <typename MatrixType,
457 typename = std::enable_if_t<gko::detail::is_matrix_type_builder<
458 MatrixType, ValueType, LocalIndexType>::value>>
459 static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
461 MatrixType matrix_template)
462 {
463 return create(
464 exec, comm,
465 matrix_template.template create<ValueType, LocalIndexType>(exec));
466 }
467
496 template <typename LocalMatrixType, typename NonLocalMatrixType,
497 typename = std::enable_if_t<
498 gko::detail::is_matrix_type_builder<
499 LocalMatrixType, ValueType, LocalIndexType>::value &&
500 gko::detail::is_matrix_type_builder<
501 NonLocalMatrixType, ValueType, LocalIndexType>::value>>
502 static std::unique_ptr<Matrix> create(
503 std::shared_ptr<const Executor> exec, mpi::communicator comm,
504 LocalMatrixType local_matrix_template,
505 NonLocalMatrixType non_local_matrix_template)
506 {
507 return create(
508 exec, comm,
509 local_matrix_template.template create<ValueType, LocalIndexType>(
510 exec),
511 non_local_matrix_template
512 .template create<ValueType, LocalIndexType>(exec));
513 }
514
529 static std::unique_ptr<Matrix> create(
530 std::shared_ptr<const Executor> exec, mpi::communicator comm,
531 ptr_param<const LinOp> matrix_template);
532
549 static std::unique_ptr<Matrix> create(
550 std::shared_ptr<const Executor> exec, mpi::communicator comm,
551 ptr_param<const LinOp> local_matrix_template,
552 ptr_param<const LinOp> non_local_matrix_template);
553
566 static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
567 mpi::communicator comm, dim<2> size,
568 std::shared_ptr<LinOp> local_linop);
569
588 static std::unique_ptr<Matrix> create(
589 std::shared_ptr<const Executor> exec, mpi::communicator comm,
590 dim<2> size, std::shared_ptr<LinOp> local_linop,
591 std::shared_ptr<LinOp> non_local_linop,
592 std::vector<comm_index_type> recv_sizes,
593 std::vector<comm_index_type> recv_offsets,
594 array<local_index_type> recv_gather_idxs);
595
604
613
614protected:
615 explicit Matrix(std::shared_ptr<const Executor> exec,
616 mpi::communicator comm);
617
618 explicit Matrix(std::shared_ptr<const Executor> exec,
620 ptr_param<const LinOp> local_matrix_template,
621 ptr_param<const LinOp> non_local_matrix_template);
622
623 explicit Matrix(std::shared_ptr<const Executor> exec,
624 mpi::communicator comm, dim<2> size,
625 std::shared_ptr<LinOp> local_linop);
626
627 explicit Matrix(std::shared_ptr<const Executor> exec,
628 mpi::communicator comm, dim<2> size,
629 std::shared_ptr<LinOp> local_linop,
630 std::shared_ptr<LinOp> non_local_linop,
631 std::vector<comm_index_type> recv_sizes,
632 std::vector<comm_index_type> recv_offsets,
633 array<local_index_type> recv_gather_idxs);
634
643 mpi::request communicate(const local_vector_type* local_b) const;
644
645 void apply_impl(const LinOp* b, LinOp* x) const override;
646
647 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
648 LinOp* x) const override;
649
650private:
651 std::vector<comm_index_type> send_offsets_;
652 std::vector<comm_index_type> send_sizes_;
653 std::vector<comm_index_type> recv_offsets_;
654 std::vector<comm_index_type> recv_sizes_;
655 array<local_index_type> gather_idxs_;
656 array<global_index_type> non_local_to_global_;
657 gko::detail::DenseCache<value_type> one_scalar_;
658 gko::detail::DenseCache<value_type> host_send_buffer_;
659 gko::detail::DenseCache<value_type> host_recv_buffer_;
660 gko::detail::DenseCache<value_type> send_buffer_;
661 gko::detail::DenseCache<value_type> recv_buffer_;
662 std::shared_ptr<LinOp> local_mtx_;
663 std::shared_ptr<LinOp> non_local_mtx_;
664};
665
666
667} // namespace distributed
668} // namespace experimental
669} // namespace gko
670
671
672#endif
673
674
675#endif // GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:470
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition lin_op.hpp:44
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition polymorphic_object.hpp:53
std::shared_ptr< const LinOp > get_non_local_matrix() const
Get read access to the stored non-local matrix.
Definition matrix.hpp:385
Matrix(Matrix &&other) noexcept
Move constructs a Matrix.
void read_distributed(const matrix_data< value_type, global_index_type > &data, std::shared_ptr< const Partition< local_index_type, global_index_type > > row_partition, std::shared_ptr< const Partition< local_index_type, global_index_type > > col_partition, assembly_mode assembly_type=assembly_mode::local_only)
Reads a matrix from the matrix_data structure, a global row partition, and a global column partition.
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, ptr_param< const LinOp > local_matrix_template, ptr_param< const LinOp > non_local_matrix_template)
Creates an empty distributed matrix with specified types for the local matrix and the non-local matri...
void col_scale(ptr_param< const global_vector_type > scaling_factors)
Scales the columns of the matrix by the respective entries of the vector.
std::shared_ptr< const LinOp > get_local_matrix() const
Get read access to the stored local matrix.
Definition matrix.hpp:378
void read_distributed(const device_matrix_data< value_type, global_index_type > &data, std::shared_ptr< const Partition< local_index_type, global_index_type > > row_partition, std::shared_ptr< const Partition< local_index_type, global_index_type > > col_partition, assembly_mode assembly_type=assembly_mode::local_only)
Reads a matrix from the device_matrix_data structure, a global row partition, and a global column par...
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm)
Creates an empty distributed matrix.
void read_distributed(const 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 matrix_data structure and a global partition.
void row_scale(ptr_param< const global_vector_type > scaling_factors)
Scales the rows of the matrix by the respective entries of the vector.
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > size, std::shared_ptr< LinOp > local_linop)
Creates a local-only distributed matrix with existent LinOp.
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > size, std::shared_ptr< LinOp > local_linop, std::shared_ptr< LinOp > non_local_linop, std::vector< comm_index_type > recv_sizes, std::vector< comm_index_type > recv_offsets, array< local_index_type > recv_gather_idxs)
Creates distributed matrix with existent local and non-local LinOp and the corresponding mapping to c...
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.
Matrix(const Matrix &other)
Copy constructs a Matrix.
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:502
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:459
Matrix & operator=(Matrix &&other)
Move assigns a Matrix.
Matrix & operator=(const Matrix &other)
Copy assigns a Matrix.
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, ptr_param< const LinOp > matrix_template)
Creates an empty distributed matrix with specified type for local matrices.
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition partition.hpp:82
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition vector.hpp:71
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition mpi.hpp:408
The request class is a light, move-only wrapper around the MPI_Request handle.
Definition mpi.hpp:319
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:121
Parallel graph match (Pgm) is the aggregate method introduced in the paper M.
Definition pgm.hpp:52
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
assembly_mode
assembly_mode defines how the read_distributed function of the distributed matrix treats non-local in...
Definition matrix.hpp:145
The matrix namespace.
Definition dense_cache.hpp:15
The multigrid components namespace.
Definition matrix.hpp:34
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:106
typename detail::next_precision_base_impl< T >::type next_precision_base
Obtains the next type in the singly-linked precision list.
Definition math.hpp:421
auto with_matrix_type(Args &&... create_args)
This function returns a type that delays a call to MatrixType::create.
Definition matrix.hpp:125
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:171
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:112
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126