Ginkgo  Generated from pipelines/3133041809 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/mpi.hpp>
17 #include <ginkgo/core/base/temporary_conversion.hpp>
18 #include <ginkgo/core/distributed/base.hpp>
19 #include <ginkgo/core/matrix/multivector.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 
63 template <typename ValueType = double>
64 class Vector : public EnableMultiVector<Vector<ValueType>>,
65  public ConvertibleTo<Vector<next_precision<ValueType>>>,
66 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
67  public ConvertibleTo<Vector<next_precision<ValueType, 2>>>,
68 #endif
69 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
70  public ConvertibleTo<Vector<next_precision<ValueType, 3>>>,
71 #endif
72  public DistributedBase {
73  friend class EnableMultiVector<Vector>;
74  friend class Vector<to_complex<ValueType>>;
75  friend class Vector<remove_complex<ValueType>>;
76  friend class Vector<previous_precision<ValueType>>;
77  friend class detail::VectorCache<ValueType>;
78  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
79 
80 public:
83  using ConvertibleTo<Vector<next_precision<ValueType>>>::convert_to;
84  using ConvertibleTo<Vector<next_precision<ValueType>>>::move_to;
85 
86  using value_type = typename EnableMultiVector<Vector>::value_type;
87  using absolute_type = typename EnableMultiVector<Vector>::absolute_type;
88  using real_type = typename EnableMultiVector<Vector>::real_type;
89  using complex_type = typename EnableMultiVector<Vector>::complex_type;
90  using norm_type = typename EnableMultiVector<Vector>::norm_type;
91  using device_view = typename EnableMultiVector<Vector>::device_view;
92  using const_device_view =
94  using local_vector_type = gko::matrix::MultiVector<value_type>;
95 
110  void read_distributed(const device_matrix_data<ValueType, int64>& data,
111  ptr_param<const Partition<int64, int64>> partition);
112 
113  void read_distributed(const device_matrix_data<ValueType, int64>& data,
114  ptr_param<const Partition<int32, int64>> partition);
115 
116  void read_distributed(const device_matrix_data<ValueType, int32>& data,
117  ptr_param<const Partition<int32, int32>> partition);
118 
128  void read_distributed(const matrix_data<ValueType, int64>& data,
129  ptr_param<const Partition<int64, int64>> partition);
130 
131  void read_distributed(const matrix_data<ValueType, int64>& data,
132  ptr_param<const Partition<int32, int64>> partition);
133 
134  void read_distributed(const matrix_data<ValueType, int32>& data,
135  ptr_param<const Partition<int32, int32>> partition);
136 
137  void convert_to(Vector<next_precision<ValueType>>* result) const override;
138 
139  void move_to(Vector<next_precision<ValueType>>* result) override;
140 
141 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
142  friend class Vector<previous_precision<ValueType, 2>>;
143  using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::convert_to;
144  using ConvertibleTo<Vector<next_precision<ValueType, 2>>>::move_to;
145 
146  void convert_to(
147  Vector<next_precision<ValueType, 2>>* result) const override;
148 
149  void move_to(Vector<next_precision<ValueType, 2>>* result) override;
150 #endif
151 
152 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
153  friend class Vector<previous_precision<ValueType, 3>>;
154  using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::convert_to;
155  using ConvertibleTo<Vector<next_precision<ValueType, 3>>>::move_to;
156 
157  void convert_to(
158  Vector<next_precision<ValueType, 3>>* result) const override;
159 
160  void move_to(Vector<next_precision<ValueType, 3>>* result) override;
161 #endif
162 
171  void compute_mean(ptr_param<AbstractMultiVector> result) const;
172 
184  void compute_mean(ptr_param<AbstractMultiVector> result,
185  array<char>& tmp) const;
186 
197  value_type& at_local(size_type row, size_type col) noexcept;
198 
202  value_type at_local(size_type row, size_type col) const noexcept;
203 
218  value_type& at_local(size_type idx) noexcept;
219 
223  value_type at_local(size_type idx) const noexcept;
224 
230  value_type* get_local_values();
231 
239  const value_type* get_const_local_values() const;
240 
246  const local_vector_type* get_local_vector() const;
247 
248  size_type get_stride() const noexcept { return local_.get_stride(); }
249 
262  template <typename OtherValueType,
263  typename = std::enable_if_t<is_complex<ValueType>() ==
264  is_complex<OtherValueType>()>>
265  [[nodiscard]] temporary_conversion<Vector<OtherValueType>> as_precision();
266 
279  template <typename OtherValueType,
280  typename = std::enable_if_t<is_complex<ValueType>() ==
281  is_complex<OtherValueType>()>>
282  [[nodiscard]] temporary_conversion<const Vector<OtherValueType>>
283  as_precision() const;
284 
296  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
297  mpi::communicator comm,
298  dim<2> global_size, dim<2> local_size,
299  size_type stride);
300 
312  static std::unique_ptr<Vector> create(std::shared_ptr<const Executor> exec,
313  mpi::communicator comm,
314  dim<2> global_size = {},
315  dim<2> local_size = {});
316 
334  static std::unique_ptr<Vector> create(
335  std::shared_ptr<const Executor> exec, mpi::communicator comm,
336  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
337 
356  static std::unique_ptr<Vector> create(
357  std::shared_ptr<const Executor> exec, mpi::communicator comm,
358  std::unique_ptr<local_vector_type> local_vector);
359 
372  static std::unique_ptr<const Vector> create_const(
373  std::shared_ptr<const Executor> exec, mpi::communicator comm,
374  dim<2> global_size,
375  std::unique_ptr<const local_vector_type> local_vector);
376 
389  static std::unique_ptr<const Vector> create_const(
390  std::shared_ptr<const Executor> exec, mpi::communicator comm,
391  std::unique_ptr<const local_vector_type> local_vector);
392 
393 protected:
394  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
395  dim<2> global_size, dim<2> local_size, size_type stride);
396 
397  explicit Vector(std::shared_ptr<const Executor> exec,
398  mpi::communicator comm, dim<2> global_size = {},
399  dim<2> local_size = {});
400 
401  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
402  dim<2> global_size, std::unique_ptr<local_vector_type> local_vector);
403 
404  Vector(std::shared_ptr<const Executor> exec, mpi::communicator comm,
405  std::unique_ptr<local_vector_type> local_vector);
406 
407  void resize(dim<2> global_size, dim<2> local_size);
408 
409  template <typename LocalIndexType, typename GlobalIndexType>
410  void read_distributed_impl(
411  const device_matrix_data<ValueType, GlobalIndexType>& data,
412  const Partition<LocalIndexType, GlobalIndexType>* partition);
413 
414  void compute_absolute_inplace_impl() override;
415 
416  [[nodiscard]] std::unique_ptr<Vector> create_with_same_config_impl()
417  const override;
418 
419  [[nodiscard]] std::unique_ptr<Vector> create_with_type_of_impl(
420  std::shared_ptr<const Executor> exec, const dim<2>& global_size,
421  const dim<2>& local_size, size_type stride) const override;
422 
423  [[nodiscard]] std::unique_ptr<Vector> create_subview_impl(
424  local_span rows, local_span columns) override;
425 
426  [[nodiscard]] std::unique_ptr<const Vector> create_subview_impl(
427  local_span rows, local_span columns) const override;
428 
429  [[nodiscard]] std::unique_ptr<Vector> create_subview_impl(
430  local_span rows, local_span columns, dim<2> global_size) override;
431 
432  [[nodiscard]] std::unique_ptr<const Vector> create_subview_impl(
433  local_span rows, local_span columns, dim<2> global_size) const override;
434 
435  [[nodiscard]] std::unique_ptr<const real_type> create_real_view_impl()
436  const override;
437 
438  [[nodiscard]] std::unique_ptr<real_type> create_real_view_impl() override;
439 
440  [[nodiscard]] std::unique_ptr<absolute_type> compute_absolute_impl()
441  const override;
442 
443  void compute_absolute_impl(absolute_type* result) const override;
444 
445  [[nodiscard]] std::unique_ptr<complex_type> make_complex_impl()
446  const override;
447 
448  [[nodiscard]] std::unique_ptr<real_type> get_real_impl() const override;
449 
450  [[nodiscard]] std::unique_ptr<real_type> get_imag_impl() const override;
451 
452  void make_complex_impl(complex_type* result) const override;
453 
454  void get_real_impl(real_type* result) const override;
455 
456  void get_imag_impl(real_type* result) const override;
457 
458  void fill_impl(value_type value) override;
459 
460  void scale_impl(scaling_param<value_type> alpha) override;
461 
462  void inv_scale_impl(scaling_param<value_type> alpha) override;
463 
464  void add_scaled_impl(scaling_param<value_type> alpha,
465  const Vector* b) override;
466 
467  void sub_scaled_impl(scaling_param<value_type> alpha,
468  const Vector* b) override;
469 
470  void compute_dot_impl(const Vector* b,
471  matrix::MultiVector<value_type>* result,
472  array<char>& tmp) const override;
473 
474  void compute_conj_dot_impl(const Vector* b,
475  matrix::MultiVector<value_type>* result,
476  array<char>& tmp) const override;
477 
478  void compute_norm2_impl(norm_type* result, array<char>& tmp) const override;
479 
480  void compute_squared_norm2_impl(norm_type* result,
481  array<char>& tmp) const override;
482 
483  void compute_norm1_impl(norm_type* result, array<char>& tmp) const override;
484 
485  device_view get_local_device_view_impl() override;
486 
487  const_device_view get_const_local_device_view_impl() const override;
488 
489 private:
490  local_vector_type local_;
491  ::gko::detail::DenseCache<ValueType> host_reduction_buffer_;
492  ::gko::detail::DenseCache<remove_complex<ValueType>> host_norm_buffer_;
493 };
494 
495 
496 } // namespace distributed
497 } // namespace experimental
498 
499 
500 namespace detail {
501 
502 
511 template <typename ValueType>
512 struct conversion_target_helper<experimental::distributed::Vector<ValueType>> {
513  template <typename SourceValueType,
514  typename = std::enable_if_t<is_complex<ValueType>() ==
515  is_complex<SourceValueType>()>>
516  static std::unique_ptr<experimental::distributed::Vector<ValueType>>
517  create_empty(
518  const experimental::distributed::Vector<SourceValueType>* source)
519  {
521  source->get_executor(), source->get_communicator());
522  }
523 };
524 
525 
526 } // namespace detail
527 } // namespace gko
528 
529 
530 #endif // GINKGO_BUILD_MPI
531 
532 
533 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_VECTOR_HPP_
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::EnableMultiVector::norm_type
matrix::MultiVector< absolute_value_type > norm_type
The vector type used to store the results of reductions to norms.
Definition: multivector_mixin.hpp:170
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::EnableMultiVector::const_device_view
AbstractMultiVector::device_view< const value_type > const_device_view
The immutable device view type of the local data.
Definition: multivector_mixin.hpp:175
gko::EnableMultiVector::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: multivector_mixin.hpp:892
gko::experimental::distributed::Vector::compute_mean
void compute_mean(ptr_param< AbstractMultiVector > result) const
Computes the column-wise mean of this (multi-)vector using a global reduction.
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::EnableMultiVector::real_type
typename traits::real_type real_type
The vector type holding real or imaginary parts of this vector.
Definition: multivector_mixin.hpp:164
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::EnableMultiVector::convert_to
void convert_to(result_type *result) const override
Definition: multivector_mixin.hpp:885
gko::matrix::MultiVector::get_stride
size_type get_stride() const noexcept
Returns the stride of the multivector.
Definition: multivector.hpp:644
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:19
gko::EnableMultiVector::absolute_type
typename traits::absolute_type absolute_type
The vector type holding absolute values of this vector.
Definition: multivector_mixin.hpp:162
gko::EnableMultiVector::device_view
AbstractMultiVector::device_view< value_type > device_view
The mutable device view type of the local data.
Definition: multivector_mixin.hpp:172
gko::matrix::MultiVector< value_type >
gko::EnableMultiVector::value_type
typename traits::value_type value_type
The value type of the stored elements.
Definition: multivector_mixin.hpp:158
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:472
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_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::experimental::distributed::Vector::as_precision
temporary_conversion< Vector< OtherValueType > > as_precision()
Converts the vector to the target precision type.
gko::EnableMultiVector::complex_type
typename traits::complex_type complex_type
The vector type holding a complex version of this vector.
Definition: multivector_mixin.hpp:166
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:263
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:282