Ginkgo Generated from develop branch based on develop. Ginkgo version 1.8.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/distributed/base.hpp>
18#include <ginkgo/core/distributed/lin_op.hpp>
19
20
21namespace gko {
22namespace matrix {
23
24
25template <typename ValueType, typename IndexType>
26class Csr;
27
28
29}
30
31
32namespace detail {
33
34
39template <typename Builder, typename ValueType, typename IndexType,
40 typename = void>
41struct is_matrix_type_builder : std::false_type {};
42
43
44template <typename Builder, typename ValueType, typename IndexType>
45struct is_matrix_type_builder<
46 Builder, ValueType, IndexType,
47 gko::xstd::void_t<
48 decltype(std::declval<Builder>().template create<ValueType, IndexType>(
49 std::declval<std::shared_ptr<const Executor>>()))>>
50 : std::true_type {};
51
52
53template <template <typename, typename> class MatrixType,
54 typename... CreateArgs>
55struct MatrixTypeBuilderFromValueAndIndex {
56 template <typename ValueType, typename IndexType, std::size_t... I>
57 auto create_impl(std::shared_ptr<const Executor> exec,
58 std::index_sequence<I...>)
59 {
60 return MatrixType<ValueType, IndexType>::create(
61 exec, std::get<I>(create_args)...);
62 }
63
64
65 template <typename ValueType, typename IndexType>
66 auto create(std::shared_ptr<const Executor> exec)
67 {
68 // with c++17 we could use std::apply
69 static constexpr auto size = sizeof...(CreateArgs);
71 std::move(exec), std::make_index_sequence<size>{});
72 }
73
74 std::tuple<CreateArgs...> create_args;
75};
76
77
78} // namespace detail
79
80
112template <template <typename, typename> class MatrixType, typename... Args>
113auto with_matrix_type(Args&&... create_args)
114{
115 return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
116 std::forward_as_tuple(create_args...)};
117}
118
119
120namespace experimental {
121namespace distributed {
122
123
124template <typename LocalIndexType, typename GlobalIndexType>
125class Partition;
126template <typename ValueType>
127class Vector;
128
129
234template <typename ValueType = default_precision,
235 typename LocalIndexType = int32, typename GlobalIndexType = int64>
237 : public EnableDistributedLinOp<
238 Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
239 public ConvertibleTo<
240 Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
241 public DistributedBase {
243 friend class Matrix<next_precision<ValueType>, LocalIndexType,
245
246public:
247 using value_type = ValueType;
248 using index_type = GlobalIndexType;
249 using local_index_type = LocalIndexType;
250 using global_index_type = GlobalIndexType;
251 using global_vector_type =
253 using local_vector_type = typename global_vector_type::local_vector_type;
254
255 using EnableDistributedLinOp<Matrix>::convert_to;
256 using EnableDistributedLinOp<Matrix>::move_to;
258 GlobalIndexType>>::convert_to;
260 GlobalIndexType>>::move_to;
261
262 void convert_to(Matrix<next_precision<value_type>, local_index_type,
263 global_index_type>* result) const override;
264
265 void move_to(Matrix<next_precision<value_type>, local_index_type,
266 global_index_type>* result) override;
267
285 partition);
286
299 partition);
300
322
338
344 std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
345
351 std::shared_ptr<const LinOp> get_non_local_matrix() const
352 {
353 return non_local_mtx_;
354 }
355
362
368 Matrix(Matrix&& other) noexcept;
369
379
398 static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
400
421 template <typename MatrixType,
422 typename = std::enable_if_t<detail::is_matrix_type_builder<
423 MatrixType, ValueType, LocalIndexType>::value>>
424 static std::unique_ptr<Matrix> create(std::shared_ptr<const Executor> exec,
427 {
428 return create(
429 exec, comm,
431 }
432
461 template <typename LocalMatrixType, typename NonLocalMatrixType,
462 typename = std::enable_if_t<
463 detail::is_matrix_type_builder<LocalMatrixType, ValueType,
464 LocalIndexType>::value &&
465 detail::is_matrix_type_builder<NonLocalMatrixType, ValueType,
466 LocalIndexType>::value>>
467 static std::unique_ptr<Matrix> create(
468 std::shared_ptr<const Executor> exec, mpi::communicator comm,
471 {
472 return create(
473 exec, comm,
475 exec),
477 .template create<ValueType, LocalIndexType>(exec));
478 }
479
494 static std::unique_ptr<Matrix> create(
495 std::shared_ptr<const Executor> exec, mpi::communicator comm,
497
514 static std::unique_ptr<Matrix> create(
515 std::shared_ptr<const Executor> exec, mpi::communicator comm,
518
519protected:
520 explicit Matrix(std::shared_ptr<const Executor> exec,
522
523 explicit Matrix(std::shared_ptr<const Executor> exec,
527
536 mpi::request communicate(const local_vector_type* local_b) const;
537
538 void apply_impl(const LinOp* b, LinOp* x) const override;
539
540 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
541 LinOp* x) const override;
542
543private:
544 std::vector<comm_index_type> send_offsets_;
545 std::vector<comm_index_type> send_sizes_;
546 std::vector<comm_index_type> recv_offsets_;
547 std::vector<comm_index_type> recv_sizes_;
548 array<local_index_type> gather_idxs_;
549 array<global_index_type> non_local_to_global_;
550 gko::detail::DenseCache<value_type> one_scalar_;
551 gko::detail::DenseCache<value_type> host_send_buffer_;
552 gko::detail::DenseCache<value_type> host_recv_buffer_;
553 gko::detail::DenseCache<value_type> send_buffer_;
554 gko::detail::DenseCache<value_type> recv_buffer_;
555 std::shared_ptr<LinOp> local_mtx_;
556 std::shared_ptr<LinOp> non_local_mtx_;
557};
558
559
560} // namespace distributed
561} // namespace experimental
562} // namespace gko
563
564
565#endif
566
567
568#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:471
Definition lin_op.hpp:118
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:167
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:35
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition lin_op.hpp:45
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition polymorphic_object.hpp:54
A base class for distributed objects.
Definition base.hpp:32
The Matrix class defines a (MPI-)distributed matrix.
Definition matrix.hpp:241
std::shared_ptr< const LinOp > get_non_local_matrix() const
Get read access to the stored non-local matrix.
Definition matrix.hpp:351
void read_distributed(const matrix_data< value_type, global_index_type > &data, ptr_param< const Partition< local_index_type, global_index_type > > partition)
Reads a square matrix from the matrix_data structure and a global partition.
Matrix(Matrix &&other) noexcept
Move constructs a Matrix.
void read_distributed(const matrix_data< value_type, global_index_type > &data, ptr_param< const Partition< local_index_type, global_index_type > > row_partition, ptr_param< const Partition< local_index_type, global_index_type > > col_partition)
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...
std::shared_ptr< const LinOp > get_local_matrix() const
Get read access to the stored local matrix.
Definition matrix.hpp:344
static std::unique_ptr< Matrix > create(std::shared_ptr< const Executor > exec, mpi::communicator comm)
Creates an empty distributed matrix.
void read_distributed(const device_matrix_data< value_type, global_index_type > &data, ptr_param< const Partition< local_index_type, global_index_type > > partition)
Reads a square matrix from the device_matrix_data structure and a global partition.
void read_distributed(const device_matrix_data< value_type, global_index_type > &data, ptr_param< const Partition< local_index_type, global_index_type > > row_partition, ptr_param< const Partition< local_index_type, global_index_type > > col_partition)
Reads a matrix from the device_matrix_data structure, a global row partition, and a global column par...
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:467
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:424
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:63
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition mpi.hpp:409
The request class is a light, move-only wrapper around the MPI_Request handle.
Definition mpi.hpp:320
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:43
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:109
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:462
auto with_matrix_type(Args &&... create_args)
This function returns a type that delays a call to MatrixType::create.
Definition matrix.hpp:113
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:177
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:115
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:127