ginkgo/core/matrix/sparsity_csr.hpp Source File

ginkgo/core/matrix/sparsity_csr.hpp Source File#

Reference API: ginkgo/core/matrix/sparsity_csr.hpp Source File
Reference API
sparsity_csr.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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
49template <typename ValueType = default_precision, typename IndexType = int32>
50class SparsityCsr : public EnableLinOp<SparsityCsr<ValueType, IndexType>>,
51 public ConvertibleTo<Csr<ValueType, IndexType>>,
52 public ConvertibleTo<Dense<ValueType>>,
53 public ReadableFromMatrixData<ValueType, IndexType>,
54 public WritableToMatrixData<ValueType, IndexType>,
55 public Transposable {
57 friend class Csr<ValueType, IndexType>;
58 friend class Dense<ValueType>;
59 friend class Fbcsr<ValueType, IndexType>;
60
61public:
62 using EnableLinOp<SparsityCsr>::convert_to;
63 using EnableLinOp<SparsityCsr>::move_to;
66 using ConvertibleTo<Dense<ValueType>>::convert_to;
67 using ConvertibleTo<Dense<ValueType>>::move_to;
68 using ReadableFromMatrixData<ValueType, IndexType>::read;
69
70 using value_type = ValueType;
71 using index_type = IndexType;
75
76 void convert_to(Csr<ValueType, IndexType>* result) const override;
77
78 void move_to(Csr<ValueType, IndexType>* result) override;
79
80 void convert_to(Dense<ValueType>* result) const override;
81
82 void move_to(Dense<ValueType>* result) override;
83
84 void read(const mat_data& data) override;
85
86 void read(const device_mat_data& data) override;
87
88 void read(device_mat_data&& data) override;
89
90 void write(mat_data& data) const override;
91
92 std::unique_ptr<LinOp> transpose() const override;
93
94 std::unique_ptr<LinOp> conj_transpose() const override;
95
105 std::unique_ptr<SparsityCsr> to_adjacency_matrix() const;
106
111
112 /*
113 * Tests if all col_idxs are sorted by column index
114 *
115 * @returns True if all col_idxs are sorted.
116 */
117 bool is_sorted_by_column_index() const;
118
124 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
125
133 const index_type* get_const_col_idxs() const noexcept
134 {
135 return col_idxs_.get_const_data();
136 }
137
143 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
144
152 const index_type* get_const_row_ptrs() const noexcept
153 {
154 return row_ptrs_.get_const_data();
155 }
156
162 value_type* get_value() noexcept { return value_.get_data(); }
163
171 const value_type* get_const_value() const noexcept
172 {
173 return value_.get_const_data();
174 }
175
181 size_type get_num_nonzeros() const noexcept { return col_idxs_.get_size(); }
182
190 static std::unique_ptr<SparsityCsr> create(
191 std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
192 size_type num_nonzeros = {});
193
213 static std::unique_ptr<SparsityCsr> create(
214 std::shared_ptr<const Executor> exec, const dim<2>& size,
215 array<index_type> col_idxs, array<index_type> row_ptrs,
216 value_type value = one<ValueType>());
217
223 template <typename ColIndexType, typename RowPtrType>
224 GKO_DEPRECATED(
225 "explicitly construct the gko::array argument instead of passing "
226 "initializer lists")
227 static std::unique_ptr<SparsityCsr> create(
228 std::shared_ptr<const Executor> exec, const dim<2>& size,
229 std::initializer_list<ColIndexType> col_idxs,
230 std::initializer_list<RowPtrType> row_ptrs,
231 value_type value = one<ValueType>())
232 {
233 return create(exec, size, array<index_type>{exec, std::move(col_idxs)},
234 array<index_type>{exec, std::move(row_ptrs)}, value);
235 }
236
244 static std::unique_ptr<SparsityCsr> create(
245 std::shared_ptr<const Executor> exec,
246 std::shared_ptr<const LinOp> matrix);
247
261 static std::unique_ptr<const SparsityCsr> create_const(
262 std::shared_ptr<const Executor> exec, const dim<2>& size,
263 gko::detail::const_array_view<IndexType>&& col_idxs,
264 gko::detail::const_array_view<IndexType>&& row_ptrs,
265 ValueType value = one<ValueType>())
266 {
267 // cast const-ness away, but return a const object afterwards,
268 // so we can ensure that no modifications take place.
269 return std::unique_ptr<const SparsityCsr>(new SparsityCsr{
270 exec, size, gko::detail::array_const_cast(std::move(col_idxs)),
271 gko::detail::array_const_cast(std::move(row_ptrs)), value});
272 }
273
279
286
292
299
300protected:
301 SparsityCsr(std::shared_ptr<const Executor> exec,
302 const dim<2>& size = dim<2>{}, size_type num_nonzeros = {});
303
304 SparsityCsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
305 array<index_type> col_idxs, array<index_type> row_ptrs,
306 value_type value = one<ValueType>());
307
308 SparsityCsr(std::shared_ptr<const Executor> exec,
309 std::shared_ptr<const LinOp> matrix);
310
311 void apply_impl(const LinOp* b, LinOp* x) const override;
312
313 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
314 LinOp* x) const override;
315
316private:
317 array<index_type> col_idxs_;
318 array<index_type> row_ptrs_;
319 array<value_type> value_;
320};
321
322
323} // namespace matrix
324} // namespace gko
325
326
327#endif // GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
Definition polymorphic_object.hpp:479
Definition lin_op.hpp:878
Definition polymorphic_object.hpp:668
Definition executor.hpp:615
Definition lin_op.hpp:117
Definition lin_op.hpp:605
Definition lin_op.hpp:433
Definition lin_op.hpp:660
Definition array.hpp:166
value_type * get_data() noexcept
Definition array.hpp:673
const value_type * get_const_data() const noexcept
Definition array.hpp:682
size_type get_size() const noexcept
Definition array.hpp:656
Definition device_matrix_data.hpp:36
Definition csr.hpp:123
Definition dense.hpp:116
Fixed-block compressed sparse row storage matrix format.
Definition fbcsr.hpp:112
Definition sparsity_csr.hpp:55
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 >())
std::unique_ptr< SparsityCsr > to_adjacency_matrix() const
SparsityCsr & operator=(SparsityCsr &&)
SparsityCsr & operator=(const SparsityCsr &)
index_type * get_col_idxs() noexcept
Definition sparsity_csr.hpp:124
SparsityCsr(SparsityCsr &&)
void read(const device_mat_data &data) override
void write(mat_data &data) const override
SparsityCsr(const SparsityCsr &)
const index_type * get_const_col_idxs() const noexcept
Definition sparsity_csr.hpp:133
void read(const mat_data &data) override
const index_type * get_const_row_ptrs() const noexcept
Definition sparsity_csr.hpp:152
void read(device_mat_data &&data) override
std::unique_ptr< LinOp > transpose() const override
size_type get_num_nonzeros() const noexcept
Definition sparsity_csr.hpp:181
const value_type * get_const_value() const noexcept
Definition sparsity_csr.hpp:171
index_type * get_row_ptrs() noexcept
Definition sparsity_csr.hpp:143
static std::unique_ptr< SparsityCsr > create(std::shared_ptr< const Executor > exec, std::shared_ptr< const LinOp > matrix)
static std::unique_ptr< SparsityCsr > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size=dim< 2 >{}, size_type num_nonzeros={})
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 >())
Definition sparsity_csr.hpp:261
value_type * get_value() noexcept
Definition sparsity_csr.hpp:162
std::unique_ptr< LinOp > conj_transpose() const override
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T one()
Definition math.hpp:630
std::size_t size_type
Definition types.hpp:89
STL namespace.
Definition dim.hpp:26
Definition matrix_data.hpp:126