Ginkgo  Generated from pipelines/1554403166 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
batch_csr.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_CSR_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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/types.hpp>
18 #include <ginkgo/core/base/utils.hpp>
19 #include <ginkgo/core/matrix/csr.hpp>
20 
21 
22 namespace gko {
23 namespace batch {
24 namespace matrix {
25 
26 
46 template <typename ValueType = default_precision, typename IndexType = int32>
47 class Csr final
48  : public EnableBatchLinOp<Csr<ValueType, IndexType>>,
49  public ConvertibleTo<Csr<next_precision<ValueType>, IndexType>> {
50  friend class EnablePolymorphicObject<Csr, BatchLinOp>;
51  friend class Csr<to_complex<ValueType>, IndexType>;
52  friend class Csr<next_precision<ValueType>, IndexType>;
53  static_assert(std::is_same<IndexType, int32>::value,
54  "IndexType must be a 32 bit integer");
55 
56 public:
59 
60  using value_type = ValueType;
61  using index_type = IndexType;
63  using absolute_type = remove_complex<Csr>;
64  using complex_type = to_complex<Csr>;
65 
66  void convert_to(
67  Csr<next_precision<ValueType>, IndexType>* result) const override;
68 
69  void move_to(Csr<next_precision<ValueType>, IndexType>* result) override;
70 
81  std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
82 
86  std::unique_ptr<const unbatch_type> create_const_view_for_item(
87  size_type item_id) const;
88 
94  value_type* get_values() noexcept { return values_.get_data(); }
95 
103  const value_type* get_const_values() const noexcept
104  {
105  return values_.get_const_data();
106  }
107 
113  index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
114 
122  const index_type* get_const_col_idxs() const noexcept
123  {
124  return col_idxs_.get_const_data();
125  }
126 
132  index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
133 
141  const index_type* get_const_row_ptrs() const noexcept
142  {
143  return row_ptrs_.get_const_data();
144  }
145 
154  {
155  return values_.get_size();
156  }
157 
164  {
165  return this->get_num_stored_elements() / this->get_num_batch_items();
166  }
167 
176  value_type* get_values_for_item(size_type batch_id) noexcept
177  {
178  GKO_ASSERT(batch_id < this->get_num_batch_items());
179  GKO_ASSERT(values_.get_size() >=
180  batch_id * this->get_num_elements_per_item());
181  return values_.get_data() +
182  batch_id * this->get_num_elements_per_item();
183  }
184 
192  const value_type* get_const_values_for_item(
193  size_type batch_id) const noexcept
194  {
195  GKO_ASSERT(batch_id < this->get_num_batch_items());
196  GKO_ASSERT(values_.get_size() >=
197  batch_id * this->get_num_elements_per_item());
198  return values_.get_const_data() +
199  batch_id * this->get_num_elements_per_item();
200  }
201 
217  static std::unique_ptr<Csr> create(
218  std::shared_ptr<const Executor> exec,
219  const batch_dim<2>& size = batch_dim<2>{},
220  size_type num_nonzeros_per_item = {});
221 
238  static std::unique_ptr<Csr> create(std::shared_ptr<const Executor> exec,
239  const batch_dim<2>& size,
240  array<value_type> values,
241  array<index_type> col_idxs,
242  array<index_type> row_ptrs);
243 
249  template <typename InputValueType, typename ColIndexType,
250  typename RowPtrType>
251  GKO_DEPRECATED(
252  "explicitly construct the gko::array arguments instead of passing "
253  "initializer lists")
254  static std::unique_ptr<Csr> create(
255  std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
256  std::initializer_list<InputValueType> values,
257  std::initializer_list<ColIndexType> col_idxs,
258  std::initializer_list<RowPtrType> row_ptrs)
259  {
260  return create(exec, size, array<value_type>{exec, std::move(values)},
261  array<index_type>{exec, std::move(col_idxs)},
262  array<index_type>{exec, std::move(row_ptrs)});
263  }
264 
283  static std::unique_ptr<const Csr> create_const(
284  std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
285  gko::detail::const_array_view<value_type>&& values,
286  gko::detail::const_array_view<index_type>&& col_idxs,
287  gko::detail::const_array_view<index_type>&& row_ptrs);
288 
298 
313 
317  const Csr* apply(ptr_param<const MultiVector<value_type>> b,
319 
325  const Csr* apply(ptr_param<const MultiVector<value_type>> alpha,
329 
336  void scale(const array<value_type>& row_scale,
337  const array<value_type>& col_scale);
338 
350  ptr_param<const MultiVector<value_type>> beta);
351 
352 private:
353  Csr(std::shared_ptr<const Executor> exec,
354  const batch_dim<2>& size = batch_dim<2>{},
355  size_type num_nonzeros_per_item = {});
356 
357  Csr(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
358  array<value_type> values, array<index_type> col_idxs,
359  array<index_type> row_ptrs)
360  : EnableBatchLinOp<Csr>(exec, size),
361  values_{exec, std::move(values)},
362  col_idxs_{exec, std::move(col_idxs)},
363  row_ptrs_{exec, std::move(row_ptrs)}
364  {
365  // Ensure that the value and col_idxs arrays have the correct size
366  auto max_num_elems = this->get_common_size()[0] *
367  this->get_common_size()[1] *
368  this->get_num_batch_items();
369  GKO_ASSERT(values_.get_size() <= max_num_elems);
370  GKO_ASSERT_EQ(row_ptrs_.get_size(), this->get_common_size()[0] + 1);
371  GKO_ASSERT_EQ(this->get_num_elements_per_item(), col_idxs_.get_size());
372  }
373 
374  void apply_impl(const MultiVector<value_type>* b,
375  MultiVector<value_type>* x) const;
376 
377  void apply_impl(const MultiVector<value_type>* alpha,
378  const MultiVector<value_type>* b,
379  const MultiVector<value_type>* beta,
380  MultiVector<value_type>* x) const;
381 
382  array<value_type> values_;
383  array<index_type> col_idxs_;
384  array<index_type> row_ptrs_;
385 };
386 
387 
388 } // namespace matrix
389 } // namespace batch
390 } // namespace gko
391 
392 
393 #endif // GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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::Csr::get_col_idxs
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition: batch_csr.hpp:113
gko::batch::matrix::Csr::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::Csr type) of one item of the batch::matrix::Csr<value_type> object...
gko::batch::matrix::Csr::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_csr.hpp:176
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:28
gko::batch::BatchLinOp
Definition: batch_lin_op.hpp:59
gko::batch::matrix::Csr::create_view_for_item
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of matrix::Csr type) of one item of the batch::matrix::Csr<value_type> object...
gko::batch::matrix::Csr::create
static std::unique_ptr< Csr > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{}, size_type num_nonzeros_per_item={})
Creates an uninitialized Csr matrix of the specified size.
gko::batch::matrix::Csr::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:86
gko::batch::matrix::Csr
Csr is a general sparse matrix format that stores the column indices for each nonzero entry and a cum...
Definition: batch_csr.hpp:47
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition: batch_multi_vector.hpp:52
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::batch::matrix::Csr::apply
Csr * apply(ptr_param< const MultiVector< value_type >> b, ptr_param< MultiVector< value_type >> x)
Apply the matrix to a multi-vector.
gko::array< value_type >
gko::batch_dim< 2 >
gko::batch::matrix::Csr::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_csr.hpp:192
gko::batch::matrix::Csr::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_csr.hpp:163
gko::batch::matrix::Csr::get_row_ptrs
index_type * get_row_ptrs() noexcept
Returns a pointer to the array of row pointers of the matrix.
Definition: batch_csr.hpp:132
gko::batch::matrix::Csr::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_csr.hpp:153
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:41
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::Csr::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::matrix::Csr::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_csr.hpp:122
gko::batch::matrix::Csr::get_const_row_ptrs
const index_type * get_const_row_ptrs() const noexcept
Returns a pointer to the array of row pointers of the matrix.
Definition: batch_csr.hpp:141
gko::batch::matrix::Csr::create_const
static std::unique_ptr< const Csr > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, gko::detail::const_array_view< value_type > &&values, gko::detail::const_array_view< index_type > &&col_idxs, gko::detail::const_array_view< index_type > &&row_ptrs)
Creates a constant (immutable) batch csr matrix from a constant array.
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::batch::matrix::Csr::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition: batch_csr.hpp:103
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::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::Csr::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: batch_csr.hpp:94
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