Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
device_matrix_data.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
34#define GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
35
36
37#include <ginkgo/core/base/array.hpp>
38#include <ginkgo/core/base/dim.hpp>
39#include <ginkgo/core/base/exception_helpers.hpp>
40#include <ginkgo/core/base/executor.hpp>
41#include <ginkgo/core/base/matrix_data.hpp>
42
43
44namespace gko {
45
46
62template <typename ValueType, typename IndexType>
64public:
65 using value_type = ValueType;
66 using index_type = IndexType;
69
79 explicit device_matrix_data(std::shared_ptr<const Executor> exec,
80 dim<2> size = {}, size_type num_entries = 0);
81
90 device_matrix_data(std::shared_ptr<const Executor> exec,
91 const device_matrix_data& data);
92
101 template <typename ValueArray, typename RowIndexArray,
102 typename ColIndexArray>
103 device_matrix_data(std::shared_ptr<const Executor> exec, dim<2> size,
104 RowIndexArray&& row_idxs, ColIndexArray&& col_idxs,
105 ValueArray&& values)
106 : size_{size},
107 row_idxs_{exec, std::forward<RowIndexArray>(row_idxs)},
108 col_idxs_{exec, std::forward<ColIndexArray>(col_idxs)},
109 values_{exec, std::forward<ValueArray>(values)}
110 {
111 GKO_ASSERT_EQ(values_.get_num_elems(), row_idxs_.get_num_elems());
112 GKO_ASSERT_EQ(values_.get_num_elems(), col_idxs_.get_num_elems());
113 }
114
122
133 std::shared_ptr<const Executor> exec, const host_type& data);
134
141
148
155
161 std::shared_ptr<const Executor> get_executor() const
162 {
163 return values_.get_executor();
164 }
165
171 dim<2> get_size() const { return size_; }
172
178 size_type get_num_elems() const { return values_.get_num_elems(); }
179
185 index_type* get_row_idxs() { return row_idxs_.get_data(); }
186
192 const index_type* get_const_row_idxs() const
193 {
194 return row_idxs_.get_const_data();
195 }
196
202 index_type* get_col_idxs() { return col_idxs_.get_data(); }
203
209 const index_type* get_const_col_idxs() const
210 {
211 return col_idxs_.get_const_data();
212 }
213
219 value_type* get_values() { return values_.get_data(); }
220
226 const value_type* get_const_values() const
227 {
228 return values_.get_const_data();
229 }
230
238
247
251 struct arrays {
252 array<index_type> row_idxs;
253 array<index_type> col_idxs;
254 array<value_type> values;
255 };
256
264
265private:
266 dim<2> size_;
267 array<index_type> row_idxs_;
268 array<index_type> col_idxs_;
269 array<value_type> values_;
270};
271
272
273namespace detail {
274
275
276template <typename ValueType, typename IndexType>
277struct temporary_clone_helper<device_matrix_data<ValueType, IndexType>> {
278 static std::unique_ptr<device_matrix_data<ValueType, IndexType>> create(
279 std::shared_ptr<const Executor> exec,
281 {
282 if (copy_data) {
283 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
284 std::move(exec), *ptr);
285 } else {
286 return std::make_unique<device_matrix_data<ValueType, IndexType>>(
287 std::move(exec), ptr->get_size(), ptr->get_num_elems());
288 }
289 }
290};
291
292template <typename ValueType, typename IndexType>
293struct temporary_clone_helper<const device_matrix_data<ValueType, IndexType>> {
294 static std::unique_ptr<const device_matrix_data<ValueType, IndexType>>
295 create(std::shared_ptr<const Executor> exec,
297 {
298 return std::make_unique<const device_matrix_data<ValueType, IndexType>>(
299 std::move(exec), *ptr);
300 }
301};
302
303
304// specialization for non-constant device_matrix_data, copying back via
305// assignment
306template <typename ValueType, typename IndexType>
307class copy_back_deleter<device_matrix_data<ValueType, IndexType>> {
308public:
310
317 copy_back_deleter(pointer original) : original_{original} {}
318
324 void operator()(pointer ptr) const
325 {
326 *original_ = *ptr;
327 delete ptr;
328 }
329
330private:
331 pointer original_;
332};
333
334
335} // namespace detail
336} // namespace gko
337
338
339#endif // GKO_PUBLIC_CORE_BASE_DEVICE_MATRIX_DATA_HPP_
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:662
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:655
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
dim< 2 > get_size() const
Returns the dimensions of the matrix.
Definition device_matrix_data.hpp:171
device_matrix_data(std::shared_ptr< const Executor > exec, dim< 2 > size, RowIndexArray &&row_idxs, ColIndexArray &&col_idxs, ValueArray &&values)
Initializes a new device_matrix_data object from existing data.
Definition device_matrix_data.hpp:103
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:192
index_type * get_row_idxs()
Returns a pointer to the row index array.
Definition device_matrix_data.hpp:185
const value_type * get_const_values() const
Returns a pointer to the constant value array.
Definition device_matrix_data.hpp:226
device_matrix_data(std::shared_ptr< const Executor > exec, dim< 2 > size={}, size_type num_entries=0)
Initializes a new device_matrix_data object.
const index_type * get_const_col_idxs() const
Returns a pointer to the constant column index array.
Definition device_matrix_data.hpp:209
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:178
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:161
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:202
value_type * get_values()
Returns a pointer to the value array.
Definition device_matrix_data.hpp:219
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...
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
Stores the internal arrays of a device_matrix_data object.
Definition device_matrix_data.hpp:251
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
Type used to store nonzeros.
Definition matrix_data.hpp:89
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155