Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
matrix.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
34#define GKO_PUBLIC_CORE_DISTRIBUTED_MATRIX_HPP_
35
36
37#include <ginkgo/config.hpp>
38
39
40#if GINKGO_BUILD_MPI
41
42
43#include <ginkgo/core/base/dense_cache.hpp>
44#include <ginkgo/core/base/mpi.hpp>
45#include <ginkgo/core/distributed/base.hpp>
46#include <ginkgo/core/distributed/lin_op.hpp>
47
48
49namespace gko {
50namespace matrix {
51
52
53template <typename ValueType, typename IndexType>
54class Csr;
55
56
57}
58
59
60namespace detail {
61
62
67template <typename Builder, typename ValueType, typename IndexType,
68 typename = void>
69struct is_matrix_type_builder : std::false_type {};
70
71
72template <typename Builder, typename ValueType, typename IndexType>
73struct is_matrix_type_builder<
74 Builder, ValueType, IndexType,
75 gko::xstd::void_t<
76 decltype(std::declval<Builder>().template create<ValueType, IndexType>(
77 std::declval<std::shared_ptr<const Executor>>()))>>
78 : std::true_type {};
79
80
81template <template <typename, typename> class MatrixType,
82 typename... CreateArgs>
83struct MatrixTypeBuilderFromValueAndIndex {
84 template <typename ValueType, typename IndexType, std::size_t... I>
85 auto create_impl(std::shared_ptr<const Executor> exec,
86 std::index_sequence<I...>)
87 {
88 return MatrixType<ValueType, IndexType>::create(
89 exec, std::get<I>(create_args)...);
90 }
91
92
93 template <typename ValueType, typename IndexType>
94 auto create(std::shared_ptr<const Executor> exec)
95 {
96 // with c++17 we could use std::apply
97 static constexpr auto size = sizeof...(CreateArgs);
99 std::move(exec), std::make_index_sequence<size>{});
100 }
101
102 std::tuple<CreateArgs...> create_args;
103};
104
105
106} // namespace detail
107
108
140template <template <typename, typename> class MatrixType, typename... Args>
141auto with_matrix_type(Args&&... create_args)
142{
143 return detail::MatrixTypeBuilderFromValueAndIndex<MatrixType, Args...>{
144 std::forward_as_tuple(create_args...)};
145}
146
147
148namespace experimental {
149namespace distributed {
150
151
152template <typename LocalIndexType, typename GlobalIndexType>
153class Partition;
154template <typename ValueType>
155class Vector;
156
157
262template <typename ValueType = default_precision,
263 typename LocalIndexType = int32, typename GlobalIndexType = int64>
265 : public EnableDistributedLinOp<
266 Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
267 public EnableCreateMethod<
268 Matrix<ValueType, LocalIndexType, GlobalIndexType>>,
269 public ConvertibleTo<
270 Matrix<next_precision<ValueType>, LocalIndexType, GlobalIndexType>>,
271 public DistributedBase {
272 friend class EnableCreateMethod<Matrix>;
274 friend class Matrix<next_precision<ValueType>, LocalIndexType,
276
277public:
278 using value_type = ValueType;
279 using index_type = GlobalIndexType;
280 using local_index_type = LocalIndexType;
281 using global_index_type = GlobalIndexType;
282 using global_vector_type =
284 using local_vector_type = typename global_vector_type::local_vector_type;
285
286 using EnableDistributedLinOp<Matrix>::convert_to;
287 using EnableDistributedLinOp<Matrix>::move_to;
289 GlobalIndexType>>::convert_to;
291 GlobalIndexType>>::move_to;
292
293 void convert_to(Matrix<next_precision<value_type>, local_index_type,
294 global_index_type>* result) const override;
295
296 void move_to(Matrix<next_precision<value_type>, local_index_type,
297 global_index_type>* result) override;
298
316 partition);
317
330 partition);
331
353
369
375 std::shared_ptr<const LinOp> get_local_matrix() const { return local_mtx_; }
376
382 std::shared_ptr<const LinOp> get_non_local_matrix() const
383 {
384 return non_local_mtx_;
385 }
386
393
399 Matrix(Matrix&& other) noexcept;
400
410
420
421protected:
429 explicit Matrix(std::shared_ptr<const Executor> exec,
431
450 template <typename MatrixType,
451 typename = std::enable_if_t<detail::is_matrix_type_builder<
452 MatrixType, ValueType, LocalIndexType>::value>>
453 explicit Matrix(std::shared_ptr<const Executor> exec,
455 : Matrix(
456 exec, comm,
457 matrix_template.template create<ValueType, LocalIndexType>(exec))
458 {}
459
486 template <typename LocalMatrixType, typename NonLocalMatrixType,
487 typename = std::enable_if_t<
488 detail::is_matrix_type_builder<LocalMatrixType, ValueType,
489 LocalIndexType>::value &&
490 detail::is_matrix_type_builder<NonLocalMatrixType, ValueType,
491 LocalIndexType>::value>>
492 explicit Matrix(std::shared_ptr<const Executor> exec,
496 : Matrix(
497 exec, comm,
499 exec),
501 .template create<ValueType, LocalIndexType>(exec))
502 {}
503
516 explicit Matrix(std::shared_ptr<const Executor> exec,
519
534 explicit Matrix(std::shared_ptr<const Executor> exec,
538
547 mpi::request communicate(const local_vector_type* local_b) const;
548
549 void apply_impl(const LinOp* b, LinOp* x) const override;
550
551 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
552 LinOp* x) const override;
553
554private:
555 std::vector<comm_index_type> send_offsets_;
556 std::vector<comm_index_type> send_sizes_;
557 std::vector<comm_index_type> recv_offsets_;
558 std::vector<comm_index_type> recv_sizes_;
559 array<local_index_type> gather_idxs_;
560 array<global_index_type> non_local_to_global_;
561 gko::detail::DenseCache<value_type> one_scalar_;
562 gko::detail::DenseCache<value_type> host_send_buffer_;
563 gko::detail::DenseCache<value_type> host_recv_buffer_;
564 gko::detail::DenseCache<value_type> send_buffer_;
565 gko::detail::DenseCache<value_type> recv_buffer_;
566 std::shared_ptr<LinOp> local_mtx_;
567 std::shared_ptr<LinOp> non_local_mtx_;
568};
569
570
571} // namespace distributed
572} // namespace experimental
573} // namespace gko
574
575
576#endif
577
578
579#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:499
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
Definition lin_op.hpp:146
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
This mixin does the same as EnableLinOp, but for concrete types that are derived from distributed::Di...
Definition lin_op.hpp:73
This mixin does the same as EnablePolymorphicObject, but for concrete types that are derived from dis...
Definition polymorphic_object.hpp:82
A base class for distributed objects.
Definition base.hpp:60
The Matrix class defines a (MPI-)distributed matrix.
Definition matrix.hpp:271
std::shared_ptr< const LinOp > get_non_local_matrix() const
Get read access to the stored non-local matrix.
Definition matrix.hpp:382
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.
std::shared_ptr< const LinOp > get_local_matrix() const
Get read access to the stored local matrix.
Definition matrix.hpp:375
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.
Matrix & operator=(Matrix &&other)
Move assigns a Matrix.
Matrix & operator=(const Matrix &other)
Copy assigns a Matrix.
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition partition.hpp:112
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition vector.hpp:92
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition mpi.hpp:437
The request class is a light, move-only wrapper around the MPI_Request handle.
Definition mpi.hpp:348
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:137
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
auto with_matrix_type(Args &&... create_args)
This function returns a type that delays a call to MatrixType::create.
Definition matrix.hpp:141
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:205
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:143
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155