Ginkgo  Generated from pipelines/1706354773 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
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 
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 EnableLinOp<Vector<ValueType>>,
69  public ConvertibleTo<Vector<next_precision<ValueType>>>,
70 #if GINKGO_ENABLE_HALF
71  public ConvertibleTo<Vector<next_precision<next_precision<ValueType>>>>,
72 #endif
73  public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
74  public DistributedBase {
75  friend class EnablePolymorphicObject<Vector, LinOp>;
76  friend class Vector<to_complex<ValueType>>;
77  friend class Vector<remove_complex<ValueType>>;
78  friend class Vector<previous_precision<ValueType>>;
79  friend class detail::VectorCache<ValueType>;
80 
81 public:
84  using ConvertibleTo<Vector<next_precision<ValueType>>>::convert_to;
85  using ConvertibleTo<Vector<next_precision<ValueType>>>::move_to;
86 
87  using value_type = ValueType;
88  using absolute_type = remove_complex<Vector>;
89  using real_type = absolute_type;
90  using complex_type = Vector<to_complex<value_type>>;
91  using local_vector_type = gko::matrix::Dense<value_type>;
92 
99  static std::unique_ptr<Vector> create_with_config_of(
100  ptr_param<const Vector> other);
101 
102 
114  static std::unique_ptr<Vector> create_with_type_of(
115  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
116 
129  static std::unique_ptr<Vector> create_with_type_of(
130  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
131  const dim<2>& global_size, const dim<2>& local_size, size_type stride);
132 
147  void read_distributed(const device_matrix_data<ValueType, int64>& data,
148  ptr_param<const Partition<int64, int64>> partition);
149 
150  void read_distributed(const device_matrix_data<ValueType, int64>& data,
151  ptr_param<const Partition<int32, int64>> partition);
152 
153  void read_distributed(const device_matrix_data<ValueType, int32>& data,
154  ptr_param<const Partition<int32, int32>> partition);
155 
165  void read_distributed(const matrix_data<ValueType, int64>& data,
166  ptr_param<const Partition<int64, int64>> partition);
167 
168  void read_distributed(const matrix_data<ValueType, int64>& data,
169  ptr_param<const Partition<int32, int64>> partition);
170 
171  void read_distributed(const matrix_data<ValueType, int32>& data,
172  ptr_param<const Partition<int32, int32>> partition);
173 
174  void convert_to(Vector<next_precision<ValueType>>* result) const override;
175 
176  void move_to(Vector<next_precision<ValueType>>* result) override;
177 
178 #if GINKGO_ENABLE_HALF
179  friend class Vector<previous_precision<previous_precision<ValueType>>>;
180  using ConvertibleTo<
181  Vector<next_precision<next_precision<ValueType>>>>::convert_to;
182  using ConvertibleTo<
183  Vector<next_precision<next_precision<ValueType>>>>::move_to;
184 
185  void convert_to(Vector<next_precision<next_precision<ValueType>>>* result)
186  const override;
187 
188  void move_to(
189  Vector<next_precision<next_precision<ValueType>>>* result) override;
190 #endif
191 
192  std::unique_ptr<absolute_type> compute_absolute() const override;
193 
194  void compute_absolute_inplace() override;
195 
200  std::unique_ptr<complex_type> make_complex() const;
201 
207  void make_complex(ptr_param<complex_type> result) const;
208 
213  std::unique_ptr<real_type> get_real() const;
214 
218  void get_real(ptr_param<real_type> result) const;
219 
224  std::unique_ptr<real_type> get_imag() const;
225 
230  void get_imag(ptr_param<real_type> result) const;
231 
237  void fill(ValueType value);
238 
248  void scale(ptr_param<const LinOp> alpha);
249 
259  void inv_scale(ptr_param<const LinOp> alpha);
260 
270  void add_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
271 
280  void sub_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
281 
291  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result) const;
292 
305  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
306  array<char>& tmp) const;
307 
317  void compute_conj_dot(ptr_param<const LinOp> b,
318  ptr_param<LinOp> result) const;
319 
332  void compute_conj_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
333  array<char>& tmp) const;
334 
343  void compute_squared_norm2(ptr_param<LinOp> result) const;
344 
356  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
357 
366  void compute_norm2(ptr_param<LinOp> result) const;
367 
379  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
380 
388  void compute_norm1(ptr_param<LinOp> result) const;
389 
401  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
402 
411  void compute_mean(ptr_param<LinOp> result) const;
412 
424  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
425 
436  value_type& at_local(size_type row, size_type col) noexcept;
437 
441  value_type at_local(size_type row, size_type col) const noexcept;
442 
457  ValueType& at_local(size_type idx) noexcept;
458 
462  ValueType at_local(size_type idx) const noexcept;
463 
469  value_type* get_local_values();
470 
478  const value_type* get_const_local_values() const;
479 
485  const local_vector_type* get_local_vector() const;
486 
494  std::unique_ptr<const real_type> create_real_view() const;
495 
499  std::unique_ptr<real_type> create_real_view();
500 
501  size_type get_stride() const noexcept { return local_.get_stride(); }
502 
514  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
515  mpi::communicator comm,
516  dim<2> global_size, dim<2> local_size,
517  size_type stride);
518 
530  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
531  mpi::communicator comm,
532  dim<2> global_size = {},
533  dim<2> local_size = {});
534 
552  static std::unique_ptr<Vector> create(
553  std::shared_ptr<const Executor> exec, mpi::communicator comm,
554  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
555 
574  static std::unique_ptr<Vector> create(
575  std::shared_ptr<const Executor> exec, mpi::communicator comm,
576  std::unique_ptr<local_vector_type> local_vector);
577 
590  static std::unique_ptr<const Vector> create_const(
591  std::shared_ptr<const Executor> exec, mpi::communicator comm,
592  dim<2> global_size,
593  std::unique_ptr<const local_vector_type> local_vector);
594 
607  static std::unique_ptr<const Vector> create_const(
608  std::shared_ptr<const Executor> exec, mpi::communicator comm,
609  std::unique_ptr<const local_vector_type> local_vector);
610 
611 protected:
612  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
613  dim<2> global_size, dim<2> local_size, size_type stride);
614 
615  explicit Vector(std::shared_ptr<const Executor> exec,
616  mpi::communicator comm, dim<2> global_size = {},
617  dim<2> local_size = {});
618 
619  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
620  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
621 
622  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
623  std::unique_ptr<local_vector_type> local_vector);
624 
625  void resize(dim<2> global_size, dim<2> local_size);
626 
627  template <typename LocalIndexType, typename GlobalIndexType>
628  void read_distributed_impl(
629  const device_matrix_data<ValueType, GlobalIndexType>& data,
630  const Partition<LocalIndexType, GlobalIndexType>* partition);
631 
632  void apply_impl(const LinOp*, LinOp*) const override;
633 
634  void apply_impl(const LinOp*, const LinOp*, const LinOp*,
635  LinOp*) const override;
636 
643  virtual std::unique_ptr<Vector> create_with_same_config() const;
644 
657  virtual std::unique_ptr<Vector> create_with_type_of_impl(
658  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
659  const dim<2>& local_size, size_type stride) const;
660 
661 private:
662  local_vector_type local_;
663  ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
664  ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
665 };
666 
667 
668 } // namespace distributed
669 } // namespace experimental
670 
671 
672 namespace detail {
673 
674 
675 template <typename TargetType>
676 struct conversion_target_helper;
677 
678 
688 template <typename ValueType>
689 struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
690  using target_type = experimental::distributed::Vector<ValueType>;
691  using source_type =
692  experimental::distributed::Vector<previous_precision<ValueType>>;
693 
694  static std::unique_ptr<target_type> create_empty(const source_type* source)
695  {
696  return target_type::create(source->get_executor(),
697  source->get_communicator());
698  }
699 
700  // Allow to create_empty of the same type
701  // For distributed case, next<next<V>> will be V in the candidate list.
702  // TODO: decide to whether to add this or add condition to the list
703  static std::unique_ptr<target_type> create_empty(const target_type* source)
704  {
705  return target_type::create(source->get_executor(),
706  source->get_communicator());
707  }
708 
709 #if GINKGO_ENABLE_HALF
710  using snd_source_type = experimental::distributed::Vector<
711  previous_precision<previous_precision<ValueType>>>;
712 
713  static std::unique_ptr<target_type> create_empty(
714  const snd_source_type* source)
715  {
716  return target_type::create(source->get_executor(),
717  source->get_communicator());
718  }
719 #endif
720 };
721 
722 
723 } // namespace detail
724 } // namespace gko
725 
726 
727 #endif // GINKGO_BUILD_MPI
728 
729 
730 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
gko::EnablePolymorphicAssignment< ConcreteLinOp >::move_to
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:751
gko::EnablePolymorphicAssignment< ConcreteLinOp >::convert_to
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:749
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:89
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:870
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::next_precision
next_precision_base< T > next_precision
Obtains the next type in the singly-linked precision list with half.
Definition: math.hpp:445
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::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::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:260
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:279
gko::experimental::distributed::Vector::scale
void scale(ptr_param< const LinOp > alpha)
Scales the vectors with a scalar (aka: BLAS scal).