Ginkgo Generated from branch based on main. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
vector.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
6#define GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_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/lin_op.hpp>
17#include <ginkgo/core/base/mpi.hpp>
18#include <ginkgo/core/distributed/base.hpp>
19#include <ginkgo/core/matrix/dense.hpp>
20
21
22namespace gko {
23namespace experimental {
24namespace distributed {
25namespace detail {
26
27
28template <typename ValueType>
29class VectorCache;
30
31
32} // namespace detail
33
34
35template <typename LocalIndexType, typename GlobalIndexType>
36class Partition;
37
38
66template <typename ValueType = double>
67class Vector
68 : public EnableLinOp<Vector<ValueType>>,
69 public ConvertibleTo<Vector<next_precision<ValueType>>>,
70#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
71 public ConvertibleTo<Vector<next_precision<ValueType, 2>>>,
72#endif
73#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
74 public ConvertibleTo<Vector<next_precision<ValueType, 3>>>,
75#endif
76 public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
77 public DistributedBase {
78 friend class EnablePolymorphicObject<Vector, LinOp>;
79 friend class Vector<to_complex<ValueType>>;
80 friend class Vector<remove_complex<ValueType>>;
81 friend class Vector<previous_precision<ValueType>>;
82 friend class detail::VectorCache<ValueType>;
83 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
84
85public:
86 using EnableLinOp<Vector>::convert_to;
87 using EnableLinOp<Vector>::move_to;
88 using ConvertibleTo<Vector<next_precision<ValueType>>>::convert_to;
89 using ConvertibleTo<Vector<next_precision<ValueType>>>::move_to;
90
91 using value_type = ValueType;
92 using absolute_type = remove_complex<Vector>;
93 using real_type = absolute_type;
94 using complex_type = Vector<to_complex<value_type>>;
95 using local_vector_type = gko::matrix::Dense<value_type>;
96
103 static std::unique_ptr<Vector> create_with_config_of(
105
106
118 static std::unique_ptr<Vector> create_with_type_of(
119 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
120
133 static std::unique_ptr<Vector> create_with_type_of(
134 ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
135 const dim<2>& global_size, const dim<2>& local_size, size_type stride);
136
152 ptr_param<const Partition<int64, int64>> partition);
153
155 ptr_param<const Partition<int32, int64>> partition);
156
158 ptr_param<const Partition<int32, int32>> partition);
159
170 ptr_param<const Partition<int64, int64>> partition);
171
173 ptr_param<const Partition<int32, int64>> partition);
174
176 ptr_param<const Partition<int32, int32>> partition);
177
178 void convert_to(Vector<next_precision<ValueType>>* result) const override;
179
180 void move_to(Vector<next_precision<ValueType>>* result) override;
181
182#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
183 friend class Vector<previous_precision<ValueType, 2>>;
184 using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::convert_to;
185 using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::move_to;
186
187 void convert_to(
188 Vector<next_precision<ValueType, 2>>* result) const override;
189
190 void move_to(Vector<next_precision<ValueType, 2>>* result) override;
191#endif
192
193#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
194 friend class Vector<previous_precision<ValueType, 3>>;
195 using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::convert_to;
196 using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::move_to;
197
198 void convert_to(
199 Vector<next_precision<ValueType, 3>>* result) const override;
200
201 void move_to(Vector<next_precision<ValueType, 3>>* result) override;
202#endif
203
204 std::unique_ptr<absolute_type> compute_absolute() const override;
205
207
212 std::unique_ptr<complex_type> make_complex() const;
213
220
225 std::unique_ptr<real_type> get_real() const;
226
230 void get_real(ptr_param<real_type> result) const;
231
236 std::unique_ptr<real_type> get_imag() const;
237
242 void get_imag(ptr_param<real_type> result) const;
243
249 void fill(ValueType value);
250
261
272
283
293
304
318 array<char>& tmp) const;
319
330 ptr_param<LinOp> result) const;
331
345 array<char>& tmp) const;
346
356
369
378 void compute_norm2(ptr_param<LinOp> result) const;
379
392
400 void compute_norm1(ptr_param<LinOp> result) const;
401
414
423 void compute_mean(ptr_param<LinOp> result) const;
424
436 void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
437
448 value_type& at_local(size_type row, size_type col) noexcept;
449
453 value_type at_local(size_type row, size_type col) const noexcept;
454
469 ValueType& at_local(size_type idx) noexcept;
470
474 ValueType at_local(size_type idx) const noexcept;
475
481 value_type* get_local_values();
482
490 const value_type* get_const_local_values() const;
491
497 const local_vector_type* get_local_vector() const;
498
506 std::unique_ptr<const real_type> create_real_view() const;
507
511 std::unique_ptr<real_type> create_real_view();
512
522 std::unique_ptr<Vector> create_submatrix(local_span rows,
523 local_span columns,
524 dim<2> global_size);
525
526 size_type get_stride() const noexcept { return local_.get_stride(); }
527
539 static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
541 dim<2> global_size, dim<2> local_size,
542 size_type stride);
543
555 static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
557 dim<2> global_size = {},
558 dim<2> local_size = {});
559
577 static std::unique_ptr<Vector> create(
578 std::shared_ptr<const Executor> exec, mpi::communicator comm,
579 dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
580
599 static std::unique_ptr<Vector> create(
600 std::shared_ptr<const Executor> exec, mpi::communicator comm,
601 std::unique_ptr<local_vector_type> local_vector);
602
615 static std::unique_ptr<const Vector> create_const(
616 std::shared_ptr<const Executor> exec, mpi::communicator comm,
617 dim<2> global_size,
618 std::unique_ptr<const local_vector_type> local_vector);
619
632 static std::unique_ptr<const Vector> create_const(
633 std::shared_ptr<const Executor> exec, mpi::communicator comm,
634 std::unique_ptr<const local_vector_type> local_vector);
635
636protected:
637 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
638 dim<2> global_size, dim<2> local_size, size_type stride);
639
640 explicit Vector(std::shared_ptr<const Executor> exec,
641 mpi::communicator comm, dim<2> global_size = {},
642 dim<2> local_size = {});
643
644 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
645 dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
646
647 Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
648 std::unique_ptr<local_vector_type> local_vector);
649
650 void resize(dim<2> global_size, dim<2> local_size);
651
652 template <typename LocalIndexType, typename GlobalIndexType>
653 void read_distributed_impl(
655 const Partition<LocalIndexType, GlobalIndexType>* partition);
656
657 void apply_impl(const LinOp*, LinOp*) const override;
658
659 void apply_impl(const LinOp*, const LinOp*, const LinOp*,
660 LinOp*) const override;
661
668 virtual std::unique_ptr<Vector> create_with_same_config() const;
669
682 virtual std::unique_ptr<Vector> create_with_type_of_impl(
683 std::shared_ptr<const Executor> exec, const dim<2>& global_size,
684 const dim<2>& local_size, size_type stride) const;
685
689 virtual std::unique_ptr<Vector> create_submatrix_impl(local_span rows,
690 local_span columns,
691 dim<2> global_size);
692
693private:
694 local_vector_type local_;
695 ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
696 ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
697};
698
699
700} // namespace distributed
701} // namespace experimental
702
703
704namespace detail {
705
706
707template <typename TargetType>
708struct conversion_target_helper;
709
710
720template <typename ValueType>
721struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
722 using target_type = experimental::distributed::Vector<ValueType>;
723 using source_type =
724 experimental::distributed::Vector<previous_precision<ValueType>>;
725
726 static std::unique_ptr<target_type> create_empty(const source_type* source)
727 {
728 return target_type::create(source->get_executor(),
729 source->get_communicator());
730 }
731
732 // Allow to create_empty of the same type
733 // For distributed case, next<next<V>> will be V in the candidate list.
734 // TODO: decide to whether to add this or add condition to the list
735 static std::unique_ptr<target_type> create_empty(const target_type* source)
736 {
737 return target_type::create(source->get_executor(),
738 source->get_communicator());
739 }
740
741#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
742 using snd_source_type =
743 experimental::distributed::Vector<previous_precision<ValueType, 2>>;
744
745 static std::unique_ptr<target_type> create_empty(
746 const snd_source_type* source)
747 {
748 return target_type::create(source->get_executor(),
749 source->get_communicator());
750 }
751#endif
752#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
753 using trd_source_type =
754 experimental::distributed::Vector<previous_precision<ValueType, 3>>;
755
756 static std::unique_ptr<target_type> create_empty(
757 const trd_source_type* source)
758 {
759 return target_type::create(source->get_executor(),
760 source->get_communicator());
761 }
762#endif
763};
764
765
766} // namespace detail
767} // namespace gko
768
769
770#endif // GINKGO_BUILD_MPI
771
772
773#endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:794
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
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
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition partition.hpp:83
value_type at_local(size_type row, size_type col) const noexcept
void compute_mean(ptr_param< LinOp > result) const
Computes the column-wise mean of this (multi-)vector using a global reduction.
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, dim< 2 > local_size, size_type stride)
Creates an empty distributed vector with a specified size.
void compute_norm2(ptr_param< LinOp > result) const
Computes the Euclidean (L^2) norm of this (multi-)vector using a global reduction.
void read_distributed(const matrix_data< ValueType, int64 > &data, ptr_param< const Partition< int64, int64 > > partition)
Reads a vector from the matrix_data structure and a global row partition.
void make_complex(ptr_param< complex_type > result) const
Writes a complex copy of the original vectors to given complex vectors.
std::unique_ptr< real_type > create_real_view()
Create a real view of the (potentially) complex original multi-vector.
void compute_squared_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the square of the column-wise Euclidean ( ) norm of this (multi-)vector using a global reduc...
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size={}, dim< 2 > local_size={})
Creates an empty distributed vector with a specified size.
std::unique_ptr< real_type > get_real() const
Creates new real vectors and extracts the real part of the original vectors into that.
static std::unique_ptr< const Vector > create_const(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, std::unique_ptr< const local_vector_type > local_vector)
Creates a constant (immutable) distributed Vector from a constant local vector.
std::unique_ptr< const real_type > create_real_view() const
Create a real view of the (potentially) complex original multi-vector.
void fill(ValueType value)
Fill the distributed vectors with a given value.
static std::unique_ptr< Vector > create_with_type_of(ptr_param< const Vector > other, std::shared_ptr< const Executor > exec, const dim< 2 > &global_size, const dim< 2 > &local_size, size_type stride)
Creates an Vector with the same type as another Vector, but on a different executor and with a differ...
static std::unique_ptr< Vector > create_with_config_of(ptr_param< const Vector > other)
Creates a distributed Vector with the same size and stride as another Vector.
value_type & at_local(size_type row, size_type col) noexcept
Returns a single element of the multi-vector.
value_type * get_local_values()
Returns a pointer to the array of local values of the multi-vector.
void compute_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the Euclidean (L^2) norm of this (multi-)vector using a global reduction.
const value_type * get_const_local_values() const
Returns a pointer to the array of local values of the multi-vector.
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this (multi-)vector and conj(b) using a global reduction.
void compute_absolute_inplace() override
Compute absolute inplace on each element.
void get_real(ptr_param< real_type > result) const
Extracts the real part of the original vectors into given real vectors.
void compute_squared_norm2(ptr_param< LinOp > result) const
Computes the square of the column-wise Euclidean ( ) norm of this (multi-)vector using a global reduc...
void sub_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Subtracts b scaled by alpha from the vectors (aka: BLAS axpy).
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this (multi-)vector and b using a global reduction.
static std::unique_ptr< const Vector > create_const(std::shared_ptr< const Executor > exec, mpi::communicator comm, std::unique_ptr< const local_vector_type > local_vector)
Creates a constant (immutable) distributed Vector from a constant local vector.
void compute_mean(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise arithmetic mean of this (multi-)vector using a global reduction.
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of the original vectors.
void compute_norm1(ptr_param< LinOp > result) const
Computes the column-wise (L^1) norm of this (multi-)vector.
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, dim< 2 > global_size, std::unique_ptr< local_vector_type > local_vector)
Creates a distributed vector from local vectors with a specified size.
void get_imag(ptr_param< real_type > result) const
Extracts the imaginary part of the original vectors into given real vectors.
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
std::unique_ptr< Vector > create_submatrix(local_span rows, local_span columns, dim< 2 > global_size)
Creates a view of a submatrix of this vector.
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of this (multi-)vector and b using a global reduction.
std::unique_ptr< real_type > get_imag() const
Creates new real vectors and extracts the imaginary part of the original vectors into that.
void inv_scale(ptr_param< const LinOp > alpha)
Scales the vectors with the inverse of a scalar.
void add_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Adds b scaled by alpha to the vectors (aka: BLAS axpy).
void scale(ptr_param< const LinOp > alpha)
Scales the vectors with a scalar (aka: BLAS scal).
void read_distributed(const device_matrix_data< ValueType, int64 > &data, ptr_param< const Partition< int64, int64 > > partition)
Reads a vector from the device_matrix_data structure and a global row partition.
const local_vector_type * get_local_vector() const
Direct (read) access to the underlying local local_vector_type vectors.
static std::unique_ptr< Vector > create_with_type_of(ptr_param< const Vector > other, std::shared_ptr< const Executor > exec)
Creates an empty Vector with the same type as another Vector, but on a different executor.
ValueType & at_local(size_type idx) noexcept
Returns a single element of the multi-vector.
void compute_norm1(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise (L^1) norm of this (multi-)vector using a global reduction.
ValueType at_local(size_type idx) const noexcept
static std::unique_ptr< Vector > create(std::shared_ptr< const Executor > exec, mpi::communicator comm, std::unique_ptr< local_vector_type > local_vector)
Creates a distributed vector from local vectors.
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of this (multi-)vector and conj(b) using a global reduction.
A thin wrapper of MPI_Comm that supports most MPI calls.
Definition mpi.hpp:419
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
The distributed namespace.
Definition polymorphic_object.hpp:19
The Ginkgo namespace.
Definition abstract_factory.hpp:20
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:264
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition math.hpp:283
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
typename detail::find_precision_impl< T, -step >::type previous_precision
Obtains the previous move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:473
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:466
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
A span that is used exclusively for local numbering.
Definition range.hpp:138
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126