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
sparsity_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_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
16namespace gko {
17namespace matrix {
18
19
20template <typename ValueType, typename IndexType>
21class Csr;
22
23
24template <typename ValueType>
25class Dense;
26
27
28template <typename ValueType, typename IndexType>
29class Fbcsr;
30
31
50template <typename ValueType = default_precision, typename IndexType = int32>
51class SparsityCsr : public EnableLinOp<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 {
58 friend class Csr<ValueType, IndexType>;
59 friend class Dense<ValueType>;
60 friend class Fbcsr<ValueType, IndexType>;
61
62public:
63 using EnableLinOp<SparsityCsr>::convert_to;
64 using EnableLinOp<SparsityCsr>::move_to;
67 using ConvertibleTo<Dense<ValueType>>::convert_to;
68 using ConvertibleTo<Dense<ValueType>>::move_to;
69 using ReadableFromMatrixData<ValueType, IndexType>::read;
70
71 using value_type = ValueType;
72 using index_type = IndexType;
73 using transposed_type = SparsityCsr<IndexType, ValueType>;
74 using mat_data = matrix_data<ValueType, IndexType>;
75 using device_mat_data = device_matrix_data<ValueType, IndexType>;
76
77 void convert_to(Csr<ValueType, IndexType>* result) const override;
78
79 void move_to(Csr<ValueType, IndexType>* result) override;
80
81 void convert_to(Dense<ValueType>* result) const override;
82
83 void move_to(Dense<ValueType>* result) override;
84
85 void read(const mat_data& data) override;
86
87 void read(const device_mat_data& data) override;
88
89 void read(device_mat_data&& data) override;
90
91 void write(mat_data& data) const override;
92
93 std::unique_ptr<LinOp> transpose() const override;
94
95 std::unique_ptr<LinOp> conj_transpose() const override;
96
106 std::unique_ptr<SparsityCsr> to_adjacency_matrix() const;
107
112
113 /*
114 * Tests if all col_idxs are sorted by column index
115 *
116 * @returns True if all col_idxs are sorted.
117 */
118 bool is_sorted_by_column_index() const;
119
125 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
126
134 const index_type* get_const_col_idxs() const noexcept
135 {
136 return col_idxs_.get_const_data();
137 }
138
144 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
145
153 const index_type* get_const_row_ptrs() const noexcept
154 {
155 return row_ptrs_.get_const_data();
156 }
157
163 value_type* get_value() noexcept { return value_.get_data(); }
164
172 const value_type* get_const_value() const noexcept
173 {
174 return value_.get_const_data();
175 }
176
182 size_type get_num_nonzeros() const noexcept { return col_idxs_.get_size(); }
183
191 static std::unique_ptr<SparsityCsr> create(
192 std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
193 size_type num_nonzeros = {});
194
214 static std::unique_ptr<SparsityCsr> create(
215 std::shared_ptr<const Executor> exec, const dim<2>& size,
216 array<index_type> col_idxs, array<index_type> row_ptrs,
217 value_type value = one<ValueType>());
218
224 template <typename ColIndexType, typename RowPtrType>
225 GKO_DEPRECATED(
226 "explicitly construct the gko::array argument instead of passing "
227 "initializer lists")
228 static std::unique_ptr<SparsityCsr> create(
229 std::shared_ptr<const Executor> exec, const dim<2>& size,
230 std::initializer_list<ColIndexType> col_idxs,
231 std::initializer_list<RowPtrType> row_ptrs,
232 value_type value = one<ValueType>())
233 {
234 return create(exec, size, array<index_type>{exec, std::move(col_idxs)},
235 array<index_type>{exec, std::move(row_ptrs)}, value);
236 }
237
245 static std::unique_ptr<SparsityCsr> create(
246 std::shared_ptr<const Executor> exec,
247 std::shared_ptr<const LinOp> matrix);
248
262 static std::unique_ptr<const SparsityCsr> create_const(
263 std::shared_ptr<const Executor> exec, const dim<2>& size,
264 gko::detail::const_array_view<IndexType>&& col_idxs,
265 gko::detail::const_array_view<IndexType>&& row_ptrs,
266 ValueType value = one<ValueType>())
267 {
268 // cast const-ness away, but return a const object afterwards,
269 // so we can ensure that no modifications take place.
270 return std::unique_ptr<const SparsityCsr>(new SparsityCsr{
271 exec, size, gko::detail::array_const_cast(std::move(col_idxs)),
272 gko::detail::array_const_cast(std::move(row_ptrs)), value});
273 }
274
280
287
293
300
301protected:
302 SparsityCsr(std::shared_ptr<const Executor> exec,
303 const dim<2>& size = dim<2>{}, size_type num_nonzeros = {});
304
305 SparsityCsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
306 array<index_type> col_idxs, array<index_type> row_ptrs,
307 value_type value = one<ValueType>());
308
309 SparsityCsr(std::shared_ptr<const Executor> exec,
310 std::shared_ptr<const LinOp> matrix);
311
312 void apply_impl(const LinOp* b, LinOp* x) const override;
313
314 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
315 LinOp* x) const override;
316
317private:
318 array<index_type> col_idxs_;
319 array<index_type> row_ptrs_;
320 array<value_type> value_;
321};
322
323
324} // namespace matrix
325} // namespace gko
326
327
328#endif // GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:470
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
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
Definition lin_op.hpp:117
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:605
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:433
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:660
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:121
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:117
Fixed-block compressed sparse row storage matrix format.
Definition fbcsr.hpp:113
static std::unique_ptr< SparsityCsr > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size, array< index_type > col_idxs, array< index_type > row_ptrs, value_type value=one< ValueType >())
Creates a SparsityCsr matrix from already allocated (and initialized) row pointer and column index ar...
std::unique_ptr< SparsityCsr > to_adjacency_matrix() const
Transforms the sparsity matrix to an adjacency matrix.
SparsityCsr & operator=(SparsityCsr &&)
Move-assigns a SparsityCsr matrix.
SparsityCsr & operator=(const SparsityCsr &)
Copy-assigns a SparsityCsr matrix.
index_type * get_col_idxs() noexcept
Returns the column indices of the matrix.
Definition sparsity_csr.hpp:125
SparsityCsr(SparsityCsr &&)
Move-constructs a SparsityCsr matrix.
SparsityCsr(const SparsityCsr &)
Copy-constructs a SparsityCsr matrix.
const index_type * get_const_col_idxs() const noexcept
Returns the column indices of the matrix.
Definition sparsity_csr.hpp:134
const index_type * get_const_row_ptrs() const noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:153
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
size_type get_num_nonzeros() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition sparsity_csr.hpp:182
const value_type * get_const_value() const noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:172
index_type * get_row_ptrs() noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:144
static std::unique_ptr< SparsityCsr > create(std::shared_ptr< const Executor > exec, std::shared_ptr< const LinOp > matrix)
Creates a Sparsity matrix from an existing matrix.
void sort_by_column_index()
Sorts each row by column index.
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.
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:262
value_type * get_value() noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:163
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
The matrix namespace.
Definition dense_cache.hpp:15
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:630
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 object.
Definition dim.hpp:26
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126