Ginkgo  Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
batch_multi_vector.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_BATCH_MULTI_VECTOR_HPP_
6 #define GKO_PUBLIC_CORE_BASE_BATCH_MULTI_VECTOR_HPP_
7 
8 
9 #include <initializer_list>
10 #include <vector>
11 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/batch_dim.hpp>
14 #include <ginkgo/core/base/dim.hpp>
15 #include <ginkgo/core/base/executor.hpp>
16 #include <ginkgo/core/base/mtx_io.hpp>
17 #include <ginkgo/core/base/polymorphic_object.hpp>
18 #include <ginkgo/core/base/range_accessors.hpp>
19 #include <ginkgo/core/base/types.hpp>
20 #include <ginkgo/core/base/utils.hpp>
21 #include <ginkgo/core/matrix/dense.hpp>
22 
23 
24 namespace gko {
25 namespace batch {
26 
27 
58 template <typename ValueType = default_precision>
60  : public PolymorphicObject,
61  public EnableCloneable<MultiVector<ValueType>>,
62 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
63  public ConvertibleTo<MultiVector<next_precision<ValueType, 2>>>,
64 #endif
65 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
66  public ConvertibleTo<MultiVector<next_precision<ValueType, 3>>>,
67 #endif
68  public ConvertibleTo<MultiVector<next_precision<ValueType>>> {
69  friend class EnableCloneable<MultiVector>;
70  friend class MultiVector<to_complex<ValueType>>;
71  friend class MultiVector<previous_precision<ValueType>>;
72  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
73 
74 public:
79 
80  using value_type = ValueType;
81  using index_type = int32;
83  using absolute_type = remove_complex<MultiVector<ValueType>>;
84  using complex_type = to_complex<MultiVector<ValueType>>;
85 
92  static std::unique_ptr<MultiVector> create_with_config_of(
94 
95  void convert_to(
96  MultiVector<next_precision<ValueType>>* result) const override;
97 
98  void move_to(MultiVector<next_precision<ValueType>>* result) override;
99 
100 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
101  friend class MultiVector<previous_precision<ValueType, 2>>;
104 
105  void convert_to(
106  MultiVector<next_precision<ValueType, 2>>* result) const override;
107 
108  void move_to(MultiVector<next_precision<ValueType, 2>>* result) override;
109 #endif
110 
111 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
112  friend class MultiVector<previous_precision<ValueType, 3>>;
115 
116  void convert_to(
117  MultiVector<next_precision<ValueType, 3>>* result) const override;
118 
119  void move_to(MultiVector<next_precision<ValueType, 3>>* result) override;
120 #endif
121 
132  std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
133 
137  std::unique_ptr<const unbatch_type> create_const_view_for_item(
138  size_type item_id) const;
139 
145  batch_dim<2> get_size() const { return batch_size_; }
146 
153  {
154  return batch_size_.get_num_batch_items();
155  }
156 
162  dim<2> get_common_size() const { return batch_size_.get_common_size(); }
163 
169  value_type* get_values() noexcept { return values_.get_data(); }
170 
178  const value_type* get_const_values() const noexcept
179  {
180  return values_.get_const_data();
181  }
182 
191  value_type* get_values_for_item(size_type batch_id) noexcept
192  {
193  GKO_ASSERT(batch_id < this->get_num_batch_items());
194  return values_.get_data() + this->get_cumulative_offset(batch_id);
195  }
196 
204  const value_type* get_const_values_for_item(
205  size_type batch_id) const noexcept
206  {
207  GKO_ASSERT(batch_id < this->get_num_batch_items());
208  return values_.get_const_data() + this->get_cumulative_offset(batch_id);
209  }
210 
219  {
220  return values_.get_size();
221  }
222 
231  {
232  return batch_id * this->get_common_size()[0] *
233  this->get_common_size()[1];
234  }
235 
247  value_type& at(size_type batch_id, size_type row, size_type col)
248  {
249  GKO_ASSERT(batch_id < this->get_num_batch_items());
250  return values_.get_data()[linearize_index(batch_id, row, col)];
251  }
252 
256  value_type at(size_type batch_id, size_type row, size_type col) const
257  {
258  GKO_ASSERT(batch_id < this->get_num_batch_items());
259  return values_.get_const_data()[linearize_index(batch_id, row, col)];
260  }
261 
276  ValueType& at(size_type batch_id, size_type idx) noexcept
277  {
278  return values_.get_data()[linearize_index(batch_id, idx)];
279  }
280 
284  ValueType at(size_type batch_id, size_type idx) const noexcept
285  {
286  return values_.get_const_data()[linearize_index(batch_id, idx)];
287  }
288 
303  void scale(ptr_param<const MultiVector<ValueType>> alpha);
304 
317  void add_scaled(ptr_param<const MultiVector<ValueType>> alpha,
319 
329  ptr_param<MultiVector<ValueType>> result) const;
330 
342  ptr_param<MultiVector<ValueType>> result) const;
343 
351  void compute_norm2(
353 
363  static std::unique_ptr<MultiVector> create(
364  std::shared_ptr<const Executor> exec,
365  const batch_dim<2>& size = batch_dim<2>{});
366 
379  static std::unique_ptr<MultiVector> create(
380  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
381  array<value_type> values);
382 
387  template <typename InputValueType>
388  GKO_DEPRECATED(
389  "explicitly construct the gko::array argument instead of passing an "
390  "initializer list")
391  static std::unique_ptr<MultiVector> create(
392  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
393  std::initializer_list<InputValueType> values)
394  {
395  return create(exec, size, array<index_type>{exec, std::move(values)});
396  }
397 
411  static std::unique_ptr<const MultiVector> create_const(
412  std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
413  gko::detail::const_array_view<ValueType>&& values);
414 
420  void fill(ValueType value);
421 
422 private:
423  inline size_type compute_num_elems(const batch_dim<2>& size)
424  {
425  return size.get_num_batch_items() * size.get_common_size()[0] *
426  size.get_common_size()[1];
427  }
428 
429 protected:
435  void set_size(const batch_dim<2>& value) noexcept;
436 
437  MultiVector(std::shared_ptr<const Executor> exec,
438  const batch_dim<2>& size = batch_dim<2>{});
439 
440  MultiVector(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
441  array<value_type> values);
442 
450  std::unique_ptr<MultiVector> create_with_same_config() const;
451 
452  size_type linearize_index(size_type batch, size_type row,
453  size_type col) const noexcept
454  {
455  return this->get_cumulative_offset(batch) +
456  row * batch_size_.get_common_size()[1] + col;
457  }
458 
459  size_type linearize_index(size_type batch, size_type idx) const noexcept
460  {
461  return linearize_index(batch, idx / this->get_common_size()[1],
462  idx % this->get_common_size()[1]);
463  }
464 
465 private:
466  batch_dim<2> batch_size_;
467  array<value_type> values_;
468 };
469 
470 
471 } // namespace batch
472 } // namespace gko
473 
474 
475 #endif // GKO_PUBLIC_CORE_BASE_BATCH_MULTI_VECTOR_HPP_
gko::batch_dim::get_num_batch_items
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition: batch_dim.hpp:35
gko::batch_dim::get_common_size
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition: batch_dim.hpp:42
gko::batch::MultiVector::at
value_type & at(size_type batch_id, size_type row, size_type col)
Returns a single element for a particular batch item.
Definition: batch_multi_vector.hpp:247
gko::batch::MultiVector::scale
void scale(ptr_param< const MultiVector< ValueType >> alpha)
Scales the vector with a scalar (aka: BLAS scal).
gko::batch::MultiVector::add_scaled
void add_scaled(ptr_param< const MultiVector< ValueType >> alpha, ptr_param< const MultiVector< ValueType >> b)
Adds b scaled by alpha to the vector (aka: BLAS axpy).
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::batch::MultiVector::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the batch matrix, cumulative across all the batch...
Definition: batch_multi_vector.hpp:218
gko::batch::MultiVector::compute_dot
void compute_dot(ptr_param< const MultiVector< ValueType >> b, ptr_param< MultiVector< ValueType >> result) const
Computes the column-wise dot product of each multi-vector in this batch and its corresponding entry i...
gko::batch::MultiVector::create_view_for_item
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of matrix::Dense type) of one item of the Batch MultiVector object.
gko::PolymorphicObject
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:46
gko::batch::MultiVector::at
ValueType at(size_type batch_id, size_type idx) const noexcept
Returns a single element for a particular batch item.
Definition: batch_multi_vector.hpp:284
gko::batch::MultiVector::get_const_values_for_item
const value_type * get_const_values_for_item(size_type batch_id) const noexcept
Returns a pointer to the array of values of the multi-vector for a specific batch item.
Definition: batch_multi_vector.hpp:204
gko::batch::MultiVector::get_values_for_item
value_type * get_values_for_item(size_type batch_id) noexcept
Returns a pointer to the array of values of the multi-vector for a specific batch item.
Definition: batch_multi_vector.hpp:191
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition: batch_multi_vector.hpp:59
gko::batch::MultiVector::fill
void fill(ValueType value)
Fills the input MultiVector with a given value.
gko::batch::MultiVector::at
ValueType & at(size_type batch_id, size_type idx) noexcept
Returns a single element for a particular batch item.
Definition: batch_multi_vector.hpp:276
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::batch::MultiVector::get_size
batch_dim< 2 > get_size() const
Returns the batch size.
Definition: batch_multi_vector.hpp:145
gko::batch::MultiVector::compute_conj_dot
void compute_conj_dot(ptr_param< const MultiVector< ValueType >> b, ptr_param< MultiVector< ValueType >> result) const
Computes the column-wise conjugate dot product of each multi-vector in this batch and its correspondi...
gko::array< value_type >
gko::batch::MultiVector::compute_norm2
void compute_norm2(ptr_param< MultiVector< remove_complex< ValueType >>> result) const
Computes the Euclidean (L^2) norm of each multi-vector in this batch.
gko::batch::MultiVector::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition: batch_multi_vector.hpp:169
gko::dim< 2 >
gko::batch::MultiVector::at
value_type at(size_type batch_id, size_type row, size_type col) const
Returns a single element for a particular batch item.
Definition: batch_multi_vector.hpp:256
gko::batch::MultiVector::create_const
static std::unique_ptr< const MultiVector > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, gko::detail::const_array_view< ValueType > &&values)
Creates a constant (immutable) batch multi-vector from a constant array.
gko::batch::MultiVector::get_common_size
dim< 2 > get_common_size() const
Returns the common size of the batch items.
Definition: batch_multi_vector.hpp:162
gko::batch::MultiVector::create_const_view_for_item
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) const
Creates a mutable view (of matrix::Dense type) of one item of the Batch MultiVector object.
gko::batch_dim< 2 >
gko::batch::MultiVector::get_num_batch_items
size_type get_num_batch_items() const
Returns the number of batch items.
Definition: batch_multi_vector.hpp:152
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:686
gko::batch::MultiVector::create
static std::unique_ptr< MultiVector > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{})
Creates an uninitialized multi-vector of the specified size.
gko::batch::MultiVector::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the multi-vector.
Definition: batch_multi_vector.hpp:178
gko::next_precision
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:465
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::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:376
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:118
gko::batch::MultiVector::create_with_config_of
static std::unique_ptr< MultiVector > create_with_config_of(ptr_param< const MultiVector > other)
Creates a MultiVector with the configuration of another MultiVector.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
gko::array::get_const_data
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:695
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
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::batch::MultiVector::get_cumulative_offset
size_type get_cumulative_offset(size_type batch_id) const
Get the cumulative storage size offset.
Definition: batch_multi_vector.hpp:230
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