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
ell.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_ELL_HPP_
34 #define GKO_CORE_MATRIX_ELL_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 template <typename ValueType>
46 class Dense;
47 
48 template <typename ValueType, typename IndexType>
49 class Csr;
50 
51 
70 template <typename ValueType = default_precision, typename IndexType = int32>
71 class Ell : public EnableLinOp<Ell<ValueType, IndexType>>,
72  public EnableCreateMethod<Ell<ValueType, IndexType>>,
73  public ConvertibleTo<Dense<ValueType>>,
74  public ConvertibleTo<Csr<ValueType, IndexType>>,
75  public ReadableFromMatrixData<ValueType, IndexType>,
76  public WritableToMatrixData<ValueType, IndexType> {
77  friend class EnableCreateMethod<Ell>;
78  friend class EnablePolymorphicObject<Ell, LinOp>;
79  friend class Dense<ValueType>;
80  friend class Csr<ValueType, IndexType>;
81 
82 public:
85 
86  using value_type = ValueType;
87  using index_type = IndexType;
88  using mat_data = matrix_data<ValueType, IndexType>;
89 
90  void convert_to(Dense<ValueType> *other) const override;
91 
92  void move_to(Dense<ValueType> *other) override;
93 
94  void convert_to(Csr<ValueType, IndexType> *other) const override;
95 
96  void move_to(Csr<ValueType, IndexType> *other) override;
97 
98  void read(const mat_data &data) override;
99 
100  void write(mat_data &data) const override;
101 
107  value_type *get_values() noexcept { return values_.get_data(); }
108 
116  const value_type *get_const_values() const noexcept
117  {
118  return values_.get_const_data();
119  }
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 
146  {
147  return num_stored_elements_per_row_;
148  }
149 
155  size_type get_stride() const noexcept { return stride_; }
156 
163  {
164  return values_.get_num_elems();
165  }
166 
177  value_type &val_at(size_type row, size_type idx) noexcept
178  {
179  return values_.get_data()[this->linearize_index(row, idx)];
180  }
181 
185  value_type val_at(size_type row, size_type idx) const noexcept
186  {
187  return values_.get_const_data()[this->linearize_index(row, idx)];
188  }
189 
200  index_type &col_at(size_type row, size_type idx) noexcept
201  {
202  return this->get_col_idxs()[this->linearize_index(row, idx)];
203  }
204 
208  index_type col_at(size_type row, size_type idx) const noexcept
209  {
210  return this->get_const_col_idxs()[this->linearize_index(row, idx)];
211  }
212 
213 protected:
223  Ell(std::shared_ptr<const Executor> exec, const dim<2> &size = dim<2>{})
224  : Ell(std::move(exec), size, size[1])
225  {}
226 
236  Ell(std::shared_ptr<const Executor> exec, const dim<2> &size,
237  size_type num_stored_elements_per_row)
238  : Ell(std::move(exec), size, num_stored_elements_per_row, size[0])
239  {}
240 
250  Ell(std::shared_ptr<const Executor> exec, const dim<2> &size,
251  size_type num_stored_elements_per_row, size_type stride)
252  : EnableLinOp<Ell>(exec, size),
253  values_(exec, stride * num_stored_elements_per_row),
254  col_idxs_(exec, stride * num_stored_elements_per_row),
255  num_stored_elements_per_row_(num_stored_elements_per_row),
256  stride_(stride)
257  {}
258 
259 
280  template <typename ValuesArray, typename ColIdxsArray>
281  Ell(std::shared_ptr<const Executor> exec, const dim<2> &size,
282  ValuesArray &&values, ColIdxsArray &&col_idxs,
283  size_type num_stored_elements_per_row, size_type stride)
284  : EnableLinOp<Ell>(exec, size),
285  values_{exec, std::forward<ValuesArray>(values)},
286  col_idxs_{exec, std::forward<ColIdxsArray>(col_idxs)},
287  num_stored_elements_per_row_{num_stored_elements_per_row},
288  stride_{stride}
289  {
290  GKO_ENSURE_IN_BOUNDS(num_stored_elements_per_row_ * stride_ - 1,
291  values_.get_num_elems());
292  GKO_ENSURE_IN_BOUNDS(num_stored_elements_per_row_ * stride_ - 1,
293  col_idxs_.get_num_elems());
294  }
295 
296  void apply_impl(const LinOp *b, LinOp *x) const override;
297 
298  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
299  LinOp *x) const override;
300 
301  size_type linearize_index(size_type row, size_type col) const noexcept
302  {
303  return row + stride_ * col;
304  }
305 
306 private:
307  Array<value_type> values_;
308  Array<index_type> col_idxs_;
309  size_type num_stored_elements_per_row_;
310  size_type stride_;
311 };
312 
313 
314 } // namespace matrix
315 } // namespace gko
316 
317 
318 #endif // GKO_CORE_MATRIX_ELL_HPP_
value_type * get_values() noexcept
Returns the values of the matrix.
Definition: ell.hpp:107
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
value_type val_at(size_type row, size_type idx) const noexcept
Returns the idx-th non-zero element of the row-th row .
Definition: ell.hpp:185
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:53
value_type & val_at(size_type row, size_type idx) noexcept
Returns the idx-th non-zero element of the row-th row .
Definition: ell.hpp:177
index_type * get_col_idxs() noexcept
Returns the column indexes of the matrix.
Definition: ell.hpp:126
const index_type * get_const_col_idxs() const noexcept
Returns the column indexes of the matrix.
Definition: ell.hpp:135
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_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: ell.hpp:162
index_type & col_at(size_type row, size_type idx) noexcept
Returns the idx-th column index of the row-th row .
Definition: ell.hpp:200
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 convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:558
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:560
const value_type * get_const_values() const noexcept
Returns the values of the matrix.
Definition: ell.hpp:116
size_type get_num_stored_elements_per_row() const noexcept
Returns the number of stored elements per row.
Definition: ell.hpp:145
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
index_type col_at(size_type row, size_type idx) const noexcept
Returns the idx-th column index of the row-th row .
Definition: ell.hpp:208
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: ell.hpp:155