Ginkgo Generated from develop branch based on develop. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
device_matrix_data.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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
15
16namespace gko {
17
18
34template <typename ValueType, typename IndexType>
36public:
37 using value_type = ValueType;
38 using index_type = IndexType;
41
51 explicit device_matrix_data(std::shared_ptr<const Executor> exec,
52 dim<2> size = {}, size_type num_entries = 0);
53
62 device_matrix_data(std::shared_ptr<const Executor> exec,
63 const device_matrix_data& data);
64
73 device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
74 array<index_type> row_idxs, array<index_type> col_idxs,
75 array<value_type> values);
76
81 template <typename InputValueType, typename RowIndexType,
82 typename ColIndexType>
83 GKO_DEPRECATED(
84 "explicitly construct the gko::array arguments instead of passing "
85 "initializer lists")
86 device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
87 std::initializer_list<RowIndexType> row_idxs,
88 std::initializer_list<ColIndexType> col_idxs,
90 : device_matrix_data{exec, size,
91 array<index_type>{exec, std::move(row_idxs)},
92 array<index_type>{exec, std::move(col_idxs)},
93 array<value_type>{exec, std::move(values)}}
94 {}
95
103
114 std::shared_ptr<const Executor> exec, const host_type& data);
115
122
129
136
142 std::shared_ptr<const Executor> get_executor() const
143 {
144 return values_.get_executor();
145 }
146
152 dim<2> get_size() const { return size_; }
153
159 GKO_DEPRECATED("use get_num_stored_elements()")
161
167 size_type get_num_stored_elements() const { return values_.get_size(); }
168
174 index_type* get_row_idxs() { return row_idxs_.get_data(); }
175
181 const index_type* get_const_row_idxs() const
182 {
183 return row_idxs_.get_const_data();
184 }
185
191 index_type* get_col_idxs() { return col_idxs_.get_data(); }
192
198 const index_type* get_const_col_idxs() const
199 {
200 return col_idxs_.get_const_data();
201 }
202
208 value_type* get_values() { return values_.get_data(); }
209
215 const value_type* get_const_values() const
216 {
217 return values_.get_const_data();
218 }
219
227
236
240 struct arrays {
241 array<index_type> row_idxs;
242 array<index_type> col_idxs;
243 array<value_type> values;
244 };
245
253
254private:
255 dim<2> size_;
256 array<index_type> row_idxs_;
257 array<index_type> col_idxs_;
258 array<value_type> values_;
259};
260
261
262namespace detail {
263
264
265template <typename ValueType, typename IndexType>
266struct temporary_clone_helper<device_matrix_data<ValueType, IndexType>> {
267 static std::unique_ptr<device_matrix_data<ValueType, IndexType>> create(
268 std::shared_ptr<const Executor> exec,
270 {
271 if (copy_data) {
272 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
273 std::move(exec), *ptr);
274 } else {
275 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
276 std::move(exec), ptr->get_size(),
278 }
279 }
280};
281
282template <typename ValueType, typename IndexType>
283struct temporary_clone_helper<const device_matrix_data<ValueType, IndexType>> {
284 static std::unique_ptr<const device_matrix_data<ValueType, IndexType>>
285 create(std::shared_ptr<const Executor> exec,
287 {
288 return std::make_unique<const device_matrix_data<ValueType, IndexType>>(
289 std::move(exec), *ptr);
290 }
291};
292
293
294// specialization for non-constant device_matrix_data, copying back via
295// assignment
296template <typename ValueType, typename IndexType>
297class copy_back_deleter<device_matrix_data<ValueType, IndexType>> {
298public:
300
307 copy_back_deleter(pointer original) : original_{original} {}
308
314 void operator()(pointer ptr) const
315 {
316 *original_ = *ptr;
317 delete ptr;
318 }
319
320private:
321 pointer original_;
322};
323
324
325} // namespace detail
326} // namespace gko
327
328
329#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:616
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:167
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:690
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:683
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:657
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:35
dim< 2 > get_size() const
Returns the dimensions of the matrix.
Definition device_matrix_data.hpp:152
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:181
index_type * get_row_idxs()
Returns a pointer to the row index array.
Definition device_matrix_data.hpp:174
const value_type * get_const_values() const
Returns a pointer to the constant value array.
Definition device_matrix_data.hpp:215
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:167
const index_type * get_const_col_idxs() const
Returns a pointer to the constant column index array.
Definition device_matrix_data.hpp:198
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
Returns the number of stored elements of the matrix.
Definition device_matrix_data.hpp:160
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:142
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:191
value_type * get_values()
Returns a pointer to the value array.
Definition device_matrix_data.hpp:208
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
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:775
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:92
Stores the internal arrays of a device_matrix_data object.
Definition device_matrix_data.hpp:240
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:27
Type used to store nonzeros.
Definition matrix_data.hpp:61
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:127