Ginkgo Generated from branch based on main. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
 
Loading...
Searching...
No Matches
batch_multi_vector.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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>
52class MultiVector
53 : public EnablePolymorphicObject<MultiVector<ValueType>>,
54 public EnablePolymorphicAssignment<MultiVector<ValueType>>,
55#if GINKGO_ENABLE_HALF
56 public ConvertibleTo<
57 MultiVector<next_precision<next_precision<ValueType>>>>,
58#endif
59 public ConvertibleTo<MultiVector<next_precision<ValueType>>> {
60 friend class EnablePolymorphicObject<MultiVector>;
61 friend class MultiVector<to_complex<ValueType>>;
62 friend class MultiVector<previous_precision<ValueType>>;
63
64public:
65 using EnablePolymorphicAssignment<MultiVector>::convert_to;
66 using EnablePolymorphicAssignment<MultiVector>::move_to;
67 using ConvertibleTo<MultiVector<next_precision<ValueType>>>::convert_to;
68 using ConvertibleTo<MultiVector<next_precision<ValueType>>>::move_to;
69
70 using value_type = ValueType;
71 using index_type = int32;
72 using unbatch_type = gko::matrix::Dense<ValueType>;
73 using absolute_type = remove_complex<MultiVector<ValueType>>;
74 using complex_type = to_complex<MultiVector<ValueType>>;
75
82 static std::unique_ptr<MultiVector> create_with_config_of(
84
85 void convert_to(
86 MultiVector<next_precision<ValueType>>* result) const override;
87
88 void move_to(MultiVector<next_precision<ValueType>>* result) override;
89
90#if GINKGO_ENABLE_HALF
91 friend class MultiVector<previous_precision<previous_precision<ValueType>>>;
92 using ConvertibleTo<
93 MultiVector<next_precision<next_precision<ValueType>>>>::convert_to;
94 using ConvertibleTo<
95 MultiVector<next_precision<next_precision<ValueType>>>>::move_to;
96
97 void convert_to(MultiVector<next_precision<next_precision<ValueType>>>*
98 result) const override;
99
100 void move_to(MultiVector<next_precision<next_precision<ValueType>>>* result)
101 override;
102#endif
103
114 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
115
119 std::unique_ptr<const unbatch_type> create_const_view_for_item(
120 size_type item_id) const;
121
127 batch_dim<2> get_size() const { return batch_size_; }
128
135 {
136 return batch_size_.get_num_batch_items();
137 }
138
144 dim<2> get_common_size() const { return batch_size_.get_common_size(); }
145
151 value_type* get_values() noexcept { return values_.get_data(); }
152
160 const value_type* get_const_values() const noexcept
161 {
162 return values_.get_const_data();
163 }
164
173 value_type* get_values_for_item(size_type batch_id) noexcept
174 {
175 GKO_ASSERT(batch_id < this->get_num_batch_items());
176 return values_.get_data() + this->get_cumulative_offset(batch_id);
177 }
178
186 const value_type* get_const_values_for_item(
187 size_type batch_id) const noexcept
188 {
189 GKO_ASSERT(batch_id < this->get_num_batch_items());
190 return values_.get_const_data() + this->get_cumulative_offset(batch_id);
191 }
192
201 {
202 return values_.get_size();
203 }
204
213 {
214 return batch_id * this->get_common_size()[0] *
215 this->get_common_size()[1];
216 }
217
229 value_type& at(size_type batch_id, size_type row, size_type col)
230 {
231 GKO_ASSERT(batch_id < this->get_num_batch_items());
232 return values_.get_data()[linearize_index(batch_id, row, col)];
233 }
234
238 value_type at(size_type batch_id, size_type row, size_type col) const
239 {
240 GKO_ASSERT(batch_id < this->get_num_batch_items());
241 return values_.get_const_data()[linearize_index(batch_id, row, col)];
242 }
243
258 ValueType& at(size_type batch_id, size_type idx) noexcept
259 {
260 return values_.get_data()[linearize_index(batch_id, idx)];
261 }
262
266 ValueType at(size_type batch_id, size_type idx) const noexcept
267 {
268 return values_.get_const_data()[linearize_index(batch_id, idx)];
269 }
270
285 void scale(ptr_param<const MultiVector<ValueType>> alpha);
286
299 void add_scaled(ptr_param<const MultiVector<ValueType>> alpha,
300 ptr_param<const MultiVector<ValueType>> b);
301
310 void compute_dot(ptr_param<const MultiVector<ValueType>> b,
311 ptr_param<MultiVector<ValueType>> result) const;
312
323 void compute_conj_dot(ptr_param<const MultiVector<ValueType>> b,
324 ptr_param<MultiVector<ValueType>> result) const;
325
334 ptr_param<MultiVector<remove_complex<ValueType>>> result) const;
335
345 static std::unique_ptr<MultiVector> create(
346 std::shared_ptr<const Executor> exec,
347 const batch_dim<2>& size = batch_dim<2>{});
348
361 static std::unique_ptr<MultiVector> create(
362 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
363 array<value_type> values);
364
369 template <typename InputValueType>
370 GKO_DEPRECATED(
371 "explicitly construct the gko::array argument instead of passing an "
372 "initializer list")
373 static std::unique_ptr<MultiVector> create(
374 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
375 std::initializer_list<InputValueType> values)
376 {
377 return create(exec, size, array<index_type>{exec, std::move(values)});
378 }
379
393 static std::unique_ptr<const MultiVector> create_const(
394 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
395 gko::detail::const_array_view<ValueType>&& values);
396
402 void fill(ValueType value);
403
404private:
405 inline size_type compute_num_elems(const batch_dim<2>& size)
406 {
407 return size.get_num_batch_items() * size.get_common_size()[0] *
408 size.get_common_size()[1];
409 }
410
411protected:
417 void set_size(const batch_dim<2>& value) noexcept;
418
419 MultiVector(std::shared_ptr<const Executor> exec,
420 const batch_dim<2>& size = batch_dim<2>{});
421
422 MultiVector(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
423 array<value_type> values);
424
432 std::unique_ptr<MultiVector> create_with_same_config() const;
433
434 size_type linearize_index(size_type batch, size_type row,
435 size_type col) const noexcept
436 {
437 return this->get_cumulative_offset(batch) +
438 row * batch_size_.get_common_size()[1] + col;
439 }
440
441 size_type linearize_index(size_type batch, size_type idx) const noexcept
442 {
443 return linearize_index(batch, idx / this->get_common_size()[1],
444 idx % this->get_common_size()[1]);
445 }
446
447private:
448 batch_dim<2> batch_size_;
449 array<value_type> values_;
450};
451
452
453} // namespace batch
454} // namespace gko
455
456
457#endif // GKO_PUBLIC_CORE_BASE_BATCH_MULTI_VECTOR_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:470
This mixin is used to enable a default PolymorphicObject::copy_from() implementation for objects that...
Definition polymorphic_object.hpp:723
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:662
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:59
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:173
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...
size_type get_cumulative_offset(size_type batch_id) const
Get the cumulative storage size offset.
Definition batch_multi_vector.hpp:212
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:186
void scale(ptr_param< const MultiVector< ValueType > > alpha)
Scales the vector with a scalar (aka: BLAS scal).
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.
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_multi_vector.hpp:151
dim< 2 > get_common_size() const
Returns the common size of the batch items.
Definition batch_multi_vector.hpp:144
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.
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...
void fill(ValueType value)
Fills the input MultiVector with a given value.
ValueType & at(size_type batch_id, size_type idx) noexcept
Returns a single element for a particular batch item.
Definition batch_multi_vector.hpp:258
size_type get_num_batch_items() const
Returns the number of batch items.
Definition batch_multi_vector.hpp:134
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:200
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:266
batch_dim< 2 > get_size() const
Returns the batch size.
Definition batch_multi_vector.hpp:127
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.
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:160
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.
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).
void compute_norm2(ptr_param< MultiVector< remove_complex< ValueType > > > result) const
Computes the Euclidean (L^2) norm of each multi-vector in this batch.
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:238
static std::unique_ptr< MultiVector > create_with_config_of(ptr_param< const MultiVector > other)
Creates a MultiVector with the configuration of another MultiVector.
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:229
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.
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:117
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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:260
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:106
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list with half.
Definition math.hpp:438
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:279
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
STL namespace.
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:27
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition batch_dim.hpp:43
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition batch_dim.hpp:36
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26