Ginkgo Generated from branch based on master. Ginkgo version 1.8.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
12#include <ginkgo/core/base/array.hpp>
13#include <ginkgo/core/base/lin_op.hpp>
14#include <ginkgo/core/base/polymorphic_object.hpp>
15
16
17namespace gko {
18namespace matrix {
19
20
21template <typename ValueType, typename IndexType>
22class Csr;
23
24
25template <typename ValueType>
26class Dense;
27
28
29template <typename ValueType, typename IndexType>
30class Fbcsr;
31
32
51template <typename ValueType = default_precision, typename IndexType = int32>
52class SparsityCsr : public EnableLinOp<SparsityCsr<ValueType, IndexType>>,
53 public ConvertibleTo<Csr<ValueType, IndexType>>,
54 public ConvertibleTo<Dense<ValueType>>,
55 public ReadableFromMatrixData<ValueType, IndexType>,
56 public WritableToMatrixData<ValueType, IndexType>,
57 public Transposable {
59 friend class Csr<ValueType, IndexType>;
60 friend class Dense<ValueType>;
61 friend class Fbcsr<ValueType, IndexType>;
62
63public:
64 using EnableLinOp<SparsityCsr>::convert_to;
65 using EnableLinOp<SparsityCsr>::move_to;
68 using ConvertibleTo<Dense<ValueType>>::convert_to;
69 using ConvertibleTo<Dense<ValueType>>::move_to;
70 using ReadableFromMatrixData<ValueType, IndexType>::read;
71
72 using value_type = ValueType;
73 using index_type = 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
92 void write(mat_data& data) const override;
93
94 std::unique_ptr<LinOp> transpose() const override;
95
96 std::unique_ptr<LinOp> conj_transpose() const override;
97
107 std::unique_ptr<SparsityCsr> to_adjacency_matrix() const;
108
113
114 /*
115 * Tests if all col_idxs are sorted by column index
116 *
117 * @returns True if all col_idxs are sorted.
118 */
119 bool is_sorted_by_column_index() const;
120
126 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
127
135 const index_type* get_const_col_idxs() const noexcept
136 {
137 return col_idxs_.get_const_data();
138 }
139
145 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
146
154 const index_type* get_const_row_ptrs() const noexcept
155 {
156 return row_ptrs_.get_const_data();
157 }
158
164 value_type* get_value() noexcept { return value_.get_data(); }
165
173 const value_type* get_const_value() const noexcept
174 {
175 return value_.get_const_data();
176 }
177
183 size_type get_num_nonzeros() const noexcept { return col_idxs_.get_size(); }
184
192 static std::unique_ptr<SparsityCsr> create(
193 std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
194 size_type num_nonzeros = {});
195
215 static std::unique_ptr<SparsityCsr> create(
216 std::shared_ptr<const Executor> exec, const dim<2>& size,
217 array<index_type> col_idxs, array<index_type> row_ptrs,
218 value_type value = one<ValueType>());
219
225 template <typename ColIndexType, typename RowPtrType>
226 GKO_DEPRECATED(
227 "explicitly construct the gko::array argument instead of passing "
228 "initializer lists")
229 static std::unique_ptr<SparsityCsr> create(
230 std::shared_ptr<const Executor> exec, const dim<2>& size,
231 std::initializer_list<ColIndexType> col_idxs,
232 std::initializer_list<RowPtrType> row_ptrs,
233 value_type value = one<ValueType>())
234 {
235 return create(exec, size, array<index_type>{exec, std::move(col_idxs)},
236 array<index_type>{exec, std::move(row_ptrs)}, value);
237 }
238
246 static std::unique_ptr<SparsityCsr> create(
247 std::shared_ptr<const Executor> exec,
248 std::shared_ptr<const LinOp> matrix);
249
263 static std::unique_ptr<const SparsityCsr> create_const(
264 std::shared_ptr<const Executor> exec, const dim<2>& size,
265 gko::detail::const_array_view<IndexType>&& col_idxs,
266 gko::detail::const_array_view<IndexType>&& row_ptrs,
267 ValueType value = one<ValueType>())
268 {
269 // cast const-ness away, but return a const object afterwards,
270 // so we can ensure that no modifications take place.
271 return std::unique_ptr<const SparsityCsr>(new SparsityCsr{
272 exec, size, gko::detail::array_const_cast(std::move(col_idxs)),
273 gko::detail::array_const_cast(std::move(row_ptrs)), value});
274 }
275
281
288
294
301
302protected:
303 SparsityCsr(std::shared_ptr<const Executor> exec,
304 const dim<2>& size = dim<2>{}, size_type num_nonzeros = {});
305
306 SparsityCsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
307 array<index_type> col_idxs, array<index_type> row_ptrs,
308 value_type value = one<ValueType>());
309
310 SparsityCsr(std::shared_ptr<const Executor> exec,
311 std::shared_ptr<const LinOp> matrix);
312
313 void apply_impl(const LinOp* b, LinOp* x) const override;
314
315 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
316 LinOp* x) const override;
317
318private:
319 array<index_type> col_idxs_;
320 array<index_type> row_ptrs_;
321 array<value_type> value_;
322};
323
324
325} // namespace matrix
326} // namespace gko
327
328
329#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:471
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:880
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:616
Definition lin_op.hpp:118
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:606
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:434
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:661
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
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:683
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:657
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 sparsity_csr.hpp:22
Dense is a matrix format which explicitly stores all values of the matrix.
Definition sparsity_csr.hpp:26
Fixed-block compressed sparse row storage matrix format.
Definition sparsity_csr.hpp:30
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition sparsity_csr.hpp:57
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:126
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:135
const index_type * get_const_row_ptrs() const noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:154
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:183
const value_type * get_const_value() const noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:173
index_type * get_row_ptrs() noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:145
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:263
value_type * get_value() noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:164
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
std::unique_ptr< MatrixType > read(StreamType &&is, MatrixArgs &&... args)
Reads a matrix stored in matrix market format from an input stream.
Definition mtx_io.hpp:160
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:27
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:127