Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
multivector.hpp
1 // SPDX-FileCopyrightText: 2025 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #pragma once
6 
7 #include <tuple>
8 #include <variant>
9 
10 #include <ginkgo/config.hpp>
11 #include <ginkgo/core/base/math.hpp>
12 #include <ginkgo/core/base/precision.hpp>
13 #include <ginkgo/core/base/range.hpp>
14 #include <ginkgo/core/base/temporary_conversion.hpp>
15 #include <ginkgo/core/base/type_traits.hpp>
16 #include <ginkgo/core/matrix/device_views.hpp>
17 
18 
19 namespace gko {
20 namespace matrix {
21 
22 
23 template <typename ValueType>
24 class MultiVector;
25 
26 
27 }
28 
29 
37 struct any_scalar {
38  using variant_type = syn::variant_from_list<supported_value_types>;
39 
40  template <
41  typename T,
42  std::enable_if_t<std::is_constructible_v<variant_type, T&&>, int> = 0>
43  any_scalar(T&& value) : variant(std::forward<T>(value))
44  {}
45 
46  // Allow constructing from int or similar
47  template <typename T,
48  std::enable_if_t<!std::is_constructible_v<variant_type, T&&> &&
49  std::is_convertible_v<T, double>,
50  int> = 1>
51  any_scalar(T&& value) : variant(static_cast<double>(value))
52  {}
53 
54  variant_type variant;
55 };
56 
57 
82 public:
83  template <typename ValueType>
85 
94  [[nodiscard]] static std::unique_ptr<AbstractMultiVector>
96 
108  [[nodiscard]] static std::unique_ptr<AbstractMultiVector>
110  std::shared_ptr<const Executor> exec);
111 
129  [[nodiscard]] static std::unique_ptr<AbstractMultiVector>
131  std::shared_ptr<const Executor> exec,
132  const dim<2>& global_size, const dim<2>& local_size);
133 
149  [[nodiscard]] static std::unique_ptr<AbstractMultiVector>
151  std::shared_ptr<const Executor> exec,
152  const dim<2>& global_size, const dim<2>& local_size,
153  size_type stride);
154 
162  [[nodiscard]] std::unique_ptr<AbstractMultiVector> clone(
163  std::shared_ptr<const Executor> exec) const;
164 
170  [[nodiscard]] std::unique_ptr<AbstractMultiVector> clone() const;
171 
183 
196 
205  [[nodiscard]] std::unique_ptr<AbstractMultiVector> create_default(
206  std::shared_ptr<const Executor> exec) const;
207 
214  [[nodiscard]] std::unique_ptr<AbstractMultiVector> create_default() const;
215 
225  [[nodiscard]] std::unique_ptr<AbstractMultiVector> compute_absolute() const;
226 
241 
249 
257  [[nodiscard]] std::unique_ptr<AbstractMultiVector> make_complex() const;
258 
272  void make_complex(ptr_param<AbstractMultiVector> result) const;
273 
279  [[nodiscard]] std::unique_ptr<AbstractMultiVector> get_real() const;
280 
292  void get_real(ptr_param<AbstractMultiVector> result) const;
293 
301  [[nodiscard]] std::unique_ptr<AbstractMultiVector> get_imag() const;
302 
316  void get_imag(ptr_param<AbstractMultiVector> result) const;
317 
323  void fill(any_scalar value);
324 
338 
352 
368 
384 
396  ptr_param<AbstractMultiVector> result) const;
397 
413  array<char>& tmp) const;
414 
426  ptr_param<AbstractMultiVector> result) const;
427 
443  array<char>& tmp) const;
444 
455 
469  array<char>& tmp) const;
470 
481 
495  array<char>& tmp) const;
496 
507 
521  array<char>& tmp) const;
522 
530  [[nodiscard]] std::unique_ptr<const AbstractMultiVector> create_real_view()
531  const;
532 
534  [[nodiscard]] std::unique_ptr<AbstractMultiVector> create_real_view();
535 
542  [[nodiscard]] std::unique_ptr<AbstractMultiVector> create_subview(
543  local_span rows, local_span columns);
544 
551  [[nodiscard]] std::unique_ptr<const AbstractMultiVector> create_subview(
552  local_span rows, local_span columns) const;
553 
555  [[nodiscard]] std::unique_ptr<AbstractMultiVector> create_subview(
556  local_span rows, local_span columns, dim<2> global_size);
557 
561  [[nodiscard]] std::unique_ptr<const AbstractMultiVector> create_subview(
562  local_span rows, local_span columns, dim<2> global_size) const;
563 
575  template <typename ValueType>
577 
579  template <typename ValueType>
581  const;
582 
598  precision p);
599 
610 
618  precision p) const;
619 
623 
629  [[nodiscard]] precision get_precision() const noexcept;
630 
636  [[nodiscard]] dim<2> get_size() const noexcept;
637 
643  [[nodiscard]] dim<2> get_local_size() const noexcept;
644 
646 
647  AbstractMultiVector(AbstractMultiVector&& other) noexcept;
648 
655 
662 
663 protected:
664  explicit AbstractMultiVector(std::shared_ptr<const Executor> exec,
665  const dim<2>& size, const dim<2>& local_size,
666  precision p);
667 
668  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
669  create_generic_with_same_config_impl() const = 0;
670 
671  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
672  create_generic_with_type_of_impl(std::shared_ptr<const Executor> exec,
673  const dim<2>& global_size,
674  const dim<2>& local_size,
675  size_type stride) const = 0;
676 
677  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
678  compute_absolute_generic_impl() const = 0;
679 
680  virtual void compute_absolute_generic_impl(
681  AbstractMultiVector* result) const = 0;
682 
683  virtual void compute_absolute_inplace_impl() = 0;
684 
685  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
686  make_complex_generic_impl() const = 0;
687 
688  virtual void make_complex_generic_impl(
689  AbstractMultiVector* result) const = 0;
690 
691  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
692  get_real_generic_impl() const = 0;
693 
694  virtual void get_real_generic_impl(AbstractMultiVector* result) const = 0;
695 
696  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
697  get_imag_generic_impl() const = 0;
698 
699  virtual void get_imag_generic_impl(AbstractMultiVector* result) const = 0;
700 
701  virtual void fill_impl(any_scalar value) = 0;
702 
703  virtual void scale_impl(const AbstractMultiVector* alpha) = 0;
704 
705  virtual void inv_scale_impl(const AbstractMultiVector* alpha) = 0;
706 
707  virtual void add_scaled_impl(const AbstractMultiVector* alpha,
708  const AbstractMultiVector* b) = 0;
709 
710  virtual void sub_scaled_impl(const AbstractMultiVector* alpha,
711  const AbstractMultiVector* b) = 0;
712 
713  virtual void compute_dot_impl(const AbstractMultiVector* b,
714  AbstractMultiVector* result,
715  array<char>& tmp) const = 0;
716 
717  virtual void compute_conj_dot_impl(const AbstractMultiVector* b,
718  AbstractMultiVector* result,
719  array<char>& tmp) const = 0;
720 
721  virtual void compute_norm2_impl(AbstractMultiVector* result,
722  array<char>& tmp) const = 0;
723 
724  virtual void compute_squared_norm2_impl(AbstractMultiVector* result,
725  array<char>& tmp) const = 0;
726 
727  virtual void compute_norm1_impl(AbstractMultiVector* result,
728  array<char>& tmp) const = 0;
729 
730  [[nodiscard]] virtual std::unique_ptr<const AbstractMultiVector>
731  create_real_view_generic_impl() const = 0;
732 
733  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
734  create_real_view_generic_impl() = 0;
735 
736  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
737  create_subview_generic_impl(local_span rows, local_span columns) = 0;
738 
739  [[nodiscard]] virtual std::unique_ptr<const AbstractMultiVector>
740  create_subview_generic_impl(local_span rows, local_span columns) const = 0;
741 
742  [[nodiscard]] virtual std::unique_ptr<AbstractMultiVector>
743  create_subview_generic_impl(local_span rows, local_span columns,
744  dim<2> global_size) = 0;
745 
746  [[nodiscard]] virtual std::unique_ptr<const AbstractMultiVector>
747  create_subview_generic_impl(local_span rows, local_span columns,
748  dim<2> global_size) const = 0;
749 
750  [[nodiscard]] virtual std::variant<
751 #if GINKGO_ENABLE_HALF
753 #endif
754 #if GINKGO_ENABLE_BFLOAT16
756 #endif
759  get_local_device_view_generic_impl() = 0;
760 
761  [[nodiscard]] virtual std::variant<
762 #if GINKGO_ENABLE_HALF
764 #endif
765 #if GINKGO_ENABLE_BFLOAT16
767 #endif
770  get_const_local_device_view_generic_impl() const = 0;
771 
772  [[nodiscard]] virtual temporary_conversion<AbstractMultiVector>
773  as_precision_impl(precision p) = 0;
774 
776  as_precision_impl(precision p) const = 0;
777 
778  void set_size(const dim<2>& global_size, const dim<2>& local_size);
779 
780 private:
781  dim<2> size_;
782  dim<2> local_size_;
783  precision precision_;
784 };
785 
786 
787 } // namespace gko
gko::matrix::view::dense
Non-owning view of a matrix::MultiVector to be used inside device kernels.
Definition: device_views.hpp:27
gko::AbstractMultiVector::get_local_size
dim< 2 > get_local_size() const noexcept
Gets the size (number of rows and columns) locally owned by this vector.
gko::AbstractMultiVector::clone
std::unique_ptr< AbstractMultiVector > clone() const
Creates a copy of this vector on this vector's executor.
gko::AbstractMultiVector::create_with_config_of
static std::unique_ptr< AbstractMultiVector > create_with_config_of(ptr_param< const AbstractMultiVector > other)
Creates an empty multi-vector with the same type, executor, size, stride and precision as other.
gko::AbstractMultiVector::compute_squared_norm2
void compute_squared_norm2(ptr_param< AbstractMultiVector > result) const
Computes the square of the column-wise Euclidean (L^2) norm of this.
gko::AbstractMultiVector::operator=
AbstractMultiVector & operator=(const AbstractMultiVector &other)
Copies the contents of another multi-vector.
gko::AbstractMultiVector::compute_norm2
void compute_norm2(ptr_param< AbstractMultiVector > result) const
Computes the column-wise Euclidean (L^2) norm of this.
gko::AbstractMultiVector::get_imag
std::unique_ptr< AbstractMultiVector > get_imag() const
Creates a new real vector with the imaginary parts of this vector.
gko::AbstractMultiVector::get_const_local_device_view
device_view< const ValueType > get_const_local_device_view() const
Gets a local device view of this vector for the specified value type.
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:46
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::AbstractMultiVector::compute_norm1
void compute_norm1(ptr_param< AbstractMultiVector > result) const
Computes the column-wise (L^1) norm of this.
gko::AbstractMultiVector::create_with_type_of
static std::unique_ptr< AbstractMultiVector > create_with_type_of(ptr_param< const AbstractMultiVector > other, std::shared_ptr< const Executor > exec)
Creates an empty multi-vector with the same type as other on exec.
gko::AbstractMultiVector::as_precision
temporary_conversion< AbstractMultiVector > as_precision(precision p)
Creates a temporary conversion of this vector with another precision.
gko::AbstractMultiVector::compute_dot
void compute_dot(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > result) const
Computes the column-wise dot product of this and b.
gko::AbstractMultiVector::create_real_view
std::unique_ptr< const AbstractMultiVector > create_real_view() const
Create a real view of the (potentially) complex original multi-vector.
gko::any_scalar
Helper type for type-erased scalar arguments.
Definition: multivector.hpp:37
gko::precision
precision
An enum to specify the precision of stored data.
Definition: precision.hpp:19
gko::AbstractMultiVector::inv_scale
void inv_scale(ptr_param< const AbstractMultiVector > alpha)
Divides this multi-vector element-wise by alpha.
gko::AbstractMultiVector::get_size
dim< 2 > get_size() const noexcept
Gets the size (number of rows and columns) of this vector.
gko::AbstractMultiVector::get_local_device_view
device_view< ValueType > get_local_device_view()
Gets a local device view of this vector for the specified value type.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::array< char >
gko::dim< 2 >
gko::AbstractMultiVector::make_complex
std::unique_ptr< AbstractMultiVector > make_complex() const
Creates a complex copy of this vector.
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::AbstractMultiVector::add_scaled
void add_scaled(ptr_param< const AbstractMultiVector > alpha, ptr_param< const AbstractMultiVector > b)
Adds b scaled by alpha to this (aka: BLAS axpy).
gko::AbstractMultiVector::sub_scaled
void sub_scaled(ptr_param< const AbstractMultiVector > alpha, ptr_param< const AbstractMultiVector > b)
Subtracts b scaled by alpha from this (aka: BLAS axpy).
gko::AbstractMultiVector::get_real
std::unique_ptr< AbstractMultiVector > get_real() const
Creates a new real vector with the real parts of this vector.
gko::AbstractMultiVector::create_subview
std::unique_ptr< AbstractMultiVector > create_subview(local_span rows, local_span columns)
Creates a view containing the selected local rows and columns.
gko::AbstractMultiVector::create_default
std::unique_ptr< AbstractMultiVector > create_default() const
Creates an empty vector with the same type as this vector on this vector's executor.
gko::Cloneable
Interface for objects that can be cloned.
Definition: polymorphic_object.hpp:305
gko::AbstractMultiVector::compute_absolute_inplace
void compute_absolute_inplace()
Replaces each element of this vector with its absolute value.
gko::AbstractMultiVector::move_from
AbstractMultiVector * move_from(ptr_param< AbstractMultiVector > other)
Moves the data of other into this vector.
gko::temporary_conversion
A temporary_conversion is a special smart pointer-like object that is designed to hold an object temp...
Definition: temporary_conversion.hpp:129
gko::AbstractMultiVector::scale
void scale(ptr_param< const AbstractMultiVector > alpha)
Scales this multi-vector element-wise by alpha.
gko::AbstractMultiVector::copy_from
AbstractMultiVector * copy_from(ptr_param< const AbstractMultiVector > other)
Copies the data of other into this vector.
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
gko::local_span
Same as span, except that it only represents a range of local rows/columns, and that it is copyable/m...
Definition: range.hpp:138
gko::AbstractMultiVector::compute_absolute
std::unique_ptr< AbstractMultiVector > compute_absolute() const
Creates a new vector with the element-wise absolute values of this vector.
gko::AbstractMultiVector::get_precision
precision get_precision() const noexcept
Gets the precision of this vector.
gko::AbstractMultiVector::compute_conj_dot
void compute_conj_dot(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > result) const
Computes the column-wise dot product of conj(this) and b.
gko::AbstractMultiVector::fill
void fill(any_scalar value)
Fills this vector with a single value.