Ginkgo Generated from branch based on main. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
device_matrix_data.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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
17namespace gko {
18
19
35template <typename ValueType, typename IndexType>
37 GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
38
39public:
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
76 device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
77 array<index_type> row_idxs, array<index_type> col_idxs,
78 array<value_type> values);
79
84 template <typename InputValueType, typename RowIndexType,
85 typename ColIndexType>
86 GKO_DEPRECATED(
87 "explicitly construct the gko::array arguments instead of passing "
88 "initializer lists")
89 device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
90 std::initializer_list<RowIndexType> row_idxs,
91 std::initializer_list<ColIndexType> col_idxs,
92 std::initializer_list<InputValueType> values)
93 : device_matrix_data{exec, size,
94 array<index_type>{exec, std::move(row_idxs)},
95 array<index_type>{exec, std::move(col_idxs)},
96 array<value_type>{exec, std::move(values)}}
97 {}
98
105 host_type copy_to_host() const;
106
117 std::shared_ptr<const Executor> exec, const host_type& data);
118
122 void fill_zero();
123
130
137
144
150 std::shared_ptr<const Executor> get_executor() const
151 {
152 return values_.get_executor();
153 }
154
160 dim<2> get_size() const { return size_; }
161
167 GKO_DEPRECATED("use get_num_stored_elements()")
169
175 size_type get_num_stored_elements() const { return values_.get_size(); }
176
182 index_type* get_row_idxs() { return row_idxs_.get_data(); }
183
189 const index_type* get_const_row_idxs() const
190 {
191 return row_idxs_.get_const_data();
192 }
193
199 index_type* get_col_idxs() { return col_idxs_.get_data(); }
200
206 const index_type* get_const_col_idxs() const
207 {
208 return col_idxs_.get_const_data();
209 }
210
216 value_type* get_values() { return values_.get_data(); }
217
223 const value_type* get_const_values() const
224 {
225 return values_.get_const_data();
226 }
227
234 void resize_and_reset(size_type new_num_entries);
235
243 void resize_and_reset(dim<2> new_size, size_type new_num_entries);
244
248 struct arrays {
249 array<index_type> row_idxs;
250 array<index_type> col_idxs;
251 array<value_type> values;
252 };
253
260 arrays empty_out();
261
262private:
263 dim<2> size_;
264 array<index_type> row_idxs_;
265 array<index_type> col_idxs_;
266 array<value_type> values_;
267};
268
269
270namespace detail {
271
272
273template <typename ValueType, typename IndexType>
274struct temporary_clone_helper<device_matrix_data<ValueType, IndexType>> {
275 static std::unique_ptr<device_matrix_data<ValueType, IndexType>> create(
276 std::shared_ptr<const Executor> exec,
277 device_matrix_data<ValueType, IndexType>* ptr, bool copy_data)
278 {
279 if (copy_data) {
280 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
281 std::move(exec), *ptr);
282 } else {
283 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
284 std::move(exec), ptr->get_size(),
286 }
287 }
288};
289
290template <typename ValueType, typename IndexType>
291struct temporary_clone_helper<const device_matrix_data<ValueType, IndexType>> {
292 static std::unique_ptr<const device_matrix_data<ValueType, IndexType>>
293 create(std::shared_ptr<const Executor> exec,
294 const device_matrix_data<ValueType, IndexType>* ptr, bool)
295 {
296 return std::make_unique<const device_matrix_data<ValueType, IndexType>>(
297 std::move(exec), *ptr);
298 }
299};
300
301
302template <typename ValueType, typename IndexType>
303class copy_back_deleter<device_matrix_data<ValueType, IndexType>>
304 : public copy_back_deleter_from_assignment<
305 device_matrix_data<ValueType, IndexType>> {
306public:
307 using copy_back_deleter_from_assignment<device_matrix_data<
308 ValueType, IndexType>>::copy_back_deleter_from_assignment;
309};
310
311
312} // namespace detail
313} // namespace gko
314
315
316#endif // GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
dim< 2 > get_size() const
Returns the dimensions of the matrix.
Definition device_matrix_data.hpp:160
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.
const index_type * get_const_row_idxs() const
Returns a pointer to the constant row index array.
Definition device_matrix_data.hpp:189
index_type * get_row_idxs()
Returns a pointer to the row index array.
Definition device_matrix_data.hpp:182
const value_type * get_const_values() const
Returns a pointer to the constant value array.
Definition device_matrix_data.hpp:223
device_matrix_data(std::shared_ptr< const Executor > exec, dim< 2 > size={}, size_type num_entries=0)
Initializes a new device_matrix_data object.
size_type get_num_stored_elements() const
Returns the number of stored elements of the matrix.
Definition device_matrix_data.hpp:175
const index_type * get_const_col_idxs() const
Returns a pointer to the constant column index array.
Definition device_matrix_data.hpp:206
void resize_and_reset(size_type new_num_entries)
Resizes the internal storage to the given number of stored matrix entries.
void resize_and_reset(dim< 2 > new_size, size_type new_num_entries)
Resizes the matrix and internal storage to the given dimensions.
size_type get_num_elems() const
Definition device_matrix_data.hpp:168
void fill_zero()
Fills the matrix entries with zeros.
void sort_row_major()
Sorts the matrix entries in row-major order This means that they will be sorted by row index first,...
void sum_duplicates()
Sums up all duplicate entries pointing to the same non-zero location.
arrays empty_out()
Moves out the internal arrays of the device_matrix_data object and resets it to an empty 0x0 matrix.
std::shared_ptr< const Executor > get_executor() const
Returns the executor used to store the device_matrix_data entries.
Definition device_matrix_data.hpp:150
device_matrix_data(std::shared_ptr< const Executor > exec, const device_matrix_data &data)
Initializes a device_matrix_data object by copying an existing object on another executor.
index_type * get_col_idxs()
Returns a pointer to the column index array.
Definition device_matrix_data.hpp:199
value_type * get_values()
Returns a pointer to the value array.
Definition device_matrix_data.hpp:216
void remove_zeros()
Removes all zero entries from the storage.
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...
device_matrix_data(std::shared_ptr< const Executor > exec, dim< 2 > size, array< index_type > row_idxs, array< index_type > col_idxs, array< value_type > values)
Initializes a new device_matrix_data object from existing data.
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
STL namespace.
Stores the internal arrays of a device_matrix_data object.
Definition device_matrix_data.hpp:248
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
Type used to store nonzeros.
Definition matrix_data.hpp:60
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126