Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
sparsity_csr.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_SPARSITY_CSR_HPP_
35
36
37#include <vector>
38
39
40#include <ginkgo/core/base/array.hpp>
41#include <ginkgo/core/base/lin_op.hpp>
42#include <ginkgo/core/base/polymorphic_object.hpp>
43
44
45namespace gko {
46namespace matrix {
47
48
49template <typename ValueType, typename IndexType>
50class Csr;
51
52
53template <typename ValueType>
54class Dense;
55
56
57template <typename ValueType, typename IndexType>
58class Fbcsr;
59
60
79template <typename ValueType = default_precision, typename IndexType = int32>
81 : public EnableLinOp<SparsityCsr<ValueType, IndexType>>,
82 public EnableCreateMethod<SparsityCsr<ValueType, IndexType>>,
83 public ConvertibleTo<Csr<ValueType, IndexType>>,
84 public ConvertibleTo<Dense<ValueType>>,
85 public ReadableFromMatrixData<ValueType, IndexType>,
86 public WritableToMatrixData<ValueType, IndexType>,
87 public Transposable {
88 friend class EnableCreateMethod<SparsityCsr>;
90 friend class Csr<ValueType, IndexType>;
91 friend class Dense<ValueType>;
92 friend class Fbcsr<ValueType, IndexType>;
93
94public:
95 using EnableLinOp<SparsityCsr>::convert_to;
96 using EnableLinOp<SparsityCsr>::move_to;
99 using ConvertibleTo<Dense<ValueType>>::convert_to;
100 using ConvertibleTo<Dense<ValueType>>::move_to;
101 using ReadableFromMatrixData<ValueType, IndexType>::read;
102
103 using value_type = ValueType;
104 using index_type = IndexType;
108
109 void convert_to(Csr<ValueType, IndexType>* result) const override;
110
111 void move_to(Csr<ValueType, IndexType>* result) override;
112
113 void convert_to(Dense<ValueType>* result) const override;
114
115 void move_to(Dense<ValueType>* result) override;
116
117 void read(const mat_data& data) override;
118
119 void read(const device_mat_data& data) override;
120
121 void read(device_mat_data&& data) override;
122
123 void write(mat_data& data) const override;
124
125 std::unique_ptr<LinOp> transpose() const override;
126
127 std::unique_ptr<LinOp> conj_transpose() const override;
128
138 std::unique_ptr<SparsityCsr> to_adjacency_matrix() const;
139
144
145 /*
146 * Tests if all col_idxs are sorted by column index
147 *
148 * @returns True if all col_idxs are sorted.
149 */
150 bool is_sorted_by_column_index() const;
151
157 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
158
167 {
168 return col_idxs_.get_const_data();
169 }
170
176 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
177
186 {
187 return row_ptrs_.get_const_data();
188 }
189
195 value_type* get_value() noexcept { return value_.get_data(); }
196
204 const value_type* get_const_value() const noexcept
205 {
206 return value_.get_const_data();
207 }
208
209
216 {
217 return col_idxs_.get_num_elems();
218 }
219
233 static std::unique_ptr<const SparsityCsr> create_const(
234 std::shared_ptr<const Executor> exec, const dim<2>& size,
235 gko::detail::const_array_view<IndexType>&& col_idxs,
236 gko::detail::const_array_view<IndexType>&& row_ptrs,
237 ValueType value = one<ValueType>())
238 {
239 // cast const-ness away, but return a const object afterwards,
240 // so we can ensure that no modifications take place.
241 return std::unique_ptr<const SparsityCsr>(new SparsityCsr{
242 exec, size, gko::detail::array_const_cast(std::move(col_idxs)),
243 gko::detail::array_const_cast(std::move(row_ptrs)), value});
244 }
245
251
258
264
271
272protected:
280 SparsityCsr(std::shared_ptr<const Executor> exec,
281 const dim<2>& size = dim<2>{}, size_type num_nonzeros = {})
282 : EnableLinOp<SparsityCsr>(exec, size),
283 col_idxs_(exec, num_nonzeros),
284 row_ptrs_(exec, size[0] + 1),
285 value_(exec, {one<ValueType>()})
286 {
287 row_ptrs_.fill(0);
288 }
289
309 template <typename ColIdxsArray, typename RowPtrsArray>
310 SparsityCsr(std::shared_ptr<const Executor> exec, const dim<2>& size,
311 ColIdxsArray&& col_idxs, RowPtrsArray&& row_ptrs,
312 value_type value = one<ValueType>())
313 : EnableLinOp<SparsityCsr>(exec, size),
314 col_idxs_{exec, std::forward<ColIdxsArray>(col_idxs)},
315 row_ptrs_{exec, std::forward<RowPtrsArray>(row_ptrs)},
316 value_{exec, {value}}
317 {
318 GKO_ASSERT_EQ(this->get_size()[0] + 1, row_ptrs_.get_num_elems());
319 }
320
328 SparsityCsr(std::shared_ptr<const Executor> exec,
329 std::shared_ptr<const LinOp> matrix)
330 : EnableLinOp<SparsityCsr>(exec, matrix->get_size())
331 {
332 auto tmp_ = copy_and_convert_to<SparsityCsr>(exec, matrix);
333 this->copy_from(tmp_);
334 }
335
336 void apply_impl(const LinOp* b, LinOp* x) const override;
337
338 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
339 LinOp* x) const override;
340
341private:
342 array<index_type> col_idxs_;
343 array<index_type> row_ptrs_;
344 array<value_type> value_;
345};
346
347
348} // namespace matrix
349} // namespace gko
350
351
352#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:499
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:634
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:689
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
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:655
void fill(const value_type value)
Fill the array with the given value.
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:146
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
Fixed-block compressed sparse row storage matrix format.
Definition fbcsr.hpp:138
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition sparsity_csr.hpp:87
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:157
SparsityCsr(SparsityCsr &&)
Move-constructs a SparsityCsr matrix.
void read(const device_mat_data &data) override
Reads a matrix from a device_matrix_data structure.
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
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:166
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
const index_type * get_const_row_ptrs() const noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:185
void read(device_mat_data &&data) override
Reads a matrix from a device_matrix_data structure.
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:215
const value_type * get_const_value() const noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:204
index_type * get_row_ptrs() noexcept
Returns the row pointers of the matrix.
Definition sparsity_csr.hpp:176
void sort_by_column_index()
Sorts each row by column index.
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:233
value_type * get_value() noexcept
Returns the value stored in the matrix.
Definition sparsity_csr.hpp:195
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:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155