Ginkgo  Generated from pipelines/2406079695 branch based on develop. Ginkgo version 1.12.0
A numerical linear algebra library targeting many-core architectures
device_views.hpp
1 // SPDX-FileCopyrightText: 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
7 
8 #include <cassert>
9 
10 #include <ginkgo/core/base/dim.hpp>
11 
12 
13 namespace gko {
14 namespace matrix {
15 namespace view {
16 
17 
26 template <typename ValueType>
27 struct dense {
28  dim<2> size;
29  size_type stride;
30  ValueType* values;
31 
33  constexpr dense(dim<2> size, size_type stride, ValueType* values)
34  : size{size}, stride{stride}, values{values}
35  {
36  assert(stride >= size[1]);
37  }
38 
40  constexpr dense<const ValueType> as_const() const
41  {
42  return dense<const ValueType>{size, stride, values};
43  }
44 
46  constexpr ValueType& operator()(size_type row, size_type col) const
47  {
48  assert(row < size[0] && col < size[1]);
49  return values[row * stride + col];
50  }
51 };
52 
53 
62 template <typename ValueType, typename IndexType>
63 struct coo {
64  dim<2> size;
65  size_type num_stored_elements;
66  ValueType* values;
67  IndexType* row_idxs;
68  IndexType* col_idxs;
69 
71  constexpr coo(dim<2> size, size_type num_stored_elements, ValueType* values,
72  IndexType* row_idxs, IndexType* col_idxs)
73  : size{size},
74  num_stored_elements{num_stored_elements},
75  values{values},
76  row_idxs{row_idxs},
77  col_idxs{col_idxs}
78  {}
79 
82  {
84  size, num_stored_elements, values, row_idxs, col_idxs};
85  }
86 };
87 
88 
97 template <typename ValueType, typename IndexType>
98 struct ell {
99  dim<2> size;
100  size_type num_stored_elements_per_row;
101  size_type stride;
102  ValueType* values;
103  IndexType* col_idxs;
104 
106  constexpr ell(dim<2> size, size_type num_stored_elements_per_row,
107  size_type stride, ValueType* values, IndexType* col_idxs)
108  : size{size},
109  num_stored_elements_per_row{num_stored_elements_per_row},
110  stride{stride},
111  values{values},
112  col_idxs(col_idxs)
113  {
114  assert(stride >= size[0]);
115  }
116 
119  {
121  size, num_stored_elements_per_row, stride, values, col_idxs};
122  }
123 
125  constexpr ValueType& val_at(size_type row, size_type idx) const
126  {
127  return values[this->linearize_index(row, idx)];
128  }
129 
131  constexpr IndexType& col_at(size_type row, size_type idx) const
132  {
133  return col_idxs[this->linearize_index(row, idx)];
134  }
135 
136 private:
138  constexpr size_type linearize_index(size_type row,
139  size_type idx) const noexcept
140  {
141  assert(idx < num_stored_elements_per_row && row < size[0]);
142  return row + stride * idx;
143  }
144 };
145 
146 
147 } // namespace view
148 } // namespace matrix
149 } // namespace gko
150 
151 
152 #endif // GKO_PUBLIC_CORE_MATRIX_DEVICE_DENSE_HPP_
gko::matrix::view::dense
Non-owning view of a matrix::Dense to be used inside device kernels.
Definition: device_views.hpp:27
gko::matrix::view::ell::as_const
constexpr ell< const ValueType, const IndexType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:118
gko::matrix::view::coo
Non-owning view of a matrix::Coo to be used inside device kernels.
Definition: device_views.hpp:63
gko::matrix::view::ell::col_at
constexpr IndexType & col_at(size_type row, size_type idx) const
accessing the column index of the idx-th element within the given row
Definition: device_views.hpp:131
gko::matrix::view::ell::ell
constexpr ell(dim< 2 > size, size_type num_stored_elements_per_row, size_type stride, ValueType *values, IndexType *col_idxs)
Constructs a ell view.
Definition: device_views.hpp:106
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::matrix::view::ell
Non-owning view of a matrix::Ell to be used inside device kernels.
Definition: device_views.hpp:98
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::dim< 2 >
gko::matrix::view::coo::as_const
constexpr coo< const ValueType, const IndexType > as_const() const
Returns a const view of the same data.
Definition: device_views.hpp:81
gko::matrix::view::dense::dense
constexpr dense(dim< 2 > size, size_type stride, ValueType *values)
Constructs a dense view from size, stride and values.
Definition: device_views.hpp:33
gko::matrix::view::ell::val_at
constexpr ValueType & val_at(size_type row, size_type idx) const
accessing the value of the idx-th element within the given row
Definition: device_views.hpp:125
gko::matrix::view::dense::operator()
constexpr ValueType & operator()(size_type row, size_type col) const
Subscript operator accessing the given row and column.
Definition: device_views.hpp:46
gko::matrix::view::dense::as_const
constexpr dense< const ValueType > as_const() const
Returns a const view of the same values.
Definition: device_views.hpp:40
gko::matrix::view::coo::coo
constexpr coo(dim< 2 > size, size_type num_stored_elements, ValueType *values, IndexType *row_idxs, IndexType *col_idxs)
Constructs a coo view from size, nnz, values, row and column indices.
Definition: device_views.hpp:71