Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
vector.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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 
22 namespace gko {
23 namespace experimental {
24 namespace distributed {
25 namespace detail {
26 
27 
28 template <typename ValueType>
29 class VectorCache;
30 
31 
32 } // namespace detail
33 
34 
35 template <typename LocalIndexType, typename GlobalIndexType>
36 class Partition;
37 
38 
66 template <typename ValueType = double>
67 class Vector
68  : public LinOp,
69  public EnableCloneable<Vector<ValueType>>,
70  public ConvertibleTo<Vector<next_precision<ValueType>>>,
71 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
72  public ConvertibleTo<Vector<next_precision<ValueType, 2>>>,
73 #endif
74 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
75  public ConvertibleTo<Vector<next_precision<ValueType, 3>>>,
76 #endif
77  public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
78  public DistributedBase {
79  friend class EnableCloneable<Vector>;
80  friend class Vector<to_complex<ValueType>>;
81  friend class Vector<remove_complex<ValueType>>;
82  friend class Vector<previous_precision<ValueType>>;
83  friend class detail::VectorCache<ValueType>;
84  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
85 
86 public:
89  using ConvertibleTo<Vector<next_precision<ValueType>>>::convert_to;
90  using ConvertibleTo<Vector<next_precision<ValueType>>>::move_to;
91 
92  using value_type = ValueType;
93  using absolute_type = remove_complex<Vector>;
94  using real_type = absolute_type;
95  using complex_type = Vector<to_complex<value_type>>;
96  using local_vector_type = gko::matrix::Dense<value_type>;
97 
104  static std::unique_ptr<Vector> create_with_config_of(
105  ptr_param<const Vector> other);
106 
107 
119  static std::unique_ptr<Vector> create_with_type_of(
120  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
121 
134  static std::unique_ptr<Vector> create_with_type_of(
135  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
136  const dim<2>& global_size, const dim<2>& local_size, size_type stride);
137 
152  void read_distributed(const device_matrix_data<ValueType, int64>& data,
153  ptr_param<const Partition<int64, int64>> partition);
154 
155  void read_distributed(const device_matrix_data<ValueType, int64>& data,
156  ptr_param<const Partition<int32, int64>> partition);
157 
158  void read_distributed(const device_matrix_data<ValueType, int32>& data,
159  ptr_param<const Partition<int32, int32>> partition);
160 
170  void read_distributed(const matrix_data<ValueType, int64>& data,
171  ptr_param<const Partition<int64, int64>> partition);
172 
173  void read_distributed(const matrix_data<ValueType, int64>& data,
174  ptr_param<const Partition<int32, int64>> partition);
175 
176  void read_distributed(const matrix_data<ValueType, int32>& data,
177  ptr_param<const Partition<int32, int32>> partition);
178 
179  void convert_to(Vector<next_precision<ValueType>>* result) const override;
180 
181  void move_to(Vector<next_precision<ValueType>>* result) override;
182 
183 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
184  friend class Vector<previous_precision<ValueType, 2>>;
185  using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::convert_to;
186  using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::move_to;
187 
188  void convert_to(
189  Vector<next_precision<ValueType, 2>>* result) const override;
190 
191  void move_to(Vector<next_precision<ValueType, 2>>* result) override;
192 #endif
193 
194 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
195  friend class Vector<previous_precision<ValueType, 3>>;
196  using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::convert_to;
197  using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::move_to;
198 
199  void convert_to(
200  Vector<next_precision<ValueType, 3>>* result) const override;
201 
202  void move_to(Vector<next_precision<ValueType, 3>>* result) override;
203 #endif
204 
205  std::unique_ptr<absolute_type> compute_absolute() const override;
206 
207  void compute_absolute_inplace() override;
208 
213  std::unique_ptr<complex_type> make_complex() const;
214 
220  void make_complex(ptr_param<complex_type> result) const;
221 
226  std::unique_ptr<real_type> get_real() const;
227 
231  void get_real(ptr_param<real_type> result) const;
232 
237  std::unique_ptr<real_type> get_imag() const;
238 
243  void get_imag(ptr_param<real_type> result) const;
244 
250  void fill(ValueType value);
251 
261  void scale(ptr_param<const LinOp> alpha);
262 
272  void inv_scale(ptr_param<const LinOp> alpha);
273 
283  void add_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
284 
293  void sub_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
294 
304  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result) const;
305 
318  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
319  array<char>& tmp) const;
320 
330  void compute_conj_dot(ptr_param<const LinOp> b,
331  ptr_param<LinOp> result) const;
332 
345  void compute_conj_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
346  array<char>& tmp) const;
347 
356  void compute_squared_norm2(ptr_param<LinOp> result) const;
357 
369  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
370 
379  void compute_norm2(ptr_param<LinOp> result) const;
380 
392  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
393 
401  void compute_norm1(ptr_param<LinOp> result) const;
402 
414  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
415 
424  void compute_mean(ptr_param<LinOp> result) const;
425 
437  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
438 
449  value_type& at_local(size_type row, size_type col) noexcept;
450 
454  value_type at_local(size_type row, size_type col) const noexcept;
455 
470  ValueType& at_local(size_type idx) noexcept;
471 
475  ValueType at_local(size_type idx) const noexcept;
476 
482  value_type* get_local_values();
483 
491  const value_type* get_const_local_values() const;
492 
498  const local_vector_type* get_local_vector() const;
499 
507  std::unique_ptr<const real_type> create_real_view() const;
508 
512  std::unique_ptr<real_type> create_real_view();
513 
523  std::unique_ptr<Vector> create_submatrix(local_span rows,
524  local_span columns,
525  dim<2> global_size);
526 
527  size_type get_stride() const noexcept { return local_.get_stride(); }
528 
540  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
541  mpi::communicator comm,
542  dim<2> global_size, dim<2> local_size,
543  size_type stride);
544 
556  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
557  mpi::communicator comm,
558  dim<2> global_size = {},
559  dim<2> local_size = {});
560 
578  static std::unique_ptr<Vector> create(
579  std::shared_ptr<const Executor> exec, mpi::communicator comm,
580  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
581 
600  static std::unique_ptr<Vector> create(
601  std::shared_ptr<const Executor> exec, mpi::communicator comm,
602  std::unique_ptr<local_vector_type> local_vector);
603 
616  static std::unique_ptr<const Vector> create_const(
617  std::shared_ptr<const Executor> exec, mpi::communicator comm,
618  dim<2> global_size,
619  std::unique_ptr<const local_vector_type> local_vector);
620 
633  static std::unique_ptr<const Vector> create_const(
634  std::shared_ptr<const Executor> exec, mpi::communicator comm,
635  std::unique_ptr<const local_vector_type> local_vector);
636 
637 protected:
638  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
639  dim<2> global_size, dim<2> local_size, size_type stride);
640 
641  explicit Vector(std::shared_ptr<const Executor> exec,
642  mpi::communicator comm, dim<2> global_size = {},
643  dim<2> local_size = {});
644 
645  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
646  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
647 
648  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
649  std::unique_ptr<local_vector_type> local_vector);
650 
651  void resize(dim<2> global_size, dim<2> local_size);
652 
653  template <typename LocalIndexType, typename GlobalIndexType>
654  void read_distributed_impl(
655  const device_matrix_data<ValueType, GlobalIndexType>& data,
656  const Partition<LocalIndexType, GlobalIndexType>* partition);
657 
658  void apply_impl(const LinOp*, LinOp*) const override;
659 
660  void apply_impl(const LinOp*, const LinOp*, const LinOp*,
661  LinOp*) const override;
662 
669  virtual std::unique_ptr<Vector> create_with_same_config() const;
670 
683  virtual std::unique_ptr<Vector> create_with_type_of_impl(
684  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
685  const dim<2>& local_size, size_type stride) const;
686 
690  virtual std::unique_ptr<Vector> create_submatrix_impl(local_span rows,
691  local_span columns,
692  dim<2> global_size);
693 
694 private:
695  local_vector_type local_;
696  ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
697  ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
698 };
699 
700 
701 } // namespace distributed
702 } // namespace experimental
703 
704 
705 namespace detail {
706 
707 
708 template <typename TargetType>
709 struct conversion_target_helper;
710 
711 
721 template <typename ValueType>
722 struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
723  using target_type = experimental::distributed::Vector<ValueType>;
724  using source_type =
725  experimental::distributed::Vector<previous_precision<ValueType>>;
726 
727  static std::unique_ptr<target_type> create_empty(const source_type* source)
728  {
729  return target_type::create(source->get_executor(),
730  source->get_communicator());
731  }
732 
733  // Allow to create_empty of the same type
734  // For distributed case, next<next<V>> will be V in the candidate list.
735  // TODO: decide to whether to add this or add condition to the list
736  static std::unique_ptr<target_type> create_empty(const target_type* source)
737  {
738  return target_type::create(source->get_executor(),
739  source->get_communicator());
740  }
741 
742 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
743  using snd_source_type =
744  experimental::distributed::Vector<previous_precision<ValueType, 2>>;
745 
746  static std::unique_ptr<target_type> create_empty(
747  const snd_source_type* source)
748  {
749  return target_type::create(source->get_executor(),
750  source->get_communicator());
751  }
752 #endif
753 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
754  using trd_source_type =
755  experimental::distributed::Vector<previous_precision<ValueType, 3>>;
756 
757  static std::unique_ptr<target_type> create_empty(
758  const trd_source_type* source)
759  {
760  return target_type::create(source->get_executor(),
761  source->get_communicator());
762  }
763 #endif
764 };
765 
766 
767 } // namespace detail
768 } // namespace gko
769 
770 
771 #endif // GINKGO_BUILD_MPI
772 
773 
774 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
gko::experimental::distributed::Vector::create_submatrix
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.
gko::experimental::distributed::Vector::at_local
value_type & at_local(size_type row, size_type col) noexcept
Returns a single element of the multi-vector.
gko::matrix::Dense< value_type >
gko::experimental::distributed::Vector::make_complex
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of the original vectors.
gko::experimental::distributed::Vector::create_with_type_of
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.
gko::experimental::distributed::Vector::compute_squared_norm2
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...
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::experimental::distributed::Vector::create
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.
gko::experimental::distributed::Vector::read_distributed
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.
gko::experimental::distributed::Vector::create_real_view
std::unique_ptr< const real_type > create_real_view() const
Create a real view of the (potentially) complex original multi-vector.
gko::experimental::distributed::Vector::compute_norm1
void compute_norm1(ptr_param< LinOp > result) const
Computes the column-wise (L^1) norm of this (multi-)vector.
gko::experimental::distributed::Vector::get_real
std::unique_ptr< real_type > get_real() const
Creates new real vectors and extracts the real part of the original vectors into that.
gko::experimental::distributed::Vector::get_local_values
value_type * get_local_values()
Returns a pointer to the array of local values of the multi-vector.
gko::matrix::Dense::get_stride
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: dense.hpp:884
gko::experimental::distributed::Vector::get_local_vector
const local_vector_type * get_local_vector() const
Direct (read) access to the underlying local local_vector_type vectors.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::experimental::distributed::Vector::fill
void fill(ValueType value)
Fill the distributed vectors with a given value.
gko::experimental::distributed::Vector::compute_mean
void compute_mean(ptr_param< LinOp > result) const
Computes the column-wise mean of this (multi-)vector using a global reduction.
gko::experimental::distributed::Vector::create_with_config_of
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.
gko::experimental::distributed::Vector::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
gko::experimental::distributed::Vector::add_scaled
void add_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Adds b scaled by alpha to the vectors (aka: BLAS axpy).
gko::experimental::distributed::Vector::compute_dot
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.
gko::experimental::distributed::Vector::compute_conj_dot
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.
gko::EnableCloneable::convert_to
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition: polymorphic_object.hpp:404
gko::previous_precision
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
gko::experimental::distributed::Vector::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::experimental::distributed::Vector::create_const
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.
gko::experimental::distributed::Vector::get_imag
std::unique_ptr< real_type > get_imag() const
Creates new real vectors and extracts the imaginary part of the original vectors into that.
gko::experimental::distributed::Vector::compute_norm2
void compute_norm2(ptr_param< LinOp > result) const
Computes the Euclidean (L^2) norm of this (multi-)vector using a global reduction.
gko::experimental::distributed::Vector::get_const_local_values
const value_type * get_const_local_values() const
Returns a pointer to the array of local values of the multi-vector.
gko::EnableCloneable::move_to
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition: polymorphic_object.hpp:406
gko::remove_complex
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
gko::experimental::distributed::Vector::inv_scale
void inv_scale(ptr_param< const LinOp > alpha)
Scales the vectors with the inverse of a scalar.
gko::experimental::distributed::Vector::sub_scaled
void sub_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Subtracts b scaled by alpha from the vectors (aka: BLAS axpy).
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
gko::to_complex
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
gko::experimental::distributed::Vector::scale
void scale(ptr_param< const LinOp > alpha)
Scales the vectors with a scalar (aka: BLAS scal).