Ginkgo  Generated from pipelines/2897303314 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
sparsity_csr.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
7 
8 
9 #include <vector>
10 
11 #include <ginkgo/core/base/array.hpp>
12 #include <ginkgo/core/base/lin_op.hpp>
13 #include <ginkgo/core/base/polymorphic_object.hpp>
14 
15 
16 namespace gko {
17 namespace matrix {
18 
19 
20 template <typename ValueType, typename IndexType>
21 class Csr;
22 
23 
24 template <typename ValueType>
25 class Dense;
26 
27 
28 template <typename ValueType, typename IndexType>
29 class Fbcsr;
30 
31 
49 template <typename ValueType = default_precision, typename IndexType = int32>
50 class SparsityCsr : public LinOp,
51  public EnableCloneable<SparsityCsr<ValueType, IndexType>>,
52  public ConvertibleTo<Csr<ValueType, IndexType>>,
53  public ConvertibleTo<Dense<ValueType>>,
54  public ReadableFromMatrixData<ValueType, IndexType>,
55  public WritableToMatrixData<ValueType, IndexType>,
56  public Transposable {
57  friend class EnableCloneable<SparsityCsr>;
58  friend class Csr<ValueType, IndexType>;
59  friend class Dense<ValueType>;
60  friend class Fbcsr<ValueType, IndexType>;
61  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
62 
63 public:
66  using ConvertibleTo<Csr<ValueType, IndexType>>::convert_to;
67  using ConvertibleTo<Csr<ValueType, IndexType>>::move_to;
68  using ConvertibleTo<Dense<ValueType>>::convert_to;
69  using ConvertibleTo<Dense<ValueType>>::move_to;
71 
72  using value_type = ValueType;
73  using index_type = IndexType;
74  using transposed_type = SparsityCsr<IndexType, ValueType>;
75  using mat_data = matrix_data<ValueType, IndexType>;
76  using device_mat_data = device_matrix_data<ValueType, IndexType>;
77 
78  void convert_to(Csr<ValueType, IndexType>* result) const override;
79 
80  void move_to(Csr<ValueType, IndexType>* result) override;
81 
82  void convert_to(Dense<ValueType>* result) const override;
83 
84  void move_to(Dense<ValueType>* result) override;
85 
86  void read(const mat_data& data) override;
87 
88  void read(const device_mat_data& data) override;
89 
90  void read(device_mat_data&& data) override;
91 
93 
94  void write(mat_data& data) const override;
95 
96  std::unique_ptr<LinOp> transpose() const override;
97 
98  std::unique_ptr<LinOp> conj_transpose() const override;
99 
109  std::unique_ptr<SparsityCsr> to_adjacency_matrix() const;
110 
114  void sort_by_column_index();
115 
116  /*
117  * Tests if all col_idxs are sorted by column index
118  *
119  * @returns True if all col_idxs are sorted.
120  */
121  bool is_sorted_by_column_index() const;
122 
128  index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
129 
137  const index_type* get_const_col_idxs() const noexcept
138  {
139  return col_idxs_.get_const_data();
140  }
141 
147  index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
148 
156  const index_type* get_const_row_ptrs() const noexcept
157  {
158  return row_ptrs_.get_const_data();
159  }
160 
166  value_type* get_value() noexcept { return value_.get_data(); }
167 
175  const value_type* get_const_value() const noexcept
176  {
177  return value_.get_const_data();
178  }
179 
185  size_type get_num_nonzeros() const noexcept { return col_idxs_.get_size(); }
186 
194  static std::unique_ptr<SparsityCsr> create(
195  std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
196  size_type num_nonzeros = {});
197 
217  static std::unique_ptr<SparsityCsr> create(
218  std::shared_ptr<const Executor> exec, const dim<2>& size,
219  array<index_type> col_idxs, array<index_type> row_ptrs,
220  value_type value = one<ValueType>());
221 
227  template <typename ColIndexType, typename RowPtrType>
228  GKO_DEPRECATED(
229  "explicitly construct the gko::array argument instead of passing "
230  "initializer lists")
231  static std::unique_ptr<SparsityCsr> create(
232  std::shared_ptr<const Executor> exec, const dim<2>& size,
233  std::initializer_list<ColIndexType> col_idxs,
234  std::initializer_list<RowPtrType> row_ptrs,
235  value_type value = one<ValueType>())
236  {
237  return create(exec, size, array<index_type>{exec, std::move(col_idxs)},
238  array<index_type>{exec, std::move(row_ptrs)}, value);
239  }
240 
248  static std::unique_ptr<SparsityCsr> create(
249  std::shared_ptr<const Executor> exec,
250  std::shared_ptr<const LinOp> matrix);
251 
265  static std::unique_ptr<const SparsityCsr> create_const(
266  std::shared_ptr<const Executor> exec, const dim<2>& size,
267  gko::detail::const_array_view<IndexType>&& col_idxs,
268  gko::detail::const_array_view<IndexType>&& row_ptrs,
269  ValueType value = one<ValueType>())
270  {
271  // cast const-ness away, but return a const object afterwards,
272  // so we can ensure that no modifications take place.
273  return std::unique_ptr<const SparsityCsr>(new SparsityCsr{
274  exec, size, gko::detail::array_const_cast(std::move(col_idxs)),
275  gko::detail::array_const_cast(std::move(row_ptrs)), value});
276  }
277 
283 
290 
295  SparsityCsr(const SparsityCsr&);
296 
303 
304 protected:
305  SparsityCsr(std::shared_ptr<const Executor> exec,
306  const dim<2>& size = dim<2>{}, size_type num_nonzeros = {});
307 
308  SparsityCsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
309  array<index_type> col_idxs, array<index_type> row_ptrs,
310  value_type value = one<ValueType>());
311 
312  SparsityCsr(std::shared_ptr<const Executor> exec,
313  std::shared_ptr<const LinOp> matrix);
314 
315  void apply_impl(const LinOp* b, LinOp* x) const override;
316 
317  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
318  LinOp* x) const override;
319 
320 private:
321  array<index_type> col_idxs_;
322  array<index_type> row_ptrs_;
323  array<value_type> value_;
324 };
325 
326 
327 } // namespace matrix
328 } // namespace gko
329 
330 
331 #endif // GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
gko::matrix::SparsityCsr::get_const_col_idxs
const index_type * get_const_col_idxs() const noexcept
Returns the column indices of the matrix.
Definition: sparsity_csr.hpp:137
gko::ReadableFromMatrixData::read
virtual void read(const matrix_data< ValueType, IndexType > &data)=0
Reads a matrix from a matrix_data structure.
gko::matrix::SparsityCsr
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition: csr.hpp:41
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::matrix::SparsityCsr::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::matrix::SparsityCsr::get_const_row_ptrs
const index_type * get_const_row_ptrs() const noexcept
Returns the row pointers of the matrix.
Definition: sparsity_csr.hpp:156
gko::matrix::SparsityCsr::get_col_idxs
index_type * get_col_idxs() noexcept
Returns the column indices of the matrix.
Definition: sparsity_csr.hpp:128
gko::matrix::SparsityCsr::create
static std::unique_ptr< SparsityCsr > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size=dim< 2 >{}, size_type num_nonzeros={})
Creates an uninitialized SparsityCsr matrix of the specified size.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::matrix::SparsityCsr::get_value
value_type * get_value() noexcept
Returns the value stored in the matrix.
Definition: sparsity_csr.hpp:166
gko::array< index_type >
gko::matrix::SparsityCsr::read
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
gko::dim< 2 >
gko::matrix::SparsityCsr::get_num_nonzeros
size_type get_num_nonzeros() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: sparsity_csr.hpp:185
gko::matrix::SparsityCsr::sort_by_column_index
void sort_by_column_index()
Sorts each row by column index.
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:686
gko::matrix::SparsityCsr::create_const
static std::unique_ptr< const SparsityCsr > create_const(std::shared_ptr< const Executor > exec, const dim< 2 > &size, gko::detail::const_array_view< IndexType > &&col_idxs, gko::detail::const_array_view< IndexType > &&row_ptrs, ValueType value=one< ValueType >())
Creates a constant (immutable) SparsityCsr matrix from constant arrays.
Definition: sparsity_csr.hpp:265
gko::WritableToMatrixData::write
virtual void write(matrix_data< ValueType, IndexType > &data) const =0
Writes a matrix to a matrix_data structure.
gko::matrix::SparsityCsr::SparsityCsr
SparsityCsr(const SparsityCsr &)
Copy-constructs a SparsityCsr matrix.
gko::EnableCloneable::convert_to
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition: polymorphic_object.hpp:411
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:695
gko::matrix::SparsityCsr::get_row_ptrs
index_type * get_row_ptrs() noexcept
Returns the row pointers of the matrix.
Definition: sparsity_csr.hpp:147
gko::EnableCloneable::move_to
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition: polymorphic_object.hpp:413
gko::matrix::SparsityCsr::operator=
SparsityCsr & operator=(const SparsityCsr &)
Copy-assigns a SparsityCsr matrix.
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
gko::matrix::SparsityCsr::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::matrix::SparsityCsr::get_const_value
const value_type * get_const_value() const noexcept
Returns the value stored in the matrix.
Definition: sparsity_csr.hpp:175
gko::matrix::SparsityCsr::to_adjacency_matrix
std::unique_ptr< SparsityCsr > to_adjacency_matrix() const
Transforms the sparsity matrix to an adjacency matrix.
gko::matrix::SparsityCsr::write
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
gko::one
constexpr T one()
Returns the multiplicative identity for T.
Definition: math.hpp:653