Ginkgo  Generated from tags/v1.0.0^0 branch based on master. Ginkgo version 1.0.0
A numerical linear algebra library targeting many-core architectures
sellp.hpp
1 /*******************************<GINKGO LICENSE>******************************
2 Copyright (c) 2017-2019, the Ginkgo authors
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************<GINKGO LICENSE>*******************************/
32 
33 #ifndef GKO_CORE_MATRIX_SELLP_HPP_
34 #define GKO_CORE_MATRIX_SELLP_HPP_
35 
36 
37 #include <ginkgo/core/base/array.hpp>
38 #include <ginkgo/core/base/lin_op.hpp>
39 
40 
41 namespace gko {
42 namespace matrix {
43 
44 
45 constexpr int default_slice_size = 64;
46 constexpr int default_stride_factor = 1;
47 
48 
49 template <typename ValueType>
50 class Dense;
51 
52 template <typename ValueType, typename IndexType>
53 class Csr;
54 
67 template <typename ValueType = default_precision, typename IndexType = int32>
68 class Sellp : public EnableLinOp<Sellp<ValueType, IndexType>>,
69  public EnableCreateMethod<Sellp<ValueType, IndexType>>,
70  public ConvertibleTo<Dense<ValueType>>,
71  public ConvertibleTo<Csr<ValueType, IndexType>>,
72  public ReadableFromMatrixData<ValueType, IndexType>,
73  public WritableToMatrixData<ValueType, IndexType> {
74  friend class EnableCreateMethod<Sellp>;
75  friend class EnablePolymorphicObject<Sellp, LinOp>;
76  friend class Dense<ValueType>;
77  friend class Csr<ValueType, IndexType>;
78 
79 public:
82 
83  using value_type = ValueType;
84  using index_type = IndexType;
85  using mat_data = matrix_data<ValueType, IndexType>;
86 
87  void convert_to(Dense<ValueType> *other) const override;
88 
89  void move_to(Dense<ValueType> *other) override;
90 
91  void convert_to(Csr<ValueType, IndexType> *other) const override;
92 
93  void move_to(Csr<ValueType, IndexType> *other) override;
94 
95  void read(const mat_data &data) override;
96 
97  void write(mat_data &data) const override;
98 
104  value_type *get_values() noexcept { return values_.get_data(); }
105 
113  const value_type *get_const_values() const noexcept
114  {
115  return values_.get_const_data();
116  }
117 
123  index_type *get_col_idxs() noexcept { return col_idxs_.get_data(); }
124 
132  const index_type *get_const_col_idxs() const noexcept
133  {
134  return col_idxs_.get_const_data();
135  }
136 
143  {
144  return slice_lengths_.get_data();
145  }
146 
154  const size_type *get_const_slice_lengths() const noexcept
155  {
156  return slice_lengths_.get_const_data();
157  }
158 
164  size_type *get_slice_sets() noexcept { return slice_sets_.get_data(); }
165 
173  const size_type *get_const_slice_sets() const noexcept
174  {
175  return slice_sets_.get_const_data();
176  }
177 
183  size_type get_slice_size() const noexcept { return slice_size_; }
184 
190  size_type get_stride_factor() const noexcept { return stride_factor_; }
191 
197  size_type get_total_cols() const noexcept { return total_cols_; }
198 
205  {
206  return values_.get_num_elems();
207  }
208 
221  value_type &val_at(size_type row, size_type slice_set,
222  size_type idx) noexcept
223  {
224  return values_.get_data()[this->linearize_index(row, slice_set, idx)];
225  }
226 
230  value_type val_at(size_type row, size_type slice_set, size_type idx) const
231  noexcept
232  {
233  return values_
234  .get_const_data()[this->linearize_index(row, slice_set, idx)];
235  }
236 
249  index_type &col_at(size_type row, size_type slice_set,
250  size_type idx) noexcept
251  {
252  return this->get_col_idxs()[this->linearize_index(row, slice_set, idx)];
253  }
254 
258  index_type col_at(size_type row, size_type slice_set, size_type idx) const
259  noexcept
260  {
261  return this
262  ->get_const_col_idxs()[this->linearize_index(row, slice_set, idx)];
263  }
264 
265 protected:
274  Sellp(std::shared_ptr<const Executor> exec, const dim<2> &size = dim<2>{})
275  : Sellp(std::move(exec), size,
276  ceildiv(size[0], default_slice_size) * size[1])
277  {}
278 
287  Sellp(std::shared_ptr<const Executor> exec, const dim<2> &size,
288  size_type total_cols)
289  : Sellp(std::move(exec), size, default_slice_size,
290  default_stride_factor, total_cols)
291  {}
292 
303  Sellp(std::shared_ptr<const Executor> exec, const dim<2> &size,
304  size_type slice_size, size_type stride_factor, size_type total_cols)
305  : EnableLinOp<Sellp>(exec, size),
306  values_(exec, slice_size * total_cols),
307  col_idxs_(exec, slice_size * total_cols),
308  slice_lengths_(exec,
309  (size[0] == 0) ? 0 : ceildiv(size[0], slice_size)),
310  slice_sets_(exec,
311  (size[0] == 0) ? 0 : ceildiv(size[0], slice_size) + 1),
312  slice_size_(slice_size),
313  stride_factor_(stride_factor),
314  total_cols_(total_cols)
315  {}
316 
317  void apply_impl(const LinOp *b, LinOp *x) const override;
318 
319  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
320  LinOp *x) const override;
321 
322  size_type linearize_index(size_type row, size_type slice_set,
323  size_type col) const noexcept
324  {
325  return (slice_set + col) * slice_size_ + row;
326  }
327 
328 private:
329  Array<value_type> values_{0};
330  Array<index_type> col_idxs_{0};
331  Array<size_type> slice_lengths_{0};
332  Array<size_type> slice_sets_{0};
333  size_type slice_size_;
334  size_type stride_factor_;
335  size_type total_cols_{0};
336 };
337 
338 
339 } // namespace matrix
340 } // namespace gko
341 
342 
343 #endif // GKO_CORE_MATRIX_SELLP_HPP_
constexpr int64 ceildiv(int64 num, int64 den)
Performs integer division with rounding up.
Definition: math.hpp:280
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:221
size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
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:258
size_type get_total_cols() const noexcept
Returns the total column number.
Definition: sellp.hpp:197
const size_type * get_const_slice_sets() const noexcept
Returns the offsets of slices.
Definition: sellp.hpp:173
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
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:406
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
size_type get_slice_size() const noexcept
Returns the size of a slice.
Definition: sellp.hpp:183
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:249
value_type * get_values() noexcept
Returns the values of the matrix.
Definition: sellp.hpp:104
SELL-P is a matrix format similar to ELL format.
Definition: csr.hpp:57
Definition: lin_op.hpp:134
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
const index_type * get_const_col_idxs() const noexcept
Returns the column indexes of the matrix.
Definition: sellp.hpp:132
const value_type * get_const_values() const noexcept
Returns the values of the matrix.
Definition: sellp.hpp:113
size_type * get_slice_lengths() noexcept
Returns the lengths(columns) of slices.
Definition: sellp.hpp:142
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:558
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:560
size_type * get_slice_sets() noexcept
Returns the offsets of slices.
Definition: sellp.hpp:164
size_type get_stride_factor() const noexcept
Returns the stride factor(t) of SELL-P.
Definition: sellp.hpp:190
index_type * get_col_idxs() noexcept
Returns the column indexes of the matrix.
Definition: sellp.hpp:123
const size_type * get_const_slice_lengths() const noexcept
Returns the lengths(columns) of slices.
Definition: sellp.hpp:154
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: sellp.hpp:204
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the Array. ...
Definition: array.hpp:397
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:230