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
sellp.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_SELLP_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_SELLP_HPP_
35
36
37#include <ginkgo/core/base/array.hpp>
38#include <ginkgo/core/base/lin_op.hpp>
39
40
41namespace gko {
42namespace matrix {
43
44
45constexpr int default_slice_size = 64;
46constexpr int default_stride_factor = 1;
47
48
49template <typename ValueType>
50class Dense;
51
52template <typename ValueType, typename IndexType>
53class Csr;
54
70template <typename ValueType = default_precision, typename IndexType = int32>
71class Sellp : public EnableLinOp<Sellp<ValueType, IndexType>>,
72 public EnableCreateMethod<Sellp<ValueType, IndexType>>,
73 public ConvertibleTo<Sellp<next_precision<ValueType>, IndexType>>,
74 public ConvertibleTo<Dense<ValueType>>,
75 public ConvertibleTo<Csr<ValueType, IndexType>>,
76 public DiagonalExtractable<ValueType>,
77 public ReadableFromMatrixData<ValueType, IndexType>,
78 public WritableToMatrixData<ValueType, IndexType>,
80 remove_complex<Sellp<ValueType, IndexType>>> {
81 friend class EnableCreateMethod<Sellp>;
82 friend class EnablePolymorphicObject<Sellp, LinOp>;
83 friend class Dense<ValueType>;
84 friend class Csr<ValueType, IndexType>;
85 friend class Sellp<to_complex<ValueType>, IndexType>;
86
87public:
88 using EnableLinOp<Sellp>::convert_to;
89 using EnableLinOp<Sellp>::move_to;
90 using ConvertibleTo<
91 Sellp<next_precision<ValueType>, IndexType>>::convert_to;
92 using ConvertibleTo<Sellp<next_precision<ValueType>, IndexType>>::move_to;
93 using ConvertibleTo<Dense<ValueType>>::convert_to;
94 using ConvertibleTo<Dense<ValueType>>::move_to;
97 using ReadableFromMatrixData<ValueType, IndexType>::read;
98
99 using value_type = ValueType;
100 using index_type = IndexType;
103 using absolute_type = remove_complex<Sellp>;
104
105 friend class Sellp<next_precision<ValueType>, IndexType>;
106
107 void convert_to(
108 Sellp<next_precision<ValueType>, IndexType>* result) const override;
109
110 void move_to(Sellp<next_precision<ValueType>, IndexType>* result) override;
111
112 void convert_to(Dense<ValueType>* other) const override;
113
114 void move_to(Dense<ValueType>* other) override;
115
116 void convert_to(Csr<ValueType, IndexType>* other) const override;
117
118 void move_to(Csr<ValueType, IndexType>* other) override;
119
120 void read(const mat_data& data) override;
121
122 void read(const device_mat_data& data) override;
123
124 void read(device_mat_data&& data) override;
125
126 void write(mat_data& data) const override;
127
128 std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
129
130 std::unique_ptr<absolute_type> compute_absolute() const override;
131
133
139 value_type* get_values() noexcept { return values_.get_data(); }
140
148 const value_type* get_const_values() const noexcept
149 {
150 return values_.get_const_data();
151 }
152
158 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
159
168 {
169 return col_idxs_.get_const_data();
170 }
171
178 {
179 return slice_lengths_.get_data();
180 }
181
190 {
191 return slice_lengths_.get_const_data();
192 }
193
199 size_type* get_slice_sets() noexcept { return slice_sets_.get_data(); }
200
209 {
210 return slice_sets_.get_const_data();
211 }
212
218 size_type get_slice_size() const noexcept { return slice_size_; }
219
225 size_type get_stride_factor() const noexcept { return stride_factor_; }
226
233 {
234 return values_.get_num_elems() / slice_size_;
235 }
236
243 {
244 return values_.get_num_elems();
245 }
246
260 size_type idx) noexcept
261 {
262 return values_.get_data()[this->linearize_index(row, slice_set, idx)];
263 }
264
269 size_type idx) const noexcept
270 {
271 return values_
272 .get_const_data()[this->linearize_index(row, slice_set, idx)];
273 }
274
288 size_type idx) noexcept
289 {
290 return this->get_col_idxs()[this->linearize_index(row, slice_set, idx)];
291 }
292
297 size_type idx) const noexcept
298 {
299 return this
300 ->get_const_col_idxs()[this->linearize_index(row, slice_set, idx)];
301 }
302
308
315
320 Sellp(const Sellp&);
321
328
329protected:
338 Sellp(std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{})
339 : Sellp(std::move(exec), size,
340 ceildiv(size[0], default_slice_size) * size[1])
341 {}
342
351 Sellp(std::shared_ptr<const Executor> exec, const dim<2>& size,
353 : Sellp(std::move(exec), size, default_slice_size,
354 default_stride_factor, total_cols)
355 {}
356
367 Sellp(std::shared_ptr<const Executor> exec, const dim<2>& size,
369 : EnableLinOp<Sellp>(exec, size),
370 values_(exec, slice_size * total_cols),
371 col_idxs_(exec, slice_size * total_cols),
372 slice_lengths_(exec, ceildiv(size[0], slice_size)),
373 slice_sets_(exec, ceildiv(size[0], slice_size) + 1),
374 slice_size_(slice_size),
375 stride_factor_(stride_factor)
376 {
377 slice_sets_.fill(0);
378 slice_lengths_.fill(0);
379 }
380
381 void apply_impl(const LinOp* b, LinOp* x) const override;
382
383 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
384 LinOp* x) const override;
385
386 size_type linearize_index(size_type row, size_type slice_set,
387 size_type col) const noexcept
388 {
389 return (slice_set + col) * slice_size_ + row;
390 }
391
392private:
393 array<value_type> values_;
394 array<index_type> col_idxs_;
395 array<size_type> slice_lengths_;
396 array<size_type> slice_sets_;
397 size_type slice_size_;
398 size_type stride_factor_;
399};
400
401
402} // namespace matrix
403} // namespace gko
404
405
406#endif // GKO_PUBLIC_CORE_MATRIX_SELLP_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:499
The diagonal of a LinOp implementing this interface can be extracted.
Definition lin_op.hpp:772
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:823
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
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
SELL-P is a matrix format similar to ELL format.
Definition sellp.hpp:80
size_type get_slice_size() const noexcept
Returns the size of a slice.
Definition sellp.hpp:218
void read(const device_mat_data &data) override
Reads a matrix from a device_matrix_data structure.
value_type & val_at(size_type row, size_type slice_set, size_type idx) noexcept
Returns the idx-th non-zero element of the row-th row with slice_set slice set.
Definition sellp.hpp:259
index_type & col_at(size_type row, size_type slice_set, size_type idx) noexcept
Returns the idx-th column index of the row-th row with slice_set slice set.
Definition sellp.hpp:287
Sellp(const Sellp &)
Copy-assigns a Sellp matrix.
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
value_type * get_values() noexcept
Returns the values of the matrix.
Definition sellp.hpp:139
void read(device_mat_data &&data) override
Reads a matrix from a device_matrix_data structure.
Sellp & operator=(Sellp &&)
Move-assigns a Sellp matrix.
size_type get_stride_factor() const noexcept
Returns the stride factor(t) of SELL-P.
Definition sellp.hpp:225
const size_type * get_const_slice_sets() const noexcept
Returns the offsets of slices.
Definition sellp.hpp:208
index_type * get_col_idxs() noexcept
Returns the column indexes of the matrix.
Definition sellp.hpp:158
size_type get_total_cols() const noexcept
Returns the total column number.
Definition sellp.hpp:232
index_type col_at(size_type row, size_type slice_set, size_type idx) const noexcept
Returns the idx-th column index of the row-th row with slice_set slice set.
Definition sellp.hpp:296
size_type * get_slice_lengths() noexcept
Returns the lengths(columns) of slices.
Definition sellp.hpp:177
Sellp & operator=(const Sellp &)
Copy-assigns a Sellp matrix.
void compute_absolute_inplace() override
Compute absolute inplace on each element.
size_type * get_slice_sets() noexcept
Returns the offsets of slices.
Definition sellp.hpp:199
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition sellp.hpp:242
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
value_type val_at(size_type row, size_type slice_set, size_type idx) const noexcept
Returns the idx-th non-zero element of the row-th row with slice_set slice set.
Definition sellp.hpp:268
const index_type * get_const_col_idxs() const noexcept
Returns the column indexes of the matrix.
Definition sellp.hpp:167
Sellp(Sellp &&)
Move-assigns a Sellp matrix.
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
const size_type * get_const_slice_lengths() const noexcept
Returns the lengths(columns) of slices.
Definition sellp.hpp:189
const value_type * get_const_values() const noexcept
Returns the values of the matrix.
Definition sellp.hpp:148
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:354
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition math.hpp:373
constexpr int64 ceildiv(int64 num, int64 den)
Performs integer division with rounding up.
Definition math.hpp:641
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