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
coo.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_COO_HPP_
34 #define GKO_CORE_MATRIX_COO_HPP_
35 
36 
37 #include <ginkgo/core/base/array.hpp>
38 #include <ginkgo/core/base/lin_op.hpp>
39 
40 
41 namespace gko {
47 namespace matrix {
48 
49 
50 template <typename ValueType, typename IndexType>
51 class Csr;
52 
53 
54 template <typename ValueType>
55 class Dense;
56 
57 
72 template <typename ValueType = default_precision, typename IndexType = int32>
73 class Coo : public EnableLinOp<Coo<ValueType, IndexType>>,
74  public EnableCreateMethod<Coo<ValueType, IndexType>>,
75  public ConvertibleTo<Csr<ValueType, IndexType>>,
76  public ConvertibleTo<Dense<ValueType>>,
77  public ReadableFromMatrixData<ValueType, IndexType>,
78  public WritableToMatrixData<ValueType, IndexType> {
79  friend class EnableCreateMethod<Coo>;
80  friend class EnablePolymorphicObject<Coo, LinOp>;
81  friend class Csr<ValueType, IndexType>;
82  friend class Dense<ValueType>;
83 
84 public:
87 
88  using value_type = ValueType;
89  using index_type = IndexType;
91 
92  void convert_to(Csr<ValueType, IndexType> *other) const override;
93 
94  void move_to(Csr<ValueType, IndexType> *other) override;
95 
96  void convert_to(Dense<ValueType> *other) const override;
97 
98  void move_to(Dense<ValueType> *other) override;
99 
100  void read(const mat_data &data) override;
101 
102  void write(mat_data &data) const override;
103 
109  value_type *get_values() noexcept { return values_.get_data(); }
110 
118  const value_type *get_const_values() const noexcept
119  {
120  return values_.get_const_data();
121  }
122 
128  index_type *get_col_idxs() noexcept { return col_idxs_.get_data(); }
129 
137  const index_type *get_const_col_idxs() const noexcept
138  {
139  return col_idxs_.get_const_data();
140  }
141 
147  index_type *get_row_idxs() noexcept { return row_idxs_.get_data(); }
148 
156  const index_type *get_const_row_idxs() const noexcept
157  {
158  return row_idxs_.get_const_data();
159  }
160 
167  {
168  return values_.get_num_elems();
169  }
170 
181  LinOp *apply2(const LinOp *b, LinOp *x)
182  {
183  this->validate_application_parameters(b, x);
184  auto exec = this->get_executor();
185  this->apply2_impl(make_temporary_clone(exec, b).get(),
186  make_temporary_clone(exec, x).get());
187  return this;
188  }
189 
193  const LinOp *apply2(const LinOp *b, LinOp *x) const
194  {
195  this->validate_application_parameters(b, x);
196  auto exec = this->get_executor();
197  this->apply2_impl(make_temporary_clone(exec, b).get(),
198  make_temporary_clone(exec, x).get());
199  return this;
200  }
201 
211  LinOp *apply2(const LinOp *alpha, const LinOp *b, LinOp *x)
212  {
213  this->validate_application_parameters(b, x);
214  GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
215  auto exec = this->get_executor();
216  this->apply2_impl(make_temporary_clone(exec, alpha).get(),
217  make_temporary_clone(exec, b).get(),
218  make_temporary_clone(exec, x).get());
219  return this;
220  }
221 
225  const LinOp *apply2(const LinOp *alpha, const LinOp *b, LinOp *x) const
226  {
227  this->validate_application_parameters(b, x);
228  GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
229  auto exec = this->get_executor();
230  this->apply2_impl(make_temporary_clone(exec, alpha).get(),
231  make_temporary_clone(exec, b).get(),
232  make_temporary_clone(exec, x).get());
233  return this;
234  }
235 
236 protected:
244  Coo(std::shared_ptr<const Executor> exec, const dim<2> &size = dim<2>{},
245  size_type num_nonzeros = {})
246  : EnableLinOp<Coo>(exec, size),
247  values_(exec, num_nonzeros),
248  col_idxs_(exec, num_nonzeros),
249  row_idxs_(exec, num_nonzeros)
250  {}
251 
272  template <typename ValuesArray, typename ColIdxsArray,
273  typename RowIdxsArray>
274  Coo(std::shared_ptr<const Executor> exec, const dim<2> &size,
275  ValuesArray &&values, ColIdxsArray &&col_idxs, RowIdxsArray &&row_idxs)
276  : EnableLinOp<Coo>(exec, size),
277  values_{exec, std::forward<ValuesArray>(values)},
278  col_idxs_{exec, std::forward<ColIdxsArray>(col_idxs)},
279  row_idxs_{exec, std::forward<RowIdxsArray>(row_idxs)}
280  {
281  GKO_ENSURE_IN_BOUNDS(values_.get_num_elems() - 1,
282  col_idxs_.get_num_elems());
283  GKO_ENSURE_IN_BOUNDS(values_.get_num_elems() - 1,
284  row_idxs_.get_num_elems());
285  }
286 
287  void apply_impl(const LinOp *b, LinOp *x) const override;
288 
289  void apply_impl(const LinOp *alpha, const LinOp *b, const LinOp *beta,
290  LinOp *x) const override;
291 
292  void apply2_impl(const LinOp *b, LinOp *x) const;
293 
294  void apply2_impl(const LinOp *alpha, const LinOp *b, LinOp *x) const;
295 
296 private:
297  Array<value_type> values_;
298  Array<index_type> col_idxs_;
299  Array<index_type> row_idxs_;
300 };
301 
302 
303 } // namespace matrix
304 } // namespace gko
305 
306 
307 #endif // GKO_CORE_MATRIX_COO_HPP_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory...
Definition: polymorphic_object.hpp:576
size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
const LinOp * apply2(const LinOp *b, LinOp *x) const
Definition: coo.hpp:193
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:380
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
const index_type * get_const_row_idxs() const noexcept
Definition: coo.hpp:156
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:505
index_type * get_col_idxs() noexcept
Returns the column indexes of the matrix.
Definition: coo.hpp:128
LinOp * apply2(const LinOp *alpha, const LinOp *b, LinOp *x)
Performs the operation x = alpha * Coo * b + x.
Definition: coo.hpp:211
const value_type * get_const_values() const noexcept
Returns the values of the matrix.
Definition: coo.hpp:118
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
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
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: coo.hpp:51
const LinOp * apply2(const LinOp *alpha, const LinOp *b, LinOp *x) const
Performs the operation x = alpha * Coo * b + x.
Definition: coo.hpp:225
index_type * get_row_idxs() noexcept
Returns the row indexes of the matrix.
Definition: coo.hpp:147
value_type * get_values() noexcept
Returns the values of the matrix.
Definition: coo.hpp:109
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: coo.hpp:55
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:73
const index_type * get_const_col_idxs() const noexcept
Returns the column indexes of the matrix.
Definition: coo.hpp:137
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:201
LinOp * apply2(const LinOp *b, LinOp *x)
Applies Coo matrix axpy to a vector (or a sequence of vectors).
Definition: coo.hpp:181
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:446
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
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: coo.hpp:166
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:102
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition: lin_op.hpp:426
temporary_clone< T > make_temporary_clone(std::shared_ptr< const Executor > exec, T *ptr)
Creates a temporary_clone.
Definition: utils.hpp:474