Ginkgo: ginkgo/core/base/batch_multi_vector.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
batch_multi_vector.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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
24namespace gko {
25namespace batch {
26
27
51template <typename ValueType = default_precision>
52 class MultiVector
53 : public EnablePolymorphicObject<MultiVector<ValueType>>,
54 public EnablePolymorphicAssignment<MultiVector<ValueType>>,
55#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
56 public ConvertibleTo<MultiVector<next_precision<ValueType, 2>>>,
57#endif
58#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
59 public ConvertibleTo<MultiVector<next_precision<ValueType, 3>>>,
60#endif
61 public ConvertibleTo<MultiVector<next_precision<ValueType>>> {
62 friend class EnablePolymorphicObject<MultiVector>;
63 friend class MultiVector<to_complex<ValueType>>;
64 friend class MultiVector<previous_precision<ValueType>>;
65 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
66
67public:
68 using EnablePolymorphicAssignment<MultiVector>::convert_to;
69 using EnablePolymorphicAssignment<MultiVector>::move_to;
70 using ConvertibleTo<MultiVector<next_precision<ValueType>>>::convert_to;
71 using ConvertibleTo<MultiVector<next_precision<ValueType>>>::move_to;
72
73 using value_type = ValueType;
74 using index_type = int32;
75 using unbatch_type = gko::matrix::Dense<ValueType>;
76 using absolute_type = remove_complex<MultiVector<ValueType>>;
77 using complex_type = to_complex<MultiVector<ValueType>>;
78
85 static std::unique_ptr<MultiVector> create_with_config_of(
86 ptr_param<const MultiVector> other);
87
88 void convert_to(
89 MultiVector<next_precision<ValueType>>* result) const override;
90
91 void move_to(MultiVector<next_precision<ValueType>>* result) override;
92
93#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
94 friend class MultiVector<previous_precision<ValueType, 2>>;
95 using ConvertibleTo<MultiVector<next_precision<ValueType, 2>>>::convert_to;
96 using ConvertibleTo<MultiVector<next_precision<ValueType, 2>>>::move_to;
97
98 void convert_to(
99 MultiVector<next_precision<ValueType, 2>>* result) const override;
100
101 void move_to(MultiVector<next_precision<ValueType, 2>>* result) override;
102#endif
103
104#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
105 friend class MultiVector<previous_precision<ValueType, 3>>;
106 using ConvertibleTo<MultiVector<next_precision<ValueType, 3>>>::convert_to;
107 using ConvertibleTo<MultiVector<next_precision<ValueType, 3>>>::move_to;
108
109 void convert_to(
110 MultiVector<next_precision<ValueType, 3>>* result) const override;
111
112 void move_to(MultiVector<next_precision<ValueType, 3>>* result) override;
113#endif
114
125 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
126
130 std::unique_ptr<const unbatch_type> create_const_view_for_item(
131 size_type item_id) const;
132
138 batch_dim<2> get_size() const { return batch_size_; }
139
145 size_type get_num_batch_items() const
146 {
147 return batch_size_.get_num_batch_items();
148 }
149
155 dim<2> get_common_size() const { return batch_size_.get_common_size(); }
156
162 value_type* get_values() noexcept { return values_.get_data(); }
163
171 const value_type* get_const_values() const noexcept
172 {
173 return values_.get_const_data();
174 }
175
184 value_type* get_values_for_item(size_type batch_id) noexcept
185 {
186 GKO_ASSERT(batch_id < this->get_num_batch_items());
187 return values_.get_data() + this->get_cumulative_offset(batch_id);
188 }
189
197 const value_type* get_const_values_for_item(
198 size_type batch_id) const noexcept
199 {
200 GKO_ASSERT(batch_id < this->get_num_batch_items());
201 return values_.get_const_data() + this->get_cumulative_offset(batch_id);
202 }
203
211 size_type get_num_stored_elements() const noexcept
212 {
213 return values_.get_size();
214 }
215
223 size_type get_cumulative_offset(size_type batch_id) const
224 {
225 return batch_id * this->get_common_size()[0] *
226 this->get_common_size()[1];
227 }
228
240 value_type& at(size_type batch_id, size_type row, size_type col)
241 {
242 GKO_ASSERT(batch_id < this->get_num_batch_items());
243 return values_.get_data()[linearize_index(batch_id, row, col)];
244 }
245
249 value_type at(size_type batch_id, size_type row, size_type col) const
250 {
251 GKO_ASSERT(batch_id < this->get_num_batch_items());
252 return values_.get_const_data()[linearize_index(batch_id, row, col)];
253 }
254
269 ValueType& at(size_type batch_id, size_type idx) noexcept
270 {
271 return values_.get_data()[linearize_index(batch_id, idx)];
272 }
273
277 ValueType at(size_type batch_id, size_type idx) const noexcept
278 {
279 return values_.get_const_data()[linearize_index(batch_id, idx)];
280 }
281
296 void scale(ptr_param<const MultiVector<ValueType>> alpha);
297
310 void add_scaled(ptr_param<const MultiVector<ValueType>> alpha,
311 ptr_param<const MultiVector<ValueType>> b);
312
321 void compute_dot(ptr_param<const MultiVector<ValueType>> b,
322 ptr_param<MultiVector<ValueType>> result) const;
323
334 void compute_conj_dot(ptr_param<const MultiVector<ValueType>> b,
335 ptr_param<MultiVector<ValueType>> result) const;
336
344 void compute_norm2(
345 ptr_param<MultiVector<remove_complex<ValueType>>> result) const;
346
356 static std::unique_ptr<MultiVector> create(
357 std::shared_ptr<const Executor> exec,
358 const batch_dim<2>& size = batch_dim<2>{});
359
372 static std::unique_ptr<MultiVector> create(
373 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
374 array<value_type> values);
375
380 template <typename InputValueType>
381 GKO_DEPRECATED(
382 "explicitly construct the gko::array argument instead of passing an "
383 "initializer list")
384 static std::unique_ptr<MultiVector> create(
385 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
386 std::initializer_list<InputValueType> values)
387 {
388 return create(exec, size, array<index_type>{exec, std::move(values)});
389 }
390
404 static std::unique_ptr<const MultiVector> create_const(
405 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
406 gko::detail::const_array_view<ValueType>&& values);
407
413 void fill(ValueType value);
414
415private:
416 inline size_type compute_num_elems(const batch_dim<2>& size)
417 {
418 return size.get_num_batch_items() * size.get_common_size()[0] *
419 size.get_common_size()[1];
420 }
421
422protected:
428 void set_size(const batch_dim<2>& value) noexcept;
429
430 MultiVector(std::shared_ptr<const Executor> exec,
431 const batch_dim<2>& size = batch_dim<2>{});
432
433 MultiVector(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
434 array<value_type> values);
435
443 std::unique_ptr<MultiVector> create_with_same_config() const;
444
445 size_type linearize_index(size_type batch, size_type row,
446 size_type col) const noexcept
447 {
448 return this->get_cumulative_offset(batch) +
449 row * batch_size_.get_common_size()[1] + col;
450 }
451
452 size_type linearize_index(size_type batch, size_type idx) const noexcept
453 {
454 return linearize_index(batch, idx / this->get_common_size()[1],
455 idx % this->get_common_size()[1]);
456 }
457
458private:
459 batch_dim<2> batch_size_;
460 array<value_type> values_;
461};
462
463
464} // namespace batch
465} // namespace gko
466
467
468#endif // GKO_PUBLIC_CORE_BASE_BATCH_MULTI_VECTOR_HPP_
gko::EnablePolymorphicAssignment
This mixin is used to enable a default PolymorphicObject::copy_from() implementation for objects that...
Definition polymorphic_object.hpp:743
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:61
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:184
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::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:223
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:197
gko::batch::MultiVector::scale
void scale(ptr_param< const MultiVector< ValueType > > alpha)
Scales the vector with a scalar (aka: BLAS scal).
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_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_multi_vector.hpp:162
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:155
gko::batch::MultiVector::create
static std::unique_ptr< MultiVector > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size, array< value_type > values)
Creates a MultiVector from an already allocated (and initialized) array.
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::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:269
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:145
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:211
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:277
gko::batch::MultiVector::get_size
batch_dim< 2 > get_size() const
Returns the batch size.
Definition batch_multi_vector.hpp:138
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::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:171
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::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::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::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:249
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::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:240
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::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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:264
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:107
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:283
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
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:473
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:466
std
STL namespace.
gko::batch_dim
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:27
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:43
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:36
gko::dim
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26