Ginkgo  Generated from pipelines/1362335637 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
batch_ell.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 EnableBatchLinOp<Ell<ValueType, IndexType>>,
54  public ConvertibleTo<Ell<next_precision<ValueType>, IndexType>> {
55  friend class EnablePolymorphicObject<Ell, BatchLinOp>;
56  friend class Ell<to_complex<ValueType>, IndexType>;
57  friend class Ell<next_precision<ValueType>, IndexType>;
58  static_assert(std::is_same<IndexType, int32>::value,
59  "IndexType must be a 32 bit integer");
60 
61 public:
64 
65  using value_type = ValueType;
66  using index_type = IndexType;
68  using absolute_type = remove_complex<Ell>;
69  using complex_type = to_complex<Ell>;
70 
71  void convert_to(
72  Ell<next_precision<ValueType>, IndexType>* result) const override;
73 
74  void move_to(Ell<next_precision<ValueType>, IndexType>* result) override;
75 
86  std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
87 
91  std::unique_ptr<const unbatch_type> create_const_view_for_item(
92  size_type item_id) const;
93 
99  value_type* get_values() noexcept { return values_.get_data(); }
100 
108  const value_type* get_const_values() const noexcept
109  {
110  return values_.get_const_data();
111  }
112 
118  index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
119 
127  const index_type* get_const_col_idxs() const noexcept
128  {
129  return col_idxs_.get_const_data();
130  }
131 
138  index_type get_num_stored_elements_per_row() const noexcept
139  {
140  return num_elems_per_row_;
141  }
142 
151  {
152  return values_.get_size();
153  }
154 
161  {
162  return this->get_num_stored_elements() / this->get_num_batch_items();
163  }
164 
173  index_type* get_col_idxs_for_item(size_type batch_id) noexcept
174  {
175  GKO_ASSERT(batch_id < this->get_num_batch_items());
176  return col_idxs_.get_data();
177  }
178 
186  const index_type* get_const_col_idxs_for_item(
187  size_type batch_id) const noexcept
188  {
189  GKO_ASSERT(batch_id < this->get_num_batch_items());
190  return col_idxs_.get_const_data();
191  }
192 
201  value_type* get_values_for_item(size_type batch_id) noexcept
202  {
203  GKO_ASSERT(batch_id < this->get_num_batch_items());
204  return values_.get_data() +
205  batch_id * this->get_num_elements_per_item();
206  }
207 
215  const value_type* get_const_values_for_item(
216  size_type batch_id) const noexcept
217  {
218  GKO_ASSERT(batch_id < this->get_num_batch_items());
219  return values_.get_const_data() +
220  batch_id * this->get_num_elements_per_item();
221  }
222 
232  static std::unique_ptr<Ell> create(
233  std::shared_ptr<const Executor> exec,
234  const batch_dim<2>& size = batch_dim<2>{},
235  const IndexType num_elems_per_row = 0);
236 
253  static std::unique_ptr<Ell> create(std::shared_ptr<const Executor> exec,
254  const batch_dim<2>& size,
255  const IndexType num_elems_per_row,
256  array<value_type> values,
257  array<index_type> col_idxs);
258 
264  template <typename InputValueType, typename ColIndexType>
265  GKO_DEPRECATED(
266  "explicitly construct the gko::array arguments instead of passing "
267  "initializer lists")
268  static std::unique_ptr<Ell> create(
269  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
270  const IndexType num_elems_per_row,
271  std::initializer_list<InputValueType> values,
272  std::initializer_list<ColIndexType> col_idxs)
273  {
274  return create(exec, size, num_elems_per_row,
275  array<value_type>{exec, std::move(values)},
276  array<index_type>{exec, std::move(col_idxs)});
277  }
278 
295  static std::unique_ptr<const Ell> create_const(
296  std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
297  const index_type num_elems_per_row,
298  gko::detail::const_array_view<value_type>&& values,
299  gko::detail::const_array_view<index_type>&& col_idxs);
300 
310 
325 
329  const Ell* apply(ptr_param<const MultiVector<value_type>> b,
331 
337  const Ell* apply(ptr_param<const MultiVector<value_type>> alpha,
341 
348  void scale(const array<value_type>& row_scale,
349  const array<value_type>& col_scale);
350 
362  ptr_param<const MultiVector<value_type>> beta);
363 
364 private:
365  size_type compute_num_elems(const batch_dim<2>& size,
366  IndexType num_elems_per_row)
367  {
368  return size.get_num_batch_items() * size.get_common_size()[0] *
369  num_elems_per_row;
370  }
371 
372  Ell(std::shared_ptr<const Executor> exec,
373  const batch_dim<2>& size = batch_dim<2>{},
374  const IndexType num_elems_per_row = 0);
375 
376  Ell(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
377  const IndexType num_elems_per_row, array<value_type> values,
378  array<index_type> col_idxs);
379 
380  void apply_impl(const MultiVector<value_type>* b,
381  MultiVector<value_type>* x) const;
382 
383  void apply_impl(const MultiVector<value_type>* alpha,
384  const MultiVector<value_type>* b,
385  const MultiVector<value_type>* beta,
386  MultiVector<value_type>* x) const;
387 
388  index_type num_elems_per_row_;
389  array<value_type> values_;
390  array<index_type> col_idxs_;
391 };
392 
393 
394 } // namespace matrix
395 } // namespace batch
396 } // namespace gko
397 
398 
399 #endif // GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
gko::batch::EnableBatchLinOp
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition: batch_lin_op.hpp:250
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:138
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:99
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:150
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::BatchLinOp
Definition: batch_lin_op.hpp:59
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:108
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:201
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:173
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:215
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:127
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
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:118
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::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:461
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:42
gko::batch::matrix::Ell::apply
Ell * apply(ptr_param< const MultiVector< value_type >> b, ptr_param< MultiVector< value_type >> x)
Apply the matrix to a multi-vector.
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:673
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:186
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:160
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:470
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:682
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:656
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:325
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:108
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:344
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:661