Ginkgo  Generated from pipelines/1554403166 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
vector.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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/mpi.hpp>
17 #include <ginkgo/core/distributed/base.hpp>
18 #include <ginkgo/core/distributed/lin_op.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 EnableDistributedLinOp<Vector<ValueType>>,
69  public ConvertibleTo<Vector<next_precision<ValueType>>>,
70  public EnableAbsoluteComputation<remove_complex<Vector<ValueType>>>,
71  public DistributedBase {
72  friend class EnableDistributedPolymorphicObject<Vector, LinOp>;
73  friend class Vector<to_complex<ValueType>>;
74  friend class Vector<remove_complex<ValueType>>;
75  friend class Vector<next_precision<ValueType>>;
76  friend class detail::VectorCache<ValueType>;
77 
78 public:
81  using ConvertibleTo<Vector<next_precision<ValueType>>>::convert_to;
82  using ConvertibleTo<Vector<next_precision<ValueType>>>::move_to;
83 
84  using value_type = ValueType;
85  using absolute_type = remove_complex<Vector>;
86  using real_type = absolute_type;
87  using complex_type = Vector<to_complex<value_type>>;
88  using local_vector_type = gko::matrix::Dense<value_type>;
89 
96  static std::unique_ptr<Vector> create_with_config_of(
97  ptr_param<const Vector> other);
98 
99 
111  static std::unique_ptr<Vector> create_with_type_of(
112  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec);
113 
126  static std::unique_ptr<Vector> create_with_type_of(
127  ptr_param<const Vector> other, std::shared_ptr<const Executor> exec,
128  const dim<2>& global_size, const dim<2>& local_size, size_type stride);
129 
144  void read_distributed(const device_matrix_data<ValueType, int64>& data,
145  ptr_param<const Partition<int64, int64>> partition);
146 
147  void read_distributed(const device_matrix_data<ValueType, int64>& data,
148  ptr_param<const Partition<int32, int64>> partition);
149 
150  void read_distributed(const device_matrix_data<ValueType, int32>& data,
151  ptr_param<const Partition<int32, int32>> partition);
152 
162  void read_distributed(const matrix_data<ValueType, int64>& data,
163  ptr_param<const Partition<int64, int64>> partition);
164 
165  void read_distributed(const matrix_data<ValueType, int64>& data,
166  ptr_param<const Partition<int32, int64>> partition);
167 
168  void read_distributed(const matrix_data<ValueType, int32>& data,
169  ptr_param<const Partition<int32, int32>> partition);
170 
171  void convert_to(Vector<next_precision<ValueType>>* result) const override;
172 
173  void move_to(Vector<next_precision<ValueType>>* result) override;
174 
175  std::unique_ptr<absolute_type> compute_absolute() const override;
176 
177  void compute_absolute_inplace() override;
178 
183  std::unique_ptr<complex_type> make_complex() const;
184 
190  void make_complex(ptr_param<complex_type> result) const;
191 
196  std::unique_ptr<real_type> get_real() const;
197 
201  void get_real(ptr_param<real_type> result) const;
202 
207  std::unique_ptr<real_type> get_imag() const;
208 
213  void get_imag(ptr_param<real_type> result) const;
214 
220  void fill(ValueType value);
221 
231  void scale(ptr_param<const LinOp> alpha);
232 
242  void inv_scale(ptr_param<const LinOp> alpha);
243 
253  void add_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
254 
263  void sub_scaled(ptr_param<const LinOp> alpha, ptr_param<const LinOp> b);
264 
274  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result) const;
275 
288  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
289  array<char>& tmp) const;
290 
300  void compute_conj_dot(ptr_param<const LinOp> b,
301  ptr_param<LinOp> result) const;
302 
315  void compute_conj_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result,
316  array<char>& tmp) const;
317 
326  void compute_squared_norm2(ptr_param<LinOp> result) const;
327 
339  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
340 
349  void compute_norm2(ptr_param<LinOp> result) const;
350 
362  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
363 
371  void compute_norm1(ptr_param<LinOp> result) const;
372 
384  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
385 
394  void compute_mean(ptr_param<LinOp> result) const;
395 
407  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
408 
419  value_type& at_local(size_type row, size_type col) noexcept;
420 
424  value_type at_local(size_type row, size_type col) const noexcept;
425 
440  ValueType& at_local(size_type idx) noexcept;
441 
445  ValueType at_local(size_type idx) const noexcept;
446 
452  value_type* get_local_values();
453 
461  const value_type* get_const_local_values() const;
462 
468  const local_vector_type* get_local_vector() const;
469 
477  std::unique_ptr<const real_type> create_real_view() const;
478 
482  std::unique_ptr<real_type> create_real_view();
483 
484  size_type get_stride() const noexcept { return local_.get_stride(); }
485 
497  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
498  mpi::communicator comm,
499  dim<2> global_size, dim<2> local_size,
500  size_type stride);
501 
513  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
514  mpi::communicator comm,
515  dim<2> global_size = {},
516  dim<2> local_size = {});
517 
535  static std::unique_ptr<Vector> create(
536  std::shared_ptr<const Executor> exec, mpi::communicator comm,
537  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
538 
557  static std::unique_ptr<Vector> create(
558  std::shared_ptr<const Executor> exec, mpi::communicator comm,
559  std::unique_ptr<local_vector_type> local_vector);
560 
573  static std::unique_ptr<const Vector> create_const(
574  std::shared_ptr<const Executor> exec, mpi::communicator comm,
575  dim<2> global_size,
576  std::unique_ptr<const 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  std::unique_ptr<const local_vector_type> local_vector);
593 
594 protected:
595  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
596  dim<2> global_size, dim<2> local_size, size_type stride);
597 
598  explicit Vector(std::shared_ptr<const Executor> exec,
599  mpi::communicator comm, dim<2> global_size = {},
600  dim<2> local_size = {});
601 
602  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
603  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
604 
605  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
606  std::unique_ptr<local_vector_type> local_vector);
607 
608  void resize(dim<2> global_size, dim<2> local_size);
609 
610  template <typename LocalIndexType, typename GlobalIndexType>
611  void read_distributed_impl(
612  const device_matrix_data<ValueType, GlobalIndexType>& data,
613  const Partition<LocalIndexType, GlobalIndexType>* partition);
614 
615  void apply_impl(const LinOp*, LinOp*) const override;
616 
617  void apply_impl(const LinOp*, const LinOp*, const LinOp*,
618  LinOp*) const override;
619 
626  virtual std::unique_ptr<Vector> create_with_same_config() const;
627 
640  virtual std::unique_ptr<Vector> create_with_type_of_impl(
641  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
642  const dim<2>& local_size, size_type stride) const;
643 
644 private:
645  local_vector_type local_;
646  ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
647  ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
648 };
649 
650 
651 } // namespace distributed
652 } // namespace experimental
653 
654 
655 namespace detail {
656 
657 
658 template <typename TargetType>
659 struct conversion_target_helper;
660 
661 
671 template <typename ValueType>
672 struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
673  using target_type = experimental::distributed::Vector<ValueType>;
674  using source_type =
675  experimental::distributed::Vector<previous_precision<ValueType>>;
676 
677  static std::unique_ptr<target_type> create_empty(const source_type* source)
678  {
679  return target_type::create(source->get_executor(),
680  source->get_communicator());
681  }
682 };
683 
684 
685 } // namespace detail
686 } // namespace gko
687 
688 
689 #endif // GINKGO_BUILD_MPI
690 
691 
692 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
gko::EnablePolymorphicAssignment< ConcreteLinOp >::move_to
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:731
gko::EnablePolymorphicAssignment< ConcreteLinOp >::convert_to
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:729
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:86
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:853
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::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:461
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::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:325
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:344
gko::experimental::distributed::Vector::scale
void scale(ptr_param< const LinOp > alpha)
Scales the vectors with a scalar (aka: BLAS scal).