Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
batch_ell.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
7 
8 
9 #include <initializer_list>
10 #include <vector>
11 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/batch_lin_op.hpp>
14 #include <ginkgo/core/base/batch_multi_vector.hpp>
15 #include <ginkgo/core/base/executor.hpp>
16 #include <ginkgo/core/base/mtx_io.hpp>
17 #include <ginkgo/core/base/range_accessors.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils.hpp>
20 #include <ginkgo/core/base/utils_helper.hpp>
21 #include <ginkgo/core/matrix/ell.hpp>
22 
23 
24 namespace gko {
25 namespace batch {
26 namespace matrix {
27 
28 
51 template <typename ValueType = default_precision, typename IndexType = int32>
52 class Ell final
53  : public BatchLinOp,
54  public EnableCloneable<Ell<ValueType, IndexType>>,
55 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
56  public ConvertibleTo<Ell<next_precision<ValueType, 2>, IndexType>>,
57 #endif
58 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
59  public ConvertibleTo<Ell<next_precision<ValueType, 3>, IndexType>>,
60 #endif
61  public ConvertibleTo<Ell<next_precision<ValueType>, IndexType>> {
62  friend class EnableCloneable<Ell>;
63  friend class Ell<to_complex<ValueType>, IndexType>;
64  friend class Ell<previous_precision<ValueType>, IndexType>;
65  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
66  static_assert(std::is_same<IndexType, int32>::value,
67  "IndexType must be a 32 bit integer");
68 
69 public:
72 
73  using value_type = ValueType;
74  using index_type = IndexType;
76  using absolute_type = remove_complex<Ell>;
77  using complex_type = to_complex<Ell>;
78 
79  void convert_to(
80  Ell<next_precision<ValueType>, IndexType>* result) const override;
81 
82  void move_to(Ell<next_precision<ValueType>, IndexType>* result) override;
83 
84 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
85  friend class Ell<previous_precision<ValueType, 2>, IndexType>;
86  using ConvertibleTo<
87  Ell<next_precision<ValueType, 2>, IndexType>>::convert_to;
88  using ConvertibleTo<Ell<next_precision<ValueType, 2>, IndexType>>::move_to;
89 
90  void convert_to(
91  Ell<next_precision<ValueType, 2>, IndexType>* result) const override;
92 
93  void move_to(Ell<next_precision<ValueType, 2>, IndexType>* result) override;
94 #endif
95 
96 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
97  friend class Ell<previous_precision<ValueType, 3>, IndexType>;
98  using ConvertibleTo<
99  Ell<next_precision<ValueType, 3>, IndexType>>::convert_to;
100  using ConvertibleTo<Ell<next_precision<ValueType, 3>, IndexType>>::move_to;
101 
102  void convert_to(
103  Ell<next_precision<ValueType, 3>, IndexType>* result) const override;
104 
105  void move_to(Ell<next_precision<ValueType, 3>, IndexType>* result) override;
106 #endif
107 
118  std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
119 
123  std::unique_ptr<const unbatch_type> create_const_view_for_item(
124  size_type item_id) const;
125 
131  value_type* get_values() noexcept { return values_.get_data(); }
132 
140  const value_type* get_const_values() const noexcept
141  {
142  return values_.get_const_data();
143  }
144 
150  index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
151 
159  const index_type* get_const_col_idxs() const noexcept
160  {
161  return col_idxs_.get_const_data();
162  }
163 
170  index_type get_num_stored_elements_per_row() const noexcept
171  {
172  return num_elems_per_row_;
173  }
174 
183  {
184  return values_.get_size();
185  }
186 
193  {
194  return this->get_num_stored_elements() / this->get_num_batch_items();
195  }
196 
205  index_type* get_col_idxs_for_item(size_type batch_id) noexcept
206  {
207  GKO_ASSERT(batch_id < this->get_num_batch_items());
208  return col_idxs_.get_data();
209  }
210 
218  const index_type* get_const_col_idxs_for_item(
219  size_type batch_id) const noexcept
220  {
221  GKO_ASSERT(batch_id < this->get_num_batch_items());
222  return col_idxs_.get_const_data();
223  }
224 
233  value_type* get_values_for_item(size_type batch_id) noexcept
234  {
235  GKO_ASSERT(batch_id < this->get_num_batch_items());
236  return values_.get_data() +
237  batch_id * this->get_num_elements_per_item();
238  }
239 
247  const value_type* get_const_values_for_item(
248  size_type batch_id) const noexcept
249  {
250  GKO_ASSERT(batch_id < this->get_num_batch_items());
251  return values_.get_const_data() +
252  batch_id * this->get_num_elements_per_item();
253  }
254 
264  static std::unique_ptr<Ell> create(
265  std::shared_ptr<const Executor> exec,
266  const batch_dim<2>& size = batch_dim<2>{},
267  const IndexType num_elems_per_row = 0);
268 
285  static std::unique_ptr<Ell> create(std::shared_ptr<const Executor> exec,
286  const batch_dim<2>& size,
287  const IndexType num_elems_per_row,
288  array<value_type> values,
289  array<index_type> col_idxs);
290 
296  template <typename InputValueType, typename ColIndexType>
297  GKO_DEPRECATED(
298  "explicitly construct the gko::array arguments instead of passing "
299  "initializer lists")
300  static std::unique_ptr<Ell> create(
301  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
302  const IndexType num_elems_per_row,
303  std::initializer_list<InputValueType> values,
304  std::initializer_list<ColIndexType> col_idxs)
305  {
306  return create(exec, size, num_elems_per_row,
307  array<value_type>{exec, std::move(values)},
308  array<index_type>{exec, std::move(col_idxs)});
309  }
310 
327  static std::unique_ptr<const Ell> create_const(
328  std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
329  const index_type num_elems_per_row,
330  gko::detail::const_array_view<value_type>&& values,
331  gko::detail::const_array_view<index_type>&& col_idxs);
332 
342 
353  void apply(ptr_param<const MultiVector<value_type>> alpha,
357 
363 
369  void apply(ptr_param<const MultiVector<value_type>> alpha,
373 
380  void scale(const array<value_type>& row_scale,
381  const array<value_type>& col_scale);
382 
394  ptr_param<const MultiVector<value_type>> beta);
395 
396 private:
397  size_type compute_num_elems(const batch_dim<2>& size,
398  IndexType num_elems_per_row)
399  {
400  return size.get_num_batch_items() * size.get_common_size()[0] *
401  num_elems_per_row;
402  }
403 
404  Ell(std::shared_ptr<const Executor> exec,
405  const batch_dim<2>& size = batch_dim<2>{},
406  const IndexType num_elems_per_row = 0);
407 
408  Ell(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
409  const IndexType num_elems_per_row, array<value_type> values,
410  array<index_type> col_idxs);
411 
412  void apply_impl(const MultiVector<value_type>* b,
413  MultiVector<value_type>* x) const;
414 
415  void apply_impl(const MultiVector<value_type>* alpha,
416  const MultiVector<value_type>* b,
417  const MultiVector<value_type>* beta,
418  MultiVector<value_type>* x) const;
419 
420  index_type num_elems_per_row_;
421  array<value_type> values_;
422  array<index_type> col_idxs_;
423 };
424 
425 
426 } // namespace matrix
427 } // namespace batch
428 } // namespace gko
429 
430 
431 #endif // GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
gko::batch::matrix::Ell::get_num_stored_elements_per_row
index_type get_num_stored_elements_per_row() const noexcept
Returns the number of elements per row explicitly stored.
Definition: batch_ell.hpp:170
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::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::matrix::Ell::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: batch_ell.hpp:131
gko::batch::matrix::Ell::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_ell.hpp:182
gko::batch::matrix::Ell
Ell is a sparse matrix format that stores the same number of nonzeros in each row,...
Definition: batch_ell.hpp:52
gko::batch::matrix::Ell::apply
void apply(ptr_param< const MultiVector< value_type >> b, ptr_param< MultiVector< value_type >> x)
Apply the matrix to a multi-vector.
gko::batch::BatchLinOp
Definition: batch_lin_op.hpp:59
gko::batch::BatchLinOp::get_num_batch_items
size_type get_num_batch_items() const noexcept
Returns the number of items in the batch operator.
Definition: batch_lin_op.hpp:66
gko::batch::matrix::Ell::scale
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::batch::matrix::Ell::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 matrix for a specific batch item.
Definition: batch_ell.hpp:233
gko::batch::matrix::Ell::get_col_idxs_for_item
index_type * get_col_idxs_for_item(size_type batch_id) noexcept
Returns a pointer to the array of col_idxs of the matrix.
Definition: batch_ell.hpp:205
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition: batch_multi_vector.hpp:52
gko::batch::matrix::Ell::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 matrix for a specific batch item.
Definition: batch_ell.hpp:247
gko::batch::matrix::Ell::get_const_col_idxs
const index_type * get_const_col_idxs() const noexcept
Returns a pointer to the array of column indices of the matrix.
Definition: batch_ell.hpp:159
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::batch::matrix::Ell::create_view_for_item
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of matrix::Ell type) of one item of the batch::matrix::Ell<value_type> object...
gko::array< value_type >
gko::batch::matrix::Ell::get_col_idxs
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition: batch_ell.hpp:150
gko::batch::matrix::Ell::add_scaled_identity
void add_scaled_identity(ptr_param< const MultiVector< value_type >> alpha, ptr_param< const MultiVector< value_type >> beta)
Performs the operation this = alpha*I + beta*this.
gko::batch_dim< 2 >
gko::batch::matrix::Ell::create
static std::unique_ptr< Ell > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{}, const IndexType num_elems_per_row=0)
Creates an uninitialized Ell matrix of the specified size.
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:687
gko::batch::matrix::Ell::create_const
static std::unique_ptr< const Ell > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, const index_type num_elems_per_row, gko::detail::const_array_view< value_type > &&values, gko::detail::const_array_view< index_type > &&col_idxs)
Creates a constant (immutable) batch ell matrix from a constant array.
gko::batch::matrix::Ell::get_const_col_idxs_for_item
const index_type * get_const_col_idxs_for_item(size_type batch_id) const noexcept
Returns a pointer to the array of col_idxs of the matrix.
Definition: batch_ell.hpp:218
gko::batch::matrix::Ell::get_num_elements_per_item
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition: batch_ell.hpp:192
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
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::EnableCloneable
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:369
gko::matrix::Ell
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:31
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:140
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
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:696
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:670
gko::batch::matrix::Ell::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::Ell type) of one item of the batch::matrix::Ell<value_type> object...
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::batch::matrix::Ell::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition: batch_ell.hpp:140
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