Ginkgo  Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
device_matrix_data.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
6 #define GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
7 
8 
9 #include <ginkgo/core/base/array.hpp>
10 #include <ginkgo/core/base/dim.hpp>
11 #include <ginkgo/core/base/exception_helpers.hpp>
12 #include <ginkgo/core/base/executor.hpp>
13 #include <ginkgo/core/base/matrix_data.hpp>
14 #include <ginkgo/core/base/temporary_clone.hpp>
15 
16 
17 namespace gko {
18 
19 
35 template <typename ValueType, typename IndexType>
37  GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
38 
39 public:
40  using value_type = ValueType;
41  using index_type = IndexType;
44 
54  explicit device_matrix_data(std::shared_ptr<const Executor> exec,
55  dim<2> size = {}, size_type num_entries = 0);
56 
65  device_matrix_data(std::shared_ptr<const Executor> exec,
66  const device_matrix_data& data);
67 
77  device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
78  array<index_type> row_idxs, array<index_type> col_idxs,
79  array<value_type> values);
80 
85  template <typename InputValueType, typename RowIndexType,
86  typename ColIndexType>
87  GKO_DEPRECATED(
88  "explicitly construct the gko::array arguments instead of passing "
89  "initializer lists")
90  device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
91  std::initializer_list<RowIndexType> row_idxs,
92  std::initializer_list<ColIndexType> col_idxs,
93  std::initializer_list<InputValueType> values)
94  : device_matrix_data{exec, size,
95  array<index_type>{exec, std::move(row_idxs)},
96  array<index_type>{exec, std::move(col_idxs)},
97  array<value_type>{exec, std::move(values)}}
98  {}
99 
106  host_type copy_to_host() const;
107 
118  std::shared_ptr<const Executor> exec, const host_type& data);
119 
123  void fill_zero();
124 
130  void sort_row_major();
131 
137  void remove_zeros();
138 
144  void sum_duplicates();
145 
151  std::shared_ptr<const Executor> get_executor() const
152  {
153  return values_.get_executor();
154  }
155 
161  dim<2> get_size() const { return size_; }
162 
168  GKO_DEPRECATED("use get_num_stored_elements()")
170 
176  size_type get_num_stored_elements() const { return values_.get_size(); }
177 
183  index_type* get_row_idxs() { return row_idxs_.get_data(); }
184 
190  const index_type* get_const_row_idxs() const
191  {
192  return row_idxs_.get_const_data();
193  }
194 
200  index_type* get_col_idxs() { return col_idxs_.get_data(); }
201 
207  const index_type* get_const_col_idxs() const
208  {
209  return col_idxs_.get_const_data();
210  }
211 
217  value_type* get_values() { return values_.get_data(); }
218 
224  const value_type* get_const_values() const
225  {
226  return values_.get_const_data();
227  }
228 
235  void resize_and_reset(size_type new_num_entries);
236 
244  void resize_and_reset(dim<2> new_size, size_type new_num_entries);
245 
249  struct arrays {
250  array<index_type> row_idxs;
251  array<index_type> col_idxs;
252  array<value_type> values;
253  };
254 
261  arrays empty_out();
262 
263 private:
264  dim<2> size_;
265  array<index_type> row_idxs_;
266  array<index_type> col_idxs_;
267  array<value_type> values_;
268 };
269 
270 
271 namespace detail {
272 
273 
274 template <typename ValueType, typename IndexType>
275 struct temporary_clone_helper<device_matrix_data<ValueType, IndexType>> {
276  static std::unique_ptr<device_matrix_data<ValueType, IndexType>> create(
277  std::shared_ptr<const Executor> exec,
278  device_matrix_data<ValueType, IndexType>* ptr, bool copy_data)
279  {
280  if (copy_data) {
281  return std::make_unique<device_matrix_data<ValueType, IndexType>>(
282  std::move(exec), *ptr);
283  } else {
284  return std::make_unique<device_matrix_data<ValueType, IndexType>>(
285  std::move(exec), ptr->get_size(),
286  ptr->get_num_stored_elements());
287  }
288  }
289 };
290 
291 template <typename ValueType, typename IndexType>
292 struct temporary_clone_helper<const device_matrix_data<ValueType, IndexType>> {
293  static std::unique_ptr<const device_matrix_data<ValueType, IndexType>>
294  create(std::shared_ptr<const Executor> exec,
295  const device_matrix_data<ValueType, IndexType>* ptr, bool)
296  {
297  return std::make_unique<const device_matrix_data<ValueType, IndexType>>(
298  std::move(exec), *ptr);
299  }
300 };
301 
302 
303 template <typename ValueType, typename IndexType>
304 class copy_back_deleter<device_matrix_data<ValueType, IndexType>>
305  : public copy_back_deleter_from_assignment<
306  device_matrix_data<ValueType, IndexType>> {
307 public:
308  using copy_back_deleter_from_assignment<device_matrix_data<
309  ValueType, IndexType>>::copy_back_deleter_from_assignment;
310 };
311 
312 
313 } // namespace detail
314 } // namespace gko
315 
316 
317 #endif // GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
gko::device_matrix_data::copy_to_host
host_type copy_to_host() const
Copies the device_matrix_data entries to the host to return a regular matrix_data object with the sam...
gko::device_matrix_data::get_values
value_type * get_values()
Returns a pointer to the value array.
Definition: device_matrix_data.hpp:217
gko::device_matrix_data::get_const_row_idxs
const index_type * get_const_row_idxs() const
Returns a pointer to the constant row index array.
Definition: device_matrix_data.hpp:190
gko::matrix_data_entry
Type used to store nonzeros.
Definition: matrix_data.hpp:60
gko::device_matrix_data::get_executor
std::shared_ptr< const Executor > get_executor() const
Returns the executor used to store the device_matrix_data entries.
Definition: device_matrix_data.hpp:151
gko::device_matrix_data::get_row_idxs
index_type * get_row_idxs()
Returns a pointer to the row index array.
Definition: device_matrix_data.hpp:183
gko::device_matrix_data::create_from_host
static device_matrix_data create_from_host(std::shared_ptr< const Executor > exec, const host_type &data)
Creates a device_matrix_data object from the given host data on the given executor.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::device_matrix_data::remove_zeros
void remove_zeros()
Removes all zero entries from the storage.
gko::device_matrix_data::empty_out
arrays empty_out()
Moves out the internal arrays of the device_matrix_data object and resets it to an empty 0x0 matrix.
gko::device_matrix_data::sum_duplicates
void sum_duplicates()
Sums up all duplicate entries pointing to the same non-zero location.
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::device_matrix_data::get_num_elems
size_type get_num_elems() const
Returns the number of stored elements of the matrix.
Definition: device_matrix_data.hpp:169
gko::array< index_type >
gko::device_matrix_data::get_col_idxs
index_type * get_col_idxs()
Returns a pointer to the column index array.
Definition: device_matrix_data.hpp:200
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:686
gko::device_matrix_data::get_size
dim< 2 > get_size() const
Returns the dimensions of the matrix.
Definition: device_matrix_data.hpp:161
gko::device_matrix_data::get_const_values
const value_type * get_const_values() const
Returns a pointer to the constant value array.
Definition: device_matrix_data.hpp:224
gko::array::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition: array.hpp:702
gko::device_matrix_data::get_num_stored_elements
size_type get_num_stored_elements() const
Returns the number of stored elements of the matrix.
Definition: device_matrix_data.hpp:176
gko::device_matrix_data::fill_zero
void fill_zero()
Fills the matrix entries with zeros.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
gko::array::get_const_data
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:695
gko::device_matrix_data::sort_row_major
void sort_row_major()
Sorts the matrix entries in row-major order This means that they will be sorted by row index first,...
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
gko::device_matrix_data::device_matrix_data
device_matrix_data(std::shared_ptr< const Executor > exec, dim< 2 > size={}, size_type num_entries=0)
Initializes a new device_matrix_data object.
gko::device_matrix_data
This type is a device-side equivalent to matrix_data.
Definition: device_matrix_data.hpp:36
gko::device_matrix_data::resize_and_reset
void resize_and_reset(size_type new_num_entries)
Resizes the internal storage to the given number of stored matrix entries.
gko::device_matrix_data::get_const_col_idxs
const index_type * get_const_col_idxs() const
Returns a pointer to the constant column index array.
Definition: device_matrix_data.hpp:207
gko::device_matrix_data::arrays
Stores the internal arrays of a device_matrix_data object.
Definition: device_matrix_data.hpp:249